UNPKG

@dcoffey/espells

Version:

Pure JS/TS spellchecker, using Hunspell dictionaries. Based on Spylls.

51 lines 1.94 kB
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { CONSTANTS as C } from "../constants.js"; import { lowercase, ngram } from "../util.js"; import { finalScore, rootScore, ScoresList } from "./scores.js"; /** * Builder for phonetic suggestions. * * @see {@link PhonetTable} */ export class PhonetSuggestionBuilder { /** * @param misspelling - The misspelling to build suggestions for. * @param table - The {@link PhonetTable} to use. */ constructor(misspelling, table) { this.misspelling = misspelling; this.misspellingPH = table.metaphone(misspelling); this.table = table; this.scores = new ScoresList(C.PHONET_MAX_ROOTS); } /** * Steps the builder forward by providing another {@link Word} to process. * * @param word - The {@link Word} to process. */ step(word) { if (Math.abs(word.stem.length - this.misspelling.length) > 3) return; let nscore = rootScore(this.misspelling, word.stem); if (word.altSpellings?.size) { for (const variant of word.altSpellings) { nscore = Math.max(nscore, rootScore(this.misspelling, variant)); } } if (nscore <= 2) return; const score = 2 * ngram(3, this.misspellingPH, this.table.metaphone(word.stem), false, false, true); this.scores.add(score, word.stem); } /** Finishes the builder and yields the resulting suggestions (as strings). */ *finish() { const guesses = this.scores.finish(([score, word]) => [score + finalScore(this.misspelling, lowercase(word)), word]); for (const [suggestion] of guesses) { yield suggestion; } } } //# sourceMappingURL=phonet.js.map