/
SpellingDictionaryFromTrie.ts
228 lines (212 loc) · 8.21 KB
/
SpellingDictionaryFromTrie.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import type {
FindFullResult,
FindWordOptions,
SuggestionCollector,
SuggestionResult,
WeightMap,
} from 'cspell-trie-lib';
import { CompoundWordsMethod, importTrie, suggestionCollector, Trie } from 'cspell-trie-lib';
import { getDefaultSettings } from '../Settings';
import { memorizer } from '../util/Memorizer';
import { createMapper } from '../util/repMap';
import { clean } from '../util/util';
import { charsetToRegExp } from './charset';
import {
FindResult,
HasOptions,
SpellingDictionary,
SpellingDictionaryOptions,
SuggestOptions,
} from './SpellingDictionary';
import {
createWeightMapFromDictionaryInformation,
defaultNumSuggestions,
hasOptionToSearchOption,
impersonateCollector,
SuggestArgs,
suggestArgsToSuggestOptions,
wordSearchForms,
wordSuggestFormsArray,
} from './SpellingDictionaryMethods';
export class SpellingDictionaryFromTrie implements SpellingDictionary {
static readonly cachedWordsLimit = 50000;
private _size = 0;
readonly knownWords = new Set<string>();
readonly unknownWords = new Set<string>();
readonly mapWord: (word: string) => string;
readonly type = 'SpellingDictionaryFromTrie';
readonly isDictionaryCaseSensitive: boolean;
readonly containsNoSuggestWords: boolean;
readonly ignoreCharactersRegExp: RegExp | undefined;
private weightMap: WeightMap | undefined;
constructor(
readonly trie: Trie,
readonly name: string,
readonly options: SpellingDictionaryOptions,
readonly source = 'from trie',
size?: number
) {
this.mapWord = createMapper(options.repMap || []);
this.isDictionaryCaseSensitive = options.caseSensitive ?? !trie.isLegacy;
this.containsNoSuggestWords = options.noSuggest || false;
this._size = size || 0;
this.weightMap = options.weightMap || createWeightMapFromDictionaryInformation(options.dictionaryInformation);
this.ignoreCharactersRegExp = charsetToRegExp(this.options.dictionaryInformation?.ignore);
}
public get size(): number {
if (!this._size) {
// walk the trie and get the approximate size.
const i = this.trie.iterate();
let deeper = true;
let size = 0;
for (let r = i.next(); !r.done; r = i.next(deeper)) {
// count all nodes even though they are not words.
// because we are not going to all the leaves, this should give a good enough approximation.
size += 1;
deeper = r.value.text.length < 5;
}
this._size = size;
}
return this._size;
}
public has(word: string, hasOptions?: HasOptions): boolean {
const { useCompounds, ignoreCase } = this.resolveOptions(hasOptions);
const r = this._find(word, useCompounds, ignoreCase);
return !!r && !r.forbidden && !!r.found;
}
public find(word: string, hasOptions?: HasOptions): FindResult | undefined {
const { useCompounds, ignoreCase } = this.resolveOptions(hasOptions);
const r = this._find(word, useCompounds, ignoreCase);
const { forbidden = this.isForbidden(word) } = r || {};
if (!r && !forbidden) return undefined;
const { found = forbidden ? word : false } = r || {};
const noSuggest = found !== false && this.containsNoSuggestWords;
return { found, forbidden, noSuggest };
}
private resolveOptions(hasOptions?: HasOptions): {
useCompounds: HasOptions['useCompounds'] | undefined;
ignoreCase: boolean;
} {
const { useCompounds = this.options.useCompounds, ignoreCase = true } = hasOptionToSearchOption(hasOptions);
return { useCompounds, ignoreCase };
}
private _find = memorizer(
(word: string, useCompounds: number | boolean | undefined, ignoreCase: boolean) =>
this.findAnyForm(word, useCompounds, ignoreCase),
SpellingDictionaryFromTrie.cachedWordsLimit
);
private findAnyForm(
word: string,
useCompounds: number | boolean | undefined,
ignoreCase: boolean
): FindAnyFormResult | undefined {
const outerForms = new Set([word]);
if (this.ignoreCharactersRegExp) {
outerForms.add(word.replace(this.ignoreCharactersRegExp, ''));
outerForms.add(word.normalize('NFD').replace(this.ignoreCharactersRegExp, ''));
outerForms.add(word.normalize('NFC').replace(this.ignoreCharactersRegExp, ''));
}
for (const form of outerForms) {
const r = this._findAnyForm(form, useCompounds, ignoreCase);
if (r) return r;
}
return undefined;
}
private _findAnyForm(
word: string,
useCompounds: number | boolean | undefined,
ignoreCase: boolean
): FindAnyFormResult | undefined {
const mWord = this.mapWord(word.normalize('NFC'));
const opts: FindWordOptions = { caseSensitive: !ignoreCase };
const findResult = this.trie.findWord(mWord, opts);
if (findResult.found !== false) {
return findResult;
}
const forms = wordSearchForms(mWord, this.isDictionaryCaseSensitive, ignoreCase);
for (const w of forms) {
const findResult = this.trie.findWord(w, opts);
if (findResult.found !== false) {
return findResult;
}
}
if (useCompounds) {
opts.useLegacyWordCompounds = useCompounds;
for (const w of forms) {
const findResult = this.trie.findWord(w, opts);
if (findResult.found !== false) {
return findResult;
}
}
}
return undefined;
}
public isNoSuggestWord(word: string, options?: HasOptions): boolean {
return this.containsNoSuggestWords ? this.has(word, options) : false;
}
public isForbidden(word: string): boolean {
return this.trie.isForbiddenWord(word);
}
public suggest(
word: string,
numSuggestions?: number,
compoundMethod?: CompoundWordsMethod,
numChanges?: number,
ignoreCase?: boolean
): SuggestionResult[];
public suggest(word: string, suggestOptions: SuggestOptions): SuggestionResult[];
public suggest(...args: SuggestArgs): SuggestionResult[] {
const [word] = args;
const suggestOptions = suggestArgsToSuggestOptions(args);
return this._suggest(word, suggestOptions);
}
private _suggest(word: string, suggestOptions: SuggestOptions): SuggestionResult[] {
const {
numSuggestions = getDefaultSettings(false).numSuggestions || defaultNumSuggestions,
numChanges,
includeTies,
ignoreCase,
timeout,
} = suggestOptions;
function filter(_word: string): boolean {
return true;
}
const collector = suggestionCollector(
word,
clean({
numSuggestions,
filter,
changeLimit: numChanges,
includeTies,
ignoreCase,
timeout,
weightMap: this.weightMap,
})
);
this.genSuggestions(collector, suggestOptions);
return collector.suggestions.map((r) => ({ ...r, word: r.word }));
}
public genSuggestions(collector: SuggestionCollector, suggestOptions: SuggestOptions): void {
if (this.options.noSuggest) return;
const _compoundMethod =
suggestOptions.compoundMethod ??
(this.options.useCompounds ? CompoundWordsMethod.JOIN_WORDS : CompoundWordsMethod.NONE);
wordSuggestFormsArray(collector.word).forEach((w) =>
this.trie.genSuggestions(impersonateCollector(collector, w), _compoundMethod)
);
}
public getErrors(): Error[] {
return [];
}
}
type FindAnyFormResult = FindFullResult;
export function createSpellingDictionaryTrie(
data: Iterable<string>,
name: string,
source: string,
options: SpellingDictionaryOptions
): SpellingDictionary {
const trieNode = importTrie(data);
const trie = new Trie(trieNode);
return new SpellingDictionaryFromTrie(trie, name, options, source);
}