Archive SHA-256 9cf076646f6b54fa5c1bcbddae3746e94425a9fc4be4836505da6ceec5841cf5 · 43.1 KB · built Nov 27, 2026
src/text.js
// Deterministic tokenisation for term trends: Unicode-aware words, English stopwords, bigrams that
// never cross punctuation or stopwords.
export const STOPWORDS = new Set(
(
"a about above after again against all almost also am among an and any are aren't around as at be because been before being below between both but by " +
"can can't cannot could couldn't did didn't do does doesn't doing don't down during each either else even ever every few for from further get gets got " +
"had hadn't has hasn't have haven't having he he'd he'll he's her here here's hers herself him himself his how how's however i i'd i'll i'm i've if in into is isn't it " +
"it's its itself just let's like made make makes many may me might more most much must mustn't my myself near need new news no nor not now of off often on once one only or other " +
"others ought our ours ourselves out over own per put rather really said same say says see seen shan't she she'd she'll she's should shouldn't since so some still such " +
"than that that's the their theirs them themselves then there there's these they they'd they'll they're they've this those though through thus to too toward " +
"towards under until up upon us use used using very via want was wasn't way we we'd we'll we're we've well were weren't what what's when when's where where's " +
"whether which while who who's whom whose why why's will with within without won't would wouldn't yet you you'd you'll you're you've your yours yourself yourselves " +
"http https www com html amp nbsp read more continue reading comments comment link click here today week year years day days time two three first last"
).split(/\s+/),
);
// Phrase boundaries: punctuation, dashes, and sentence-final periods (but not the dot in "node.js" or "3.5").
const PHRASE_SPLIT = /[,;:!?()[\]{}"“”|–—/\\<>«»•·…]+|\.(?=\s|$)|\s-\s/u;
const TOKEN_RE = /[\p{L}\p{N}]+(?:['’.+#-][\p{L}\p{N}]+)*[+#]*/gu;
export function tokensOfPhrase(phrase) {
const out = [];
for (const m of phrase.matchAll(TOKEN_RE)) {
let t = m[0].replace(/’/g, "'");
if (t.endsWith("'s")) t = t.slice(0, -2);
out.push(t);
}
return out;
}
export function isContentToken(t, stop) {
if (t.length < 2 || t.length > 40) return false;
if (/^[\p{N}.,'-]+$/u.test(t)) return false;
return !stop.has(t);
}
/**
* Singular key for counting, so "server" and "servers" are one term. Only regular English plurals
* of words longer than three letters change ("-ies" to "-y", a final "s" dropped except after
* s, u or i); "ss", "us", "is" endings and short words such as "aws" are left alone.
*/
export function singularKey(token) {
if (token.length <= 3 || /[^\p{L}]/u.test(token.slice(-2))) return token;
if (token.endsWith("ies") && token.length > 4) return `${token.slice(0, -3)}y`;
if (token.endsWith("s") && !/(ss|us|is)$/.test(token)) return token.slice(0, -1);
return token;
}
/**
* Terms in a text as a Map of counting key -> Set of surface forms seen in this text (unigrams and
* bigrams, lowercased, NFKC). With mergePlurals the key is singular; surface forms keep the wording.
*/
export function termMapOf(text, stop = STOPWORDS, includeBigrams = true, mergePlurals = true) {
const terms = new Map();
const key = mergePlurals ? singularKey : (t) => t;
const add = (k, surface) => {
if (!terms.has(k)) terms.set(k, new Set());
terms.get(k).add(surface);
};
const norm = String(text).normalize("NFKC").toLowerCase();
for (const phrase of norm.split(PHRASE_SPLIT)) {
const toks = tokensOfPhrase(phrase);
for (let i = 0; i < toks.length; i++) {
const a = toks[i];
if (!isContentToken(a, stop)) continue;
add(key(a), a);
if (includeBigrams && i + 1 < toks.length && isContentToken(toks[i + 1], stop)) add(`${key(a)} ${key(toks[i + 1])}`, `${a} ${toks[i + 1]}`);
}
}
return terms;
}
/** Unique unigram and bigram terms in a text (lowercased, NFKC). */
export function termsOf(text, stop = STOPWORDS, includeBigrams = true) {
const terms = new Set();
const norm = String(text).normalize("NFKC").toLowerCase();
for (const phrase of norm.split(PHRASE_SPLIT)) {
const toks = tokensOfPhrase(phrase);
for (let i = 0; i < toks.length; i++) {
const a = toks[i];
if (!isContentToken(a, stop)) continue;
terms.add(a);
if (includeBigrams && i + 1 < toks.length && isContentToken(toks[i + 1], stop)) terms.add(`${a} ${toks[i + 1]}`);
}
}
return terms;
}