-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressor.js
80 lines (70 loc) · 1.71 KB
/
compressor.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
var fs = require('fs');
class Noeud {
constructor(mot) {
this.val = mot[0];
this.enfants = [];
if(mot.length > 1) {
this.ajoutNoeud(mot)
}
console.log("Ajout Noeud ("+ this.val +") "+mot)
}
ajoutNoeud(mot) {
if(mot.length > 1) {
mot = mot.slice(1)
let indice = this.noeudEnfantExiste(mot)
if(indice >= 0) {
this.enfants[indice].ajoutNoeud(mot)
return true
}
this.enfants[this.enfants.length] = new Noeud(mot)
}
return true
}
noeudEnfantExiste(mot) {
let c = mot[0]
let taille = this.enfants.length
for(let i=0; i<taille; i++) {
if(c == this.enfants[i].val) {
return i
}
}
return -1
}
decompte() {
let total = 0
let taille = this.enfants.length
for(let i=0; i<taille; i++) {
total += this.enfants[i].decompte()
}
total += taille
return total
}
}
function texteVersMots(texte) {
return texte.split(" ")
}
function comptageNoeuds(mots) {
let arbre = new Noeud(' ')
let taille = mots.length
for(let i=0; i<taille; i++) {
console.log("-> "+mots[i])
arbre.ajoutNoeud(" "+mots[i])
}
return arbre.decompte()
}
function script(texte) {
let liste = texteVersMots(texte)
let nbNoeuds = comptageNoeuds(liste)
console.log("### Bench JS Arbre ###")
console.log("Texte : "+ texte.length +" caractères, "+ liste.length +" mots, "+ nbNoeuds +" noeuds")
return nbNoeuds
}
function main () {
let debut = Date.now()
let repo = "/home/user/"
let texte = fs.readFileSync(repo+"dico_en.txt", 'utf8')
script(texte)
let fin = Date.now()
console.log("Temps : "+ new Intl.NumberFormat('fr-FR', { maximumFractionDigits: 2}).format(fin-debut) +"s")
}
main()