-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
650 lines (529 loc) · 17.8 KB
/
main.js
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
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
const path = require('path')
const { app, ipcMain, ipcRenderer } = require('electron')
const DataJson = require('./class/DataJson')
const Window = require('./class/Window')
const DataStore = require('./class/DataStore')
const fs = require('fs')
// Permet de sauvegarde notre json le bon dossier
app.setPath("userData", __dirname + "/config")
// DataStore stocke le texte à annoter dans un fichier JSON
const textData = new DataStore({ name: 'TextMain' })
// DataStructure contient l'annotation
const DataStructure = new DataJson({ name: 'DataStruct' })
const config = require('./config/DataStruct.json')
const { text } = require('d3')
/*
ipcMain.on(msg,value) ou .once(msg,value) sont les fonctions qui
exécutent un programme lors de la réception du message
msg et d'une value.
.send(msg,value) envoie du message msg et de la value aux
sous processus depuis le main processus.
Les console.log sont commentés pour éviter de surcharger le terminal mais si
besoin les décommenter.
*/
function main() {
// Création de la fenêtre principale
let mainWindow = new Window({
file: path.join(__dirname, 'index.html'),
backgroundcolor: "#818181"
})
mainWindow.once('ready-to-show', () => {
mainWindow.show();
})
mainWindow.once('show', () => {
mainWindow.webContents.send('inputstoPrint', textData.inputs);
})
// Fenêtre secondaire qui va nous permettre d'écrire le texte à annoter
let addWin
ipcMain.on('add-window', () => {
// Si la fenêtre n'existe pas
if (!addWin) {
// création de la nouvelle fenêtre
addWin = new Window({
file: path.join('src', 'ann_type.html'),
width: 450,
height: 400,
parent: mainWindow
})
addWin.once('ready-to-show', () => {
addWin.show()
})
ipcMain.once('closeAddWInn', () => {
addWin.close()
addWin = null
})
}
})
// Fenêtre secondaire qui va nous permettre d'ajouter l'annotation
let annWin
ipcMain.on('add-ann-window', () => {
if (!addWin) {
annWin = new Window({
file: path.join('src', 'annotation.html'),
width: 450,
height: 180,
parent: mainWindow
})
annWin.once('ready-to-show', () => {
annWin.show();
})
ipcMain.once('closeannWin', () => {
annWin.close()
annWin = null
})
}
})
/* Fenêtre annotation spécifique */
let annSpecWin
ipcMain.on('add-ann-specifique-window', () => {
if (!addWin && !annWin) {
annSpecWin = new Window({
file: path.join('src', 'annotation_spec.html'),
width: 450,
height: 180,
parent: mainWindow
})
annSpecWin.once('ready-to-show', () => {
annSpecWin.show();
})
ipcMain.once('closeAnnSpecWin', () => {
annSpecWin.close()
annSpecWin = null
})
}
})
// Mise à jour de l'affichage du texte
ipcMain.on('maj', (event) => {
mainWindow.send('inputstoPrint', textData.inputs);
})
// Fenêtre secondaire qui va nous permettre de choisir la key pour Json
let jsonWin
ipcMain.on('add-json-window', () => {
if (!jsonWin) {
jsonWin = new Window({
file: path.join('src', 'page_json.html'),
width: 450,
height: 180,
parent: mainWindow
})
jsonWin.once('ready-to-show', () => {
jsonWin.show();
})
ipcMain.once('closejsonWin', () => {
jsonWin.close()
jsonWin = null
})
}
})
// Fenêtre secondaire qui va nous permettre de choisir la key et
// separator pour fichier csv
let csvWin
ipcMain.on('add-csv-window', () => {
if (!csvWin) {
csvWin = new Window({
file: path.join('src', 'page_csv.html'),
width: 450,
height: 180,
parent: mainWindow
})
csvWin.once('ready-to-show', () => {
csvWin.show();
})
csvWin.on('closed', () => {
csvWin = null;
})
ipcMain.on('closecsvWin', () => {
csvWin.close()
csvWin = null
})
}
})
ipcMain.on('add-json-key', (event, key) => {
mainWindow.send('key-json', key);
})
ipcMain.on('add-csv-key', (event, key) => {
//console.log("key: " + key);
mainWindow.send('key-csv', key);
})
// Message add-text de la fenêtre ann_type_win
// Lorsque le main process reçoit 'add-text' il ajoute txt dans le fichier JSON textData
// puis envoie ce fichier à un renderer process (cf ann_menu.js)
ipcMain.on('add-text', (event, txt) => {
const updatedText = textData.addinputText(txt).inputs;
/*
console.log(updatedText)
console.log(DataStructure.addText(txt).text)
*/
console.log(mainWindow.send('inputstoPrint', updatedText));
})
// add-txt ajoute le texte venant du fichier
ipcMain.on('add-txt', (event, data) => {
//console.log(data)
mainWindow.send('inputstoPrint', textData.addinputText(data).inputs);
DataStructure.addText(data).text;
})
// Message clear-txt efface le texte affiché dans la fenêtre d'annotation
// Supprime le contenu de textData
ipcMain.on('clear-txt', (event) => {
textData.deleteText();
DataStructure.clear();
// Supprime le contenu de DataStorage.json
fs.readFile('./config/DataStorage.json', 'utf8', (err, jsonString) => {
if (err) {
console.log("DataStruct.json (reading fail) : ", err);
} else {
fs.writeFile('./config/DataStorage.json', '[]', 'utf8', function (err) {
if (err) {
console.log(err);
}
})
}
})
mainWindow.send('inputstoPrint', textData.inputs);
})
/* ANNOTATION DE TOUT LE TEXTE */
ipcMain.on('annotate-object', (event, elt) => {
/*
console.log('click');
console.log(elt);
*/
ipcMain.once('add-annotation', (event, annotation) => {
DataStructure.addType(annotation).type;
var length_input = textData.getinputs().length;
var num = 0;
if (length_input == 1) {
console.log(mainWindow.send('annAddList', 'Annotation du texte', annotation, 0));
} else {
for (var i = 0; i < length_input; i++) {
if (elt.localeCompare(textData.getinputs()[i]) == 0) {
console.log(mainWindow.send('annAddList', 'Annotation Objet' + (i + 1).toString(), annotation, i));
}
}
}
// Ajouter l'objet JSON dans un fichier sauvegarde dans config
fs.readFile('./config/DataStruct.json', 'utf8', (err, jsonString) => {
if (err) {
console.log("DataStruct.json (reading fail) : ", err);
} else {
/* textmain contient tout le texte à annoter */
const textMain = elt;
/* jsonString contient l'annotation */
const dataStruct = JSON.parse(jsonString);
/* Fonctionnera avec un seul objet pour le moment */
var jsonString2 = { 'text': textMain, "type": dataStruct['type'] }
/*
console.log('Objet json qui va être ajouté à DataStorage.json')
console.log(jsonString2)
*/
openJsonAdd('./config/DataStorage.json', jsonString2);
}
})
})
})
/* Fonction qui ouvre DataStorage pour ajouter les annotations */
function openJsonAdd(filename, jsonString2) {
// Ouvre DataStorage.json qui va contenir toutes les annotations
fs.open(filename, 'r+', function (err, fd) {
if (err) {
// Si n'existe pas, il est crée avec le contenu '[]'
fs.writeFile(filename, '[]', 'utf8', function (err) {
if (err) {
console.log(err);
} else {
//console.log("DataStorage.json successfully created")
addObjectJson(filename, jsonString2);
}
});
} else {
// Il faudra laisser la possibilité de recharger le travail précédent
// console.log("DataStorage.json already exists")
addObjectJson(filename, jsonString2);
}
});
}
/* Fonction qui ajoute les objets JSON concernant l'annotation de tout le texte dans DataStorage.json */
function addObjectJson(filename, jsonString2) {
// Ouvre filename en écriture + lecture
fs.readFile(filename, 'utf8', function (err, data) {
if (err) {
console.log(err);
} else {
/*
console.log('data addObjectJson');
console.log(data);
console.log(JSON.parse(data));
*/
const file = JSON.parse(data);
file.push(jsonString2);
const json = JSON.stringify(file);
/*
console.log('json addObjectJson');
console.log(json);
*/
fs.writeFile(filename, json, 'utf8', function (err) {
if (err) {
console.log(err);
}
/*
else {
console.log('Data written to file DataStorage.json');
}*/
});
}
});
};
/* ANNOTATION SPECIFIQUE */
ipcMain.on('text-selection', (event, txt, range, objectText) => {
/*
console.log("Texte sélectionné");
console.log(txt);
console.log(range);
console.log(objectText);
*/
/* Une fois qu'on a reçu l'annotation de annotation_spec.js */
ipcMain.once('text-selection-annotation', (event, annotation, annotateAll) => {
/*
console.log('icpmain in ipcmain');
console.log(txt);
console.log(annotation);
console.log(annotateAll);
*/
console.log(mainWindow.send('annAddList', txt, annotation, 0));
DataStructure.addType(annotation).type;
/*
On récupére le rang de l'objet dans le fichier d'entrée
Pour recalculer le range qui compte le nombre de caractère depuis le début
du premier objet du fichier d'entrée et non depuis le début de l'objet
concerné
*/
var length_input = textData.getinputs().length;
var start = 0;
for (var i = 0; i < length_input; i++) {
if (txt.localeCompare(textData.getinputs()[i]) == 0 && i == 0) {
break;
} else {
/*
console.log("Objet");
console.log(i);
*/
if (objectText.localeCompare(textData.getinputs()[i]) == 0) {
break;
}
start = start + textData.getinputs()[i].length;
}
};
range = [range[0] - start, range[1] - start];
//DataStructure.addType(annotation).type;
var jsonString2 = { 'text': objectText, 'type': annotation };
/*
console.log('jsonString2');
console.log(jsonString2);
*/
openJsonAddAnnSpec('./config/DataStorage.json', jsonString2, annotateAll, range, txt);
})
})
/* Écriture du json avec annotation spécifique */
function openJsonAddAnnSpec(filename, jsonString2, annotateAll, range, txt) {
// Ouvre DataStorage.json qui va contenir toutes les annotations
fs.open(filename, 'r+', function (err, fd) {
if (err) {
// Si n'existe pas, il est crée avec le contenu '[]'
fs.writeFile(filename, '[]', 'utf8', function (err) {
if (err) {
console.log(err);
} else {
//console.log("DataStorage.json successfully created")
addObjectJsonAnnSpec(filename, jsonString2, annotateAll, range, txt);
}
});
} else {
// Il faudra laisser la possibilité de recharger le travail précédent
//console.log("DataStorage.json already exists")
addObjectJsonAnnSpec(filename, jsonString2, annotateAll, range, txt);
}
});
}
/* Ajoute les objets JSON concernant les annotations spécifiques dans DataStorage.json */
function addObjectJsonAnnSpec(filename, jsonAnnSpec, annotateAll, range, txt) {
if (annotateAll) {
list_positions = recherche(txt, jsonAnnSpec['type'], jsonAnnSpec['text']);
} else {
list_positions = [[range[0], range[1], jsonAnnSpec['type']]];
}
/*
console.log("list_positions");
console.log(list_positions);
*/
// Ouvre filename en écriture + lecture
fs.readFile(filename, 'utf8', function (err, data) {
if (err) {
console.log(err);
} else {
/* On récupère le contenu de filename */
/*
console.log('data addObjectJsonAnnSpec');
console.log(data);
*/
const file = JSON.parse(data);
/* On ajoute au contenu de filename l'objet JSON qui concerne l'annotation spécifique */
var objet = { "text": jsonAnnSpec['text'], "type": "", "entities": list_positions };
//console.log(objet);
file.push(objet);
/* Écriture dans DataStorage.json */
const json = JSON.stringify(file);
/*
console.log('json addObjectJsonAnnSpec');
console.log(json);
*/
fs.writeFile(filename, json, 'utf8', function (err) {
if (err) {
console.log(err);
}
/*
else {
console.log('Data written to file');
}
*/
});
}
});
};
/* Fonction qui cherche les occurrences du mot motachercher dans le texte concerné */
function recherche(motachercher, categorie, objectText) {
var textentier = objectText;
var taille = motachercher.length;
const txt = textentier.split(' ');
const mot = motachercher.split(' ');
const nbtext = txt.length;
const nbmot = mot.length;
var a = 0;
var posi = 0;
var matrice = [];
for (let i = 0; i < nbtext; i++) {
var début = 0;
var end = 0;
var add = 0;
var n = 0;
var j = 0;
while (j < nbmot) {
var virg = mot[j].toLowerCase().concat(',');
var point = mot[j].toLowerCase().concat('.');
if (i + j < nbtext) {
if (mot[j].toLowerCase() == txt[i + j].toLowerCase()) {
n++;
j++;
}
else if (virg == txt[i + j].toLowerCase() || point == txt[i + j].toLowerCase()) {
n++;
j++;
add++;
}
else {
j = nbtext;
}
}
else {
j = nbtext;
}
}
if (n == nbmot) {
a++;
début = posi;
end = posi + taille + add;
var tab = [début, end, categorie];
matrice.push(tab);
}
posi = posi + txt[i].length + 1;
}
return matrice
}
/*
Cette partie sert à récupérer le fichier DataStructure.json dans le
dossier annotation-devtool. Il contient toutes les annotations que l'utilisateur a faite
*/
ipcMain.on('json', (event) => {
/* Ouverture du fichier de sauvegarde situé dans config */
fs.readFile('./config/DataStorage.json', 'utf8', (err, jsonString) => {
if (err) {
console.log("File read failed:", err);
}
else {
/* Ouvrir DataStruct.json pour récupérer le texte qu'on stocke dans textToAnn */
fs.readFile('./config/DataStruct.json', 'utf8', (err, textToAnn) => {
if (err) {
console.log("DataStruct failed to read", err);
}
else {
//console.log('tentative de téléchargement');
/* Parsing de DataStorage.json fichier de sauvegarde de toutes les annotations et le texte leur étant associé */
const contentDataStorage = JSON.parse(jsonString);
//console.log(contentDataStorage);
/* On récupère le texte à annoter qui est dans TextMain.json */
//console.log("textToAnn");
var textToAnn = textData.getinputs();
//console.log(textToAnn);
var jsonToDownload = [];
/* Recherche de toutes les annotations liées au texte */
for (var l = 0; l < textToAnn.length; l++) {
var entities = [];
var type = [];
for (var i = 0; i < contentDataStorage.length; i++) {
var found = 0;
if (textToAnn[l].localeCompare(contentDataStorage[i]['text']) == 0) {
/* Si Annotation spécifique, on sait que type est vide */
if (contentDataStorage[i]['type'] == '') {
entities = entities.concat(contentDataStorage[i]['entities']);
}
/* Sinon */
else {
/* Vérification pour éviter les doublons dans type */
if (type.length == 0) {
type.push(contentDataStorage[i]['type']);
}
else {
for (var j = 0; j < type.length; j++) {
if (type[j].localeCompare(contentDataStorage[i]['type']) == 0) {
found = 1;
}
}
if (found == 0) {
type.push(contentDataStorage[i]['type']);
}
}
}
};
};
var objet = { "text": textToAnn[l], "entities": entities, "type": type };
jsonToDownload.push(objet);
};
/* Écriture de DataStructure.json qui est le fichier téléchargé par l'utilisateur */
const json = JSON.stringify(jsonToDownload);
fs.open('DataStructure.json','r+', function (err, fd) {
if (err) {
fs.writeFile('DataStructure.json', json, (err) => {
if (err) {
alert("An error ocurred creating the file " + err.message);
}
});
} else {
fs.writeFile('DataStructure.json', json, (err) => {
if (err) {
alert("An error ocurred creating the file " + err.message);
}
});
}
})
}
})
}
})
});
mainWindow.on('uncaughtException', function (error) {
// Handle the error
console.log(error);
})
}
app.on('ready', main)
app.on('window-all-closed', function () {
app.quit();
})