tinyfix 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Caleb Fahlgren
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,137 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <img src=".github/banner.png" alt="tinyfix: autocorrect that reads both sides of a word" width="880">
3
+ </p>
2
4
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
5
+ Spell check can't tell that “form” was meant to be “from”, since both are real words. tinyfix can. It's a 2 MB model that reads the words on both sides of each word, and it runs in the browser, so nothing anyone types leaves the page.
6
+
7
+ ```bash
8
+ npm install tinyfix
9
+ ```
10
+
11
+ ```ts
12
+ import { loadTinyfix } from "tinyfix";
13
+
14
+ const tinyfix = await loadTinyfix();
15
+
16
+ await tinyfix.fix("I got a letter form the bank and did not recieve it.");
17
+ // "I got a letter from the bank and did not receive it."
18
+ ```
19
+
20
+ <p align="center">
21
+ <img src=".github/what-it-does.png" alt="You type: I got a letter form the bank. Their is a problem, and I am to tired to recieve it. You get: I got a letter from the bank. There is a problem, and I am too tired to receive it." width="880">
22
+ </p>
23
+
24
+ **[Try it in your browser](https://huggingface.co/spaces/paperweight/tinyfix).** It's also the autocorrect in [offlinepage.app](https://offlinepage.app), a writing app that works in airplane mode.
25
+
26
+ ## Getting the fixes
27
+
28
+ `check` returns what it would change and where, so you can show it your own way: underline it, ask first, or apply it.
29
+
30
+ ```ts
31
+ const text = "I got a letter form the bank and did not recieve it.";
32
+ const fixes = await tinyfix.check(text);
33
+ // [
34
+ // { from: 15, to: 19, original: "form", replacement: "from", kind: "context", confidence: 1 },
35
+ // { from: 41, to: 48, original: "recieve", replacement: "receive", kind: "spelling", confidence: 0.95 },
36
+ // ]
37
+
38
+ tinyfix.apply(text, fixes);
39
+ // "I got a letter from the bank and did not receive it."
40
+ ```
41
+
42
+ A fix is one of two kinds:
43
+
44
+ - `"spelling"`: the word isn't a word. “recieve” becomes “receive”.
45
+ - `"context"`: the word is real but doesn't fit the words around it. “form” becomes “from”.
46
+
47
+ ## API
48
+
49
+ | Call | What it does |
50
+ | ------------------------------- | ------------------------------------------------------------------------ |
51
+ | `loadTinyfix(options?)` | Downloads the model and word lists, and returns a checker. Call it once. |
52
+ | `tinyfix.check(text, options?)` | Returns the fixes for a text, in order. They never overlap. |
53
+ | `tinyfix.apply(text, fixes)` | Returns the text with those fixes made. |
54
+ | `tinyfix.fix(text, options?)` | `check`, then `apply`. |
55
+ | `tinyfix.ignore(word)` | Never change this word again. |
56
+ | `tinyfix.dispose()` | Frees the model. |
57
+
58
+ ### `loadTinyfix` options
59
+
60
+ | Option | Default | What it does |
61
+ | ----------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
62
+ | `baseUrl` | The [model repo](https://huggingface.co/paperweight/tinyfix) on Hugging Face | Where the four model files are, if you host them yourself |
63
+ | `model` | `true` | `false` skips the model. Only the word list is downloaded and only misspellings are fixed. |
64
+ | `mode` | `"balanced"` | `"careful"` makes fewer wrong changes and fixes fewer wrong words |
65
+ | `wasmPaths` | Found by your bundler | Where ONNX Runtime's `.wasm` files are, if you host those too |
66
+
67
+ ### `check` options
68
+
69
+ | Option | What it does |
70
+ | ------- | ---------------------------------------------------------------------------------------------- |
71
+ | `range` | `[from, to]`. Only words inside it are checked; the rest of the text is still read as context. |
72
+ | `kinds` | `["spelling"]`, `["context"]` or both. Both by default. |
73
+
74
+ ## Fixing as someone types
75
+
76
+ `check` treats the text as finished writing. To fix while someone types, check one word at a time with `range`, so you never re-check the whole page on a keystroke. offlinepage.app does this:
77
+
78
+ ```ts
79
+ // When a word is finished: is it misspelled?
80
+ await tinyfix.check(paragraph, { range: [wordStart, wordEnd], kinds: ["spelling"] });
81
+
82
+ // Two words later, when it has words on both sides: is it the wrong word?
83
+ await tinyfix.check(paragraph, { range: [earlierStart, earlierEnd], kinds: ["context"] });
84
+ ```
85
+
86
+ When someone undoes a fix, call `tinyfix.ignore(fix.original)` so it doesn't come back.
87
+
88
+ ## How well it works
89
+
90
+ <table>
91
+ <tr>
92
+ <td align="center" width="33%"><h3>94%</h3>of its fixes are right</td>
93
+ <td align="center" width="33%"><h3>40%</h3>of wrong words fixed</td>
94
+ <td align="center" width="33%"><h3>~3 ms</h3>per check in Chrome</td>
95
+ </tr>
96
+ </table>
97
+
98
+ On about 110,000 words of correct writing it wrongly changed about 1 word in 2,000. It would rather leave a typo alone than change a word you meant, so it misses more wrong words than it catches. The [model card](https://huggingface.co/paperweight/tinyfix) has the test sets, what it misses and how it was trained.
99
+
100
+ ## What it leaves alone
101
+
102
+ - Code in backticks, inline or fenced
103
+ - Web addresses, emails, file paths, `#tags` and words with digits
104
+ - Names, and words in ALL CAPS
105
+ - Anything you pass to `ignore`
106
+
107
+ A line break counts as the end of a sentence. Positions are JavaScript string indexes.
108
+
109
+ ## What it downloads
110
+
111
+ About 3.5 MB the first time, from Hugging Face: the 2.1 MB model, its 20,000-word vocabulary, a list of words people mix up, and an 80,000-word spelling list. Each version of this package loads one exact revision of those files, so it always behaves the same.
112
+
113
+ To serve them from your own site, copy the four files from the [model repo](https://huggingface.co/paperweight/tinyfix/tree/main) (`wrong-word.onnx`, `wrong-word-vocab.txt`, `wrong-word-confusions.txt`, `words.txt`) and point `baseUrl` at the folder:
114
+
115
+ ```ts
116
+ const tinyfix = await loadTinyfix({ baseUrl: "/models/" });
117
+ ```
118
+
119
+ The model runs on [ONNX Runtime Web](https://onnxruntime.ai/docs/tutorials/web/), which is installed with this package. On a page it runs in tinyfix's own worker, so loading it and checking words don't freeze typing. Vite and webpack give the worker a file of its own with no setup. With a bundler that doesn't, the model runs on the page instead, which works the same but can pause typing briefly while it loads.
120
+
121
+ ## Where it runs
122
+
123
+ Browsers, Web Workers and Node 18 or later. English only.
124
+
125
+ ## Development
126
+
127
+ ```bash
128
+ npm test # downloads the model files once, then runs the tests
129
+ npm run check # types
130
+ npm run lint
131
+ ```
132
+
133
+ The [demo page](https://huggingface.co/spaces/paperweight/tinyfix) is in [`demo/`](demo), and runs on this repo's build of the package.
134
+
135
+ ## License
136
+
137
+ MIT, for this package and for the model.
@@ -0,0 +1,65 @@
1
+ import { WordTrie } from "./trie.js";
2
+ export interface ContextScorer {
3
+ /** Returns the log probability of each word appearing right after `textBefore`. */
4
+ score(textBefore: string, words: readonly string[]): Promise<number[]>;
5
+ }
6
+ export interface FixerWeights {
7
+ frequency: number;
8
+ context: number;
9
+ keepCost: number;
10
+ minConfidence: number;
11
+ }
12
+ export declare const DEFAULT_WEIGHTS: FixerWeights;
13
+ /** Use with a context scorer, so the model helps choose between spelling candidates. */
14
+ export declare const WITH_MODEL_WEIGHTS: FixerWeights;
15
+ export interface WordEntry {
16
+ word: string;
17
+ count: number;
18
+ }
19
+ export interface Candidate {
20
+ word: string;
21
+ cost: number;
22
+ logFrequency: number;
23
+ contextGain: number;
24
+ }
25
+ export interface Correction {
26
+ original: string;
27
+ replacement: string;
28
+ confidence: number;
29
+ /** True when the word was spelled correctly but didn't fit the sentence. */
30
+ usedContext?: boolean;
31
+ }
32
+ export interface FixerOptions {
33
+ words: readonly WordEntry[];
34
+ /** A trie already built from `words`, in the same order. Built here when missing. */
35
+ trie?: WordTrie;
36
+ scorer?: ContextScorer;
37
+ weights?: Partial<FixerWeights>;
38
+ maxCandidates?: number;
39
+ }
40
+ export declare function parseWordList(text: string): WordEntry[];
41
+ export declare function pickCandidate(candidates: readonly Candidate[], weights: FixerWeights): {
42
+ word: string;
43
+ confidence: number;
44
+ } | null;
45
+ export declare class Fixer {
46
+ private readonly words;
47
+ private readonly known;
48
+ private readonly trie;
49
+ private readonly scorer;
50
+ private readonly weights;
51
+ private readonly maxCandidates;
52
+ private readonly totalCount;
53
+ constructor(options: FixerOptions);
54
+ /** Treats a word as correct from now on, for example after the user undoes a fix. */
55
+ learn(word: string): void;
56
+ isKnown(word: string): boolean;
57
+ /** Ranks spelling candidates for an unknown word, or returns null when the word should be left alone. */
58
+ analyze(textBefore: string, word: string): Promise<Candidate[] | null>;
59
+ /** Returns a correction for the word just finished after `textBefore`, or null to keep it. */
60
+ correct(textBefore: string, word: string): Promise<Correction | null>;
61
+ private readonly undone;
62
+ /** Stops a fixed rule from firing again for a word the user restored. */
63
+ rememberUndo(word: string): void;
64
+ wasUndone(word: string): boolean;
65
+ }
@@ -0,0 +1,99 @@
1
+ import { applyFixedRule, isBritishSpelling, isCheckable, isNewerWord, matchCase } from "./rules.js";
2
+ import { WordTrie } from "./trie.js";
3
+ import { typoCost } from "./typoCost.js";
4
+ export const DEFAULT_WEIGHTS = { frequency: 0.5, context: 0, keepCost: 10, minConfidence: 0.7 };
5
+ /** Use with a context scorer, so the model helps choose between spelling candidates. */
6
+ export const WITH_MODEL_WEIGHTS = { ...DEFAULT_WEIGHTS, context: 0.25 };
7
+ export function parseWordList(text) {
8
+ return text.split("\n").flatMap((line) => {
9
+ const [word, count] = line.trim().split(/\s+/);
10
+ return word && count ? [{ word, count: Number(count) }] : [];
11
+ });
12
+ }
13
+ export function pickCandidate(candidates, weights) {
14
+ if (candidates.length === 0)
15
+ return null;
16
+ const scored = candidates.map((candidate) => ({
17
+ word: candidate.word,
18
+ score: -candidate.cost + weights.frequency * candidate.logFrequency + weights.context * candidate.contextGain,
19
+ }));
20
+ const best = scored.reduce((top, entry) => (entry.score > top.score ? entry : top));
21
+ const total = scored.reduce((sum, entry) => sum + Math.exp(entry.score - best.score), Math.exp(-weights.keepCost - best.score));
22
+ const confidence = 1 / total;
23
+ return confidence >= weights.minConfidence ? { word: best.word, confidence } : null;
24
+ }
25
+ export class Fixer {
26
+ words;
27
+ known = new Set();
28
+ trie;
29
+ scorer;
30
+ weights;
31
+ maxCandidates;
32
+ totalCount;
33
+ constructor(options) {
34
+ this.words = options.words;
35
+ this.words.forEach((entry) => this.known.add(entry.word));
36
+ this.trie = options.trie ?? new WordTrie(this.words.map((entry) => entry.word));
37
+ this.scorer = options.scorer;
38
+ this.weights = { ...DEFAULT_WEIGHTS, ...options.weights };
39
+ this.maxCandidates = options.maxCandidates ?? 12;
40
+ this.totalCount = this.words.reduce((sum, entry) => sum + entry.count, 0);
41
+ }
42
+ /** Treats a word as correct from now on, for example after the user undoes a fix. */
43
+ learn(word) {
44
+ this.known.add(word.toLowerCase());
45
+ }
46
+ isKnown(word) {
47
+ const lower = word.toLowerCase();
48
+ const isListed = (candidate) => this.known.has(candidate);
49
+ return isListed(lower) || isNewerWord(lower) || isBritishSpelling(lower, isListed);
50
+ }
51
+ /** Ranks spelling candidates for an unknown word, or returns null when the word should be left alone. */
52
+ async analyze(textBefore, word) {
53
+ if (!isCheckable(word, textBefore) || this.isKnown(word))
54
+ return null;
55
+ const lower = word.toLowerCase();
56
+ const maxEdits = lower.length <= 4 ? 1 : 2;
57
+ const ranked = this.trie
58
+ .search(lower, maxEdits)
59
+ .map(({ index }) => {
60
+ const entry = this.words[index];
61
+ return {
62
+ word: entry.word,
63
+ cost: typoCost(lower, entry.word),
64
+ logFrequency: Math.log(entry.count / this.totalCount),
65
+ contextGain: 0,
66
+ };
67
+ })
68
+ .sort((first, second) => second.logFrequency - second.cost - (first.logFrequency - first.cost))
69
+ .slice(0, this.maxCandidates);
70
+ if (!this.scorer || ranked.length === 0)
71
+ return ranked;
72
+ const scores = await this.scorer.score(textBefore, [
73
+ word,
74
+ ...ranked.map((candidate) => matchCase(word, candidate.word)),
75
+ ]);
76
+ const typedScore = scores[0];
77
+ return ranked.map((candidate, index) => ({ ...candidate, contextGain: scores[index + 1] - typedScore }));
78
+ }
79
+ /** Returns a correction for the word just finished after `textBefore`, or null to keep it. */
80
+ async correct(textBefore, word) {
81
+ const ruled = applyFixedRule(word);
82
+ if (ruled && ruled !== word && !this.wasUndone(word))
83
+ return { original: word, replacement: ruled, confidence: 1 };
84
+ const candidates = await this.analyze(textBefore, word);
85
+ if (!candidates)
86
+ return null;
87
+ const pick = pickCandidate(candidates, this.weights);
88
+ return pick ? { original: word, replacement: matchCase(word, pick.word), confidence: pick.confidence } : null;
89
+ }
90
+ undone = new Set();
91
+ /** Stops a fixed rule from firing again for a word the user restored. */
92
+ rememberUndo(word) {
93
+ this.undone.add(word.toLowerCase());
94
+ this.learn(word);
95
+ }
96
+ wasUndone(word) {
97
+ return this.undone.has(word.toLowerCase());
98
+ }
99
+ }
@@ -0,0 +1,44 @@
1
+ import type * as Ort from "onnxruntime-web/wasm";
2
+ /** One input to the wrong-word model: token ids, as many rows as there are words to score. */
3
+ export interface ModelInput {
4
+ data: BigInt64Array;
5
+ dims: [rows: number, width: number];
6
+ }
7
+ export interface ModelInputs {
8
+ left: ModelInput;
9
+ right: ModelInput;
10
+ candidates: ModelInput;
11
+ }
12
+ /** The wrong-word model, wherever it runs: in this thread, or in tinyfix's own worker. */
13
+ export interface Model {
14
+ /** Scores each row's candidate words. */
15
+ run(inputs: ModelInputs): Promise<Float32Array>;
16
+ release(): Promise<void>;
17
+ }
18
+ /** What the page sends tinyfix's worker. */
19
+ export type ToWorker = {
20
+ type: "load";
21
+ model: ArrayBuffer;
22
+ wasmPaths: string | undefined;
23
+ } | {
24
+ type: "run";
25
+ id: number;
26
+ inputs: ModelInputs;
27
+ };
28
+ /** What tinyfix's worker sends back. */
29
+ export type FromWorker = {
30
+ type: "loaded";
31
+ } | {
32
+ type: "load-failed";
33
+ message: string;
34
+ } | {
35
+ type: "result";
36
+ id: number;
37
+ scores: Float32Array;
38
+ } | {
39
+ type: "run-failed";
40
+ id: number;
41
+ message: string;
42
+ };
43
+ /** Starts the model in this thread. ONNX Runtime is passed in, so a worker can import it and a page needn't. */
44
+ export declare function createModel(ort: typeof Ort, bytes: ArrayBuffer, wasmPaths: string | undefined): Promise<Model>;
@@ -0,0 +1,20 @@
1
+ /** Starts the model in this thread. ONNX Runtime is passed in, so a worker can import it and a page needn't. */
2
+ export async function createModel(ort, bytes, wasmPaths) {
3
+ // One thread, so a page needs no cross-origin isolation headers.
4
+ ort.env.wasm.numThreads = 1;
5
+ if (wasmPaths)
6
+ ort.env.wasm.wasmPaths = wasmPaths;
7
+ const session = await ort.InferenceSession.create(new Uint8Array(bytes), { executionProviders: ["wasm"] });
8
+ const tensor = (input) => new ort.Tensor("int64", input.data, input.dims);
9
+ return {
10
+ async run(inputs) {
11
+ const output = await session.run({
12
+ left: tensor(inputs.left),
13
+ right: tensor(inputs.right),
14
+ candidates: tensor(inputs.candidates),
15
+ });
16
+ return output.scores.data;
17
+ },
18
+ release: () => session.release(),
19
+ };
20
+ }
@@ -0,0 +1,7 @@
1
+ import type { Model } from "./model.js";
2
+ /**
3
+ * Starts the model in tinyfix's own worker. Bundlers that understand `new Worker(new URL(…, import.meta.url))`, such
4
+ * as Vite and webpack, give the worker a file of its own. If the worker can't start, this rejects, and tinyfix runs
5
+ * the model on the page instead.
6
+ */
7
+ export declare function loadModelInWorker(bytes: ArrayBuffer, wasmPaths: string | undefined): Promise<Model>;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Starts the model in tinyfix's own worker. Bundlers that understand `new Worker(new URL(…, import.meta.url))`, such
3
+ * as Vite and webpack, give the worker a file of its own. If the worker can't start, this rejects, and tinyfix runs
4
+ * the model on the page instead.
5
+ */
6
+ export function loadModelInWorker(bytes, wasmPaths) {
7
+ return new Promise((resolve, reject) => {
8
+ const worker = new Worker(new URL("./modelWorker.js", import.meta.url), { type: "module", name: "tinyfix" });
9
+ const send = (message, transfer = []) => worker.postMessage(message, transfer);
10
+ const pending = new Map();
11
+ let nextId = 0;
12
+ const fail = (error) => {
13
+ reject(error);
14
+ for (const run of pending.values())
15
+ run.reject(error);
16
+ pending.clear();
17
+ worker.terminate();
18
+ };
19
+ const model = {
20
+ run: (inputs) => new Promise((resolveRun, rejectRun) => {
21
+ const id = nextId++;
22
+ pending.set(id, { resolve: resolveRun, reject: rejectRun });
23
+ const buffers = [inputs.left.data.buffer, inputs.right.data.buffer, inputs.candidates.data.buffer];
24
+ send({ type: "run", id, inputs }, buffers);
25
+ }),
26
+ release: async () => worker.terminate(),
27
+ };
28
+ worker.onerror = (event) => {
29
+ event.preventDefault();
30
+ fail(new Error(`tinyfix's worker stopped: ${event.message || "it couldn't start"}.`));
31
+ };
32
+ worker.onmessage = ({ data }) => {
33
+ switch (data.type) {
34
+ case "loaded":
35
+ resolve(model);
36
+ return;
37
+ case "load-failed":
38
+ fail(new Error(`tinyfix's model couldn't start: ${data.message}`));
39
+ return;
40
+ case "result":
41
+ pending.get(data.id)?.resolve(data.scores);
42
+ pending.delete(data.id);
43
+ return;
44
+ case "run-failed":
45
+ pending.get(data.id)?.reject(new Error(data.message));
46
+ pending.delete(data.id);
47
+ return;
48
+ default:
49
+ data;
50
+ }
51
+ };
52
+ send({ type: "load", model: bytes, wasmPaths }, [bytes]);
53
+ });
54
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ // tinyfix's worker: it holds the model, so starting it and running it never block the page. It only imports ONNX
2
+ // Runtime and the code below, never the page's own scripts.
3
+ import * as ort from "onnxruntime-web/wasm";
4
+ import { createModel } from "./model.js";
5
+ const reply = globalThis.postMessage;
6
+ let model = null;
7
+ globalThis.onmessage = async ({ data }) => {
8
+ switch (data.type) {
9
+ case "load":
10
+ try {
11
+ model = await createModel(ort, data.model, data.wasmPaths);
12
+ reply({ type: "loaded" });
13
+ }
14
+ catch (error) {
15
+ reply({ type: "load-failed", message: String(error) });
16
+ }
17
+ return;
18
+ case "run":
19
+ try {
20
+ if (!model)
21
+ throw new Error("The model isn't loaded yet.");
22
+ const scores = await model.run(data.inputs);
23
+ reply({ type: "result", id: data.id, scores }, [scores.buffer]);
24
+ }
25
+ catch (error) {
26
+ reply({ type: "run-failed", id: data.id, message: String(error) });
27
+ }
28
+ return;
29
+ default:
30
+ data;
31
+ }
32
+ };
@@ -0,0 +1,10 @@
1
+ /** True for newer everyday words the word list doesn't have. */
2
+ export declare const isNewerWord: (lower: string) => boolean;
3
+ /** Recognizes British spellings, so "colour" isn't treated as a misspelling of "color". */
4
+ export declare function isBritishSpelling(lower: string, isKnown: (word: string) => boolean): boolean;
5
+ export declare function isSentenceStart(textBefore: string): boolean;
6
+ /** Applies typed casing to a replacement, so "Teh" becomes "The". */
7
+ export declare function matchCase(typed: string, replacement: string): string;
8
+ export declare function applyFixedRule(word: string): string | null;
9
+ /** Words the engine must leave alone: names, acronyms, identifiers and anything not plainly a word. */
10
+ export declare function isCheckable(word: string, textBefore: string): boolean;
@@ -0,0 +1,117 @@
1
+ const CONTRACTIONS = new Map(Object.entries({
2
+ arent: "aren't",
3
+ cant: "can't",
4
+ couldnt: "couldn't",
5
+ didnt: "didn't",
6
+ doesnt: "doesn't",
7
+ dont: "don't",
8
+ hadnt: "hadn't",
9
+ hasnt: "hasn't",
10
+ havent: "haven't",
11
+ im: "I'm",
12
+ isnt: "isn't",
13
+ ive: "I've",
14
+ shouldnt: "shouldn't",
15
+ thats: "that's",
16
+ theres: "there's",
17
+ theyre: "they're",
18
+ theyve: "they've",
19
+ wasnt: "wasn't",
20
+ werent: "weren't",
21
+ weve: "we've",
22
+ whats: "what's",
23
+ wont: "won't",
24
+ wouldnt: "wouldn't",
25
+ youre: "you're",
26
+ youve: "you've",
27
+ }));
28
+ const BRITISH_OUR_ENDING = /(.{2,})our(s|ed|ing|ite|ites|ful|able|ably|less|hood|hoods|ly|ist|ists|er|ers)?$/;
29
+ const BRITISH_ISE_ENDING = /(is|ys)(e|es|ed|ing|er|ers|ation|ations|able)$/;
30
+ const BRITISH_DOUBLED_L = /^(un|re|mis|pre)?(travel|cancel|model|label|signal|fuel|level|total|marvel|counsel|jewel|tunnel|channel|shovel|dial|equal|quarrel|rival|panel|funnel|pencil|spiral|trial)l(ed|ing|er|ers|or|ors)$/;
31
+ const MIN_LENGTH_FOR_ISE_RULE = 7;
32
+ /** "councillor", "panellist", "pedalling": a doubled l before an ending where American English has one. */
33
+ const BRITISH_DOUBLED_L_ENDING = /ll(ed|ing|er|ers|or|ors|ist|ists|ous)$/;
34
+ /** "epicentre", "kilometres", "centred": re where American English has er. */
35
+ const BRITISH_RE_ENDING = /([^aeiou])re(s|d)?$/;
36
+ const MIN_LENGTH_FOR_RE_RULE = 5;
37
+ /** "paediatric", "haemorrhage", "oesophagus": ae and oe where American English has e. */
38
+ const BRITISH_DIGRAPH = /(ae|oe)/;
39
+ const MIN_LENGTH_FOR_DIGRAPH_RULE = 7;
40
+ const BRITISH_WORDS = new Set(("theatre centre centred metre litre fibre calibre sombre spectre defence offence licence pretence catalogue analogue programme judgement " +
41
+ "ageing aluminium fulfil enrol skilful wilful jewellery tyre grey mould moulding plough sceptical sceptic cheque kerb draught manoeuvre " +
42
+ "paediatric anaesthetic foetus oestrogen moustache pyjamas storey acknowledgement instalment distil instil appal " +
43
+ "whisky enquiry amongst whilst")
44
+ .split(" ")
45
+ .flatMap((word) => [word, `${word}s`]));
46
+ /** Everyday words newer than the word list, which would otherwise be "corrected" (emoji → meiji, todo → too). */
47
+ const NEWER_WORDS = new Set(("https emoji todo selfie hashtag livestream chatbot vlog vlogger influencer fintech covid podcaster blogger unfollow " +
48
+ "unfriend retweet screenshare webinar onboarding offboarding skincare smartwatch earbud airpod wifi bluetooth " +
49
+ "async runtime frontend backend dataset username login logout signup checkout touchscreen spreadsheet " +
50
+ "workflow timeline toolbar dropdown popup tooltip codebase repo config dev devops cybersecurity crypto " +
51
+ "kombucha matcha ramen udon pho bao boba poke quinoa kale hummus falafel tahini sriracha kimchi gochujang " +
52
+ "tofu tempeh seitan acai chia granola brunch latte cappuccino espresso macchiato americano frappe smoothie " +
53
+ "barista crema robusta arabica croissant focaccia ciabatta brioche sourdough gelato prosecco charcuterie " +
54
+ "hygge wellness mindfulness hangry yeet meme binge rewatch preorder crowdfund carpool rideshare " +
55
+ "staycation glamping bae vibe vibes ghosting doomscroll doomscrolling cringe adulting")
56
+ .split(" ")
57
+ .flatMap((word) => [word, `${word}s`]));
58
+ /** True for newer everyday words the word list doesn't have. */
59
+ export const isNewerWord = (lower) => NEWER_WORDS.has(lower);
60
+ /** Recognizes British spellings, so "colour" isn't treated as a misspelling of "color". */
61
+ export function isBritishSpelling(lower, isKnown) {
62
+ if (BRITISH_WORDS.has(lower))
63
+ return true;
64
+ const doubled = BRITISH_DOUBLED_L.exec(lower);
65
+ if (doubled)
66
+ return true;
67
+ const withOr = lower.replace(BRITISH_OUR_ENDING, "$1or$2");
68
+ if (withOr !== lower && isKnown(withOr))
69
+ return true;
70
+ const singleL = lower.replace(BRITISH_DOUBLED_L_ENDING, "l$1");
71
+ if (singleL !== lower && isKnown(singleL))
72
+ return true;
73
+ const withEr = lower.length < MIN_LENGTH_FOR_RE_RULE
74
+ ? lower
75
+ : lower.replace(BRITISH_RE_ENDING, (_, before, ending = "") => `${before}er${ending === "d" ? "ed" : ending}`);
76
+ if (withEr !== lower && isKnown(withEr))
77
+ return true;
78
+ if (lower.length >= MIN_LENGTH_FOR_DIGRAPH_RULE &&
79
+ BRITISH_DIGRAPH.test(lower) &&
80
+ isKnown(lower.replace(BRITISH_DIGRAPH, "e")))
81
+ return true;
82
+ if (lower.length < MIN_LENGTH_FOR_ISE_RULE)
83
+ return false;
84
+ const withZ = lower.replace(BRITISH_ISE_ENDING, (_, stem, ending) => `${stem === "is" ? "iz" : "yz"}${ending}`);
85
+ return withZ !== lower && isKnown(withZ);
86
+ }
87
+ export function isSentenceStart(textBefore) {
88
+ return /(^|[.!?:\n]\s*|^\s*[-*•]\s+)$/.test(textBefore);
89
+ }
90
+ /** Applies typed casing to a replacement, so "Teh" becomes "The". */
91
+ export function matchCase(typed, replacement) {
92
+ const isCapitalized = typed[0] !== typed[0].toLowerCase();
93
+ return isCapitalized ? replacement[0].toUpperCase() + replacement.slice(1) : replacement;
94
+ }
95
+ export function applyFixedRule(word) {
96
+ if (word === "i")
97
+ return "I";
98
+ const lower = word.toLowerCase();
99
+ const contraction = CONTRACTIONS.get(lower);
100
+ if (!contraction)
101
+ return null;
102
+ return contraction.startsWith("I") ? contraction : matchCase(word, contraction);
103
+ }
104
+ /** Words the engine must leave alone: names, acronyms, identifiers and anything not plainly a word. */
105
+ export function isCheckable(word, textBefore) {
106
+ if (word.length < 3 || !/^[A-Za-z]+$/.test(word))
107
+ return false;
108
+ if (word === word.toUpperCase())
109
+ return false;
110
+ if (/[A-Z]/.test(word.slice(1)))
111
+ return false;
112
+ const isCapitalized = word[0] !== word[0].toLowerCase();
113
+ if (isCapitalized && !isSentenceStart(textBefore))
114
+ return false;
115
+ const previousToken = textBefore.trimEnd().split(/\s/).pop() ?? "";
116
+ return !/[/@\\_#]|\.\w/.test(previousToken);
117
+ }
@@ -0,0 +1,18 @@
1
+ export interface TrieMatch {
2
+ index: number;
3
+ distance: number;
4
+ }
5
+ /** Compact trie that finds dictionary words within a small edit distance of a query. */
6
+ export declare class WordTrie {
7
+ private readonly character;
8
+ private readonly firstChild;
9
+ private readonly nextSibling;
10
+ private readonly wordIndex;
11
+ constructor(words: readonly string[]);
12
+ /** Builds the same trie a few thousand words at a time, calling `pause` in between so a page stays responsive. */
13
+ static inSteps(words: readonly string[], pause: () => Promise<void>, step?: number): Promise<WordTrie>;
14
+ private insert;
15
+ private findOrCreateChild;
16
+ /** Returns every word within `maxEdits` insertions, deletions, substitutions or adjacent swaps. */
17
+ search(query: string, maxEdits: number): TrieMatch[];
18
+ }