1. One transaction#
Here is a line from a bank export. It is real, apart from the digits.
SQ *BLUE BOTTLE COF SAN FRANCISCO CA
The job: turn that into something a person can read on a spending report.
Blue Bottle Coffee Dining > Coffee San Francisco
You just did it in your head. That is the whole problem, and doing it once by hand in slow motion is the most useful twenty minutes of this course, because the system you are about to build is a transcript of what you just did.
Watch yourself do it#
Read the string again and notice the order your attention moved in.
"SQ star, that's just Square, ignore it." You did not read SQ * as part of the name. You recognised it as something the payment network added. But notice what you learned by ignoring it: SQ * means this was a card tapped at a physical counter, processed by Square. The junk you threw away was evidence. Hold on to that, it comes back in chapter 2.
"SAN FRANCISCO CA is where, not who." You split the remaining text into two parts without being told there were two parts. You knew SAN FRANCISCO CA was a place because you have heard of San Francisco, not because of anything in the string. A comma would have helped. There is no comma.
"BLUE BOTTLE COF. That's Blue Bottle Coffee." Here you did something different in kind. The previous two steps rearranged the string. This one reached outside it. COF is not a word. You expanded a truncation into a company you have heard of, using knowledge that is nowhere in the input.
"Blue Bottle is a coffee chain, so: Dining, Coffee." Also outside knowledge, and a second, separate act. Even someone who correctly reads BLUE BOTTLE COF as Blue Bottle Coffee cannot produce Dining > Coffee unless they also know what the company sells and which list of categories you are using.
"I'm sure." The last step, and the one people forget is a step. You assigned yourself a confidence, and if it had been low you would have done something different: asked someone, or left it for later. A system that cannot say "I do not know" will instead say something wrong with total conviction, which is much worse.
Write the steps down#
1. context what do I know before reading? (which bank, what kind of account)
2. normalize remove what the bank added -> and keep it as evidence
3. split separate who from where
4. identify which merchant is this, and what category
5. confidence how sure am I, and should a human look?
Five steps. Now the only question that matters for the rest of this course:
Which of these five need to know things about the world?
Steps 1, 2, 3 and 5 do not. They are procedures. "Strip a known prefix." "A trailing city and state code is a location." "If nothing matched, say so." You can write those down completely, once, and they work on transactions you have never seen, in industries you have never heard of.
Step 4 is different. BLUE BOTTLE COF means Blue Bottle Coffee for exactly one reason: somebody knows that. There is no procedure that derives it. It is a fact, and there are hundreds of thousands of facts like it, and new ones appear every week because companies keep being founded.
That split, procedures on one side and facts on the other, is the entire architecture. Procedures become code you write once. Facts become rows in a file. And once the facts live in a file rather than in the code, something interesting becomes possible: a machine can write them.
Try it#
You will not get an answer yet. That is the point of chapter 2.
cd learn/mini
node classify.mjs "SQ *BLUE BOTTLE COF SAN FRANCISCO CA"
Next: 2. The pipeline