-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDictionary.js
More file actions
166 lines (151 loc) · 4.77 KB
/
Dictionary.js
File metadata and controls
166 lines (151 loc) · 4.77 KB
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
function rand(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
class Node {
constructor() {
this.child = new Array(26).fill(null);
this.exist = 0;
this.cnt = 0;
this.mean = '';
}
}
class Dictionary {
constructor() {
this.cur = 0;
this.root = new Node();
}
async init(file) {
try {
var response = await fetch(file);
var temp = await response.text();
var lines = temp.split('\n');
///đếm số từ xem đủ không
var cnt = 0;
lines.forEach(line => {
//line = line.trim(); này code cũ
//this.addWord(line);
//console.log(line);
/// code mới thay thế
cnt++;
const [vocab, definition] = line.split(':');
if (vocab && definition) {
this.addWord(vocab, definition);
}
//console.log(vocab + " " + definition);
});
//console.log(cnt);
} catch (error) {
console.error('error read file!!!', error);
}
}
addWord(s, mean = '') {
let p = this.root;
for (let i = 0; i < s.length; i++) {
const f = s.charAt(i);
const c = f.charCodeAt(0) - 'a'.charCodeAt(0);
if (!p.child[c]) p.child[c] = new Node();
p = p.child[c];
p.cnt++;
}
p.exist++;
p.mean = mean;
}
getMean(s) {
let p = this.root;
for (let i = 0; i < s.length; i++) {
const f = s.charAt(i);
const c = f.charCodeAt(0) - 'a'.charCodeAt(0);
if (!p.child[c]) return null;
p = p.child[c];
}
if (p.exist > 0) return p.mean;
return null;
}
deleteWordRecursive(p, s, i) {
if (i !== s.length) {
const c = s.charCodeAt(i) - 'a'.charCodeAt(0);
const isChildDeleted = this.deleteWordRecursive(p.child[c], s, i + 1);
if (isChildDeleted) p.child[c] = null;
}
else p.exist--;
if (p !== this.root) {
p.cnt--;
if (p.cnt === 0) {
return true;
}
}
return false;
}
deleteWord(s) {
if (!this.findWord(s)) return;
this.deleteWordRecursive(this.root, s, 0);
}
findWord(s) {
let p = this.root;
for (let i = 0; i < s.length; i++) {
const f = s.charAt(i);
const c = f.charCodeAt(0) - 'a'.charCodeAt(0);
if (!p.child[c]) return false;
p = p.child[c];
}
return p.exist !== 0;
}
size() {
let sum = 0;
for (let i = 0; i < 26; i++) {
if (this.root.child[i] !== null) sum += this.root.child[i].cnt;
}
return sum;
}
aWordStartWith(c) {
//console.log("--------------------------------");
var index = c.charCodeAt(0) - 'a'.charCodeAt(0);
let p = this.root.child[index];
let S = c;
while (p !== null && p.cnt > 0) {
index = 0;
var list = [...Array(25).keys()];
list.splice(list.indexOf(index), 1);
while (p.child[index] === null) {
if (list.length === 0) return S;
index = list[Math.floor(Math.random() * list.length)];
list.splice(list.indexOf(index), 1);
}
//console.log(list);
S += String.fromCharCode(index + 'a'.charCodeAt(0));
p = p.child[index];
}
return S;
}
maximumWord(c) {
var word = this.aWordStartWith(c);
for (var i = 0; i < 10; i++) {
var temp = this.aWordStartWith(c);
if (temp.length > word.length) word = temp;
}
return word;
}
suggestHelper(node, list, curr, maxx) {
if (node.exist > 0 && list.length < maxx) {
list.push(curr);
}
for (let i = 0; i < 26; i++) {
if (node.child[i] !== null) {
this.suggestHelper(node.child[i], list, curr + String.fromCharCode(i + 'a'.charCodeAt(0)), maxx);
}
}
}
suggest(word, maxx = 20) {
let node = this.root;
for (let i = 0; i < word.length; i++) {
const c = word.charCodeAt(i) - 'a'.charCodeAt(0);
if (!node.child[c]) {
return;
}
node = node.child[c];
}
let list = [];
this.suggestHelper(node, list, word, maxx);
return list;
}
}