r/SQLv2 • • 4h ago

SQLv2 Lesson 12: teach the database your supplier's shorthand. 'blk mech kbd us' matches nothing at 0.35; run it through an 11-row abbreviation table and the same line hits 'Mechanical keyboard, US layout' at 0.81, then one word stays too vague to book (real output)

Lesson 11 ended on a promise. When 'blk' and 'ANSI' stopped matching once a second colour and layout existed, I said the fix was "a lookup table of your supplier's abbreviations, joined before Step 2." This is that table. (This sub is run by the SynapCores team; run on 2.5.1 this morning, 2.3.2 gave the same numbers.)

The problem

A new batch from the supplier, in their in-house shorthand. Six lines:

blk mech kbd us
usb-c chgr 65w
alu stnd
4k mon 27in
nc hdset mic
mon

You read every one at a glance. The catalog does not (the 12 items from Lesson 11; CREATE and INSERTs in the first comment). Match the raw shorthand by meaning, Lesson 8's COSINE_SIMILARITY, and most of it falls under Lesson 8's 0.43 floor: the keyboard line scores 0.35, the stand 0.27, the headset 0.41. Booked by similarity alone, they'd all go to NO MATCH.

Step 1: keep your shorthand in a table

One row per abbreviation, a table you grow when a new one shows up: abbrev(seq, short, long), 11 rows, in the first comment.

Step 2: expand each line before you embed it

REPLACE swaps one word at a time, so fold the whole dictionary over the line with a recursive CTE, one abbreviation per step (full query in the first comment). Pad with spaces so 'us' expands the word and not the 'us' inside 'usb-c':

1  black mechanical keyboard US layout
2  usb-c charger 65w
3  aluminium stand
4  4k monitor 27in
5  noise-cancelling headset microphone
6  monitor

Step 3, the payoff: expand, then match by meaning, in one statement

Wrap that expansion in the query that embeds the expanded phrase and scores it against the catalog (COSINE_SIMILARITY over EMBED, Lesson 8). Each line's best catalog match, scored as raw shorthand vs expanded:

blk mech kbd us -> Mechanical keyboard, US layout              0.35 -> 0.81
usb-c chgr 65w  -> USB-C laptop charger, 65 W                  0.69 -> 0.91
alu stnd        -> Aluminium laptop stand                      0.27 -> 0.84
4k mon 27in     -> 27-inch 4K monitor                          0.61 -> 0.94
nc hdset mic    -> Noise-cancelling headset with microphone    0.41 -> 0.98
mon             -> 27-inch 4K monitor                          0.16 -> 0.62
6 rows in 0.09 s

The three lines under the floor as shorthand (0.35, 0.27, 0.41) clear 0.80 once expanded. The dictionary is plain SQL: REPLACE and a recursive CTE over a table you grow one row at a time. The meaning match is the SynapCores part, EMBED turns the expanded phrase into a vector inside the engine and COSINE_SIMILARITY scores it, no API and no key.

When do you trust it? Look at line 6.

'mon' expands to 'monitor', and that is all it says. Two monitors in the catalog, so the same query that books the rest sends this one to review:

line  expanded             pick                 top   2nd   action
1  ...keyboard US layout   Mechanical keyboard   0.81  0.69  BOOK
4  4k monitor 27in         27-inch 4K monitor    0.94  0.72  BOOK
6  monitor                 27-inch 4K monitor    0.62  0.60  REVIEW (too close to call)

Line 4, "4k mon 27in", books the 4K monitor at 0.94: the '4k' and '27in' it kept are enough to tell them apart. Line 6 is the bare word 'mon', 0.62 for the 4K and 0.60 for the 1080p, a 0.02 gap. The score is the tell. A clear leader books; a near-tie goes to a person. One word was never going to pick a resolution, and the query says so instead of guessing.

Lesson 11: https://www.reddit.com/r/SQLv2/comments/1yb1cm6/

Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-lesson-12

Homework. Your shorthand grows. The supplier starts writing 'wl mse' for a wireless mouse.

  1. Add (12,'wl','wireless') and (13,'mse','mouse') to abbrev. What does 'wl mse blk' expand to, and which catalog item does it match, above or below 0.43?
  2. A line that just says 'kbd' expands to 'keyboard'. With a US and a UK keyboard in the catalog, what does the decide query do with it, and is that the answer you want?

What shorthand do your suppliers (or your own team) use that a new hire just has to learn?

1 Upvotes

1 comment sorted by

1

u/Alternative_Pin9598 4h ago

Reader path. The catalog is the same 12 items from Lesson 11, with a vector column so the meaning match has something to compare against:

CREATE TABLE catalog (id INTEGER PRIMARY KEY, name TEXT, price INTEGER, vec VECTOR(384));
INSERT INTO catalog (id, name, price) VALUES
  (101,'USB-C laptop charger, 65 W',39), (102,'Wireless mouse, black',19),
  (103,'HDMI cable, 2 m',9), (104,'27-inch 4K monitor',289),
  (105,'Mechanical keyboard, US layout',79), (106,'USB-C docking station, dual display',129),
  (107,'Noise-cancelling headset with microphone',99), (108,'Aluminium laptop stand',35),
  (109,'Wireless mouse, white',19), (110,'USB-C laptop charger, 100 W',59),
  (111,'27-inch 1080p monitor',179), (112,'Mechanical keyboard, UK layout',79);
UPDATE catalog SET vec = EMBED(name);

The supplier's shorthand, one row per line:

CREATE TABLE supplier (line INTEGER PRIMARY KEY, raw TEXT);
INSERT INTO supplier (line, raw) VALUES
  (1,'blk mech kbd us'), (2,'usb-c chgr 65w'), (3,'alu stnd'),
  (4,'4k mon 27in'), (5,'nc hdset mic'), (6,'mon');

The abbreviation dictionary from Step 1, one row per shorthand:

CREATE TABLE abbrev (seq INTEGER PRIMARY KEY, short TEXT, long TEXT);
INSERT INTO abbrev (seq, short, long) VALUES
  (1,'blk','black'), (2,'mech','mechanical'), (3,'kbd','keyboard'),
  (4,'us','US layout'), (5,'chgr','charger'), (6,'alu','aluminium'),
  (7,'stnd','stand'), (8,'mon','monitor'), (9,'nc','noise-cancelling'),
  (10,'hdset','headset'), (11,'mic','microphone');

The decide query from Step 3, in full: expand each line, embed the expanded phrase, score it against every catalog row, keep the top two, and let the gap decide:

WITH RECURSIVE
expand(line, step, txt) AS (
  SELECT line, 0, ' ' || LOWER(raw) || ' ' FROM supplier
  UNION ALL
  SELECT e.line, e.step + 1,
         REPLACE(e.txt, ' ' || a.short || ' ', ' ' || a.long || ' ')
  FROM expand e JOIN abbrev a ON a.seq = e.step + 1
  WHERE e.step < (SELECT COUNT(*) FROM abbrev)),
phrase AS (
  SELECT line, TRIM(txt) AS txt
  FROM expand WHERE step = (SELECT COUNT(*) FROM abbrev)),
scored AS (
  SELECT p.line, c.name,
         ROUND(COSINE_SIMILARITY(EMBED(p.txt), c.vec), 2) AS sim
  FROM phrase p CROSS JOIN catalog c),
best AS (
  SELECT line, name AS pick, sim AS top
  FROM scored x
  WHERE sim = (SELECT MAX(sim) FROM scored y WHERE y.line = x.line)),
second AS (
  SELECT x.line, MAX(x.sim) AS runner
  FROM scored x JOIN best b ON b.line = x.line AND x.name <> b.pick
  GROUP BY x.line)
SELECT b.line, b.pick, b.top, r.runner,
       CASE WHEN b.top >= 0.60 AND b.top - r.runner >= 0.05
            THEN 'BOOK' ELSE 'REVIEW (too close to call)' END AS action
FROM best b JOIN second r ON r.line = b.line
ORDER BY b.line;

Everything above ran on SynapCores Community Edition, local, no API key. The weekly build log and the "Ask the maintainers" thread are over in r/synapcores if you want to see what's changing under it. Free CE: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-lesson-12