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
|
const dDefaultDictionaries = new Map([
["fr", "fr.json"],
["en", "en.json"]
]);
class Spellchecker {
constructor (sLangCode, mainDic=null, extentedDic=null, personalDic=null, sPath="") {
// returns true if the main dictionary is loaded
this.sLangCode = sLangCode;
console.log(sLangCode);
console.log(mainDic);
if (mainDic === null) {
mainDic = dDefaultDictionaries.gl_get(sLangCode, "");
}
this.oMainDic = this._loadDictionary(mainDic, sPath, true);
this.oExtendedDic = this._loadDictionary(extentedDic, sPath);
this.oPersonalDic = this._loadDictionary(personalDic, sPath);
}
_loadDictionary (dictionary, sPath, bNecessary=false) {
// returns an IBDAWG object
if (dictionary === null) {
return null;
}
try {
if (typeof(require) !== 'undefined') {
console.log(">>>> <resource:>");
return new ibdawg.IBDAWG(dictionary); // dictionary can be a filename or a JSON object
} else {
console.log(">>>> no <resource:>");
return new IBDAWG(dictionary, sPath); // dictionary can be a filename or a JSON object
}
}
catch (e) {
if (bNecessary) {
throw e.message;
}
console.log(e.message);
return null;
}
}
setMainDictionary (dictionary) {
// returns true if the dictionary is loaded
this.oMainDic = this._loadDictionary(dictionary);
return bool(this.oMainDic);
}
setExtendedDictionary (dictionary) {
// returns true if the dictionary is loaded
this.oExtendedDic = this._loadDictionary(dictionary);
return bool(this.oExtendedDic);
}
setPersonalDictionary (dictionary) {
// returns true if the dictionary is loaded
this.oPersonalDic = this._loadDictionary(dictionary);
return bool(this.oPersonalDic);
}
// IBDAWG functions
isValidToken (sToken) {
// checks if sToken is valid (if there is hyphens in sToken, sToken is split, each part is checked)
if (this.oMainDic.isValidToken(sToken)) {
return true;
}
if (this.oExtendedDic && this.oExtendedDic.isValidToken(sToken)) {
return true;
}
if (this.oPersonalDic && this.oPersonalDic.isValidToken(sToken)) {
return true;
}
return false;
}
isValid (sWord) {
// checks if sWord is valid (different casing tested if the first letter is a capital)
if (this.oMainDic.isValid(sToken)) {
return true;
}
if (this.oExtendedDic && this.oExtendedDic.isValid(sToken)) {
return true;
}
if (this.oPersonalDic && this.oPersonalDic.isValid(sToken)) {
return true;
}
return false;
}
lookup (sWord) {
// checks if sWord is in dictionary as is (strict verification)
if (this.oMainDic.lookup(sToken)) {
return true;
}
if (this.oExtendedDic && this.oExtendedDic.lookup(sToken)) {
return true;
}
if (this.oPersonalDic && this.oPersonalDic.lookup(sToken)) {
return true;
}
return false;
}
getMorph (sWord) {
// retrieves morphologies list, different casing allowed
let lResult = this.oMainDic.getMorph(sToken);
if (this.oExtendedDic) {
lResult.extends(this.oExtendedDic.getMorph(sToken));
}
if (this.oPersonalDic) {
lResult.extends(this.oPersonalDic.getMorph(sToken));
}
return lResult;
}
* suggest (sWord, nSuggLimit=10) {
// generator: returns 1,2 or 3 lists of suggestions
yield this.oMainDic.suggest(sWord, nSuggLimit);
if (this.oExtendedDic) {
yield this.oExtendedDic.suggest(sWord, nSuggLimit);
}
if (this.oPersonalDic) {
yield this.oPersonalDic.suggest(sWord, nSuggLimit);
}
|
|
|
<
<
|
<
<
>
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
const dDefaultDictionaries = new Map([
["fr", "fr.json"],
["en", "en.json"]
]);
class SpellChecker {
constructor (sLangCode, sPath="", mainDic=null, extentedDic=null, personalDic=null) {
// returns true if the main dictionary is loaded
this.sLangCode = sLangCode;
if (mainDic === null) {
mainDic = dDefaultDictionaries.gl_get(sLangCode, "");
}
this.oMainDic = this._loadDictionary(mainDic, sPath, true);
this.oExtendedDic = this._loadDictionary(extentedDic, sPath);
this.oPersonalDic = this._loadDictionary(personalDic, sPath);
}
_loadDictionary (dictionary, sPath, bNecessary=false) {
// returns an IBDAWG object
if (dictionary === null) {
return null;
}
try {
if (typeof(require) !== 'undefined') {
return new ibdawg.IBDAWG(dictionary); // dictionary can be a filename or a JSON object
} else {
return new IBDAWG(dictionary, sPath); // dictionary can be a filename or a JSON object
}
}
catch (e) {
let sfDictionary = (typeof(dictionary) == "string") ? dictionary : dictionary.sLangName + "/" + dictionary.sFileName;
if (bNecessary) {
throw "Error: <" + sfDictionary + "> not loaded. " + e.message;
}
console.log("Error: <" + sfDictionary + "> not loaded.")
console.log(e.message);
return null;
}
}
setMainDictionary (dictionary) {
// returns true if the dictionary is loaded
this.oMainDic = this._loadDictionary(dictionary);
return Boolean(this.oMainDic);
}
setExtendedDictionary (dictionary) {
// returns true if the dictionary is loaded
this.oExtendedDic = this._loadDictionary(dictionary);
return Boolean(this.oExtendedDic);
}
setPersonalDictionary (dictionary) {
// returns true if the dictionary is loaded
this.oPersonalDic = this._loadDictionary(dictionary);
return Boolean(this.oPersonalDic);
}
// IBDAWG functions
isValidToken (sToken) {
// checks if sToken is valid (if there is hyphens in sToken, sToken is split, each part is checked)
if (this.oMainDic.isValidToken(sToken)) {
return true;
}
if (this.oExtendedDic && this.oExtendedDic.isValidToken(sToken)) {
return true;
}
if (this.oPersonalDic && this.oPersonalDic.isValidToken(sToken)) {
return true;
}
return false;
}
isValid (sWord) {
// checks if sWord is valid (different casing tested if the first letter is a capital)
if (this.oMainDic.isValid(sWord)) {
return true;
}
if (this.oExtendedDic && this.oExtendedDic.isValid(sWord)) {
return true;
}
if (this.oPersonalDic && this.oPersonalDic.isValid(sWord)) {
return true;
}
return false;
}
lookup (sWord) {
// checks if sWord is in dictionary as is (strict verification)
if (this.oMainDic.lookup(sWord)) {
return true;
}
if (this.oExtendedDic && this.oExtendedDic.lookup(sWord)) {
return true;
}
if (this.oPersonalDic && this.oPersonalDic.lookup(sWord)) {
return true;
}
return false;
}
getMorph (sWord) {
// retrieves morphologies list, different casing allowed
let lResult = this.oMainDic.getMorph(sWord);
if (this.oExtendedDic) {
lResult.push(...this.oExtendedDic.getMorph(sWord));
}
if (this.oPersonalDic) {
lResult.push(...this.oPersonalDic.getMorph(sWord));
}
return lResult;
}
* suggest (sWord, nSuggLimit=10) {
// generator: returns 1, 2 or 3 lists of suggestions
yield this.oMainDic.suggest(sWord, nSuggLimit);
if (this.oExtendedDic) {
yield this.oExtendedDic.suggest(sWord, nSuggLimit);
}
if (this.oPersonalDic) {
yield this.oPersonalDic.suggest(sWord, nSuggLimit);
}
|