Files
explicata/server/links/words.ts
T
thalladaandClaude Opus 5.5 9f8245e6d7 Links: drop what the scanner computed or accepted for nobody
scan() also built a Related list that no caller reads (the page asks /links/related, which
related() answers); the `within` option was never sent; near() was only ever given a vector, so its
embed-the-text fallback and null answer could not happen. extract.ts and words.ts re-exported
phraseKey and singular from src/core/links, where they now come from directly. The suggestions
are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
2026-11-17 16:10:45 -05:00

41 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* How common an English word is, from a bundled frequency list (words.txt: 20,000 words, most
* frequent first). A word's rank is its line; a word not in the list ranks after all of them.
* Rarity is the cheapest link-worthiness signal there is: "octopus" (rank ~9,700) and "cephalopod"
* (absent) are worth a link where "brain" (~900) is not.
*/
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { singular } from "../../src/core/links";
let ranks: Map<string, number> | null = null;
function load(): Map<string, number> {
if (ranks) return ranks;
ranks = new Map();
const file = fileURLToPath(new URL("./words.txt", import.meta.url));
let i = 0;
for (const line of readFileSync(file, "utf8").split("\n")) {
if (!line || line.startsWith("#")) continue;
ranks.set(line, ++i);
}
return ranks;
}
/** Ranks run 1 (most common) to 20,000; a word not in the list gets this. */
export const UNLISTED = 50_000;
/** The rank of a word (case-insensitive), with a plural or possessive read as its singular. */
export function rank(word: string): number {
const w = word.toLowerCase().replace(/['’]s$/, "");
const r = load();
const direct = r.get(w);
if (direct !== undefined) return direct;
const s = singular(w);
return s !== w ? (r.get(s) ?? UNLISTED) : UNLISTED;
}
/** Whether a lower-case word is in the list at all. */
export const listed = (word: string) => load().has(word.toLowerCase());