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
|
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
|
-
+
+
-
-
-
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
-
-
+
+
+
-
+
+
+
-
-
-
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
-
-
+
-
+
-
-
+
+
+
|
var char_player = require("./char_player.js");
}
class SuggResult {
// Structure for storing, classifying and filtering suggestions
constructor (sWord, nDistLimit=-1) {
constructor (sWord, nSuggLimit=10, nDistLimit=-1) {
this.sWord = sWord;
this.sSimplifiedWord = str_transform.simplifyWord(sWord);
this.nDistLimit = (nDistLimit >= 0) ? nDistLimit : Math.floor(sWord.length / 3) + 1;
this.nMinDist = 1000;
// Temporary sets
this.aSugg = new Set();
this.dSugg = new Map([ [0, []], [1, []], [2, []] ]);
this.aAllSugg = new Set(); // all found words even those refused
this.aAllSugg = new Set(); // All suggestions, even the one rejected
this.dGoodSugg = new Map(); // Acceptable suggestions
this.dBestSugg = new Map(); // Best suggestions
// Parameters
this.nSuggLimit = nSuggLimit;
this.nSuggLimitExt = nSuggLimit + 2; // we add few entries in case suggestions merge after casing modifications
this.nBestSuggLimit = Math.floor(nSuggLimit * 1.5); // n times the requested limit
this.nGoodSuggLimit = nSuggLimit * 15; // n times the requested limit
}
addSugg (sSugg, nDeep=0) {
addSugg (sSugg) {
// add a suggestion
if (this.aAllSugg.has(sSugg)) {
return;
}
this.aAllSugg.add(sSugg);
if (!this.aSugg.has(sSugg)) {
//let nDist = Math.floor(str_transform.distanceDamerauLevenshtein(this.sSimplifiedWord, str_transform.simplifyWord(sSugg)));
let nDist = Math.floor((1 - str_transform.distanceJaroWinkler(this.sSimplifiedWord, str_transform.simplifyWord(sSugg)))*10);
if (nDist <= this.nDistLimit) {
if (sSugg.includes(" ")) { // add 1 to distance for split suggestions
nDist += 1;
}
if (!this.dSugg.has(nDist)) {
this.dSugg.set(nDist, []);
}
// jaro 0->1 1 les chaines sont égale
let nDistJaro = 1 - str_transform.distanceJaroWinkler(this.sSimplifiedWord, str_transform.simplifyWord(sSugg));
let nDist = Math.floor(nDistJaro * 10);
if (nDistJaro < .11) { // Best suggestions
this.dBestSugg.set(sSugg, Math.round(nDistJaro*1000));
if (this.dBestSugg.size > this.nBestSuggLimit) {
this.nDistLimit = -1; // make suggest() to end search
}
} else if (nDistJaro < .33) { // Good suggestions
this.dGoodSugg.set(sSugg, Math.round(nDistJaro*1000));
if (this.dGoodSugg.size > this.nGoodSuggLimit) {
this.nDistLimit = -1; // make suggest() to end search
}
this.dSugg.get(nDist).push(sSugg);
this.aSugg.add(sSugg);
if (nDist < this.nMinDist) {
this.nMinDist = nDist;
}
this.nDistLimit = Math.min(this.nDistLimit, this.nMinDist+1);
}
} else {
if (nDist < this.nMinDist) {
this.nMinDist = nDist;
}
this.nDistLimit = Math.min(this.nDistLimit, this.nMinDist);
}
if (nDist <= this.nDistLimit) {
if (nDist < this.nMinDist) {
this.nMinDist = nDist;
}
this.nDistLimit = Math.min(this.nDistLimit, this.nMinDist+1);
}
}
getSuggestions (nSuggLimit=10) {
getSuggestions () {
// return a list of suggestions
let lRes = [];
if (this.dBestSugg.size > 0) {
let bFirstListSorted = false;
for (let [nDist, lSugg] of this.dSugg.entries()) {
if (nDist > this.nDistLimit) {
// sort only with simplified words
let lResTmp = [...this.dBestSugg.entries()].sort((a, b) => { return a[1] - b[1]; });
let nSize = Math.min(this.nSuggLimitExt, lResTmp.length);
break;
for (let i=0; i < nSize; i++){
lRes.push(lResTmp[i][0]);
}
}
if (!bFirstListSorted && lSugg.length > 1) {
//lRes.sort((a, b) => { return str_transform.distanceDamerauLevenshtein(this.sWord, a) - str_transform.distanceDamerauLevenshtein(this.sWord, b); });
lRes.sort((a, b) => { return str_transform.distanceJaroWinkler(this.sWord, b) - str_transform.distanceJaroWinkler(this.sWord, a); });
if (lRes.length < this.nSuggLimitExt) {
// sort with simplified words and original word
let lResTmp = [...this.dGoodSugg.entries()].sort((a, b) => {
// Low precision to rely more on simplified words
let nJaroA = Math.round(str_transform.distanceJaroWinkler(this.sWord, a[0]) * 10);
let nJaroB = Math.round(str_transform.distanceJaroWinkler(this.sWord, b[0]) * 10);
bFirstListSorted = true;
}
lRes.push(...lSugg);
if (nJaroA == nJaroB) {
return a[1] - b[1]; // warning: both lists are NOT sorted the same way (key: a-b)
} else {
return nJaroB - nJaroA; // warning: both lists are NOT sorted the same way (key: b-a)
}
}).slice(0, this.nSuggLimitExt);
let nSize = Math.min(this.nSuggLimitExt, lResTmp.length);
for (let i=0; i < nSize; i++){
lRes.push(lResTmp[i][0]);
if (lRes.length > nSuggLimit) {
break;
}
}
// casing
if (this.sWord.gl_isUpperCase()) {
lRes = lRes.map((sSugg) => { return sSugg.toUpperCase(); });
lRes = [...new Set(lRes)];
}
else if (this.sWord.slice(0,1).gl_isUpperCase()) {
lRes = lRes.map((sSugg) => { return sSugg.slice(0,1).toUpperCase() + sSugg.slice(1); });
lRes = [...new Set(lRes)];
}
return lRes.slice(0, nSuggLimit);
return lRes.slice(0, this.nSuggLimit);
}
reset () {
this.aSugg.clear();
this.dSugg.clear();
this.dSugg.clear();
this.dGoodSugg.clear();
this.dBestSugg.clear();
}
}
class IBDAWG {
// INDEXABLE BINARY DIRECT ACYCLIC WORD GRAPH
|
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
-
+
+
-
+
|
if (this.lexicographer) {
[sPfx, sWord, sSfx] = this.lexicographer.split(sWord);
}
let nMaxSwitch = Math.max(Math.floor(sWord.length / 3), 1);
let nMaxDel = Math.floor(sWord.length / 5);
let nMaxHardRepl = Math.max(Math.floor((sWord.length - 5) / 4), 1);
let nMaxJump = Math.max(Math.floor(sWord.length / 4), 1);
let oSuggResult = new SuggResult(sWord);
let oSuggResult = new SuggResult(sWord, nSuggLimit);
let sWord = str_transform.cleanWord(sWord);
if (bSplitTrailingNumbers) {
this._splitTrailingNumbers(oSuggResult, sWord);
}
this._splitSuggest(oSuggResult, sWord);
this._suggest(oSuggResult, sWord, nMaxSwitch, nMaxDel, nMaxHardRepl, nMaxJump);
let aSugg = oSuggResult.getSuggestions(nSuggLimit);
let aSugg = oSuggResult.getSuggestions();
if (this.lexicographer) {
aSugg = this.lexicographer.filterSugg(aSugg);
}
if (sSfx || sPfx) {
// we add what we removed
return aSugg.map( (sSugg) => { return sPfx + sSugg + sSfx; } );
}
|