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
|
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
|
-
-
-
-
+
+
+
-
-
+
-
-
+
-
+
+
-
+
-
+
-
-
-
-
+
+
+
-
+
-
-
+
+
|
// Connect it to message port.
self.addEventListener("message", msg => worker.handleMessage(msg));
// end of copy/paste
// no console here, use “dump”
let gce = null; // module: grammar checker engine
let text = null;
let gc_engine = null; // module: grammar checker engine
let gc_options = null; // module: grammar checker options
let text = null; // module: text
let tkz = null; // module: tokenizer
let lxg = null; // module: lexicographer
let lexgraph_fr = null; // module: lexicographer
let helpers = null;
let oTokenizer = null;
let oSpellChecker = null;
let oLxg = null;
function loadGrammarChecker (sGCOptions="", sContext="JavaScript") {
if (gce === null) {
if (gc_engine === null) {
try {
gc_options = require("resource://grammalecte/fr/gc_options.js");
gce = require("resource://grammalecte/fr/gc_engine.js");
gc_engine = require("resource://grammalecte/fr/gc_engine.js");
helpers = require("resource://grammalecte/graphspell/helpers.js");
text = require("resource://grammalecte/text.js");
tkz = require("resource://grammalecte/graphspell/tokenizer.js");
//lexgraph_fr = require("resource://grammalecte/graphspell/lexgraph_fr.js");
//lxg = require("resource://grammalecte/fr/lexicographe.js");
oTokenizer = new tkz.Tokenizer("fr");
gce.load(sContext, "sCSS");
oSpellChecker = gce.getSpellChecker();
gc_engine.load(sContext, "sCSS");
oSpellChecker = gc_engine.getSpellChecker();
if (sGCOptions !== "") {
console.log("set options:", sGCOptions);
gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
gc_options.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
}
// we always retrieve options from the gce, for setOptions filters obsolete options
return gce.getOptions().gl_toString();
// we always retrieve options from the gc_engine, for setOptions filters obsolete options
return gc_options.getOptions().gl_toString();
}
catch (e) {
console.log("# Error: " + e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}
}
}
|
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
|
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
|
-
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
|
}
catch (e) {
console.error(e);
}
}
function parse (sText, sCountry, bDebug, bContext) {
let aGrammErr = gce.parse(sText, sCountry, bDebug, bContext);
let aGrammErr = gc_engine.parse(sText, sCountry, bDebug, bContext);
return JSON.stringify(aGrammErr);
}
function parseAndSpellcheck (sText, sCountry, bDebug, bContext) {
let aGrammErr = gce.parse(sText, sCountry, bDebug, null, bContext);
let aGrammErr = gc_engine.parse(sText, sCountry, bDebug, null, bContext);
let aSpellErr = oSpellChecker.parseParagraph(sText);
return JSON.stringify({ aGrammErr: aGrammErr, aSpellErr: aSpellErr });
}
function suggest (sWord, nSuggLimit=10) {
let lSugg = []
for (let aSugg of oSpellChecker.suggest(sWord, nSuggLimit)) {
lSugg.push(...aSugg);
}
return lSugg.join("|");
}
function getOptions () {
return gce.getOptions().gl_toString();
return gc_options.getOptions().gl_toString();
}
function getDefaultOptions () {
return gce.getDefaultOptions().gl_toString();
return gc_options.getDefaultOptions().gl_toString();
}
function setOptions (sGCOptions) {
gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
return gce.getOptions().gl_toString();
gc_options.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
return gc_options.getOptions().gl_toString();
}
function setOption (sOptName, bValue) {
gce.setOptions(new Map([ [sOptName, bValue] ]));
return gce.getOptions().gl_toString();
gc_options.setOptions(new Map([ [sOptName, bValue] ]));
return gc_options.getOptions().gl_toString();
}
function resetOptions () {
gce.resetOptions();
return gce.getOptions().gl_toString();
gc_options.resetOptions();
return gc_options.getOptions().gl_toString();
}
function fullTests (sGCOptions="") {
if (!gce || !oSpellChecker) {
if (!gc_engine || !oSpellChecker || !gc_options) {
return "# Error: grammar checker or dictionary not loaded."
}
let dMemoOptions = gce.getOptions();
let dMemoOptions = gc_options.getOptions();
if (sGCOptions) {
gce.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
gc_options.setOptions(helpers.objectToMap(JSON.parse(sGCOptions)));
}
let tests = require("resource://grammalecte/tests.js");
let oTest = new tests.TestGrammarChecking(gce);
let oTest = new tests.TestGrammarChecking(gc_engine, gc_options);
let sAllRes = "";
for (let sRes of oTest.testParse()) {
console.log(sRes+"\n");
sAllRes += sRes+"\n";
}
gce.setOptions(dMemoOptions);
gc_options.setOptions(dMemoOptions);
return sAllRes;
}
|