Grammalecte  Diff

Differences From Artifact [7139f8d80b]:

To Artifact [ec0315fba1]:


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
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












+
+
+
+
+
+
+
+
+














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






+
-
+
+
+
+





-
+







// JavaScript

"use strict";

// Chrome don’t follow the W3C specification:
// https://browserext.github.io/browserext/
let bChrome = false;
if (typeof(browser) !== "object") {
    var browser = chrome;
    bChrome = true;
}


/*
    Common functions
*/

function showError (e) {
    console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}

function createNode  (sType, oAttr, oDataset=null) {
    try {
        let xNode = document.createElement(sType);
        Object.assign(xNode, oAttr);
        if (oDataset) {
            Object.assign(xNode.dataset, oDataset);
        }
        return xNode;
    }
    catch (e) {
        showError(e);
    }
}

function showError (e) {
function showElement (sElemId) {
    console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
}


document.getElementById("lexicon_button").addEventListener("click", () => { oWidgets.showPage("lexicon"); }, false);
document.getElementById("add_word_button").addEventListener("click", () => { oWidgets.showPage("lemma"); }, false);

document.getElementById("table").addEventListener("click", (xEvent) => { oWidgets.onTableClick(xEvent); }, false);
document.getElementById("save_button").addEventListener("click", () => { oLexicon.save(); }, false);
document.getElementById("export_button").addEventListener("click", () => { oBinaryDict.export(); }, false);

document.getElementById("editor").addEventListener("click", (xEvent) => { oWidgets.onSelectionClick(xEvent); }, false);
document.getElementById("lemma").addEventListener("keyup", () => { oWidgets.onWrite(); }, false);
document.getElementById("lemma2").addEventListener("keyup", () => { oWidgets.onWrite2(); }, false);
document.getElementById("verb_pattern").addEventListener("keyup", () => { oFlexGen.update(); }, false);
document.getElementById("flexion").addEventListener("keyup", () => { oFlexGen.update(); }, false);
document.getElementById("tags").addEventListener("keyup", () => { oFlexGen.update(); }, false);
document.getElementById("add_to_lexicon").addEventListener("click", () => { oFlexGen.addToLexicon(); }, false);



/*
    ACTIONS
*/

const oWidgets = {

    showPage: function (sPage) {
        if (sPage == "lexicon") {
            this.hideElement("add_word_page");
            this.showElement("lexicon_page");
            document.getElementById("lexicon_button").style.backgroundColor = "hsl(210, 80%, 90%)";
            document.getElementById("add_word_button").style.backgroundColor = "hsl(210, 10%, 95%)";
        } else {
            this.hideElement("lexicon_page");
            this.showElement("add_word_page");
            document.getElementById("lexicon_button").style.backgroundColor = "hsl(210, 10%, 95%)";
            document.getElementById("add_word_button").style.backgroundColor = "hsl(210, 80%, 90%)";
            document.getElementById("lemma").focus();
        }
    },

    showElement: function (sElemId) {
        if (document.getElementById(sElemId)) {
            document.getElementById(sElemId).style.display = "block";
        } else {
            console.log("HTML node named <" + sElemId + "> not found.")
        }
    if (document.getElementById(sElemId)) {
        document.getElementById(sElemId).style.display = "block";
    } else {
        console.log("HTML node named <" + sElemId + "> not found.")
    }
    },

    hideElement: function (sElemId) {
        if (document.getElementById(sElemId)) {
            document.getElementById(sElemId).style.display = "none";
        } else {
            console.log("HTML node named <" + sElemId + "> not found.")
        }
    },

    hideAllSections: function () {
        for (let xElem of document.getElementById("sections").childNodes) {
            if (xElem.id) {
                xElem.style.display = "none";
            }
}

function hideElement (sElemId) {
    if (document.getElementById(sElemId)) {
        document.getElementById(sElemId).style.display = "none";
    } else {
        console.log("HTML node named <" + sElemId + "> not found.")
    }
}

function showPage (sPage) {
    if (sPage == "lexicon") {
        hideElement("add_word_page");
        showElement("lexicon_page");
        document.getElementById("lexicon_button").style.backgroundColor = "hsl(210, 80%, 90%)";
        document.getElementById("add_word_button").style.backgroundColor = "hsl(210, 10%, 95%)";
    } else {
        hideElement("lexicon_page");
        showElement("add_word_page");
        document.getElementById("lexicon_button").style.backgroundColor = "hsl(210, 10%, 95%)";
        document.getElementById("add_word_button").style.backgroundColor = "hsl(210, 80%, 90%)";
        document.getElementById("lemma").focus();
    }
}


document.getElementById("lexicon_button").addEventListener("click", () => { showPage("lexicon"); }, false);
document.getElementById("add_word_button").addEventListener("click", () => { showPage("lemma"); }, false);

document.getElementById("save_button").addEventListener("click", () => { oBinaryDict.build(); }, false);



class Table {

    constructor (sNodeId, lColumn, sProgressBarId, sResultId="") {
        this.sNodeId = sNodeId;
        this.xTable = document.getElementById(sNodeId);
        this.nColumn = lColumn.length;
        this.lColumn = lColumn;
        this.xProgressBar = document.getElementById(sProgressBarId);
        this.xNumEntry = document.getElementById(sResultId);
        this.iEntryIndex = 0;
        this.lEntry = [];
        this.nEntry = 0
        this._createHeader();
        this.listen();
    }

    _createHeader () {
        let xRowNode = createNode("tr");
        xRowNode.appendChild(createNode("th", { textContent: "·", width: "12px" }));
        //xRowNode.appendChild(createNode("th", { textContent: "#" }));
        for (let sColumn of this.lColumn) {
            xRowNode.appendChild(createNode("th", { textContent: sColumn }));
        }
        this.xTable.appendChild(xRowNode);
    }

    clear () {
        while (this.xTable.firstChild) {
            this.xTable.removeChild(this.xTable.firstChild);
        }
        this.iEntryIndex = 0;
        this._createHeader();
    }

    fill (lFlex) {
        this.clear();
        if (lFlex.length > 0) {
            this.xProgressBar.max = lFlex.length;
            this.xProgressBar.value = 1;
            for (let lData of lFlex) {
                this._addRow(lData);
                this.xProgressBar.value += 1;
            }
            this.xProgressBar.value = this.xProgressBar.max;
        }
        this.lEntry = lFlex;
        this.nEntry = lFlex.length;
        this.showEntryNumber();
    }

    addEntries (lFlex) {
        this.lEntry.push(...lFlex);
        for (let lData of lFlex) {
            this._addRow(lData);
        }
        this.nEntry += lFlex.length;
        this.showEntryNumber();
    }

    showEntryNumber () {
        if (this.xNumEntry) {
            this.xNumEntry.textContent = this.nEntry;
        }
    }

    _addRow (lData) {
        let xRowNode = createNode("tr", { id: this.sNodeId + "_row_" + this.iEntryIndex });
        xRowNode.appendChild(createNode("td", { textContent: "×", className: "delete_entry", title: "Effacer cette entrée" }, { id_entry: this.iEntryIndex }));
        //xRowNode.appendChild(createNode("td", { textContent: this.iEntryIndex }));
        for (let data of lData) {
            xRowNode.appendChild(createNode("td", { textContent: data }));
        }
        this.xTable.appendChild(xRowNode);
        this.iEntryIndex += 1;
    }

    listen () {
        this.xTable.addEventListener("click", (xEvent) => { this.onTableClick(xEvent); }, false);
    }

    onTableClick (xEvent) {
        try {
            let xElem = xEvent.target;
            if (xElem.className) {
                if (xElem.className == "delete_entry") {
                    this.deleteRow(xElem.dataset.id_entry);
                }
            }
        }
        catch (e) {
            showError(e);
        }
    }

    deleteRow (iEntry) {
        this.lEntry[parseInt(iEntry)] = null;
        if (document.getElementById(this.sNodeId + "_row_" + iEntry)) {
            document.getElementById(this.sNodeId + "_row_" + iEntry).style.display = "none";
        }
        this.nEntry -= 1;
        this.showEntryNumber();
    }

    getEntries () {
        return this.lEntry.filter((e) => e !== null);
    }
}


const oGenerator = {

    sLemma: "",

    cMainTag: "",

    lFlexion: [],

    listen: function () {
        document.getElementById("editor").addEventListener("click", (xEvent) => { this.onSelectionClick(xEvent); }, false);
        document.getElementById("lemma").addEventListener("keyup", () => { this.onWrite(); }, false);
        document.getElementById("lemma2").addEventListener("keyup", () => { this.onWrite2(); }, false);
        document.getElementById("verb_pattern").addEventListener("keyup", () => { this.update(); }, false);
        document.getElementById("flexion").addEventListener("keyup", () => { this.update(); }, false);
        document.getElementById("tags").addEventListener("keyup", () => { this.update(); }, false);
        document.getElementById("add_to_lexicon").addEventListener("click", () => { this.addToLexicon(); }, false);
    },

    lSection: ["nom", "verbe", "adverbe", "nom_propre", "autre"],

    hideAllSections: function () {
        for (let sSection of this.lSection) {
            hideElement("section_" + sSection);
            document.getElementById("select_" + sSection).style.backgroundColor = "";
        }
    },

    showSection: function (sName) {
        this.clear();
        this.hideAllSections();
        if (document.getElementById(sName).style.display == "none") {
        this.showElement(sName);
            showElement(sName);
        } else {
            hideElement(sName);
        }
    },

    clear: function () {
        try {
            document.getElementById("lemma2").value = "";
            this.hideElement("word_section2");
            hideElement("word_section2");
            // nom, adjectif, noms propres
            for (let xElem of document.getElementsByName("POS")) {
                xElem.checked = false;
            }
            for (let xElem of document.getElementsByName("POS2")) {
                xElem.checked = false;
            }
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
506

507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

585
586
587
588
589
590
591
592
593
594
595



596
597










598

599
600
601
602
603
604
605








606
607
608






609
610

611
612
613
614
615
616

617
618
619
620
621
622
623
624
625




626


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
506
507
508
509
510


511


512
513
514

515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531







+
-
-
+
+

-
+










-
-
+
+

-
-
+
-





-
-
+
+

-
+

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-




-
+
-
-
-
+
+








-
-
+
+


-
-
+
+


-
+







-
-
+
+


-
-
+
+


-
+





-
+


-
+












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




-
+




-
+






-
-
+
+

-
+

-
-
+
+

-
+









-
-
+
+

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

-
+
-
-
-
-
-
-






-
+




-
+






-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







-
-
-
-
+
+
+
+

-
+
-
-
-
-
-




-
+
+


-
-
+
+
-
-
-
+









-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
















-
+








-
-
-
+
+
+


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



-
+









+
+
+
+

+
+

    onSelectionClick: function (xEvent) {
        try {
            let xElem = xEvent.target;
            if (xElem.id) {
                if (xElem.id.startsWith("select_")) {
                    this.showSection("section_" + xElem.id.slice(7));
                    xElem.style.backgroundColor = "hsl(210, 50%, 90%)";
                    oFlexGen.setMainTag(xElem.dataset.tag);
                    oFlexGen.update();
                    this.cMainTag = xElem.dataset.tag;
                    this.update();
                } else if (xElem.id.startsWith("up_")) {
                    oFlexGen.update();
                    this.update();
                }
            }
        }
        catch (e) {
            showError(e);
        }
    },

    onWrite: function () {
        if (document.getElementById("lemma").value.trim() !== "") {
            this.showElement("editor");
            oFlexGen.update();
            showElement("editor");
            this.update();
        } else {
            this.showSection("section_vide");
            this.hideElement("editor");
            hideElement("editor");
            this.hideElement("actions");
        }
    },

    onWrite2: function () {
        if (document.getElementById("lemma2").value.trim() !== "") {
            this.showElement("word_section2");
            oFlexGen.update();
            showElement("word_section2");
            this.update();
        } else {
            this.hideElement("word_section2");
            hideElement("word_section2");
        }
    },

    createTableHeader: function () {
        let xRowNode = createNode("tr");
        xRowNode.appendChild(createNode("th", { textContent: "·" }));
        xRowNode.appendChild(createNode("th", { textContent: "#" }));
        xRowNode.appendChild(createNode("th", { textContent: "Forme fléchie" }));
        xRowNode.appendChild(createNode("th", { textContent: "Lemme" }));
        xRowNode.appendChild(createNode("th", { textContent: "Étiquettes" }));
        return xRowNode;
    },

    createRowNode: function (n, sFlexion, sLemma, sTags) {
        let xRowNode = createNode("tr", { id: "row_" + n });
        xRowNode.appendChild(createNode("td", { textContent: "×", className: "delete_entry", title: "Effacer cette entrée" }, { id_entry: n }));
        xRowNode.appendChild(createNode("td", { textContent: n }));
        xRowNode.appendChild(createNode("td", { textContent: sFlexion }));
        xRowNode.appendChild(createNode("td", { textContent: sLemma }));
        xRowNode.appendChild(createNode("td", { textContent: sTags }));
        return xRowNode;
    },

    displayTable: function (lFlex) {
        this.clearTable();
        if (lFlex.length > 0) {
            this.showElement("display_progress");
            let xDisplayProgress = document.getElementById("display_progress");
            let xTable = document.getElementById("table");
            let n = 0;
            xDisplayProgress.max = lFlex.length;
            xDisplayProgress.value = 1;
            this.hideElement("no_elem_line");
            xTable.appendChild(this.createTableHeader());
            for (let [sFlexion, sLemma, sTags] of lFlex) {
                xTable.appendChild(this.createRowNode(n, sFlexion, sLemma, sTags));
                n += 1;
                xDisplayProgress.value += 1;
            }
            xDisplayProgress.value = xDisplayProgress.max;
            this.hideElement("display_progress");
        } else {
            this.showElement("no_elem_line");
        }
        this.updateData();
    },

    clearTable: function () {
        let xTable = document.getElementById("table");
        while (xTable.firstChild) {
            xTable.removeChild(xTable.firstChild);
        }
    },

    onTableClick: function (xEvent) {
        try {
            let xElem = xEvent.target;
            if (xElem.className) {
                if (xElem.className == "delete_entry") {
                    this.deleteEntry(xElem.dataset.id_entry);
                }
            }
        }
        catch (e) {
            showError(e);
        }
    },

    addEntriesToTable: function (iStart, lFlex) {
        let xTable = document.getElementById("table");
        if (lFlex.length > 0) {
            if (document.getElementById("no_elem_line").style.display !== "none") {
                this.hideElement("no_elem_line");
                xTable.appendChild(this.createTableHeader());
            }
            for (let [sFlexion, sLemma, sTags] of lFlex) {
                xTable.appendChild(this.createRowNode(iStart, sFlexion, sLemma, sTags));
                iStart += 1;
            }
        }
        this.updateData();
    },

    deleteEntry: function (iEntry) {
        oLexicon.deleteEntry(iEntry);
        this.hideElement("row_"+iEntry);
        this.showElement("save_button");
        this.updateData();
    },

    updateData: function () {
        document.getElementById("num_added_entries").textContent = oLexicon.nAddedEntries;
        document.getElementById("num_deleted_entries").textContent = oLexicon.nDeletedEntries;
        document.getElementById("num_entries").textContent = oLexicon.nEntries;
    },

    setDictData: function (nEntries, sDate) {
        document.getElementById("num_entries_saved").textContent = nEntries;
        document.getElementById("save_date").textContent = sDate;
    }
}



const oFlexGen = {

    cMainTag: "",

    lFlexion: [],

    clear: function () {
        this.lFlexion = [];
        oWidgets.hideElement("actions");
    },

    setMainTag: function (sValue) {
        this.cMainTag = sValue;
    },

    addFlexion: function (sFlexion, sLemma, sTag) {
        this.lFlexion.push( [sFlexion, sLemma, sTag] );
    },

    update: function () {
        try {
            this.clear();
            this.lFlexion = [];
            let sGenderTag = "";
            let sLemma = document.getElementById("lemma").value.trim();
            if (sLemma.length > 0) {
            this.sLemma = document.getElementById("lemma").value.trim();
            if (this.sLemma.length > 0) {
                switch (this.cMainTag) {
                    case "N":
                        if (!this.getRadioValue("POS") || !this.getRadioValue("genre")) {
                            break;
                        }
                        let sTag = this.getRadioValue("POS") + this.getRadioValue("genre");
                        switch (this.getRadioValue("pluriel")) {
                            case "s":
                                this.addFlexion(sLemma, sLemma, sTag+":s/*");
                                this.addFlexion(sLemma+"s", sLemma, sTag+":p/*");
                                this.lFlexion.push([this.sLemma, sTag+":s/*"]);
                                this.lFlexion.push([this.sLemma+"s", sTag+":p/*"]);
                                break;
                            case "x":
                                this.addFlexion(sLemma, sLemma, sTag+":s/*");
                                this.addFlexion(sLemma+"x", sLemma, sTag+":p/*");
                                this.lFlexion.push([this.sLemma, sTag+":s/*"]);
                                this.lFlexion.push([this.sLemma+"x", sTag+":p/*"]);
                                break;
                            case "i":
                                this.addFlexion(sLemma, sLemma, sTag+":i/*");
                                this.lFlexion.push([this.sLemma, sTag+":i/*"]);
                                break;
                        }
                        let sLemma2 = document.getElementById("lemma2").value.trim();
                        if (sLemma2.length > 0  &&  this.getRadioValue("POS2")  &&  this.getRadioValue("genre2")) {
                            let sTag2 = this.getRadioValue("POS2") + this.getRadioValue("genre2");
                            switch (this.getRadioValue("pluriel2")) {
                                case "s":
                                    this.addFlexion(sLemma2, sLemma, sTag2+":s/*");
                                    this.addFlexion(sLemma2+"s", sLemma, sTag2+":p/*");
                                    this.lFlexion.push([sLemma2, sTag2+":s/*"]);
                                    this.lFlexion.push([sLemma2+"s", sTag2+":p/*"]);
                                    break;
                                case "x":
                                    this.addFlexion(sLemma2, sLemma, sTag2+":s/*");
                                    this.addFlexion(sLemma2+"x", sLemma, sTag2+":p/*");
                                    this.lFlexion.push([sLemma2, sTag2+":s/*"]);
                                    this.lFlexion.push([sLemma2+"x", sTag2+":p/*"]);
                                    break;
                                case "i":
                                    this.addFlexion(sLemma2, sLemma, sTag2+":i/*");
                                    this.lFlexion.push([sLemma2, sTag2+":i/*"]);
                                    break;
                            }
                        }
                        break;
                    case "V": {
                        if (!sLemma.endsWith("er") && !sLemma.endsWith("ir") && !sLemma.endsWith("re")) {
                        if (!this.sLemma.endsWith("er") && !this.sLemma.endsWith("ir") && !this.sLemma.endsWith("re")) {
                            break;
                        }
                        sLemma = sLemma.toLowerCase();
                        this.sLemma = this.sLemma.toLowerCase();
                        let cGroup = "";
                        let c_i = (document.getElementById("up_v_i").checked) ? "i" : "_";
                        let c_t = (document.getElementById("up_v_t").checked) ? "t" : "_";
                        let c_n = (document.getElementById("up_v_n").checked) ? "n" : "_";
                        let c_p = (document.getElementById("up_v_p").checked) ? "p" : "_";
                        let c_m = (document.getElementById("up_v_m").checked) ? "m" : "_";
                        let c_ae = (document.getElementById("up_v_ae").checked) ? "e" : "_";
                        let c_aa = (document.getElementById("up_v_aa").checked) ? "a" : "_";
                        let sVerbTag = c_i + c_t + c_n + c_p + c_m + c_ae + c_aa;
                        if (!sVerbTag.endsWith("__") && !sVerbTag.startsWith("____")) {
                            let sVerbPattern = document.getElementById("verb_pattern").value.trim();
                            if (sVerbPattern.length == 0) {
                                if (!sLemma.endsWith("er") && !sLemma.endsWith("ir")) {
                                    break;
                                }
                                // tables de conjugaison du 1er et du 2e groupe
                                // utilisation du générateur de conjugaison
                                let cGroup = (sLemma.endsWith("er")) ? "1" : "2";
                                for (let [nCut, sAdd, sFlexTags, sPattern] of this._getConjRules(sLemma)) {
                                    if (!sPattern || RegExp(sPattern).test(sLemma)) {
                                        this.addFlexion(sLemma.slice(0,-nCut)+sAdd, sLemma, ":V" + cGroup + "_" + sVerbTag + sFlexTags);
                                    }
                                }
                                // participes passés
                                let bPpasVar = (document.getElementById("up_partpas").checked) ? "var" : "invar";
                                let bVarPpas = document.getElementById("up_partpas").checked;
                                let lPpasRules = (sLemma.endsWith("er")) ? oConj["V1_ppas"][bPpasVar] : oConj["V2_ppas"][bPpasVar];
                                for (let [nCut, sAdd, sFlexTags, sPattern] of lPpasRules) {
                                for (let [sFlexion, sFlexTags] of conj_generator.conjugate(this.sLemma, sVerbTag, bVarPpas)) {
                                    if (!sPattern || RegExp(sPattern).test(sLemma)) {
                                        this.addFlexion(sLemma.slice(0,-nCut)+sAdd, sLemma, ":V" + cGroup + "_" + sVerbTag + sFlexTags);
                                    this.lFlexion.push([sFlexion, sFlexTags]);
                                    }
                                }
                            } else {
                                // copie du motif d’un autre verbe : utilisation du conjugueur
                                if (conj.isVerb(sVerbPattern)) {
                                    let oVerb = new Verb(sLemma, sVerbPattern);
                                    let oVerb = new Verb(this.sLemma, sVerbPattern);
                                    for (let [sTag1, dFlex] of oVerb.dConj.entries()) {
                                        if (sTag1 !== ":Q") {
                                            for (let [sTag2, sConj] of dFlex.entries()) {
                                                if (sTag2.startsWith(":") && sConj !== "") {
                                                    this.addFlexion(sConj, sLemma, ":V" + oVerb.cGroup + "_" + sVerbTag + sTag1 + sTag2);
                                                    this.lFlexion.push([sConj, ":V" + oVerb.cGroup + "_" + sVerbTag + sTag1 + sTag2]);
                                                }
                                            }
                                        } else {
                                            // participes passés
                                            if (dFlex.get(":Q3") !== "") {
                                                if (dFlex.get(":Q2") !== "") {
                                                    this.addFlexion(dFlex.get(":Q1"), sLemma, ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:m:s/*");
                                                    this.addFlexion(dFlex.get(":Q2"), sLemma, ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:m:p/*");
                                                    this.lFlexion.push([dFlex.get(":Q1"), ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:m:s/*"]);
                                                    this.lFlexion.push([dFlex.get(":Q2"), ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:m:p/*"]);
                                                } else {
                                                    this.addFlexion(dFlex.get(":Q1"), sLemma, ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:m:i/*");
                                                    this.lFlexion.push([dFlex.get(":Q1"), ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:m:i/*"]);
                                                }
                                                this.addFlexion(dFlex.get(":Q3"), sLemma, ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:f:s/*");
                                                this.addFlexion(dFlex.get(":Q4"), sLemma, ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:f:p/*");
                                                this.lFlexion.push([dFlex.get(":Q3"), ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:f:s/*"]);
                                                this.lFlexion.push([dFlex.get(":Q4"), ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:A:f:p/*"]);
                                            } else {
                                                this.addFlexion(dFlex.get(":Q1"), sLemma, ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:e:i/*");
                                                this.lFlexion.push([dFlex.get(":Q1"), ":V" + oVerb.cGroup + "_" + sVerbTag + ":Q:e:i/*"]);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                    case "W":
                        sLemma = sLemma.toLowerCase();
                        this.addFlexion(sLemma, sLemma, ":W/*");
                        this.sLemma = this.sLemma.toLowerCase();
                        this.lFlexion.push([this.sLemma, ":W/*"]);
                        break;
                    case "M1":
                        sLemma = sLemma.slice(0,1).toUpperCase() + sLemma.slice(1);
                        sGenderTag = this.getRadioValue("genre_m1");
                    case "M":
                        this.sLemma = this.sLemma.slice(0,1).toUpperCase() + this.sLemma.slice(1);
                        let sPOSTag = this.getRadioValue("pos_nom_propre")
                        if (sGenderTag) {
                            this.addFlexion(sLemma, sLemma, ":M1"+sGenderTag+":i/*");
                        }
                        break;
                    case "M2":
                        sLemma = sLemma.slice(0,1).toUpperCase() + sLemma.slice(1);
                        sGenderTag = this.getRadioValue("genre_m2");
                        let sGenderTag = this.getRadioValue("genre_m");
                        if (sGenderTag) {
                            this.addFlexion(sLemma, sLemma, ":M2"+sGenderTag+":i/*");
                            this.lFlexion.push([this.sLemma, sPOSTag+sGenderTag+":i/*"]);
                        }
                        break;
                    case "MP":
                        sGenderTag = this.getRadioValue("genre_mp");
                        if (sGenderTag) {
                            this.addFlexion(sLemma, sLemma, ":MP"+sGenderTag+":i/*");
                        }
                        break;
                    case "X":
                        let sFlexion = document.getElementById("flexion").value.trim();
                        let sTags = document.getElementById("tags").value.trim();
                        if (sFlexion.length > 0 && sTags.startsWith(":")) {
                            this.addFlexion(sFlexion, sLemma, sTags);
                            this.lFlexion.push([sFlexion, sTags]);
                        }
                        break;
                }
            }
            this.show();
            oGenWordsTable.fill(this.lFlexion);
        }
        catch (e) {
            showError(e);
        }
    },

    _getConjRules: function (sVerb) {
        if (sVerb.endsWith("ir")) {
            // deuxième groupe
            return oConj["V2"];
        } else if (sVerb.endsWith("er")) {
            // premier groupe, conjugaison en fonction de la terminaison du lemme
            // 5 lettres
            if (sVerb.slice(-5) in oConj["V1"]) {
                return oConj["V1"][sVerb.slice(-5)];
            }
            // 4 lettres
            if (sVerb.slice(-4) in oConj["V1"]) {
                if (sVerb.endsWith("eler") || sVerb.endsWith("eter")) {
                    return oConj["V1"][sVerb.slice(-4)]["1"];
                }
                return oConj["V1"][sVerb.slice(-4)];
            }
            // 3 lettres
            if (sVerb.slice(-3) in oConj["V1"]) {
                return oConj["V1"][sVerb.slice(-3)];
            }
            return oConj["V1"]["er"];
        } else {
            // troisième groupe
            return [ [0, "", ":Y/*", false] ];
        }
    },

    getRadioValue: function (sName) {
        if (document.querySelector('input[name="' + sName + '"]:checked')) {
            return document.querySelector('input[name="' + sName + '"]:checked').value;
        }
        return null;
    },

    show: function () {
        let sText = "";
        for (let [sFlexion, sLemma, sTag] of this.lFlexion) {
            sText += sFlexion + " (" + sLemma + ") " + sTag + "\n";
    createFlexLemmaTagArray: function () {
        let lEntry = [];
        for (let [sFlex, sTags] of oGenWordsTable.getEntries()) {
            lEntry.push([sFlex, this.sLemma, sTags]);
        }
        if (sText) {
        return lEntry;
            document.getElementById("results").textContent = sText;
            oWidgets.showElement("actions");
        } else {
            oWidgets.hideElement("actions");
        }
    },

    addToLexicon: function () {
        try {
            oLexicon.addFlexions(this.lFlexion);
            oLexiconTable.addEntries(this.createFlexLemmaTagArray());
            oGenWordsTable.clear();
            document.getElementById("lemma").value = "";
            document.getElementById("lemma").focus();
            oWidgets.showSection("section_vide");
            oWidgets.hideElement("editor");
            this.hideAllSections();
            hideElement("editor");
            oWidgets.hideElement("actions");
            oWidgets.clear();
            oWidgets.showElement("save_button");
            showElement("save_button");
            this.clear();
            this.cMainTag = "";
        }
        catch (e) {
            showError(e);
        }
    }
}



const oLexicon = {

    lFlexion: [],
    nEntries: 0,
    nDeletedEntries: 0,
    nAddedEntries: 0,

    set: function (lFlexion) {
        this.lFlexion = lFlexion;
        this.resetModif();
        oWidgets.displayTable(this.lFlexion);
        if (this.lFlexion.length > 0) {
            oWidgets.showElement("export_button");
        } else {
            oWidgets.hideElement("export_button");
        }
    },

    addFlexions: function (lNewFlex) {
        let iStart = this.lFlexion.length;
        for (let aFlex of lNewFlex) {
            this.lFlexion.push(aFlex);
        }
        this.nAddedEntries += lNewFlex.length;
        this.nEntries += lNewFlex.length;
        oWidgets.addEntriesToTable(iStart, lNewFlex);
    },

    deleteEntry: function (iEntry) {
        this.lFlexion[parseInt(iEntry)] = null;
        this.nDeletedEntries++;
        this.nEntries--;
    },

    resetModif: function () {
        this.nEntries = this.lFlexion.length;
        this.nAddedEntries = 0;
        this.nDeletedEntries = 0;
    },

    save: function () {
        oWidgets.hideElement("save_button");
        this.lFlexion = this.lFlexion.filter((e) => e !== null);
        oBinaryDict.build(this.lFlexion);
        this.resetModif();
        oWidgets.displayTable(this.lFlexion);
        oWidgets.updateData();
    }
}


const oBinaryDict = {
    
    oIBDAWG: null,

    load: function () {
        if (bChrome) {
            browser.storage.local.get("oPersonalDictionary", this._load);
            return;
        }
        let xPromise = browser.storage.local.get("oPersonalDictionary");
        xPromise.then(this._load.bind(this), showError);
    },

    _load: function (oResult) {
        if (!oResult.hasOwnProperty("oPersonalDictionary")) {
            oWidgets.hideElement("export_button");
            hideElement("export_button");
            return;
        }
        let oJSON = oResult.oPersonalDictionary;
        this.oIBDAWG = new IBDAWG(oJSON);
        let lEntry = [];
        for (let s of this.oIBDAWG.select()) {
            lEntry.push(s.split("\t"));
        }        
        oLexicon.set(lEntry);
        oWidgets.setDictData(this.oIBDAWG.nEntry, this.oIBDAWG.sDate);
        oWidgets.showElement("export_button");
        oLexiconTable.fill(lEntry);
        this.setDictData(this.oIBDAWG.nEntry, this.oIBDAWG.sDate);
        showElement("export_button");
    },

    setDictData: function (nEntries, sDate) {
        document.getElementById("dic_num_entries").textContent = nEntries;
        document.getElementById("dic_save_date").textContent = sDate;
    },

    listen: function () {
        document.getElementById("export_button").addEventListener("click", () => { this.export(); }, false);
        document.getElementById("import_button").addEventListener("click", () => { this.import(); }, false);
    },

    build: function (lEntry) {
    build: function () {
        oWidgets.showElement("build_progress");
        let xProgressNode = document.getElementById("build_progress");
        let oDAWG = new DAWG(lEntry, "S", "fr", "Français", "Dictionnaire personnel", xProgressNode);
        let oJSON = oDAWG.createBinaryJSON(1);
        this.save(oJSON);
        this.oIBDAWG = new IBDAWG(oJSON);
        oWidgets.setDictData(this.oIBDAWG.nEntry, this.oIBDAWG.sDate);
        let xProgressNode = document.getElementById("wait_progress");
        let lEntry = oLexiconTable.getEntries();
        if (lEntry.length > 0) {
            let oDAWG = new DAWG(lEntry, "S", "fr", "Français", "Dictionnaire personnel", xProgressNode);
            let oJSON = oDAWG.createBinaryJSON(1);
            browser.storage.local.set({ "oPersonalDictionary": oJSON });
            this.oIBDAWG = new IBDAWG(oJSON);
            this.setDictData(this.oIBDAWG.nEntry, this.oIBDAWG.sDate);
        oWidgets.hideElement("build_progress");
        oWidgets.showElement("export_button");
        browser.runtime.sendMessage({ sCommand: "setDictionary", dParam: {sType: "personal", oDict: oJSON}, dInfo: {} });
            browser.runtime.sendMessage({ sCommand: "setDictionary", dParam: {sType: "personal", oDict: oJSON}, dInfo: {} });
            showElement("export_button");
        } else {
            this.setDictData(0, "[néant]");
            browser.storage.local.set({ "oPersonalDictionary": "" });
            browser.runtime.sendMessage({ sCommand: "setDictionary", dParam: {sType: "personal", oDict: null}, dInfo: {} });
    },

        }
    save: function (oJSON) {
        browser.storage.local.set({ "oPersonalDictionary": oJSON });
    },

    import: function () {
        // TO DO
        console.log("import");
    },

    export: function () {
        let xBlob = new Blob([ JSON.stringify(this.oIBDAWG.getJSON()) ], {type: 'application/json'}); 
        let sURL = URL.createObjectURL(xBlob);
        browser.downloads.download({ filename: "fr.personal.json", url: sURL, saveAs: true });
    }
}


const oLexiconTable = new Table("lexicon_table", ["Flexions", "Lemmes", "Étiquettes"], "wait_progress", "num_entries");
const oGenWordsTable = new Table("generated_words_table", ["Flexions", "Étiquettes"], "wait_progress");

oBinaryDict.load();
oBinaryDict.listen();
oGenerator.listen();