-
Notifications
You must be signed in to change notification settings - Fork 3
/
dict.js
295 lines (256 loc) · 5.87 KB
/
dict.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
/**
* 生成一个节点
* @param char
* @returns {object} node
*/
function Node(char){
return {
c: char,
n: null,
f: 0,
o: ''
};
}
/**
* 查找指定字符在列表中的位置
* @param list
* @param char
* @returns int
*/
function indexOf(list, char){
var i = 0
, len = list.length;
for(; i < len && list[i].c !== char; i++);
return i < len ? i : -1;
}
/**
* export 整个`Dict`对象
*/
exports = module.exports = Dict;
/**
* Dict构造函数
* @param name 字典名称
* @param hash_len hash表的长度
* @returns
*/
function Dict(name, hash_len){
var self = this instanceof Dict
? this
: Object.create(Dict.prototype);
self.name = name || '';
self.hash_len = hash_len || 256;
self.init();
return self;
}
/**
* 初始化Dict
*/
Dict.prototype.init = function(){
this.dict = [];
// 初始化hash表
this.dict[0] = new Array(this.hash_len);
};
/**
* 插入单个pattern字符串
* @param pattern
*/
Dict.prototype.insert = function(pattern){
if(!pattern || 'string' !== typeof pattern){
return false;
}
var i = 0
, len = pattern.length
, hash_len = this.hash_len
, char
, node
, pre_node = null
, hash_index
, idx
, hash_table = this.dict[0]
, hash_list
, list;
for(; i < len; i++){
char = pattern[i].toLowerCase();
node = Node(char);
if(i == 0){
// 首个字符
// 计算hash值
hash_index = char.charCodeAt(0) % hash_len;
hash_list = hash_table[hash_index];
if(!hash_list){
// 需要初始化该链表
hash_list = [];
hash_table[hash_index] = hash_list;
}
if((idx = indexOf(hash_list, char)) === -1){
// 还没有存在于hash表
hash_list.push(node);
}else{
node = hash_list[idx];
}
}else{
// 新增
// 当前状态 index
var index = pre_node.n || this.dict.length;
list = this.dict[index];
if(!list){
// 初始化该链表
list = [];
this.dict[index] = list;
}
if((idx = indexOf(list, char)) === -1){
// 没有存在
list.push(node);
}else{
node = list[idx];
}
pre_node.n = index;
}
pre_node = node;
}
node.o = pattern;
i = len = char = node = pre_node = hash_index = idx = hash_table = hash_list = list = null;
};
/**
* 匹配模式
* @param str
* @returns {Array}
*/
Dict.prototype.match = function(str){
var dict = this.dict
, cur = 0
, i = 0
, char
, node
, pre_node
, len = str.length
, ret = [];
var charCode;
var excapeStr = ' ';
var excapeCounter = 0;
for(; i < len; i++){
char = str[i].toLowerCase();
charCode = char.charCodeAt(0);
if(charCode > 65248 && charCode < 65375){
// 增加对全角字符的匹配
char = String.fromCharCode(charCode - 65248);
}else if(charCode === 12288){
// 空格的全角编码为12288
char = ' ';
}
if(excapeStr.indexOf(char) > -1){
excapeCounter++;
continue;
}
if(pre_node){
node = this.__goto(pre_node.n, char) ||
this.__goto(pre_node.f, char);
}else{
node = this.__goto(cur, char);
}
if(!node && cur !== 0){
excapeCounter = 0;
node = this.__goto(cur = 0, char);
}
if(node && node.o){
var matchLen = node.o.length + excapeCounter;
var index = i - matchLen + 1;
ret.push([index, str.slice(index, index + matchLen)]);
excapeCounter = 0;
if(node.o.length != 1 && node.c !== pre_node.c){
// 继续匹配
i--;
}
}
pre_node = node;
cur = node.n || node.f;
}
dict = cur = i = char = node = pre_node = len = null;
return ret;
};
/**
* 构建各个节点的failure函数
*/
Dict.prototype.build = function(){
var self = this
, dict = this.dict
, cur = 0
, i = 0
, j
, idx
, char
, hash_table = dict[0]
, list
, next_list
, node
, pre_node = null
, len1 = hash_table.length
, len2;
for(; i < len1; i++){
if(!(list = hash_table[i]))
continue;
for(pre_node = null, j = 0, len2 = list.length; j < len2; j++){
node = list[j];
if(node.n){
_build_list(node, dict[node.n]);
}
}
}
self = dict = cur = i = j = idx = char = hash_table = list = next_list = node = pre_node = len1 = len2 = null;
function _build_list(pre_node, list){
if(!list || list.length < 1)
return false;
for(var node, i = 0, len = list.length; i < len; i++){
node = list[i];
node.f = self.__goto(pre_node && pre_node.f || 0, node.c).n || 0;
if(node.n){
_build_list(node, dict[node.n]);
}
}
}
};
/**
* 字典的goto函数,根据当前状态和输入字符
* 判断下一个状态
* 该函数会返回下一个状态具体的节点
* @param cur
* @param char
* @returns {Node}
*/
Dict.prototype.__goto = function(cur, char){
var dict = this.dict
, hash_index
, hash_len = this.hash_len
, idx
, node = Node(char)
, hash_list
, list;
// 当前状态为0,即是hash点
if(cur == 0){
// 首个字符
// 计算hash值
hash_index = char.charCodeAt(0) % hash_len;
hash_list = dict[0][hash_index];
if(!hash_list || (idx = indexOf(hash_list, char)) === -1){
// 还没有存在于hash表
return false;
}else{
node = hash_list[idx];
}
}else{
list = dict[cur];
if(!list || (idx = indexOf(list, char)) === -1){
// 没有存在
return false;
}else{
node = list[idx];
}
}
dict = hash_index = idx = hash_list = list = null;
return node;
};
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}