Overview
Comment: | [core][js] code clarification: separate specific inner functions from gc engine core |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk | core |
Files: | files | file ages | folders |
SHA3-256: |
1975bd13944a5a19bdfb760e3a1a8f93 |
User & Date: | olr on 2020-08-19 15:53:49 |
Other Links: | manifest | tags |
Context
2020-08-19
| ||
15:57 | [graphspell][js] remove importation dirt... check-in: 48d2a67a6a user: olr tags: trunk, graphspell | |
15:53 | [core][js] code clarification: separate specific inner functions from gc engine core check-in: 1975bd1394 user: olr tags: trunk, core | |
15:11 | [core][py] code clarification: separate specific inner functions from gc engine core check-in: 0b126eb254 user: olr tags: trunk, core | |
Changes
Modified gc_core/js/lang_core/gc_engine.js from [a87347cf36] to [1328582cce].
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 | // Grammar checker engine /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ /* global require, exports, console */ "use strict"; ${string} ${regex} ${map} if (typeof(process) !== 'undefined') { // NodeJS var gc_options = require("./gc_options.js"); var gc_rules = require("./gc_rules.js"); var gc_rules_graph = require("./gc_rules_graph.js"); var cregex = require("./cregex.js"); var text = require("../text.js"); } function capitalizeArray (aArray) { // can’t map on user defined function?? let aNew = []; for (let i = 0; i < aArray.length; i = i + 1) { aNew[i] = aArray[i].slice(0,1).toUpperCase() + aArray[i].slice(1); } return aNew; } | > > > < < < < < < | 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 | // Grammar checker engine /* jshint esversion:6, -W097 */ /* jslint esversion:6 */ /* global require, exports, console */ "use strict"; ${string} ${regex} ${map} if (typeof(process) !== 'undefined') { // NodeJS var spellchecker = require("../graphspell/spellchecker.js"); var gc_functions = require("./gc_functions.js"); var gc_options = require("./gc_options.js"); var gc_rules = require("./gc_rules.js"); var gc_rules_graph = require("./gc_rules_graph.js"); var cregex = require("./cregex.js"); var text = require("../text.js"); } function capitalizeArray (aArray) { // can’t map on user defined function?? let aNew = []; for (let i = 0; i < aArray.length; i = i + 1) { aNew[i] = aArray[i].slice(0,1).toUpperCase() + aArray[i].slice(1); } return aNew; } var gc_engine = { //// Informations lang: "${lang}", locales: ${loc}, |
︙ | ︙ | |||
56 57 58 59 60 61 62 | aIgnoredRules: new Set(), oOptionsColors: null, //// Initialization load: function (sContext="JavaScript", sColorType="aRGB", sPath="") { try { | < < > | 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | aIgnoredRules: new Set(), oOptionsColors: null, //// Initialization load: function (sContext="JavaScript", sColorType="aRGB", sPath="") { try { if (typeof(process) !== 'undefined') { this.oSpellChecker = new spellchecker.SpellChecker("${lang}", "", "${dic_main_filename_js}", "${dic_community_filename_js}", "${dic_personal_filename_js}"); } else { this.oSpellChecker = new SpellChecker("${lang}", sPath, "${dic_main_filename_js}", "${dic_community_filename_js}", "${dic_personal_filename_js}"); } this.oSpellChecker.activateStorage(); this.oTokenizer = this.oSpellChecker.getTokenizer(); gc_functions.load(sContext, this.oSpellChecker); gc_options.load(sContext) this.oOptionsColors = gc_options.getOptionsColors(sContext, sColorType); } catch (e) { console.error(e); } }, |
︙ | ︙ | |||
298 299 300 301 302 303 304 | for (let [zRegex, bUppercase, sLineId, sRuleId, nPriority, lActions, lGroups, lNegLookBefore] of lRuleGroup) { if (!gc_engine.aIgnoredRules.has(sRuleId)) { while ((m = zRegex.gl_exec2(sText, lGroups, lNegLookBefore)) !== null) { let bCondMemo = null; for (let [sFuncCond, cActionType, sWhat, ...eAct] of lActions) { // action in lActions: [ condition, action type, replacement/suggestion/action[, iGroup[, message, URL]] ] try { | > | | 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | for (let [zRegex, bUppercase, sLineId, sRuleId, nPriority, lActions, lGroups, lNegLookBefore] of lRuleGroup) { if (!gc_engine.aIgnoredRules.has(sRuleId)) { while ((m = zRegex.gl_exec2(sText, lGroups, lNegLookBefore)) !== null) { let bCondMemo = null; for (let [sFuncCond, cActionType, sWhat, ...eAct] of lActions) { // action in lActions: [ condition, action type, replacement/suggestion/action[, iGroup[, message, URL]] ] try { bCondMemo = (!sFuncCond || gc_functions[sFuncCond](sText, sText0, m, this.dTokenPos, sCountry, bCondMemo)); //bCondMemo = (!sFuncCond || oEvalFunc[sFuncCond](sText, sText0, m, this.dTokenPos, sCountry, bCondMemo)); if (bCondMemo) { switch (cActionType) { case "-": // grammar error //console.log("-> error detected in " + sLineId + "\nzRegex: " + zRegex.source); let nErrorStart = nOffset + m.start[eAct[0]]; if (!this.dError.has(nErrorStart) || nPriority > this.dErrorPriority.get(nErrorStart)) { |
︙ | ︙ | |||
323 324 325 326 327 328 329 | if (bDebug) { console.log("~ " + sText + " -- " + m[eAct[0]] + " # " + sLineId); } break; case "=": // disambiguation //console.log("-> disambiguation by " + sLineId + "\nzRegex: " + zRegex.source); | > | | 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | if (bDebug) { console.log("~ " + sText + " -- " + m[eAct[0]] + " # " + sLineId); } break; case "=": // disambiguation //console.log("-> disambiguation by " + sLineId + "\nzRegex: " + zRegex.source); gc_functions[sWhat](sText, m, this.dTokenPos); //oEvalFunc[sWhat](sText, m, this.dTokenPos); if (bDebug) { console.log("= " + m[0] + " # " + sLineId, "\nDA:", this.dTokenPos); } break; case ">": // we do nothing, this test is just a condition to apply all following actions break; |
︙ | ︙ | |||
666 667 668 669 670 671 672 | // Suggestion [ option, condition, "-", replacement/suggestion/action, iTokenStart, iTokenEnd, cStartLimit, cEndLimit, bCaseSvty, nPriority, sMessage, iURL ] // TextProcessor [ option, condition, "~", replacement/suggestion/action, iTokenStart, iTokenEnd, bCaseSvty ] // Disambiguator [ option, condition, "=", replacement/suggestion/action ] // Tag [ option, condition, "/", replacement/suggestion/action, iTokenStart, iTokenEnd ] // Immunity [ option, condition, "!", "", iTokenStart, iTokenEnd ] // Test [ option, condition, ">", "" ] if (!sOption || dOptions.gl_get(sOption, false)) { | > | | 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | // Suggestion [ option, condition, "-", replacement/suggestion/action, iTokenStart, iTokenEnd, cStartLimit, cEndLimit, bCaseSvty, nPriority, sMessage, iURL ] // TextProcessor [ option, condition, "~", replacement/suggestion/action, iTokenStart, iTokenEnd, bCaseSvty ] // Disambiguator [ option, condition, "=", replacement/suggestion/action ] // Tag [ option, condition, "/", replacement/suggestion/action, iTokenStart, iTokenEnd ] // Immunity [ option, condition, "!", "", iTokenStart, iTokenEnd ] // Test [ option, condition, ">", "" ] if (!sOption || dOptions.gl_get(sOption, false)) { bCondMemo = !sFuncCond || gc_functions[sFuncCond](this.lToken, nTokenOffset, nLastToken, sCountry, bCondMemo, this.dTags, this.sSentence, this.sSentence0); //bCondMemo = !sFuncCond || oEvalFunc[sFuncCond](this.lToken, nTokenOffset, nLastToken, sCountry, bCondMemo, this.dTags, this.sSentence, this.sSentence0); if (bCondMemo) { if (cActionType == "-") { // grammar error let [iTokenStart, iTokenEnd, cStartLimit, cEndLimit, bCaseSvty, nPriority, sMessage, iURL] = eAct; let nTokenErrorStart = (iTokenStart > 0) ? nTokenOffset + iTokenStart : nLastToken + iTokenStart; if (!this.lToken[nTokenErrorStart].hasOwnProperty("bImmune")) { let nTokenErrorEnd = (iTokenEnd > 0) ? nTokenOffset + iTokenEnd : nLastToken + iTokenEnd; |
︙ | ︙ | |||
699 700 701 702 703 704 705 | bChange = true; if (bDebug) { console.log(` TEXT_PROCESSOR: [${this.lToken[nTokenStart]["sValue"]}:${this.lToken[nTokenEnd]["sValue"]}] > ${sWhat}`); } } else if (cActionType == "=") { // disambiguation | > | | 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | bChange = true; if (bDebug) { console.log(` TEXT_PROCESSOR: [${this.lToken[nTokenStart]["sValue"]}:${this.lToken[nTokenEnd]["sValue"]}] > ${sWhat}`); } } else if (cActionType == "=") { // disambiguation gc_functions[sWhat](this.lToken, nTokenOffset, nLastToken); //oEvalFunc[sWhat](this.lToken, nTokenOffset, nLastToken); if (bDebug) { console.log(` DISAMBIGUATOR: (${sWhat}) [${this.lToken[nTokenOffset+1]["sValue"]}:${this.lToken[nLastToken]["sValue"]}]`); } } else if (cActionType == ">") { // we do nothing, this test is just a condition to apply all following actions if (bDebug) { |
︙ | ︙ | |||
781 782 783 784 785 786 787 | _createErrorFromRegex (sText, sText0, sSugg, nOffset, m, iGroup, sLineId, sRuleId, bCaseSvty, sMsg, sURL, bShowRuleId, sOption, bContext) { let nStart = nOffset + m.start[iGroup]; let nEnd = nOffset + m.end[iGroup]; // suggestions let lSugg = []; if (sSugg.startsWith("=")) { | > | > | > | > | | 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 | _createErrorFromRegex (sText, sText0, sSugg, nOffset, m, iGroup, sLineId, sRuleId, bCaseSvty, sMsg, sURL, bShowRuleId, sOption, bContext) { let nStart = nOffset + m.start[iGroup]; let nEnd = nOffset + m.end[iGroup]; // suggestions let lSugg = []; if (sSugg.startsWith("=")) { sSugg = gc_functions[sSugg.slice(1)](sText, m); //sSugg = oEvalFunc[sSugg.slice(1)](sText, m); lSugg = (sSugg) ? sSugg.split("|") : []; } else if (sSugg == "_") { lSugg = []; } else { lSugg = sSugg.gl_expand(m).split("|"); } if (bCaseSvty && lSugg.length > 0 && m[iGroup].slice(0,1).gl_isUpperCase()) { lSugg = (m[iGroup].gl_isUpperCase()) ? lSugg.map((s) => s.toUpperCase()) : capitalizeArray(lSugg); } // Message let sMessage = (sMsg.startsWith("=")) ? gc_functions[sMsg.slice(1)](sText, m) : sMsg.gl_expand(m); //let sMessage = (sMsg.startsWith("=")) ? oEvalFunc[sMsg.slice(1)](sText, m) : sMsg.gl_expand(m); if (bShowRuleId) { sMessage += " #" + sLineId + " / " + sRuleId; } // return this._createError(nStart, nEnd, sLineId, sRuleId, sOption, sMessage, lSugg, sURL, bContext); } _createErrorFromTokens (sSugg, nTokenOffset, nLastToken, iFirstToken, nStart, nEnd, sLineId, sRuleId, bCaseSvty, sMsg, sURL, bShowRuleId, sOption, bContext) { // suggestions let lSugg = []; if (sSugg.startsWith("=")) { sSugg = gc_functions[sSugg.slice(1)](this.lToken, nTokenOffset, nLastToken); //sSugg = oEvalFunc[sSugg.slice(1)](this.lToken, nTokenOffset, nLastToken); lSugg = (sSugg) ? sSugg.split("|") : []; } else if (sSugg == "_") { lSugg = []; } else { lSugg = this._expand(sSugg, nTokenOffset, nLastToken).split("|"); } if (bCaseSvty && lSugg.length > 0 && this.lToken[iFirstToken]["sValue"].slice(0,1).gl_isUpperCase()) { lSugg = (this.sSentence.slice(nStart, nEnd).gl_isUpperCase()) ? lSugg.map((s) => s.toUpperCase()) : capitalizeArray(lSugg); } // Message let sMessage = (sMsg.startsWith("=")) ? gc_functions[sMsg.slice(1)](this.lToken, nTokenOffset, nLastToken) : this._expand(sMsg, nTokenOffset, nLastToken); //let sMessage = (sMsg.startsWith("=")) ? oEvalFunc[sMsg.slice(1)](this.lToken, nTokenOffset, nLastToken) : this._expand(sMsg, nTokenOffset, nLastToken); if (bShowRuleId) { sMessage += " #" + sLineId + " / " + sRuleId; } // return this._createError(nStart, nEnd, sLineId, sRuleId, sOption, sMessage, lSugg, sURL, bContext); } |
︙ | ︙ | |||
869 870 871 872 873 874 875 | else if (sRepl === "_") { sNew = "_".repeat(ln); } else if (sRepl === "@") { sNew = "@".repeat(ln); } else if (sRepl.slice(0,1) === "=") { | > | | 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | else if (sRepl === "_") { sNew = "_".repeat(ln); } else if (sRepl === "@") { sNew = "@".repeat(ln); } else if (sRepl.slice(0,1) === "=") { sNew = gc_functions[sRepl.slice(1)](sText, m); //sNew = oEvalFunc[sRepl.slice(1)](sText, m); sNew = sNew + " ".repeat(ln-sNew.length); if (bUppercase && m[iGroup].slice(0,1).gl_isUpperCase()) { sNew = sNew.gl_toCapitalize(); } } else { sNew = sRepl.gl_expand(m); sNew = sNew + " ".repeat(ln-sNew.length); |
︙ | ︙ | |||
910 911 912 913 914 915 916 | for (let i = nTokenRewriteStart; i <= nTokenRewriteEnd; i++) { this.lToken[i]["sNewValue"] = "_"; } } } else { if (sWhat.startsWith("=")) { | > | | 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 | for (let i = nTokenRewriteStart; i <= nTokenRewriteEnd; i++) { this.lToken[i]["sNewValue"] = "_"; } } } else { if (sWhat.startsWith("=")) { sWhat = gc_functions[sWhat.slice(1)](this.lToken, nTokenOffset, nLastToken); //sWhat = oEvalFunc[sWhat.slice(1)](this.lToken, nTokenOffset, nLastToken); } else { sWhat = this._expand(sWhat, nTokenOffset, nLastToken); } let bUppercase = bCaseSvty && this.lToken[nTokenRewriteStart]["sValue"].slice(0,1).gl_isUpperCase(); if (nTokenRewriteEnd - nTokenRewriteStart == 0) { // one token if (bUppercase) { |
︙ | ︙ | |||
1018 1019 1020 1021 1022 1023 1024 | console.log(" TEXT REWRITED: " + this.sSentence); } this.lToken.length = 0; this.lToken = lNewToken; } }; | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 | console.log(" TEXT REWRITED: " + this.sSentence); } this.lToken.length = 0; this.lToken = lNewToken; } }; if (typeof(exports) !== 'undefined') { exports.lang = gc_engine.lang; exports.locales = gc_engine.locales; exports.pkg = gc_engine.pkg; exports.name = gc_engine.name; exports.version = gc_engine.version; |
︙ | ︙ |
Added gc_core/js/lang_core/gc_functions.js version [9add95291e].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | // JavaScript // Grammar checker engine functions ${string} ${regex} ${map} if (typeof(process) !== 'undefined') { var gc_options = require("./gc_options.js"); } let _sAppContext = "JavaScript"; // what software is running let _oSpellChecker = null; //////// Common functions function option (sOpt) { // return true if option sOpt is active return gc_options.dOptions.gl_get(sOpt, false); } function echo (x) { console.log(x); return true; } var re = { search: function (sRegex, sText) { if (sRegex.startsWith("(?i)")) { return sText.search(new RegExp(sRegex.slice(4), "i")) !== -1; } else { return sText.search(sRegex) !== -1; } }, createRegExp: function (sRegex) { if (sRegex.startsWith("(?i)")) { return new RegExp(sRegex.slice(4), "i"); } else { return new RegExp(sRegex); } } } //////// functions to get text outside pattern scope // warning: check compile_rules.py to understand how it works function nextword (s, iStart, n) { // get the nth word of the input string or empty string let z = new RegExp("^(?: +[a-zà-öA-Zø-ÿÀ-Ö0-9Ø-ßĀ-ʯff-stᴀ-ᶿ%_-]+){" + (n-1).toString() + "} +([a-zà-öA-Zø-ÿÀ-Ö0-9Ø-ßĀ-ʯff-stᴀ-ᶿ%_-]+)", "ig"); let m = z.exec(s.slice(iStart)); if (!m) { return null; } return [iStart + z.lastIndex - m[1].length, m[1]]; } function prevword (s, iEnd, n) { // get the (-)nth word of the input string or empty string let z = new RegExp("([a-zà-öA-Zø-ÿÀ-Ö0-9Ø-ßĀ-ʯff-stᴀ-ᶿ%_-]+) +(?:[a-zà-öA-Zø-ÿÀ-Ö0-9Ø-ßĀ-ʯff-stᴀ-ᶿ%_-]+ +){" + (n-1).toString() + "}$", "i"); let m = z.exec(s.slice(0, iEnd)); if (!m) { return null; } return [m.index, m[1]]; } function nextword1 (s, iStart) { // get next word (optimization) let _zNextWord = new RegExp ("^ +([a-zà-öA-Zø-ÿÀ-Ö0-9Ø-ßĀ-ʯff-stᴀ-ᶿ_][a-zà-öA-Zø-ÿÀ-Ö0-9Ø-ßĀ-ʯff-stᴀ-ᶿ_-]*)", "ig"); let m = _zNextWord.exec(s.slice(iStart)); if (!m) { return null; } return [iStart + _zNextWord.lastIndex - m[1].length, m[1]]; } const _zPrevWord = new RegExp ("([a-zà-öA-Zø-ÿÀ-Ö0-9Ø-ßĀ-ʯff-stᴀ-ᶿ_][a-zà-öA-Zø-ÿÀ-Ö0-9Ø-ßĀ-ʯff-stᴀ-ᶿ_-]*) +$", "i"); function prevword1 (s, iEnd) { // get previous word (optimization) let m = _zPrevWord.exec(s.slice(0, iEnd)); if (!m) { return null; } return [m.index, m[1]]; } function look (s, sPattern, sNegPattern=null) { // seek sPattern in s (before/after/fulltext), if antipattern sNegPattern not in s try { if (sNegPattern && re.search(sNegPattern, s)) { return false; } return re.search(sPattern, s); } catch (e) { console.error(e); } return false; } //////// Analyse groups for regex rules function displayInfo (dTokenPos, aWord) { // for debugging: info of word if (!aWord) { console.log("> nothing to find"); return true; } let lMorph = gc_engine.oSpellChecker.getMorph(aWord[1]); if (lMorph.length === 0) { console.log("> not in dictionary"); return true; } if (dTokenPos.has(aWord[0])) { console.log("DA: " + dTokenPos.get(aWord[0])); } console.log("FSA: " + lMorph); return true; } function morph (dTokenPos, aWord, sPattern, sNegPattern, bNoWord=false) { // analyse a tuple (position, word), returns true if not sNegPattern in word morphologies and sPattern in word morphologies (disambiguation on) if (!aWord) { return bNoWord; } let lMorph = (dTokenPos.has(aWord[0]) && dTokenPos.get(aWord[0]))["lMorph"] ? dTokenPos.get(aWord[0])["lMorph"] : gc_engine.oSpellChecker.getMorph(aWord[1]); if (lMorph.length === 0) { return false; } if (sNegPattern) { // check negative condition if (sNegPattern === "*") { // all morph must match sPattern return lMorph.every(sMorph => (sMorph.search(sPattern) !== -1)); } else { if (lMorph.some(sMorph => (sMorph.search(sNegPattern) !== -1))) { return false; } } } // search sPattern return lMorph.some(sMorph => (sMorph.search(sPattern) !== -1)); } function analyse (sWord, sPattern, sNegPattern) { // analyse a word, returns True if not sNegPattern in word morphologies and sPattern in word morphologies (disambiguation off) let lMorph = gc_engine.oSpellChecker.getMorph(sWord); if (lMorph.length === 0) { return false; } if (sNegPattern) { // check negative condition if (sNegPattern === "*") { // all morph must match sPattern return lMorph.every(sMorph => (sMorph.search(sPattern) !== -1)); } else { if (lMorph.some(sMorph => (sMorph.search(sNegPattern) !== -1))) { return false; } } } // search sPattern return lMorph.some(sMorph => (sMorph.search(sPattern) !== -1)); } //// Analyse tokens for graph rules function g_value (oToken, sValues, nLeft=null, nRight=null) { // test if <oToken['sValue']> is in sValues (each value should be separated with |) let sValue = (nLeft === null) ? "|"+oToken["sValue"]+"|" : "|"+oToken["sValue"].slice(nLeft, nRight)+"|"; if (sValues.includes(sValue)) { return true; } if (oToken["sValue"].slice(0,2).gl_isTitle()) { // we test only 2 first chars, to make valid words such as "Laissez-les", "Passe-partout". if (sValues.includes(sValue.toLowerCase())) { return true; } } else if (oToken["sValue"].gl_isUpperCase()) { //if sValue.lower() in sValues: // return true; sValue = "|"+sValue.slice(1).gl_toCapitalize(); if (sValues.includes(sValue)) { return true; } sValue = sValue.toLowerCase(); if (sValues.includes(sValue)) { return true; } } return false; } function g_morph (oToken, sPattern, sNegPattern="", nLeft=null, nRight=null, bMemorizeMorph=true) { // analyse a token, return True if <sNegPattern> not in morphologies and <sPattern> in morphologies let lMorph; if (oToken.hasOwnProperty("lMorph")) { lMorph = oToken["lMorph"]; } else { if (nLeft !== null) { let sValue = (nRight !== null) ? oToken["sValue"].slice(nLeft, nRight) : oToken["sValue"].slice(nLeft); lMorph = gc_engine.oSpellChecker.getMorph(sValue); if (bMemorizeMorph) { oToken["lMorph"] = lMorph; } } else { lMorph = gc_engine.oSpellChecker.getMorph(oToken["sValue"]); } } if (lMorph.length == 0) { return false; } // check negative condition if (sNegPattern) { if (sNegPattern == "*") { // all morph must match sPattern return lMorph.every(sMorph => (sMorph.search(sPattern) !== -1)); } else { if (lMorph.some(sMorph => (sMorph.search(sNegPattern) !== -1))) { return false; } } } // search sPattern return lMorph.some(sMorph => (sMorph.search(sPattern) !== -1)); } function g_analyse (oToken, sPattern, sNegPattern="", nLeft=null, nRight=null, bMemorizeMorph=true) { // analyse a token, return True if <sNegPattern> not in morphologies and <sPattern> in morphologies let lMorph; if (nLeft !== null) { let sValue = (nRight !== null) ? oToken["sValue"].slice(nLeft, nRight) : oToken["sValue"].slice(nLeft); lMorph = gc_engine.oSpellChecker.getMorph(sValue); if (bMemorizeMorph) { oToken["lMorph"] = lMorph; } } else { lMorph = gc_engine.oSpellChecker.getMorph(oToken["sValue"]); } if (lMorph.length == 0) { return false; } // check negative condition if (sNegPattern) { if (sNegPattern == "*") { // all morph must match sPattern return lMorph.every(sMorph => (sMorph.search(sPattern) !== -1)); } else { if (lMorph.some(sMorph => (sMorph.search(sNegPattern) !== -1))) { return false; } } } // search sPattern return lMorph.some(sMorph => (sMorph.search(sPattern) !== -1)); } function g_merged_analyse (oToken1, oToken2, cMerger, sPattern, sNegPattern="", bSetMorph=true) { // merge two token values, return True if <sNegPattern> not in morphologies and <sPattern> in morphologies (disambiguation off) let lMorph = gc_engine.oSpellChecker.getMorph(oToken1["sValue"] + cMerger + oToken2["sValue"]); if (lMorph.length == 0) { return false; } // check negative condition if (sNegPattern) { if (sNegPattern == "*") { // all morph must match sPattern let bResult = lMorph.every(sMorph => (sMorph.search(sPattern) !== -1)); if (bResult && bSetMorph) { oToken1["lMorph"] = lMorph; } return bResult; } else { if (lMorph.some(sMorph => (sMorph.search(sNegPattern) !== -1))) { return false; } } } // search sPattern let bResult = lMorph.some(sMorph => (sMorph.search(sPattern) !== -1)); if (bResult && bSetMorph) { oToken1["lMorph"] = lMorph; } return bResult; } function g_tag_before (oToken, dTags, sTag) { if (!dTags.has(sTag)) { return false; } if (oToken["i"] > dTags.get(sTag)[0]) { return true; } return false; } function g_tag_after (oToken, dTags, sTag) { if (!dTags.has(sTag)) { return false; } if (oToken["i"] < dTags.get(sTag)[1]) { return true; } return false; } function g_tag (oToken, sTag) { return oToken.hasOwnProperty("aTags") && oToken["aTags"].has(sTag); } function g_space_between_tokens (oToken1, oToken2, nMin, nMax=null) { let nSpace = oToken2["nStart"] - oToken1["nEnd"] if (nSpace < nMin) { return false; } if (nMax !== null && nSpace > nMax) { return false; } return true; } function g_token (lToken, i) { if (i < 0) { return lToken[0]; } if (i >= lToken.length) { return lToken[lToken.length-1]; } return lToken[i]; } //////// Disambiguator for regex rules function select (dTokenPos, nPos, sWord, sPattern, lDefault=null) { if (!sWord) { return true; } if (!dTokenPos.has(nPos)) { console.log("Error. There should be a token at this position: ", nPos); return true; } let lMorph = gc_engine.oSpellChecker.getMorph(sWord); if (lMorph.length === 0 || lMorph.length === 1) { return true; } let lSelect = lMorph.filter( sMorph => sMorph.search(sPattern) !== -1 ); if (lSelect.length > 0) { if (lSelect.length != lMorph.length) { dTokenPos.get(nPos)["lMorph"] = lSelect; } } else if (lDefault) { dTokenPos.get(nPos)["lMorph"] = lDefault; } return true; } function exclude (dTokenPos, nPos, sWord, sPattern, lDefault=null) { if (!sWord) { return true; } if (!dTokenPos.has(nPos)) { console.log("Error. There should be a token at this position: ", nPos); return true; } let lMorph = gc_engine.oSpellChecker.getMorph(sWord); if (lMorph.length === 0 || lMorph.length === 1) { return true; } let lSelect = lMorph.filter( sMorph => sMorph.search(sPattern) === -1 ); if (lSelect.length > 0) { if (lSelect.length != lMorph.length) { dTokenPos.get(nPos)["lMorph"] = lSelect; } } else if (lDefault) { dTokenPos.get(nPos)["lMorph"] = lDefault; } return true; } function define (dTokenPos, nPos, sMorphs) { dTokenPos.get(nPos)["lMorph"] = sMorphs.split("|"); return true; } //// Disambiguation for graph rules function g_select (oToken, sPattern, lDefault=null) { // select morphologies for <oToken> according to <sPattern>, always return true let lMorph = (oToken.hasOwnProperty("lMorph")) ? oToken["lMorph"] : gc_engine.oSpellChecker.getMorph(oToken["sValue"]); if (lMorph.length === 0 || lMorph.length === 1) { if (lDefault) { oToken["lMorph"] = lDefault; } return true; } let lSelect = lMorph.filter( sMorph => sMorph.search(sPattern) !== -1 ); if (lSelect.length > 0) { if (lSelect.length != lMorph.length) { oToken["lMorph"] = lSelect; } } else if (lDefault) { oToken["lMorph"] = lDefault; } return true; } function g_exclude (oToken, sPattern, lDefault=null) { // select morphologies for <oToken> according to <sPattern>, always return true let lMorph = (oToken.hasOwnProperty("lMorph")) ? oToken["lMorph"] : gc_engine.oSpellChecker.getMorph(oToken["sValue"]); if (lMorph.length === 0 || lMorph.length === 1) { if (lDefault) { oToken["lMorph"] = lDefault; } return true; } let lSelect = lMorph.filter( sMorph => sMorph.search(sPattern) === -1 ); if (lSelect.length > 0) { if (lSelect.length != lMorph.length) { oToken["lMorph"] = lSelect; } } else if (lDefault) { oToken["lMorph"] = lDefault; } return true; } function g_add_morph (oToken, sNewMorph) { // Disambiguation: add a morphology to a token let lMorph = (oToken.hasOwnProperty("lMorph")) ? oToken["lMorph"] : gc_engine.oSpellChecker.getMorph(oToken["sValue"]); lMorph.push(...sNewMorph.split("|")); oToken["lMorph"] = lMorph; return true; } function g_rewrite (oToken, sToReplace, sReplace) { // Disambiguation: rewrite morphologies let lMorph = (oToken.hasOwnProperty("lMorph")) ? oToken["lMorph"] : gc_engine.oSpellChecker.getMorph(oToken["sValue"]); oToken["lMorph"] = lMorph.map(s => s.replace(sToReplace, sReplace)); return true; } function g_define (oToken, sMorphs) { // set morphologies of <oToken>, always return true oToken["lMorph"] = sMorphs.split("|"); return true; } function g_define_from (oToken, nLeft=null, nRight=null) { let sValue = oToken["sValue"]; if (nLeft !== null) { sValue = (nRight !== null) ? sValue.slice(nLeft, nRight) : sValue.slice(nLeft); } oToken["lMorph"] = gc_engine.oSpellChecker.getMorph(sValue); return true; } function g_change_meta (oToken, sType) { // Disambiguation: change type of token oToken["sType"] = sType; return true; } //////// GRAMMAR CHECKER PLUGINS ${pluginsJS} // generated code, do not edit var gc_functions = { load: function (sContext, oSpellChecker) { _sAppContext = sContext _oSpellChecker = oSpellChecker }, // callables for regex rules ${callablesJS} // callables for graph rules ${graph_callablesJS} } if (typeof(exports) !== 'undefined') { exports.load = gc_functions.load; } |
Modified gc_lang/fr/webext/gce_worker.js from [c784b7c2c4] to [e351d5e6fd].
︙ | ︙ | |||
41 42 43 44 45 46 47 48 49 50 51 52 53 54 | importScripts("grammalecte/text.js"); importScripts("grammalecte/graphspell/tokenizer.js"); importScripts("grammalecte/fr/conj.js"); importScripts("grammalecte/fr/mfsp.js"); importScripts("grammalecte/fr/phonet.js"); importScripts("grammalecte/fr/cregex.js"); importScripts("grammalecte/fr/gc_options.js"); importScripts("grammalecte/fr/gc_rules.js"); importScripts("grammalecte/fr/gc_rules_graph.js"); importScripts("grammalecte/fr/gc_engine.js"); importScripts("grammalecte/tests.js"); /* Warning. Initialization can’t be completed at startup of the worker, | > | 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | importScripts("grammalecte/text.js"); importScripts("grammalecte/graphspell/tokenizer.js"); importScripts("grammalecte/fr/conj.js"); importScripts("grammalecte/fr/mfsp.js"); importScripts("grammalecte/fr/phonet.js"); importScripts("grammalecte/fr/cregex.js"); importScripts("grammalecte/fr/gc_options.js"); importScripts("grammalecte/fr/gc_functions.js"); importScripts("grammalecte/fr/gc_rules.js"); importScripts("grammalecte/fr/gc_rules_graph.js"); importScripts("grammalecte/fr/gc_engine.js"); importScripts("grammalecte/tests.js"); /* Warning. Initialization can’t be completed at startup of the worker, |
︙ | ︙ |