-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
SpellingDictionaryMethods.ts
133 lines (117 loc) · 4.25 KB
/
SpellingDictionaryMethods.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
import { CompoundWordsMethod, SuggestionCollector, SuggestionResult } from 'cspell-trie-lib';
import { genSequence } from 'gensequence';
import { isUpperCase, removeAccents, ucFirst } from '../util/text';
import { FunctionArgs } from '../util/types';
import { HasOptions, SearchOptions, SpellingDictionary, SuggestOptions } from './SpellingDictionary';
export { suggestionCollector } from 'cspell-trie-lib';
export type FilterSuggestionsPredicate = (word: SuggestionResult) => boolean;
export type SuggestArgs =
| FunctionArgs<SpellingDictionary['suggest']>
| FunctionArgs<
(
word: string,
numSuggestions?: number,
compoundMethod?: CompoundWordsMethod,
numChanges?: number,
ignoreCase?: boolean
) => SuggestionResult[]
>;
export const defaultNumSuggestions = 10;
export function impersonateCollector(collector: SuggestionCollector, word: string): SuggestionCollector {
const r = Object.create(collector);
Object.defineProperty(r, 'word', { value: word, writable: false });
return r;
}
export function wordSearchFormsArray(word: string, isDictionaryCaseSensitive: boolean, ignoreCase: boolean): string[] {
return [...wordSearchForms(word, isDictionaryCaseSensitive, ignoreCase)];
}
export function wordSearchForms(word: string, isDictionaryCaseSensitive: boolean, ignoreCase: boolean): Set<string> {
const forms = new Set<string>();
word = word.normalize('NFC');
const wordLc = word.toLowerCase();
if (ignoreCase) {
if (isDictionaryCaseSensitive) {
forms.add(wordLc);
} else {
forms.add(wordLc);
// Legacy remove any unbound accents
forms.add(wordLc.replace(/\p{M}/gu, ''));
}
} else {
if (isDictionaryCaseSensitive) {
forms.add(word);
forms.add(wordLc);
// HOUSE -> House, house
if (isUpperCase(word)) {
forms.add(ucFirst(wordLc));
}
} else {
forms.add(wordLc);
// Legacy remove any unbound accents
forms.add(wordLc.replace(/\p{M}/gu, ''));
}
}
return forms;
}
export function wordSuggestFormsArray(word: string): string[] {
return [...wordSuggestForms(word)];
}
export function wordSuggestForms(word: string): Set<string> {
word = word.normalize('NFC');
const forms = new Set<string>([word]);
const wordLc = word.toLowerCase();
forms.add(wordLc);
return forms;
}
interface DictionaryWordForm {
w: string; // the word
p: string; // prefix to add
}
function* wordDictionaryForms(word: string, prefixNoCase: string): IterableIterator<DictionaryWordForm> {
word = word.normalize('NFC');
const wordLc = word.toLowerCase();
const wordNa = removeAccents(word);
const wordLcNa = removeAccents(wordLc);
function wf(w: string, p = '') {
return { w, p };
}
const prefix = prefixNoCase;
yield wf(word);
yield wf(wordNa, prefix);
yield wf(wordLc, prefix);
yield wf(wordLcNa, prefix);
}
export function wordDictionaryFormsCollector(prefixNoCase: string): (word: string) => Iterable<string> {
const knownWords = new Set<string>();
return (word: string) => {
return genSequence(wordDictionaryForms(word, prefixNoCase))
.filter((w) => !knownWords.has(w.w))
.map((w) => w.p + w.w)
.filter((w) => !knownWords.has(w))
.map((w) => (knownWords.add(w), w));
};
}
export function hasOptionToSearchOption(opt: HasOptions | undefined): SearchOptions {
return !opt ? {} : opt;
}
export function suggestArgsToSuggestOptions(args: SuggestArgs): SuggestOptions {
const [_word, options, compoundMethod, numChanges, ignoreCase] = args;
const suggestOptions: SuggestOptions =
typeof options === 'object'
? options
: {
numSuggestions: options,
compoundMethod,
numChanges,
ignoreCase,
includeTies: undefined,
timeout: undefined,
};
return suggestOptions;
}
export const __testMethods = {
wordSearchForms,
wordSearchFormsArray,
wordDictionaryForms,
wordDictionaryFormsCollector,
};