Loading video player…

Rank Texts by Meaning

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.js embeds 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:

Language: JavaScript Filename: 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:

Language: Program Output
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:

Language: Program Output
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.

00:00 In the last lesson, embed() turned a rule into 384 numbers. In this video, you’ll compare two of those vectors, then rank a few rules by how close they are to a player’s question.

00:13 At table 6, a player in Lantern Harbor asks whether they can trade fish for oil with someone who isn’t next to them. Here are five rules from the café’s rulebooks.

00:24 The first two come from Lantern Harbor’s trading page. The other three come from three other games.

00:31 You can tell at a glance which rules are about trading. Your code can’t. It needs a number for each rule that says how close its meaning is to the question.

00:42 That number comes from the two vectors. Create a new file, rank-rules.js, and write a function, similarity. It takes two vectors, a and b, and returns one number.

00:55 The loop multiplies the numbers in matching positions, the first with the first, the second with the second, and adds each product into sum as it goes.

01:05 Here are the first three pairs for table 6’s question and the pier rule. Each product is tiny. Over all 384 pairs, they add up to 0.563.

01:17 This sum is called the dot product. Picture each vector as an arrow. This drawing has two dimensions. The real vectors have 384, but the idea holds. Because embed() scaled every vector to a size of 1, the dot product measures how closely two arrows point the same way.

01:38 That’s called cosine similarity. Higher means closer in meaning. Unrelated texts score lower, and a score can dip below 0. A vector compared with itself scores 1.

01:53 Now score every rule. The script reads question 10 from questions.json, the way your first script did, then lists the five rules. It embeds the question once.

02:05 For each rule, it embeds the rule, scores it against the question, and keeps the score with the rule. Then it sorts the list from the highest score down and prints each score to three decimal places.

02:18 Run it. The pier rule comes first, at 0.563. The seating rule from the last lesson is second, at 0.196. In this run, the other games’ rules score near 0, one a little below it.

02:34 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.

02:44 The seating rule answers part of the question and still trails. A score is this model’s estimate of closeness, not of usefulness.

02:54 Now change one number. Ask for question 2 instead. At table 2, in Orchard Rush, card 17 is a storm. Does everyone lose their ladders?

03:05 Run the script again. The same five rules come back in a new order. The ladder rule jumps from last to first, at 0.528. The pier rule falls to 0.203, far below the new top match.

03:20 Each score belongs to a pair of texts. Change the question, and every score changes with it.

03:28 It’s tempting to read table 6’s 0.563 as 56 percent and keep only scores above, say, 0.8. Watch out: a score isn’t a percentage. That cut-off would have kept nothing.

03:44 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.

03:56 So, to compare two texts by meaning, embed both with one model and take the dot product of their vectors. To find the closest rules, score each one against the question and sort.

04:08 Five sentences is a toy list. Later in the course, the same ranking runs over whole rulebooks. This video used a small model on your own computer. The next lesson, Choose Where Embeddings Come From, weighs that model against a provider’s embeddings API.

Become a Member to join the conversation.