@lp-labs/document-spellcheck
v0.1.0
Published
Caller-lexicon Unicode word spellchecking with bounded edit distance and deterministic case-aware suggestions.
Readme
@lp-labs/document-spellcheck
Caller-lexicon Unicode word spellchecking with bounded edit distance and deterministic case-aware suggestions.
Original, independent MIT implementation. Zero runtime, peer or sibling dependencies. Requires Node.js 18+; strict TypeScript declarations support both ESM and CommonJS.
Install
npm install @lp-labs/document-spellcheckQuickstart
import { tokenizeWords, createSpellchecker } from '@lp-labs/document-spellcheck';
const checker = createSpellchecker(['hello', 'world', 'café'], { maxDistance: 1 });
const errors = checker.check('Helo world');
if (
errors[0].suggestions[0] !== 'Hello' ||
checker.check('cafe\u0301').length ||
tokenizeWords('😀hello')[0].start !== 2
)
throw new Error('Spellcheck mismatch');API and types
tokenizeWords(text): SpellToken[] recognizes Unicode letters and combining marks, with internal straight/curly apostrophes; offsets are original UTF-16 start/end. createSpellchecker(lexicon: readonly string[], options?): Spellchecker owns normalized lexicon data (NFC, lowercase, curly apostrophe mapped to straight). check(text): Misspelling[] returns unknown tokens and suggestions. suggest(word): string[] ranks candidates by bounded Levenshtein code-point distance then deterministic normalized lexical order. Options: maxDistance (2 default), maxSuggestions (5 default), maxWork (2,000,000 default). Case is rendered as all caps/title case/lowercase based on the input token.
Customize
Supply your own lexicon, including domain terminology. Reduce maxDistance or maxWork for interactive use; split long text into bounded chunks. Apostrophe variants and composed/decomposed accents share normalized dictionary keys. Duplicate normalized entries use the first spelling.
Errors and bounds
10,000 lexicon words/tokens, 64 code points per word, 1,000,000 text units; maxDistance 0..3, maxSuggestions 1..20, maxWork 1..10,000,000. Suggestion work charges (token length+1)(2distance+1) for each length-eligible candidate, shared across a check invocation; overflow rejects the whole check, not truncated output. Banded DP uses O(word length) memory and O(word length*distance) work per candidate, plus candidate sorting. No dictionary, copyrighted corpus, statistical model, grammar engine, stemming, locale-specific segmentation or CJK word inference is shipped.
Invalid runtime inputs throw TypeError/RangeError or the documented typed error. Adapter errors propagate unchanged unless specifically noted; asynchronous methods reject. Inputs must be ordinary typed data, not hostile proxies or accessors. No I/O happens at import time. All runtime exports are documented above and the example executes without optional packages.
Development
npm ci --offline --ignore-scripts --no-audit --no-fund
npm run build
npm run typecheck
npm test
npm run format:checkBuild emits dist/esm and dist/cjs with matching declarations. Tests use Node's test runner; consumer.mts and consumer.cts validate meaningful bare-package imports and expected type errors. Only TypeScript ^5.9.3 and Prettier ^3.6.2 are development dependencies.
Support
If this library is useful to you, consider supporting its maintenance with a donation via PayPal.
Author
Created by Libin Prasanth. Connect on LinkedIn for updates and discussion.
