In this video, you compared two embeddings with a short similarity() function, then ranked five rules from the café’s rulebooks against a player’s question and watched the order change when the question did.
Key Points
- A score from two vectors.
similarity()multiplies the numbers in matching positions of two vectors and adds up the products. For table 6’s question and the pier rule, each of the first three products is tiny (0.0004, -0.0008, 0.0000), and all 384 add up to 0.563. - Dot product and cosine similarity. That sum is the dot product. Because
embed()scales every vector to a size of 1, the dot product measures how closely two vectors point the same way, which is called cosine similarity. Higher means closer in meaning, unrelated texts score lower, a score can dip below 0, and a vector compared with itself scores 1. - Rank by sorting.
rank-rules.jsembeds question 10 once, scores each rule against it, keeps each score with its rule, and sorts from the highest score down. Table 6’s pier rule comes first at 0.563. The seating rule from the last lesson comes second at 0.196, and in this run the other games’ rules score near 0, one a little below it. - Meaning, not matching words. Both Lantern Harbor rules mention trading, like the question, yet their scores are far apart. Each score comes from a whole sentence’s vector, not from matching words. The seating rule answers part of the question and still trails. A score is this model’s estimate of closeness, not of usefulness.
- Scores belong to a pair of texts. With question 2, about Orchard Rush’s storm card, the same five rules come back in a new order. The ladder rule jumps from last to first at 0.528, and the pier rule falls to 0.203, far below the new top match.
- A score isn’t a percentage. It’s tempting to read table 6’s 0.563 as 56 percent and keep only scores above, say, 0.8, but that cut-off would have kept nothing. Good matches often score well below 1, and the numbers depend on the model. Read them as a ranking. If you need a cut-off, choose it from your model’s scores on your own examples.
The rule:
- Compare: embed both texts with one model, then take the dot product of their vectors (their cosine similarity).
- Rank: score each text against the question, then sort, highest first.
Five rules make a toy list. Later in the course, the same ranking runs over whole rulebooks.
The Final Code
rank-rules.js goes in the project folder, next to embed.js from the last lesson. The first rule is trimmed from Lantern Harbor’s page 4 so that each output line stays short:
rank-rules.js
import { readFile } from "node:fs/promises";
import { embed } from "./embed.js";
function similarity(a, b) {
let sum = 0;
for (let i = 0; i < a.length; i++) {
sum += a[i] * b[i];
}
return sum;
}
const text = await readFile("data/questions.json", "utf8");
const questions = JSON.parse(text);
const question = questions.find((entry) => entry.id === 10);
const rules = [
"You may trade with a player whose boat is at the same pier as yours.",
"Where players sit at the table doesn't matter for trading.",
"Each player loses one ladder, not all of them.",
"Every seedling in a greenhouse dies and goes back to the supply.",
"The player who most recently ate street food goes first.",
];
const questionVector = await embed(question.text);
const ranked = [];
for (const rule of rules) {
const score = similarity(questionVector, await embed(rule));
ranked.push({ score, rule });
}
ranked.sort((first, second) => second.score - first.score);
for (const entry of ranked) {
console.log(entry.score.toFixed(3), entry.rule);
}
Run it with node rank-rules.js. Here’s a real run for question 10:
0.563 You may trade with a player whose boat is at the same pier as yours.
0.196 Where players sit at the table doesn't matter for trading.
0.059 Every seedling in a greenhouse dies and goes back to the supply.
0.054 The player who most recently ate street food goes first.
-0.039 Each player loses one ladder, not all of them.
With entry.id === 2 instead, for table 2’s storm question:
0.528 Each player loses one ladder, not all of them.
0.203 You may trade with a player whose boat is at the same pier as yours.
0.172 Every seedling in a greenhouse dies and goes back to the supply.
0.130 Where players sit at the table doesn't matter for trading.
0.069 The player who most recently ate street food goes first.