Appendix B · section 14 of 32
ask/retrieve.test.ts
From Finding It by Its Words.
import test from "node:test";
import assert from "node:assert/strict";
import { LAWS } from "./corpus.js";
import { bm25, words } from "./retrieve.js";
test("stop words go, plurals fold, and short words are left alone", () => {
assert.deepEqual(words("How far from a fire hydrant can I park?"), ["far", "fire", "hydrant", "park"]);
assert.deepEqual(words("hydrants and buses"), ["hydrant", "buse"]);
assert.deepEqual(words("the bus"), ["bus"]);
});
test("the hydrant law is first for the hydrant question", () => {
const found = bm25(LAWS, "how far from a fire hydrant can i park");
assert.equal(found[0]?.id, "MD-S01");
});
test("a question that shares no word with any law retrieves nothing", () => {
assert.deepEqual(bm25(LAWS, "¿a qué distancia de un hidrante puedo estacionar?"), []);
});
test("a learner's typing still reaches the law: a word one letter off, a word with no space", () => {
const found = bm25(LAWS, "how far behing a firehydrant must i park");
assert.ok(found.some((law) => law.id === "MD-S01"));
});
test("a question with nothing but stop words retrieves nothing", () => {
assert.deepEqual(bm25(LAWS, "can i"), []);
});
test("the state's own pages are never ranked by words", () => {
const found = bm25(LAWS, "if i fail my knowledge test how soon can i retake it", 25);
assert.ok(!found.some((law) => law.kind === "process"));
});
test("at most the limit, best first, and nothing scoring zero", () => {
const found = bm25(LAWS, "how far must i park from a crosswalk, a stop sign, a railroad crossing or a fire station", 3);
assert.equal(found.length, 3);
});