Grammalecte  Check-in [198693536c]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:[core][js] gc engine: code clarification, stop using prefix d for object variables
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | core
Files: files | file ages | folders
SHA3-256:198693536cf02eec20186f8f888fb14d6579e71d3636a134dfcd6787388b2556
User & Date: olr 2019-07-01 12:11:15
Context
2019-07-02
11:29
[fr] ajustements check-in: a250c33092 user: olr tags: fr, trunk
2019-07-01
12:11
[core][js] gc engine: code clarification, stop using prefix d for object variables check-in: 198693536c user: olr tags: core, trunk
12:05
[core][js] gc engine: code clarification, stop using prefix d for object variables check-in: 3860f0b62f user: olr tags: core, trunk
Changes

Changes to gc_core/js/lang_core/gc_engine.js.

   914    914       rewriteFromTags (bDebug=false) {
   915    915           // rewrite the sentence, modify tokens, purge the token list
   916    916           if (bDebug) {
   917    917               console.log("REWRITE");
   918    918           }
   919    919           let lNewToken = [];
   920    920           let nMergeUntil = 0;
   921         -        let dTokenMerger = null;
   922         -        for (let [iToken, dToken] of this.lToken.entries()) {
          921  +        let oMergingToken = null;
          922  +        for (let [iToken, oToken] of this.lToken.entries()) {
   923    923               let bKeepToken = true;
   924         -            if (dToken["sType"] != "INFO") {
          924  +            if (oToken["sType"] != "INFO") {
   925    925                   if (nMergeUntil && iToken <= nMergeUntil) {
   926         -                    dTokenMerger["sValue"] += " ".repeat(dToken["nStart"] - dTokenMerger["nEnd"]) + dToken["sValue"];
   927         -                    dTokenMerger["nEnd"] = dToken["nEnd"];
          926  +                    oMergingToken["sValue"] += " ".repeat(oToken["nStart"] - oMergingToken["nEnd"]) + oToken["sValue"];
          927  +                    oMergingToken["nEnd"] = oToken["nEnd"];
   928    928                       if (bDebug) {
   929         -                        console.log("  MERGED TOKEN: " + dTokenMerger["sValue"]);
          929  +                        console.log("  MERGED TOKEN: " + oMergingToken["sValue"]);
   930    930                       }
   931    931                       bKeepToken = false;
   932    932                   }
   933         -                if (dToken.hasOwnProperty("nMergeUntil")) {
          933  +                if (oToken.hasOwnProperty("nMergeUntil")) {
   934    934                       if (iToken > nMergeUntil) { // this token is not already merged with a previous token
   935         -                        dTokenMerger = dToken;
          935  +                        oMergingToken = oToken;
   936    936                       }
   937         -                    if (dToken["nMergeUntil"] > nMergeUntil) {
   938         -                        nMergeUntil = dToken["nMergeUntil"];
          937  +                    if (oToken["nMergeUntil"] > nMergeUntil) {
          938  +                        nMergeUntil = oToken["nMergeUntil"];
   939    939                       }
   940         -                    delete dToken["nMergeUntil"];
          940  +                    delete oToken["nMergeUntil"];
   941    941                   }
   942         -                else if (dToken.hasOwnProperty("bToRemove")) {
          942  +                else if (oToken.hasOwnProperty("bToRemove")) {
   943    943                       if (bDebug) {
   944         -                        console.log("  REMOVED: " + dToken["sValue"]);
          944  +                        console.log("  REMOVED: " + oToken["sValue"]);
   945    945                       }
   946         -                    this.sSentence = this.sSentence.slice(0, dToken["nStart"]) + " ".repeat(dToken["nEnd"] - dToken["nStart"]) + this.sSentence.slice(dToken["nEnd"]);
          946  +                    this.sSentence = this.sSentence.slice(0, oToken["nStart"]) + " ".repeat(oToken["nEnd"] - oToken["nStart"]) + this.sSentence.slice(oToken["nEnd"]);
   947    947                       bKeepToken = false;
   948    948                   }
   949    949               }
   950    950               //
   951    951               if (bKeepToken) {
   952         -                lNewToken.push(dToken);
   953         -                if (dToken.hasOwnProperty("sNewValue")) {
          952  +                lNewToken.push(oToken);
          953  +                if (oToken.hasOwnProperty("sNewValue")) {
   954    954                       // rewrite token and sentence
   955    955                       if (bDebug) {
   956         -                        console.log(dToken["sValue"] + " -> " + dToken["sNewValue"]);
          956  +                        console.log(oToken["sValue"] + " -> " + oToken["sNewValue"]);
   957    957                       }
   958         -                    dToken["sRealValue"] = dToken["sValue"];
   959         -                    dToken["sValue"] = dToken["sNewValue"];
   960         -                    let nDiffLen = dToken["sRealValue"].length - dToken["sNewValue"].length;
   961         -                    let sNewRepl = (nDiffLen >= 0) ? dToken["sNewValue"] + " ".repeat(nDiffLen) : dToken["sNewValue"].slice(0, dToken["sRealValue"].length);
   962         -                    this.sSentence = this.sSentence.slice(0,dToken["nStart"]) + sNewRepl + this.sSentence.slice(dToken["nEnd"]);
   963         -                    delete dToken["sNewValue"];
          958  +                    oToken["sRealValue"] = oToken["sValue"];
          959  +                    oToken["sValue"] = oToken["sNewValue"];
          960  +                    let nDiffLen = oToken["sRealValue"].length - oToken["sNewValue"].length;
          961  +                    let sNewRepl = (nDiffLen >= 0) ? oToken["sNewValue"] + " ".repeat(nDiffLen) : oToken["sNewValue"].slice(0, oToken["sRealValue"].length);
          962  +                    this.sSentence = this.sSentence.slice(0,oToken["nStart"]) + sNewRepl + this.sSentence.slice(oToken["nEnd"]);
          963  +                    delete oToken["sNewValue"];
   964    964                   }
   965    965               }
   966    966               else {
   967    967                   try {
   968         -                    this.dTokenPos.delete(dToken["nStart"]);
          968  +                    this.dTokenPos.delete(oToken["nStart"]);
   969    969                   }
   970    970                   catch (e) {
   971    971                       console.log(this.asString());
   972         -                    console.log(dToken);
          972  +                    console.log(oToken);
   973    973                   }
   974    974               }
   975    975           }
   976    976           if (bDebug) {
   977    977               console.log("  TEXT REWRITED: " + this.sSentence);
   978    978           }
   979    979           this.lToken.length = 0;
................................................................................
  1134   1134       // search sPattern
  1135   1135       return lMorph.some(sMorph  =>  (sMorph.search(sPattern) !== -1));
  1136   1136   }
  1137   1137   
  1138   1138   
  1139   1139   //// Analyse tokens for graph rules
  1140   1140   
  1141         -function g_value (dToken, sValues, nLeft=null, nRight=null) {
  1142         -    // test if <dToken['sValue']> is in sValues (each value should be separated with |)
  1143         -    let sValue = (nLeft === null) ? "|"+dToken["sValue"]+"|" : "|"+dToken["sValue"].slice(nLeft, nRight)+"|";
         1141  +function g_value (oToken, sValues, nLeft=null, nRight=null) {
         1142  +    // test if <oToken['sValue']> is in sValues (each value should be separated with |)
         1143  +    let sValue = (nLeft === null) ? "|"+oToken["sValue"]+"|" : "|"+oToken["sValue"].slice(nLeft, nRight)+"|";
  1144   1144       if (sValues.includes(sValue)) {
  1145   1145           return true;
  1146   1146       }
  1147         -    if (dToken["sValue"].slice(0,2).gl_isTitle()) { // we test only 2 first chars, to make valid words such as "Laissez-les", "Passe-partout".
         1147  +    if (oToken["sValue"].slice(0,2).gl_isTitle()) { // we test only 2 first chars, to make valid words such as "Laissez-les", "Passe-partout".
  1148   1148           if (sValues.includes(sValue.toLowerCase())) {
  1149   1149               return true;
  1150   1150           }
  1151   1151       }
  1152         -    else if (dToken["sValue"].gl_isUpperCase()) {
         1152  +    else if (oToken["sValue"].gl_isUpperCase()) {
  1153   1153           //if sValue.lower() in sValues:
  1154   1154           //    return true;
  1155   1155           sValue = "|"+sValue.slice(1).gl_toCapitalize();
  1156   1156           if (sValues.includes(sValue)) {
  1157   1157               return true;
  1158   1158           }
  1159   1159           sValue = sValue.toLowerCase();
................................................................................
  1160   1160           if (sValues.includes(sValue)) {
  1161   1161               return true;
  1162   1162           }
  1163   1163       }
  1164   1164       return false;
  1165   1165   }
  1166   1166   
  1167         -function g_morph (dToken, sPattern, sNegPattern="", nLeft=null, nRight=null, bMemorizeMorph=true) {
         1167  +function g_morph (oToken, sPattern, sNegPattern="", nLeft=null, nRight=null, bMemorizeMorph=true) {
  1168   1168       // analyse a token, return True if <sNegPattern> not in morphologies and <sPattern> in morphologies
  1169   1169       let lMorph;
  1170         -    if (dToken.hasOwnProperty("lMorph")) {
  1171         -        lMorph = dToken["lMorph"];
         1170  +    if (oToken.hasOwnProperty("lMorph")) {
         1171  +        lMorph = oToken["lMorph"];
  1172   1172       }
  1173   1173       else {
  1174   1174           if (nLeft !== null) {
  1175         -            let sValue = (nRight !== null) ? dToken["sValue"].slice(nLeft, nRight) : dToken["sValue"].slice(nLeft);
         1175  +            let sValue = (nRight !== null) ? oToken["sValue"].slice(nLeft, nRight) : oToken["sValue"].slice(nLeft);
  1176   1176               lMorph = _oSpellChecker.getMorph(sValue);
  1177   1177               if (bMemorizeMorph) {
  1178         -                dToken["lMorph"] = lMorph;
         1178  +                oToken["lMorph"] = lMorph;
  1179   1179               }
  1180   1180           } else {
  1181         -            lMorph = _oSpellChecker.getMorph(dToken["sValue"]);
         1181  +            lMorph = _oSpellChecker.getMorph(oToken["sValue"]);
  1182   1182           }
  1183   1183       }
  1184   1184       if (lMorph.length == 0) {
  1185   1185           return false;
  1186   1186       }
  1187   1187       // check negative condition
  1188   1188       if (sNegPattern) {
................................................................................
  1196   1196               }
  1197   1197           }
  1198   1198       }
  1199   1199       // search sPattern
  1200   1200       return lMorph.some(sMorph  =>  (sMorph.search(sPattern) !== -1));
  1201   1201   }
  1202   1202   
  1203         -function g_analyse (dToken, sPattern, sNegPattern="", nLeft=null, nRight=null, bMemorizeMorph=true) {
         1203  +function g_analyse (oToken, sPattern, sNegPattern="", nLeft=null, nRight=null, bMemorizeMorph=true) {
  1204   1204       // analyse a token, return True if <sNegPattern> not in morphologies and <sPattern> in morphologies
  1205   1205       let lMorph;
  1206   1206       if (nLeft !== null) {
  1207         -        let sValue = (nRight !== null) ? dToken["sValue"].slice(nLeft, nRight) : dToken["sValue"].slice(nLeft);
         1207  +        let sValue = (nRight !== null) ? oToken["sValue"].slice(nLeft, nRight) : oToken["sValue"].slice(nLeft);
  1208   1208           lMorph = _oSpellChecker.getMorph(sValue);
  1209   1209           if (bMemorizeMorph) {
  1210         -            dToken["lMorph"] = lMorph;
         1210  +            oToken["lMorph"] = lMorph;
  1211   1211           }
  1212   1212       } else {
  1213         -        lMorph = _oSpellChecker.getMorph(dToken["sValue"]);
         1213  +        lMorph = _oSpellChecker.getMorph(oToken["sValue"]);
  1214   1214       }
  1215   1215       if (lMorph.length == 0) {
  1216   1216           return false;
  1217   1217       }
  1218   1218       // check negative condition
  1219   1219       if (sNegPattern) {
  1220   1220           if (sNegPattern == "*") {
................................................................................
  1227   1227               }
  1228   1228           }
  1229   1229       }
  1230   1230       // search sPattern
  1231   1231       return lMorph.some(sMorph  =>  (sMorph.search(sPattern) !== -1));
  1232   1232   }
  1233   1233   
  1234         -function g_merged_analyse (dToken1, dToken2, cMerger, sPattern, sNegPattern="", bSetMorph=true) {
         1234  +function g_merged_analyse (oToken1, oToken2, cMerger, sPattern, sNegPattern="", bSetMorph=true) {
  1235   1235       // merge two token values, return True if <sNegPattern> not in morphologies and <sPattern> in morphologies (disambiguation off)
  1236         -    let lMorph = _oSpellChecker.getMorph(dToken1["sValue"] + cMerger + dToken2["sValue"]);
         1236  +    let lMorph = _oSpellChecker.getMorph(oToken1["sValue"] + cMerger + oToken2["sValue"]);
  1237   1237       if (lMorph.length == 0) {
  1238   1238           return false;
  1239   1239       }
  1240   1240       // check negative condition
  1241   1241       if (sNegPattern) {
  1242   1242           if (sNegPattern == "*") {
  1243   1243               // all morph must match sPattern
  1244   1244               let bResult = lMorph.every(sMorph  =>  (sMorph.search(sPattern) !== -1));
  1245   1245               if (bResult && bSetMorph) {
  1246         -                dToken1["lMorph"] = lMorph;
         1246  +                oToken1["lMorph"] = lMorph;
  1247   1247               }
  1248   1248               return bResult;
  1249   1249           }
  1250   1250           else {
  1251   1251               if (lMorph.some(sMorph  =>  (sMorph.search(sNegPattern) !== -1))) {
  1252   1252                   return false;
  1253   1253               }
  1254   1254           }
  1255   1255       }
  1256   1256       // search sPattern
  1257   1257       let bResult = lMorph.some(sMorph  =>  (sMorph.search(sPattern) !== -1));
  1258   1258       if (bResult && bSetMorph) {
  1259         -        dToken1["lMorph"] = lMorph;
         1259  +        oToken1["lMorph"] = lMorph;
  1260   1260       }
  1261   1261       return bResult;
  1262   1262   }
  1263   1263   
  1264         -function g_tag_before (dToken, dTags, sTag) {
         1264  +function g_tag_before (oToken, dTags, sTag) {
  1265   1265       if (!dTags.has(sTag)) {
  1266   1266           return false;
  1267   1267       }
  1268         -    if (dToken["i"] > dTags.get(sTag)[0]) {
         1268  +    if (oToken["i"] > dTags.get(sTag)[0]) {
  1269   1269           return true;
  1270   1270       }
  1271   1271       return false;
  1272   1272   }
  1273   1273   
  1274         -function g_tag_after (dToken, dTags, sTag) {
         1274  +function g_tag_after (oToken, dTags, sTag) {
  1275   1275       if (!dTags.has(sTag)) {
  1276   1276           return false;
  1277   1277       }
  1278         -    if (dToken["i"] < dTags.get(sTag)[1]) {
         1278  +    if (oToken["i"] < dTags.get(sTag)[1]) {
  1279   1279           return true;
  1280   1280       }
  1281   1281       return false;
  1282   1282   }
  1283   1283   
  1284         -function g_tag (dToken, sTag) {
  1285         -    return dToken.hasOwnProperty("aTags") && dToken["aTags"].has(sTag);
         1284  +function g_tag (oToken, sTag) {
         1285  +    return oToken.hasOwnProperty("aTags") && oToken["aTags"].has(sTag);
  1286   1286   }
  1287   1287   
  1288         -function g_space_between_tokens (dToken1, dToken2, nMin, nMax=null) {
  1289         -    let nSpace = dToken2["nStart"] - dToken1["nEnd"]
         1288  +function g_space_between_tokens (oToken1, oToken2, nMin, nMax=null) {
         1289  +    let nSpace = oToken2["nStart"] - oToken1["nEnd"]
  1290   1290       if (nSpace < nMin) {
  1291   1291           return false;
  1292   1292       }
  1293   1293       if (nMax !== null && nSpace > nMax) {
  1294   1294           return false;
  1295   1295       }
  1296   1296       return true;
................................................................................
  1359   1359       dTokenPos.get(nPos)["lMorph"] = lMorph;
  1360   1360       return true;
  1361   1361   }
  1362   1362   
  1363   1363   
  1364   1364   //// Disambiguation for graph rules
  1365   1365   
  1366         -function g_select (dToken, sPattern, lDefault=null) {
  1367         -    // select morphologies for <dToken> according to <sPattern>, always return true
  1368         -    let lMorph = (dToken.hasOwnProperty("lMorph")) ? dToken["lMorph"] : _oSpellChecker.getMorph(dToken["sValue"]);
         1366  +function g_select (oToken, sPattern, lDefault=null) {
         1367  +    // select morphologies for <oToken> according to <sPattern>, always return true
         1368  +    let lMorph = (oToken.hasOwnProperty("lMorph")) ? oToken["lMorph"] : _oSpellChecker.getMorph(oToken["sValue"]);
  1369   1369       if (lMorph.length === 0  || lMorph.length === 1) {
  1370   1370           if (lDefault) {
  1371         -            dToken["lMorph"] = lDefault;
         1371  +            oToken["lMorph"] = lDefault;
  1372   1372           }
  1373   1373           return true;
  1374   1374       }
  1375   1375       let lSelect = lMorph.filter( sMorph => sMorph.search(sPattern) !== -1 );
  1376   1376       if (lSelect.length > 0) {
  1377   1377           if (lSelect.length != lMorph.length) {
  1378         -            dToken["lMorph"] = lSelect;
         1378  +            oToken["lMorph"] = lSelect;
  1379   1379           }
  1380   1380       } else if (lDefault) {
  1381         -        dToken["lMorph"] = lDefault;
         1381  +        oToken["lMorph"] = lDefault;
  1382   1382       }
  1383   1383       return true;
  1384   1384   }
  1385   1385   
  1386         -function g_exclude (dToken, sPattern, lDefault=null) {
  1387         -    // select morphologies for <dToken> according to <sPattern>, always return true
  1388         -    let lMorph = (dToken.hasOwnProperty("lMorph")) ? dToken["lMorph"] : _oSpellChecker.getMorph(dToken["sValue"]);
         1386  +function g_exclude (oToken, sPattern, lDefault=null) {
         1387  +    // select morphologies for <oToken> according to <sPattern>, always return true
         1388  +    let lMorph = (oToken.hasOwnProperty("lMorph")) ? oToken["lMorph"] : _oSpellChecker.getMorph(oToken["sValue"]);
  1389   1389       if (lMorph.length === 0  || lMorph.length === 1) {
  1390   1390           if (lDefault) {
  1391         -            dToken["lMorph"] = lDefault;
         1391  +            oToken["lMorph"] = lDefault;
  1392   1392           }
  1393   1393           return true;
  1394   1394       }
  1395   1395       let lSelect = lMorph.filter( sMorph => sMorph.search(sPattern) === -1 );
  1396   1396       if (lSelect.length > 0) {
  1397   1397           if (lSelect.length != lMorph.length) {
  1398         -            dToken["lMorph"] = lSelect;
         1398  +            oToken["lMorph"] = lSelect;
  1399   1399           }
  1400   1400       } else if (lDefault) {
  1401         -        dToken["lMorph"] = lDefault;
         1401  +        oToken["lMorph"] = lDefault;
  1402   1402       }
  1403   1403       return true;
  1404   1404   }
  1405   1405   
  1406         -function g_define (dToken, lMorph) {
  1407         -    // set morphologies of <dToken>, always return true
  1408         -    dToken["lMorph"] = lMorph;
         1406  +function g_define (oToken, lMorph) {
         1407  +    // set morphologies of <oToken>, always return true
         1408  +    oToken["lMorph"] = lMorph;
  1409   1409       return true;
  1410   1410   }
  1411   1411   
  1412         -function g_define_from (dToken, nLeft=null, nRight=null) {
  1413         -    let sValue = dToken["sValue"];
         1412  +function g_define_from (oToken, nLeft=null, nRight=null) {
         1413  +    let sValue = oToken["sValue"];
  1414   1414       if (nLeft !== null) {
  1415   1415           sValue = (nRight !== null) ? sValue.slice(nLeft, nRight) : sValue.slice(nLeft);
  1416   1416       }
  1417         -    dToken["lMorph"] = _oSpellChecker.getMorph(sValue);
         1417  +    oToken["lMorph"] = _oSpellChecker.getMorph(sValue);
  1418   1418       return true;
  1419   1419   }
  1420   1420   
  1421   1421   
  1422   1422   //////// GRAMMAR CHECKER PLUGINS
  1423   1423   
  1424   1424   ${pluginsJS}