2. The pipeline#
Your five steps become five functions. Open mini/classify.mjs and read it; it is 130 lines and it contains no model, no network call, and no mention of coffee.
cd learn/mini
node classify.mjs "SQ *BLUE BOTTLE COF SAN FRANCISCO CA"
raw SQ *BLUE BOTTLE COF SAN FRANCISCO CA
normalize BLUE BOTTLE COF SAN FRANCISCO CA [rules: square-prefix]
spans who="BLUE BOTTLE COF" where="SAN FRANCISCO CA"
lookup no match; fuzzy: nothing
answer Shopping > Online Retail confidence 0.3 NEEDS REVIEW
tags channel:pos processor:square city:san-francisco region:ca country:us
Read that output next to your five steps. Steps 1, 2, 3 and 5 all worked. SQ * came off and turned into processor:square and channel:pos, exactly the "junk that was actually evidence" from chapter 1. The city came off and turned into three tags. Step 4 failed, and step 5 was honest about it.
The last line is worth a second look. The system produced no merchant and it still produced six useful facts. Partial answers are the normal case, not the failure case.
The wrong answer it gave anyway#
Shopping > Online Retail is not a guess. It is the fallback: the label this classifier returns when it has nothing. It is paired with confidence 0.3 and NEEDS REVIEW, which is the system saying "ignore the category, look at the flag".
Write your fallback down and mark it, because it will be your most common answer at the start and it will pollute every measurement you take. In the production system in this repo, that same fallback label is the top confusion for every single category, and one whole class of apparent improvement turned out to be rows moving out of a bucket they were only ever in by default.
Run it on everything#
node classify.mjs --file rows.txt
Thirty-four rows. Thirteen resolve. Twenty-one do not, and the summary line says 61.8% need review. That number is the one to care about. Not accuracy: accuracy counts rows the system was confidently wrong about as equal to rows it flagged, and those are not equal. Review rate is what a user actually feels.
How the stages were designed#
The five steps came from watching a human. Turning them into stages took five decisions, and these are the ones worth stealing.
Split procedure from fact, then put the facts in a file. normalize does not contain the string SQ *; it contains a loop over kb.rules. lookup does not contain "Blue Bottle"; it searches kb.merchants. Every word of world knowledge in this system lives in kb.json and nothing else. That single decision is what makes the rest of the course possible, because a file can be edited by a program and code cannot be, safely.
Make every stage a pure function of (text, kb, ctx). No stage reads a global, touches the network, or remembers the last row. The reason is not elegance. It is that later you will want to replay stage 2 alone on one example from six months ago, and compare stage 4 with and without a change, ten thousand times, in a second. A stage that can only run inside the whole pipeline can never be measured, and a component you cannot measure is a component you cannot improve.
Name the output of every stage. normalize returns the text and which rules fired. extractSpans returns who and where. That is not for debugging. When something is wrong you need to know which stage is wrong, and a single number at the end cannot tell you. Chapter 9 shows the production harness measuring six stages separately, and finding that a change improved one and broke another.
Decide who owns the control flow, and write it down. In this system, code decides what happens next, always. The model, when it eventually shows up, produces suggestions and nothing else: it does not choose the order of stages, it cannot call the classifier, it never touches a file. That choice has a cost (the model cannot route around a stage that is doing the wrong thing) and a benefit (nothing it does is unbounded). Make the choice deliberately. Systems that let the model own the control flow are a different animal and need a different set of protections.
Compute the rule order rather than reading it. orderedRules() sorts by kind, then by longest match, then by id. Nobody chooses where a new rule goes. If order came from file position, then "add a rule" would mean "possibly change the behaviour of every existing rule", and the thing you are building towards is a machine that adds rules on its own at 3am. Make the order a property of the rules, not of the file, and adding one becomes safe by construction.
Where the model is not#
Notice what the model is not doing here, because this is the part people get wrong.
The obvious design is: send each description to a model, get back a category. It works. It is also the wrong shape for this problem, and the reason is arithmetic.
Thirty-four rows produced twenty-one unknowns, but only six distinct merchants worth writing down. On a real import of 900 rows you get about 40. The unknown-ness is not spread evenly across transactions; it is concentrated in a small number of names, each repeated many times. And "which company is BLUE BOTTLE COF" is a question that has one answer forever, for every user, in every import.
So: ask a model once per new merchant, ever, and write the answer down. Or ask it once per transaction, forever, for every user. Same answers, and the second one costs a few million times more, needs a network connection at the moment someone opens their laptop on a plane, and gives a slightly different answer each time you run it.
That is the whole argument for the shape of this project. The model's job is to write the knowledge base, not to answer the question. Chapter 3 builds the place the answers go.
Next: 3. Knowledge is data