/
suggest-en-a-star.test.ts
253 lines (237 loc) · 8.6 KB
/
suggest-en-a-star.test.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { readTrie } from './dictionaries.test.helper';
import { GenSuggestionOptionsStrict } from './genSuggestionsOptions';
import { genCompoundableSuggestions, suggest } from './suggestAStar';
import {
suggestionCollector,
SuggestionCollectorOptions,
SuggestionResult,
SuggestionCollector,
} from './suggestCollector';
import { createTimer } from './timer';
import { CompoundWordsMethod } from './walker';
function getTrie() {
return readTrie('@cspell/dict-en_us/cspell-ext.json');
}
const timeout = 10000;
interface ExpectedSuggestion extends Partial<SuggestionResult> {
word: string;
}
describe('Validate English Suggestions', () => {
interface WordSuggestionsTest {
word: string;
expected: ExpectedSuggestion[];
}
// cspell:ignore emplode ballence catagory cateogry
test.each`
word | expected
${'hello'} | ${sr({ word: 'hello', cost: 0 })}
${'apple'} | ${sr({ word: 'apple', cost: 0 }, { word: 'apples', cost: 100 })}
${'emplode'} | ${sr('implode')}
${'dont'} | ${sr("don't")}
${'ballence'} | ${sr('balance')}
${'catagory'} | ${sr('category')}
${'cateogry'} | ${sr({ word: 'category', cost: 75 })}
`('suggestions for $word', async ({ word, expected }: WordSuggestionsTest) => {
const trie = await getTrie();
const x = suggest(trie.root, word, {});
expect(x).toEqual(expect.arrayContaining(expected.map((e) => expect.objectContaining(e))));
});
test(
'Tests suggestions "joyful"',
async () => {
const trie = await getTrie();
const collector = suggestionCollector('joyful', opts(8, undefined, 1));
collector.collect(
genCompoundableSuggestions(trie.root, collector.word, sugGenOptsFromCollector(collector)),
timeout
);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(expect.arrayContaining(['joyful']));
expect(suggestions[0]).toBe('joyful');
},
timeout
);
test(
'Tests suggestions "joyfull"',
async () => {
const trie = await getTrie();
// cspell:ignore joyfull
const collector = suggestionCollector('joyfull', opts(8, undefined, 2));
collector.collect(
genCompoundableSuggestions(
trie.root,
collector.word,
sugGenOptsFromCollector(collector, CompoundWordsMethod.SEPARATE_WORDS)
),
timeout
);
const results = collector.suggestions;
expect(results).toEqual([
{ cost: 25, word: 'joyful' },
{ cost: 100, word: 'joyfully' },
{ cost: 109, word: 'joy full' },
{ cost: 154, word: 'joyful l' },
{ cost: 155, word: 'joyful L' },
]);
},
timeout
);
test(
'Tests compound SEPARATE_WORDS suggestions',
async () => {
const trie = await getTrie();
// cspell:ignore onetwothreefour
const collector = suggestionCollector('onetwothreefour', opts(8, undefined, 3.3));
collector.collect(
genCompoundableSuggestions(
trie.root,
collector.word,
sugGenOptsFromCollector(collector, CompoundWordsMethod.SEPARATE_WORDS)
),
timeout
);
const results = collector.suggestions;
expect(results).toEqual([{ cost: 322, word: 'one two three four' }]);
},
timeout
);
test(
'Tests compound JOIN_WORDS suggestions',
async () => {
const trie = await getTrie();
// cspell:ignore onetwothrefour
const collector = suggestionCollector('onetwothreefour', opts(8, undefined, 3));
collector.collect(
genCompoundableSuggestions(
trie.root,
collector.word,
sugGenOptsFromCollector(collector, CompoundWordsMethod.JOIN_WORDS)
),
timeout
);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(expect.arrayContaining(['one+two+three+four']));
},
timeout
);
test(
'Tests compound suggestions',
async () => {
const trie = await getTrie();
// cspell:ignore onetwothrefour
const collector = suggestionCollector('onetwothreefour', opts(8, undefined, 3));
collector.collect(
genCompoundableSuggestions(
trie.root,
collector.word,
sugGenOptsFromCollector(collector, CompoundWordsMethod.JOIN_WORDS)
),
timeout
);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(expect.arrayContaining(['one+two+three+four']));
},
timeout
);
// Takes a long time.
test(
'Tests long compound suggestions `testscomputesuggestions`',
async () => {
const trie = await getTrie();
// cspell:ignore testscomputesuggestions
const collector = suggestionCollector('testscomputesuggestions', opts(2, undefined, 4, false));
collector.collect(
genCompoundableSuggestions(
trie.root,
collector.word,
sugGenOptsFromCollector(collector, CompoundWordsMethod.SEPARATE_WORDS)
),
timeout
);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual([
'tests compute suggestions',
'tests compute suggestion',
// 'test compute suggestions',
]);
},
timeout
);
// Takes a long time.
test(
'Tests long compound suggestions `testscompundsuggestions`',
async () => {
const trie = await getTrie();
// cspell:ignore testscompundsuggestions
const collector = suggestionCollector('testscompundsuggestions', opts(1, undefined, 4, false));
collector.collect(
genCompoundableSuggestions(
trie.root,
collector.word,
sugGenOptsFromCollector(collector, CompoundWordsMethod.SEPARATE_WORDS)
),
timeout
);
const results = collector.suggestions;
const suggestions = results.map((s) => s.word);
expect(suggestions).toEqual(['tests compound suggestions']);
},
timeout
);
test(
'Expensive suggestion `testscompundsuggestions`',
async () => {
const suggestionTimeout = 100;
const trie = await getTrie();
// cspell:ignore testscompundsuggestions
const collector = suggestionCollector('testscompundsuggestions', opts(1, undefined, 3, false));
const timer = createTimer();
collector.collect(
genCompoundableSuggestions(
trie.root,
collector.word,
sugGenOptsFromCollector(collector, CompoundWordsMethod.SEPARATE_WORDS)
),
suggestionTimeout
);
const elapsed = timer.elapsed();
expect(elapsed).toBeLessThan(suggestionTimeout * 2);
},
timeout
);
});
function sugGenOptsFromCollector(collector: SuggestionCollector, compoundMethod?: CompoundWordsMethod) {
const { ignoreCase, changeLimit } = collector;
const ops: GenSuggestionOptionsStrict = {
compoundMethod,
ignoreCase,
changeLimit,
};
return ops;
}
function opts(
numSuggestions: number,
filter?: SuggestionCollectorOptions['filter'],
changeLimit = 3,
includeTies = true,
ignoreCase = true
): SuggestionCollectorOptions {
return {
numSuggestions,
filter,
changeLimit,
includeTies,
ignoreCase,
timeout,
};
}
function sr(...sugs: (string | ExpectedSuggestion)[]): ExpectedSuggestion[] {
return sugs.map((s) => {
if (typeof s === 'string') return { word: s };
return s;
});
}