Skip to content

Pick a Vendor Subclass from Page Text

An accounts payable team has one class, "Vendor Invoice", with one subclass per vendor layout. Each subclass has its own zones and extraction rules, so when the wrong subclass wins, its zones point at the wrong places and the fields come back empty. A page refiner on each subclass must vote for its own vendor, and only its own vendor.

At a glance

  • Extension point Custom Condition (page refiner)
  • You need Class "Vendor Invoice" with one subclass per vendor; permission Use This App in the Intuitive AI app. No Index fields are read or written.
  • Teaches word-boundary signals with weights, a penalty for a look-alike vendor, graded confidence, and the difference between 0 and no opinion

The pattern that went wrong

A first version used this refiner on every vendor subclass, with the vendor name changed:

Do not use: substring match
var pageText = services.OcrService.GetPageOcr(state).Text;
if (pageText.indexOf("Harbor Point") != -1) {
    result.SetResult(1);
}

It picked the wrong subclass, and the documents came out with empty fields:

  1. Substring match. "Harbor Point" is in both "Harbor Point Supply" and "Harbor Point Services". Both subclasses returned 1, and the engine could not tell them apart.
  2. Name anywhere on the page. A vendor name in a "Bill To", "Ship To" or "Formerly" line counts the same as the letterhead.
  3. Case and OCR noise. indexOf is case-sensitive. "HARBOR POINT", or "Harbor Point" broken across a line, does not match.
  4. Full confidence from one weak signal. SetResult(1) says "certain" on the strength of two words.
  5. Silence when absent. When the name is missing, the script never calls SetResult. A page refiner then has no opinion, so it never votes against the subclass.

Set it up

  1. In the Intuitive AI app, open class "Vendor Invoice" › subclass "Harbor Point Supply".
  2. Use Show OCR Data on the model page. Copy the exact text of the legal name, the remit-to line, the vendor id and the tax id.
  3. In First Page Refiners, select Add Refiner: › Custom Condition.
  4. Paste the script, edit the block marked "Edit for this subclass", and save the subclass.
  5. Repeat for each vendor subclass with its own signals. The vendors in this recipe ("Harbor Point Supply" and "Harbor Point Services") are fictional.

The script

Condition_VendorSubclass.js
(function () {
    // ---- Edit for this subclass ----
    var signals = [
        { pattern: /\bharbor\s+point\s+supply\b/i, weight: 0.45 },                                   // legal name
        { pattern: /\bremit\s+(?:payment\s+)?to\b.{0,80}?\bharbor\s+point\s+supply\b/i, weight: 0.25 }, // remit-to line
        { pattern: /\bHPS-[0-9]{5}\b/, weight: 0.20 },                                               // our vendor id format
        { pattern: /\b12-3456789\b/, weight: 0.10 }                                                  // vendor tax id
    ];
    var lookAlikes = [
        { pattern: /\bharbor\s+point\s+services\b/i, penalty: 0.40 }                                 // sister company
    ];
    var minimumScore = 0.45; // Below this, the page is not this vendor.
    // ---------------------------------

    var ocr = services.OcrService.GetPageOcr(state);
    var raw = (ocr && ocr.Text) ? String(ocr.Text) : "";

    if (raw.replace(/\s+/g, "").length < 20) {
        return; // No readable text: no opinion. Other classifiers decide.
    }

    var text = raw.replace(/\s+/g, " ");

    var score = 0;
    for (var i = 0; i < signals.length; i++) {
        if (signals[i].pattern.test(text)) {
            score += signals[i].weight;
        }
    }
    for (var j = 0; j < lookAlikes.length; j++) {
        if (lookAlikes[j].pattern.test(text)) {
            score -= lookAlikes[j].penalty;
        }
    }

    if (score < minimumScore) {
        result.SetResult(0); // Not enough evidence for this vendor: vote against.
        return;
    }

    // A noisy page cannot claim full confidence.
    var quality = (ocr.Confidence > 0 && ocr.Confidence <= 1) ? ocr.Confidence : 1;
    result.SetResult(Math.min(1, score) * (0.5 + quality / 2));
})();

How it works

  • Whitespace is collapsed first, so a name broken across lines still matches.
  • Each signal is a case-insensitive regex with word boundaries. The legal-name signal needs the full name "Harbor Point Supply", so "Harbor Point Services" does not match it.
  • Signals add weight. The strongest single signal (0.45) cannot pass the threshold alone on a noisy page, so one stray mention is never "certain".
  • A look-alike vendor subtracts weight, so a sister company's letterhead pushes the score below the minimum.
  • Below the minimum, the script returns 0: a clear vote against. It gives no opinion only when the page has no readable text.
  • The OCR confidence scales the result, so a faint scan gives a weaker vote.
Page shows Score Result
Legal name only 0.45 About 0.4 to 0.45 after the OCR factor: a weak vote
Legal name, remit-to and vendor id 0.90 A strong vote
"Harbor Point Services" letterhead, no Supply signals below 0 0
A full Supply invoice that also names the sister company 0.50 Still a vote, but weaker

Test it

  1. Open the class and select Test in the ribbon.
  2. In Configure Test, upload 10 to 20 invoices from several vendors, including the look-alike vendor.
  3. Select Start Test. For each document, check Matched Subclass, then open Detailed Test Results and read the Condition Confidence of each refiner.

You should see

every Harbor Point Supply invoice matched to its subclass with a clear lead over the other vendors, and 0 from this refiner on the sister company's invoices. Two subclasses that score close together need a stronger distinguishing signal.

What can go wrong

Watch out

  • A signal regex is too wide. /\bHPS-[0-9]+\b/ can match a line-item code. Make it exact.
  • Two subclasses still tie. Add a signal only one of them has: a phone number, a bank account suffix, a form number in the footer.
  • The vendor changed its letterhead. A new layout needs a new subclass or new signals. Old signals return 0, which is the correct answer.
  • Script error. A typo stops the run for the document. The Test results show "Error In Classification". See Debugging Intuitive AI Scripts.

Variations

  • Many vendors. Keep every vendor-specific value in the edit block at the top of the script. A reviewer can then compare two subclasses side by side and spot a signal they share.
  • One revision line decides it. Use a Regex Anchor instead of a script. See Tell Form Revisions Apart.