kalob-spell 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -2
- package/dist/cjs/autocomplete.js +451 -0
- package/dist/cjs/codemirror.js +93 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/lexicon.js +6883 -0
- package/dist/cjs/morphology.js +257 -0
- package/dist/cjs/package.json +3 -0
- package/dist/esm/autocomplete.js +441 -0
- package/dist/esm/codemirror.js +85 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/lexicon.js +6880 -0
- package/dist/esm/morphology.js +249 -0
- package/dist/esm/package.json +3 -0
- package/dist/types/autocomplete.d.ts +96 -0
- package/dist/types/codemirror.d.ts +46 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/lexicon.d.ts +19 -0
- package/dist/types/morphology.d.ts +53 -0
- package/package.json +50 -4
package/README.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# kalob-spell
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Kalob spellchecker, morphological validator, Fuse-powered autocomplete, and CodeMirror helpers for `machka`, `chwktela`, and Node.js.
|
|
4
|
+
|
|
5
|
+
## Exports
|
|
6
|
+
|
|
7
|
+
- **Lexicon (`kalob-spell` / `kalob-spell/lexicon`)**
|
|
8
|
+
- `lexiconData`, `commonSuffixes`, `amData`
|
|
9
|
+
- **Spellchecking & Morphology (`kalob-spell`)**
|
|
10
|
+
- `checkSpelling(word, customWords?)`
|
|
11
|
+
- `lintKalobText(text, options?)` / `findSpellingErrors(text, options?)`
|
|
12
|
+
- `directionalConnectives`, `letterNames`, `generatedNumbers`, `compoundRegex`, `numRegex`, `nounRegex`, `baseTeenRegex`
|
|
13
|
+
- `loadStoredCompoundWords(storageKey?)`, `normalizeCustomWords(customWords?)`
|
|
14
|
+
- **Engine, Autocomplete & Custom Memory (`kalob-spell`)**
|
|
15
|
+
- `createKalobSpell(options?)` / `new KalobSpell(options?)`
|
|
16
|
+
- Supports `customWords` / `compoundWords` (array, getter, or automatic `localStorage.getItem('compoundWords')` fallback when `storageKey !== false`)
|
|
17
|
+
- Supports `memories` / `getMemories` (`!`, `!1`, `!2`, ... custom memory lookup)
|
|
18
|
+
- Methods: `check(word)`, `lint(text)`, `completeAt(text, pos, opts)`, `complete(text, pos, opts)`, `search(query, opts)`, `suggestCorrections(word, opts)`, `setMemories`, `setMemory`, `addCustomWord`, `removeCustomWord`
|
|
19
|
+
- **CodeMirror 6 Adapters (`kalob-spell` / `kalob-spell/codemirror`)**
|
|
20
|
+
- `createSpellLintSource(spellOrOptions?, options?)`
|
|
21
|
+
- `createSpellLinter(linterFn, spellOrOptions?, options?)`
|
|
22
|
+
- `createCodeMirrorAutocomplete(spellOrOptions?, options?)`
|
|
23
|
+
- `createCodeMirrorAutocompleteOverrides(spellOrOptions?, options?)`
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createKalobSpell = exports.KalobSpell = exports.matchBefore = exports.buildUnifiedLexicon = void 0;
|
|
7
|
+
const fuse_js_1 = __importDefault(require("fuse.js"));
|
|
8
|
+
const lexicon_js_1 = require("./lexicon.js");
|
|
9
|
+
const morphology_js_1 = require("./morphology.js");
|
|
10
|
+
const SUFFIX_CATEGORIES = new Set([
|
|
11
|
+
'Suffix',
|
|
12
|
+
'Primary Ending',
|
|
13
|
+
'Number Suffix',
|
|
14
|
+
'Base Number Suffix',
|
|
15
|
+
'Shared Suffix',
|
|
16
|
+
]);
|
|
17
|
+
function buildUnifiedLexicon(customWords = []) {
|
|
18
|
+
const normalizedCustom = (0, morphology_js_1.resolveCustomWords)({ customWords }).words;
|
|
19
|
+
return [
|
|
20
|
+
...lexicon_js_1.lexiconData,
|
|
21
|
+
...normalizedCustom,
|
|
22
|
+
...morphology_js_1.generatedNumbers,
|
|
23
|
+
...morphology_js_1.directionalConnectives,
|
|
24
|
+
...lexicon_js_1.commonSuffixes.map((s) => ({
|
|
25
|
+
word: s.suffix,
|
|
26
|
+
eng: s.desc,
|
|
27
|
+
cat: 'Suffix',
|
|
28
|
+
pos: s.name,
|
|
29
|
+
})),
|
|
30
|
+
...morphology_js_1.primaryEndingDefinitions.map((s) => ({
|
|
31
|
+
word: s.suffix,
|
|
32
|
+
eng: s.desc,
|
|
33
|
+
cat: 'Primary Ending',
|
|
34
|
+
pos: s.name,
|
|
35
|
+
})),
|
|
36
|
+
...morphology_js_1.numberSuffixDefinitions.map((s) => ({
|
|
37
|
+
word: s.suffix,
|
|
38
|
+
eng: s.desc,
|
|
39
|
+
cat: 'Number Suffix',
|
|
40
|
+
pos: s.name,
|
|
41
|
+
})),
|
|
42
|
+
...morphology_js_1.baseNumberSuffixDefinitions.map((s) => ({
|
|
43
|
+
word: s.suffix,
|
|
44
|
+
eng: s.desc,
|
|
45
|
+
cat: 'Base Number Suffix',
|
|
46
|
+
pos: s.name,
|
|
47
|
+
})),
|
|
48
|
+
...morphology_js_1.sharedSuffixDefinitions.map((s) => ({
|
|
49
|
+
word: s.suffix,
|
|
50
|
+
eng: s.desc,
|
|
51
|
+
cat: 'Shared Suffix',
|
|
52
|
+
pos: s.name,
|
|
53
|
+
})),
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
exports.buildUnifiedLexicon = buildUnifiedLexicon;
|
|
57
|
+
function matchBefore(text, pos, expr) {
|
|
58
|
+
const clampedPos = Math.max(0, Math.min(pos, text.length));
|
|
59
|
+
const lastNewline = text.lastIndexOf('\n', clampedPos - 1);
|
|
60
|
+
const lineFrom = lastNewline + 1;
|
|
61
|
+
const start = Math.max(lineFrom, clampedPos - 250);
|
|
62
|
+
const slice = text.slice(start, clampedPos);
|
|
63
|
+
const anchoredSource = expr.source.endsWith('$') ? expr.source : `(?:${expr.source})$`;
|
|
64
|
+
const anchoredFlags = expr.flags.replace(/g/g, '');
|
|
65
|
+
const anchored = new RegExp(anchoredSource, anchoredFlags);
|
|
66
|
+
const found = slice.search(anchored);
|
|
67
|
+
if (found < 0)
|
|
68
|
+
return null;
|
|
69
|
+
return {
|
|
70
|
+
from: start + found,
|
|
71
|
+
to: clampedPos,
|
|
72
|
+
text: slice.slice(found),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
exports.matchBefore = matchBefore;
|
|
76
|
+
function getBestRoot(text) {
|
|
77
|
+
const numMatch = text.match(morphology_js_1.numRegex);
|
|
78
|
+
const nounMatch = text.match(morphology_js_1.nounRegex);
|
|
79
|
+
let root = '';
|
|
80
|
+
let isNumber = false;
|
|
81
|
+
let isBaseTeen = false;
|
|
82
|
+
if (numMatch && nounMatch) {
|
|
83
|
+
if (numMatch[0].length >= nounMatch[0].length) {
|
|
84
|
+
root = numMatch[0];
|
|
85
|
+
isNumber = true;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
root = nounMatch[0];
|
|
89
|
+
isNumber = false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else if (numMatch) {
|
|
93
|
+
root = numMatch[0];
|
|
94
|
+
isNumber = true;
|
|
95
|
+
}
|
|
96
|
+
else if (nounMatch) {
|
|
97
|
+
root = nounMatch[0];
|
|
98
|
+
isNumber = false;
|
|
99
|
+
}
|
|
100
|
+
if (isNumber) {
|
|
101
|
+
isBaseTeen = morphology_js_1.baseTeenRegex.test(root);
|
|
102
|
+
}
|
|
103
|
+
return { root, isNumber, isBaseTeen };
|
|
104
|
+
}
|
|
105
|
+
function resolveMemoryMap(source) {
|
|
106
|
+
if (!source)
|
|
107
|
+
return undefined;
|
|
108
|
+
return typeof source === 'function' ? source() : source;
|
|
109
|
+
}
|
|
110
|
+
class KalobSpell {
|
|
111
|
+
constructor(options = {}) {
|
|
112
|
+
this.cachedSignature = null;
|
|
113
|
+
this.cachedCustomWords = [];
|
|
114
|
+
this.cachedUnifiedLexicon = [];
|
|
115
|
+
this.cachedFuse = null;
|
|
116
|
+
this.cachedCorrectionFuse = null;
|
|
117
|
+
this.options = options;
|
|
118
|
+
if (options.customWords !== undefined) {
|
|
119
|
+
this.internalCustomWords = options.customWords;
|
|
120
|
+
}
|
|
121
|
+
else if (options.compoundWords !== undefined) {
|
|
122
|
+
this.internalCustomWords = options.compoundWords;
|
|
123
|
+
}
|
|
124
|
+
this.hasInternalMemories =
|
|
125
|
+
options.memories !== undefined && typeof options.memories !== 'function';
|
|
126
|
+
this.internalMemories =
|
|
127
|
+
options.memories && typeof options.memories !== 'function' ? { ...options.memories } : {};
|
|
128
|
+
}
|
|
129
|
+
ensureFreshIndex() {
|
|
130
|
+
const { words, signature } = (0, morphology_js_1.resolveCustomWords)({
|
|
131
|
+
customWords: this.internalCustomWords,
|
|
132
|
+
storageKey: this.options.storageKey,
|
|
133
|
+
});
|
|
134
|
+
if (this.cachedSignature === signature && this.cachedFuse !== null) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this.cachedSignature = signature;
|
|
138
|
+
this.cachedCustomWords = words;
|
|
139
|
+
this.cachedUnifiedLexicon = buildUnifiedLexicon(words);
|
|
140
|
+
this.cachedFuse = new fuse_js_1.default(this.cachedUnifiedLexicon, {
|
|
141
|
+
keys: ['word', 'eng'],
|
|
142
|
+
includeScore: true,
|
|
143
|
+
threshold: 0.1,
|
|
144
|
+
...this.options.fuseOptions,
|
|
145
|
+
});
|
|
146
|
+
this.cachedCorrectionFuse = null;
|
|
147
|
+
}
|
|
148
|
+
getCorrectionFuse() {
|
|
149
|
+
this.ensureFreshIndex();
|
|
150
|
+
if (!this.cachedCorrectionFuse) {
|
|
151
|
+
const standaloneWords = this.cachedUnifiedLexicon.filter((entry) => !SUFFIX_CATEGORIES.has(entry.cat));
|
|
152
|
+
this.cachedCorrectionFuse = new fuse_js_1.default(standaloneWords, {
|
|
153
|
+
keys: ['word'],
|
|
154
|
+
includeScore: true,
|
|
155
|
+
threshold: 0.4,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return this.cachedCorrectionFuse;
|
|
159
|
+
}
|
|
160
|
+
refresh() {
|
|
161
|
+
this.cachedSignature = null;
|
|
162
|
+
this.cachedFuse = null;
|
|
163
|
+
this.cachedCorrectionFuse = null;
|
|
164
|
+
this.ensureFreshIndex();
|
|
165
|
+
}
|
|
166
|
+
setCustomWords(words) {
|
|
167
|
+
this.internalCustomWords = words;
|
|
168
|
+
this.refresh();
|
|
169
|
+
}
|
|
170
|
+
addCustomWord(word) {
|
|
171
|
+
const current = this.getCustomWords();
|
|
172
|
+
this.internalCustomWords = [...current, word];
|
|
173
|
+
this.refresh();
|
|
174
|
+
}
|
|
175
|
+
removeCustomWord(word) {
|
|
176
|
+
const lower = word.toLowerCase();
|
|
177
|
+
const current = this.getCustomWords();
|
|
178
|
+
this.internalCustomWords = current.filter((w) => w.word.toLowerCase() !== lower);
|
|
179
|
+
this.refresh();
|
|
180
|
+
}
|
|
181
|
+
getCustomWords() {
|
|
182
|
+
this.ensureFreshIndex();
|
|
183
|
+
return this.cachedCustomWords;
|
|
184
|
+
}
|
|
185
|
+
getUnifiedLexicon() {
|
|
186
|
+
this.ensureFreshIndex();
|
|
187
|
+
return this.cachedUnifiedLexicon;
|
|
188
|
+
}
|
|
189
|
+
setMemories(memories) {
|
|
190
|
+
this.internalMemories = { ...memories };
|
|
191
|
+
this.hasInternalMemories = true;
|
|
192
|
+
}
|
|
193
|
+
setMemory(key, entry) {
|
|
194
|
+
this.internalMemories = { ...this.internalMemories, [key]: entry };
|
|
195
|
+
this.hasInternalMemories = true;
|
|
196
|
+
}
|
|
197
|
+
removeMemory(key) {
|
|
198
|
+
const next = { ...this.internalMemories };
|
|
199
|
+
delete next[key];
|
|
200
|
+
this.internalMemories = next;
|
|
201
|
+
}
|
|
202
|
+
getMemories() {
|
|
203
|
+
if (this.options.getMemories) {
|
|
204
|
+
return this.options.getMemories();
|
|
205
|
+
}
|
|
206
|
+
if (typeof this.options.memories === 'function') {
|
|
207
|
+
return this.options.memories();
|
|
208
|
+
}
|
|
209
|
+
return this.hasInternalMemories ? this.internalMemories : undefined;
|
|
210
|
+
}
|
|
211
|
+
check(word) {
|
|
212
|
+
this.ensureFreshIndex();
|
|
213
|
+
return (0, morphology_js_1.checkSpelling)(word, this.cachedCustomWords);
|
|
214
|
+
}
|
|
215
|
+
lint(text, overrides) {
|
|
216
|
+
if (overrides?.customWords !== undefined ||
|
|
217
|
+
overrides?.compoundWords !== undefined ||
|
|
218
|
+
overrides?.storageKey !== undefined) {
|
|
219
|
+
return (0, morphology_js_1.lintKalobText)(text, {
|
|
220
|
+
ignoreCapitalized: this.options.ignoreCapitalized,
|
|
221
|
+
message: this.options.message,
|
|
222
|
+
...overrides,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
this.ensureFreshIndex();
|
|
226
|
+
return (0, morphology_js_1.lintKalobText)(text, {
|
|
227
|
+
ignoreCapitalized: this.options.ignoreCapitalized,
|
|
228
|
+
message: this.options.message,
|
|
229
|
+
...overrides,
|
|
230
|
+
customWords: this.cachedCustomWords,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
findErrors(text, overrides) {
|
|
234
|
+
return this.lint(text, overrides);
|
|
235
|
+
}
|
|
236
|
+
completeAt(text, pos = text.length, callOptions = {}) {
|
|
237
|
+
this.ensureFreshIndex();
|
|
238
|
+
const stripPrefix = callOptions.stripPrefix ?? false;
|
|
239
|
+
const explicit = callOptions.explicit ?? false;
|
|
240
|
+
const clampedPos = Math.max(0, Math.min(pos, text.length));
|
|
241
|
+
const memoryTag = matchBefore(text, clampedPos, /!\d*/);
|
|
242
|
+
if (memoryTag) {
|
|
243
|
+
if (stripPrefix)
|
|
244
|
+
return null;
|
|
245
|
+
const memories = resolveMemoryMap(callOptions.memories) ?? this.getMemories();
|
|
246
|
+
if (memories) {
|
|
247
|
+
if (memoryTag.text === '!') {
|
|
248
|
+
const entries = Object.entries(memories);
|
|
249
|
+
return {
|
|
250
|
+
from: memoryTag.from,
|
|
251
|
+
to: memoryTag.to,
|
|
252
|
+
options: entries.map(([num, mem]) => ({
|
|
253
|
+
label: `!${num}`,
|
|
254
|
+
type: 'Memory',
|
|
255
|
+
detail: mem.target,
|
|
256
|
+
apply: mem.target,
|
|
257
|
+
info: mem.source,
|
|
258
|
+
from: memoryTag.from,
|
|
259
|
+
to: memoryTag.to,
|
|
260
|
+
})),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
const num = parseInt(memoryTag.text.substring(1), 10);
|
|
264
|
+
const memory = memories[num];
|
|
265
|
+
if (memory) {
|
|
266
|
+
return {
|
|
267
|
+
from: memoryTag.from,
|
|
268
|
+
to: memoryTag.to,
|
|
269
|
+
options: [
|
|
270
|
+
{
|
|
271
|
+
label: `!${num}`,
|
|
272
|
+
type: 'Memory',
|
|
273
|
+
detail: memory.target,
|
|
274
|
+
apply: memory.target,
|
|
275
|
+
info: memory.source,
|
|
276
|
+
from: memoryTag.from,
|
|
277
|
+
to: memoryTag.to,
|
|
278
|
+
},
|
|
279
|
+
],
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const word = matchBefore(text, clampedPos, /[A-Za-z']*/);
|
|
285
|
+
if (!word || (word.from === word.to && !explicit)) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
const textToParse = word.text;
|
|
289
|
+
let parsedLength = 0;
|
|
290
|
+
if (stripPrefix) {
|
|
291
|
+
if (/^[aeiouvw]/.test(textToParse)) {
|
|
292
|
+
parsedLength += 1;
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
let searchTerm = textToParse.substring(parsedLength);
|
|
299
|
+
let fromPos = word.from + parsedLength;
|
|
300
|
+
let categoryFilter = null;
|
|
301
|
+
const r1 = getBestRoot(textToParse.substring(parsedLength));
|
|
302
|
+
if (r1.root) {
|
|
303
|
+
parsedLength += r1.root.length;
|
|
304
|
+
if (parsedLength < textToParse.length) {
|
|
305
|
+
if (textToParse[parsedLength] === 'u') {
|
|
306
|
+
parsedLength += 1;
|
|
307
|
+
const r2 = getBestRoot(textToParse.substring(parsedLength));
|
|
308
|
+
if (r2.root) {
|
|
309
|
+
const afterRoot2Length = parsedLength + r2.root.length;
|
|
310
|
+
if (afterRoot2Length < textToParse.length) {
|
|
311
|
+
categoryFilter = r2.isNumber
|
|
312
|
+
? ['Number Suffix', 'Shared Suffix', 'Primary Ending']
|
|
313
|
+
: ['Suffix', 'Shared Suffix', 'Primary Ending'];
|
|
314
|
+
if (r2.isBaseTeen)
|
|
315
|
+
categoryFilter.push('Base Number Suffix');
|
|
316
|
+
searchTerm = textToParse.substring(afterRoot2Length);
|
|
317
|
+
fromPos = word.from + afterRoot2Length;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
categoryFilter = ['Noun/Number'];
|
|
321
|
+
searchTerm = textToParse.substring(parsedLength);
|
|
322
|
+
fromPos = word.from + parsedLength;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
categoryFilter = ['Noun/Number'];
|
|
327
|
+
searchTerm = textToParse.substring(parsedLength);
|
|
328
|
+
fromPos = word.from + parsedLength;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
categoryFilter = r1.isNumber
|
|
333
|
+
? ['Number Suffix', 'Shared Suffix', 'Primary Ending']
|
|
334
|
+
: ['Suffix', 'Shared Suffix', 'Primary Ending'];
|
|
335
|
+
if (r1.isBaseTeen)
|
|
336
|
+
categoryFilter.push('Base Number Suffix');
|
|
337
|
+
searchTerm = textToParse.substring(parsedLength);
|
|
338
|
+
fromPos = word.from + parsedLength;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
let results = this.cachedFuse.search(searchTerm);
|
|
343
|
+
if (categoryFilter) {
|
|
344
|
+
results = results.filter((r) => categoryFilter.includes(r.item.cat));
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
from: fromPos,
|
|
348
|
+
to: word.to,
|
|
349
|
+
options: results.map((r) => ({
|
|
350
|
+
label: r.item.word,
|
|
351
|
+
type: r.item.cat,
|
|
352
|
+
detail: r.item.eng,
|
|
353
|
+
cat: r.item.cat,
|
|
354
|
+
pos: r.item.pos,
|
|
355
|
+
score: r.score,
|
|
356
|
+
from: fromPos,
|
|
357
|
+
to: word.to,
|
|
358
|
+
entry: r.item,
|
|
359
|
+
})),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
complete(text, pos = text.length, callOptions = {}) {
|
|
363
|
+
const primary = this.completeAt(text, pos, { ...callOptions, stripPrefix: false });
|
|
364
|
+
const stripped = this.completeAt(text, pos, { ...callOptions, stripPrefix: true });
|
|
365
|
+
if (!primary && !stripped)
|
|
366
|
+
return null;
|
|
367
|
+
if (!primary)
|
|
368
|
+
return stripped;
|
|
369
|
+
if (!stripped)
|
|
370
|
+
return primary;
|
|
371
|
+
const seen = new Set(primary.options.map((o) => `${o.from}:${o.label}:${o.cat ?? ''}`));
|
|
372
|
+
const mergedOptions = [...primary.options];
|
|
373
|
+
for (const opt of stripped.options) {
|
|
374
|
+
const key = `${opt.from}:${opt.label}:${opt.cat ?? ''}`;
|
|
375
|
+
if (!seen.has(key)) {
|
|
376
|
+
seen.add(key);
|
|
377
|
+
mergedOptions.push(opt);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return {
|
|
381
|
+
from: primary.from,
|
|
382
|
+
to: primary.to,
|
|
383
|
+
options: mergedOptions,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
search(query, options = {}) {
|
|
387
|
+
this.ensureFreshIndex();
|
|
388
|
+
const { threshold, keys, categories, includeSuffixes = true, limit, } = options;
|
|
389
|
+
let pool = this.cachedUnifiedLexicon;
|
|
390
|
+
if (!includeSuffixes) {
|
|
391
|
+
pool = pool.filter((entry) => !SUFFIX_CATEGORIES.has(entry.cat));
|
|
392
|
+
}
|
|
393
|
+
if (categories && categories.length > 0) {
|
|
394
|
+
const categorySet = new Set(categories);
|
|
395
|
+
pool = pool.filter((entry) => categorySet.has(entry.cat));
|
|
396
|
+
}
|
|
397
|
+
const useDefaultFuse = threshold === undefined &&
|
|
398
|
+
keys === undefined &&
|
|
399
|
+
includeSuffixes &&
|
|
400
|
+
(!categories || categories.length === 0);
|
|
401
|
+
const fuse = useDefaultFuse
|
|
402
|
+
? this.cachedFuse
|
|
403
|
+
: new fuse_js_1.default(pool, {
|
|
404
|
+
keys: keys ?? ['word', 'eng'],
|
|
405
|
+
includeScore: true,
|
|
406
|
+
threshold: threshold ?? 0.1,
|
|
407
|
+
});
|
|
408
|
+
const raw = fuse.search(query, limit !== undefined ? { limit } : undefined);
|
|
409
|
+
return raw.map((r) => ({ item: r.item, score: r.score }));
|
|
410
|
+
}
|
|
411
|
+
suggestCorrections(word, options = {}) {
|
|
412
|
+
const trimmed = word.trim();
|
|
413
|
+
if (!trimmed)
|
|
414
|
+
return [];
|
|
415
|
+
const { threshold = 0.4, limit = 10, includeSuffixes = false } = options;
|
|
416
|
+
const useCached = threshold === 0.4 && !includeSuffixes;
|
|
417
|
+
let results;
|
|
418
|
+
if (useCached) {
|
|
419
|
+
results = this.getCorrectionFuse().search(trimmed);
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
this.ensureFreshIndex();
|
|
423
|
+
const pool = includeSuffixes
|
|
424
|
+
? this.cachedUnifiedLexicon
|
|
425
|
+
: this.cachedUnifiedLexicon.filter((entry) => !SUFFIX_CATEGORIES.has(entry.cat));
|
|
426
|
+
const fuse = new fuse_js_1.default(pool, {
|
|
427
|
+
keys: ['word'],
|
|
428
|
+
includeScore: true,
|
|
429
|
+
threshold,
|
|
430
|
+
});
|
|
431
|
+
results = fuse.search(trimmed);
|
|
432
|
+
}
|
|
433
|
+
const seenWords = new Set();
|
|
434
|
+
const deduped = [];
|
|
435
|
+
for (const r of results) {
|
|
436
|
+
const key = r.item.word.toLowerCase();
|
|
437
|
+
if (seenWords.has(key))
|
|
438
|
+
continue;
|
|
439
|
+
seenWords.add(key);
|
|
440
|
+
deduped.push({ item: r.item, score: r.score });
|
|
441
|
+
if (deduped.length >= limit)
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
return deduped;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
exports.KalobSpell = KalobSpell;
|
|
448
|
+
function createKalobSpell(options) {
|
|
449
|
+
return new KalobSpell(options);
|
|
450
|
+
}
|
|
451
|
+
exports.createKalobSpell = createKalobSpell;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCodeMirrorAutocompleteOverrides = exports.createCodeMirrorAutocomplete = exports.createSpellLinter = exports.createSpellLintSource = exports.formatCompletionInfoHtml = void 0;
|
|
4
|
+
const autocomplete_js_1 = require("./autocomplete.js");
|
|
5
|
+
function escapeHtml(text) {
|
|
6
|
+
return text
|
|
7
|
+
.replace(/&/g, '&')
|
|
8
|
+
.replace(/</g, '<')
|
|
9
|
+
.replace(/>/g, '>');
|
|
10
|
+
}
|
|
11
|
+
function formatCompletionInfoHtml(cat, pos) {
|
|
12
|
+
return `<strong>Type:</strong> ${escapeHtml(cat)}\n <strong>POS:</strong> ${escapeHtml(pos)}`;
|
|
13
|
+
}
|
|
14
|
+
exports.formatCompletionInfoHtml = formatCompletionInfoHtml;
|
|
15
|
+
function toCodeMirrorCompletion(item) {
|
|
16
|
+
if (item.type === 'Memory') {
|
|
17
|
+
return {
|
|
18
|
+
label: item.label,
|
|
19
|
+
type: item.type,
|
|
20
|
+
detail: item.detail,
|
|
21
|
+
apply: item.apply,
|
|
22
|
+
info: item.info,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const cat = item.cat ?? item.type;
|
|
26
|
+
const pos = item.pos ?? '';
|
|
27
|
+
return {
|
|
28
|
+
label: item.label,
|
|
29
|
+
type: item.type,
|
|
30
|
+
detail: item.detail,
|
|
31
|
+
info: () => {
|
|
32
|
+
const infoElem = document.createElement('div');
|
|
33
|
+
infoElem.innerHTML = formatCompletionInfoHtml(cat, pos);
|
|
34
|
+
return infoElem;
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function resolveSpellAndOptions(spellOrOptions, maybeOptions) {
|
|
39
|
+
if (spellOrOptions instanceof autocomplete_js_1.KalobSpell) {
|
|
40
|
+
return { spell: spellOrOptions, options: maybeOptions };
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
spell: new autocomplete_js_1.KalobSpell(spellOrOptions ?? {}),
|
|
44
|
+
options: spellOrOptions,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function createSpellLintSource(spellOrOptions, maybeOptions) {
|
|
48
|
+
const { spell, options } = resolveSpellAndOptions(spellOrOptions, maybeOptions);
|
|
49
|
+
return (view) => {
|
|
50
|
+
const diagnostics = spell.lint(view.state.doc.toString(), options);
|
|
51
|
+
return diagnostics.map((d) => ({
|
|
52
|
+
from: d.from,
|
|
53
|
+
to: d.to,
|
|
54
|
+
severity: d.severity,
|
|
55
|
+
message: d.message,
|
|
56
|
+
}));
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
exports.createSpellLintSource = createSpellLintSource;
|
|
60
|
+
function createSpellLinter(linterFn, spellOrOptions, maybeOptions) {
|
|
61
|
+
const lintSource = createSpellLintSource(spellOrOptions, maybeOptions);
|
|
62
|
+
return linterFn((view) => lintSource(view));
|
|
63
|
+
}
|
|
64
|
+
exports.createSpellLinter = createSpellLinter;
|
|
65
|
+
function createCodeMirrorAutocomplete(spellOrOptions, maybeOptions) {
|
|
66
|
+
const { spell, options } = resolveSpellAndOptions(spellOrOptions, maybeOptions);
|
|
67
|
+
const stripPrefix = options?.stripPrefix ?? false;
|
|
68
|
+
return (context) => {
|
|
69
|
+
const match = context.matchBefore(/!\d*/) ?? context.matchBefore(/[A-Za-z']*/);
|
|
70
|
+
const syntheticText = match ? match.text : '';
|
|
71
|
+
const offsetDelta = match ? match.from : context.pos;
|
|
72
|
+
const result = spell.completeAt(syntheticText, syntheticText.length, {
|
|
73
|
+
explicit: context.explicit,
|
|
74
|
+
stripPrefix,
|
|
75
|
+
memories: options?.memories,
|
|
76
|
+
});
|
|
77
|
+
if (!result)
|
|
78
|
+
return null;
|
|
79
|
+
return {
|
|
80
|
+
from: offsetDelta + result.from,
|
|
81
|
+
options: result.options.map(toCodeMirrorCompletion),
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
exports.createCodeMirrorAutocomplete = createCodeMirrorAutocomplete;
|
|
86
|
+
function createCodeMirrorAutocompleteOverrides(spellOrOptions, maybeOptions) {
|
|
87
|
+
const { spell, options } = resolveSpellAndOptions(spellOrOptions, maybeOptions);
|
|
88
|
+
return [
|
|
89
|
+
createCodeMirrorAutocomplete(spell, { ...options, stripPrefix: false }),
|
|
90
|
+
createCodeMirrorAutocomplete(spell, { ...options, stripPrefix: true }),
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
exports.createCodeMirrorAutocompleteOverrides = createCodeMirrorAutocompleteOverrides;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./lexicon.js"), exports);
|
|
18
|
+
__exportStar(require("./morphology.js"), exports);
|
|
19
|
+
__exportStar(require("./autocomplete.js"), exports);
|
|
20
|
+
__exportStar(require("./codemirror.js"), exports);
|