Grammalecte  Diff

Differences From Artifact [7e54b6b49b]:

To Artifact [fd0872dfca]:


1
2


3
4
5
6

7
8

9
10


11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12


13
14
15
16
17
18
19
20
21


+
+




+


+
-
-
+
+







// Grammalecte - Lexicographe
// License: MPL 2
/*jslint esversion: 6*/
/*global require,exports*/

"use strict";

${string}
${map}


if (typeof(require) !== 'undefined') {
const helpers = require("resource://grammalecte/helpers.js");

    var helpers = require("resource://grammalecte/helpers.js");
}

const _dTAGS = new Map ([
    [':G', "[mot grammatical]"],
    [':N', " nom,"],
    [':A', " adjectif,"],
    [':M1', " prénom,"],
    [':M2', " patronyme,"],
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


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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
+
-

-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
-
-
-
+
+
-
-
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+

-
-
-



+
-
-
+
+
-

-
+

















-
+
-
+













-
+



+
-
+
+

    ['en', " pronom adverbial"],
    ["m'en", " (me) pronom personnel objet + (en) pronom adverbial"],
    ["t'en", " (te) pronom personnel objet + (en) pronom adverbial"],
    ["s'en", " (se) pronom personnel objet + (en) pronom adverbial"]
]);

const _dSeparator = new Map ([
    ['.', "point"],
    ['·', "point médian"],
    ['…', "points de suspension"],
    [':', "deux-points"],
    [';', "point-virgule"],
    [',', "virgule"],
    ['?', "point d’interrogation"],
    ['!', "point d’exclamation"],
    ['(', "parenthèse ouvrante"],
    [')', "parenthèse fermante"],
    ['[', "crochet ouvrante"],
    [']', "crochet fermante"],
    ['{', "accolade ouvrante"],
    ['}', "accolade fermante"],
    ['-', "tiret"],
    ['—', "tiret cadratin"],
    ['–', "tiret demi-cadratin"],
    ['«', "guillemet ouvrant (chevrons)"],
    ['»', "guillemet fermant (chevrons)"],
    ['“', "guillemet ouvrant double"],
    ['”', "guillemet fermant double"],
    ['‘', "guillemet ouvrant"],
    ['’', "guillemet fermant"],
    ['/', "signe de la division"],
    ['+', "signe de l’addition"],
    ['*', "signe de la multiplication"],
    ['=', "signe de l’égalité"],
    ['<', "inférieur à"],
    ['>', "supérieur à"],
]);


class Lexicographe {

    constructor (oDict) {
        this.oDict = oDict;
        this._zElidedPrefix = new RegExp ("^([dljmtsncç]|quoiqu|lorsqu|jusqu|puisqu|qu)['’](.+)", "i");
        this._zCompoundWord = new RegExp ("([a-zA-Zà-ö0-9À-Öø-ÿØ-ßĀ-ʯ]+)-((?:les?|la)-(?:moi|toi|lui|[nv]ous|leur)|t-(?:il|elle|on)|y|en|[mts][’'](?:y|en)|les?|l[aà]|[mt]oi|leur|lui|je|tu|ils?|elles?|on|[nv]ous)$", "i");
        this._zTag = new RegExp ("[:;/][a-zA-Zà-ö0-9À-Öø-ÿØ-ßĀ-ʯ*][^:;/]*", "g");
    };

    }
    analyzeText (sText) {
        sText = sText.replace(/[.,.?!:;…\/()\[\]“”«»"„{}–—#+*<>%=\n]/g, " ").replace(/\s+/g, " ");
        let iStart = 0;
        let iEnd = 0;
        let sHtml = '<div class="paragraph">\n';
        while ((iEnd = sText.indexOf(" ", iStart)) !== -1) {
            sHtml += this.analyzeWord(sText.slice(iStart, iEnd));
            iStart = iEnd + 1;
        }
        sHtml += this.analyzeWord(sText.slice(iStart));
        return sHtml + '</div>\n';

    getInfoForToken (oToken) {
        // Token: .sType, .sValue, .nStart, .nEnd
        // return a list [type, token_string, values]
    }

        let m = null;
    analyzeWord (sWord) {
        try {
            if (!sWord) {
                return "";
            }
            if (sWord._count("-") > 4) {
                return '<p><b class="mbok">' + sWord + "</b> <s>:</s> élément complexe indéterminé</p>\n";
            }
            if (sWord._isDigit()) {
            switch (oToken.sType) {
                case 'SEPARATOR':
                    return { sType: oToken.sType, sValue: oToken.sValue, aLabel: [_dSeparator.gl_get(oToken.sValue, "caractère indéterminé")] };
                    break;
                case 'NUM':
                    return { sType: oToken.sType, sValue: oToken.sValue, aLabel: ["nombre"] };
                    break;
                case 'LINK':
                    return { sType: oToken.sType, sValue: oToken.sValue.slice(0,40)+"…", aLabel: ["hyperlien"] };
                    break;
                case 'ELPFX':
                    let sTemp = oToken.sValue.replace("’", "").replace("'", "").replace("`", "").toLowerCase();
                    return { sType: oToken.sType, sValue: oToken.sValue, aLabel: [_dPFX.gl_get(sTemp, "préfixe élidé inconnu")] };
                    break;
                case 'WORD': 
                    if (oToken.sValue.gl_count("-") > 4) {
                        return { sType: "COMPLEX", sValue: oToken.sValue, aLabel: ["élément complexe indéterminé"] };
                    }
                    else if (this.oDict.isValidToken(oToken.sValue)) {
                return '<p><b class="nb">' + sWord + "</b> <s>:</s> nombre</p>\n";
            }

            let sHtml = "";
                        let lMorph = this.oDict.getMorph(oToken.sValue);
                        let aElem = [];
            // préfixes élidés
            let m = this._zElidedPrefix.exec(sWord);
            if (m !== null) {
                        for (let s of lMorph){
                            if (s.includes(":"))  aElem.push( this._formatTags(s) );
                sWord = m[2];
                sHtml += "<p><b>" + m[1] + "’</b> <s>:</s> " + _dPFX.get(m[1].toLowerCase()) + " </p>\n";
            }
                        }
            // mots composés
            let m2 = this._zCompoundWord.exec(sWord);
            if (m2 !== null) {
                sWord = m2[1];
            }
            // Morphologies
            let lMorph = this.oDict.getMorph(sWord);
            if (lMorph.length === 1) {
                        return { sType: oToken.sType, sValue: oToken.sValue, aLabel: aElem};
                    }
                    else if (m = this._zCompoundWord.exec(oToken.sValue)) {
                        // mots composés
                        let lMorph = this.oDict.getMorph(m[1]);
                        let aElem = [];
                sHtml += "<p><b>" + sWord + "</b> <s>:</s> " + this.formatTags(lMorph[0]) + "</p>\n";
            } else if (lMorph.length > 1) {
                sHtml += "<p><b>" + sWord + "</b><ul><li>" + [for (s of lMorph) if (s.includes(":")) this.formatTags(s)].join(" </li><li> ") + "</li></ul></p>\n";
            } else {
                sHtml += '<p><b class="unknown">' + sWord + "</b> <s>:</s>  absent du dictionnaire<p>\n";
            }
                        for (let s of lMorph){
                            if (s.includes(":"))  aElem.push( this._formatTags(s) );
                        }
                        aElem.push("-" + m[2] + ": " + this._formatSuffix(m[2].toLowerCase()));
                        return { sType: oToken.sType, sValue: oToken.sValue, aLabel: aElem };
                    }
                    else {
                        return { sType: "UNKNOWN", sValue: oToken.sValue, aLabel: ["inconnu du dictionnaire"] };
                    }
            // suffixe d’un mot composé
            if (m2) {
                sHtml += "<p>-<b>" + m2[2] + "</b> <s>:</s> " + this._formatSuffix(m2[2].toLowerCase()) + "</p>\n";
                    break;
            }
            // Verbes
            //let aVerb = new Set([ for (s of lMorph) if (s.includes(":V")) s.slice(1, s.indexOf(" ")) ]);
            return sHtml;
        }
        catch (e) {
            helpers.logerror(e);
        }
            return "#erreur";
        }
        return null;
    }
    };

    formatTags (sTags) {
    _formatTags (sTags) {
        let sRes = "";
        sTags = sTags.replace(/V([0-3][ea]?)[itpqnmr_eaxz]+/, "V$1");
        let m;
        while ((m = this._zTag.exec(sTags)) !== null) {
            sRes += _dTAGS.get(m[0]);
            if (sRes.length > 100) {
                break;
            }
        }
        if (sRes.startsWith(" verbe") && !sRes.endsWith("infinitif")) {
            sRes += " [" + sTags.slice(1, sTags.indexOf(" ")) + "]";
        }
        if (!sRes) {
            sRes = "#Erreur. Étiquette inconnue : [" + sTags + "]";
            helpers.echo(sRes);
            return sRes;
        }
        return sRes._trimRight(",");
        return sRes.gl_trimRight(",");
    };
    }

    _formatSuffix (s) {
        if (s.startsWith("t-")) {
            return "“t” euphonique +" + _dAD.get(s.slice(2));
        }
        if (!s.includes("-")) {
            return _dAD.get(s.replace("’", "'"));
        }
        if (s.endsWith("ous")) {
            s += '2';
        }
        let nPos = s.indexOf("-");
        return _dAD.get(s.slice(0, nPos)) + " +" + _dAD.get(s.slice(nPos+1));
    };
    }
}


if (typeof(exports) !== 'undefined') {
exports.Lexicographe = Lexicographe;
    exports.Lexicographe = Lexicographe;
}