-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
407 lines (390 loc) · 11.4 KB
/
index.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
const fs = require('fs');
const Dict = require('./dict');
// const iconv = require('iconv-lite');
// jschardet模块可以检测编码
const log = Math.log;
class Cut {
constructor() {
this._loaded = false
this.DEFAULT_DICT_ITEM = {
dict: __dirname + '/dict/jieba.dict.utf8',
hmmDict: __dirname + '/dict/hmm_model.utf8',
idfDict: __dirname + '/dict/idf.utf8',
userDict: __dirname + '/dict/user.txt',
stopWordDict: __dirname + '/dict/stop_words.utf8'
}
}
checkLoad() {
if (!this._loaded) {
this.load()
}
}
load(options) {
options = Object.assign(this.DEFAULT_DICT_ITEM, options)
this.dictPath = options.dict
this.hmmDictPath = options.hmmDict
this.userDictPath = options.userDict
this.idfDictPath = options.idfDict
this.stopWordDictPath = options.stopWordDict
this.dict = this._getDict();
this._initProbability();
this._loaded = true
}
_initProbability() {
const model = fs.readFileSync(this.hmmDictPath).toString();
const modelArr = model.split('\n')
const BEMS = ['B', 'E', 'M', 'S'] //用于遍历时得到BEMS
this.startProb = {} //初始状态概率
this.transProb = { //转移概率矩阵相应值
'B': {},
'E': {},
'M': {},
'S': {}
}
this.prevTrans = { //上一个状态
'B': ['E', 'S'],
'E': ['B', 'M'],
'M': ['B', 'M'],
'S': ['E', 'S']
}
this.emitProb = { //所有字符对应的发射概率
'B': {},
'E': {},
'M': {},
'S': {}
}
// 获取初始状态概率
const startIndex = modelArr.findIndex(item => {
return item === '#prob_start'
})
const startArr = modelArr[startIndex + 1].split(' ')
startArr.forEach((item, index) => {
this.startProb[BEMS[index]] = +item
})
// 获取转移概率矩阵的概率
const transIndex = modelArr.findIndex(item => {
return item === '#prob_trans 4x4 matrix'
})
new Array(4).fill(0).forEach((_, index) => {
const arr = modelArr[transIndex + index + 1].split(' ')
arr.forEach((item, ind) => {
this.transProb[BEMS[index]][BEMS[ind]] = +item
})
})
// 获取字符的发射状态概率
const emitProbStart = modelArr.findIndex(item => {
return item === '#prob_emit 4 lines';
})
const emitProbArr = modelArr.slice(emitProbStart + 1).filter(i => i.length > 10);
emitProbArr.forEach((item, index) => {
let wordAndNumArr = item.split(',');
wordAndNumArr.forEach(wordAndNum => {
let word = wordAndNum.split(':')[0];
let number = wordAndNum.split(':')[1];
this.emitProb[BEMS[index]][word] = +number
})
})
}
_getDict() {
const dictString = fs.readFileSync(this.dictPath).toString();
const dictlineArr = dictString.split('\n').filter(s => s);
const userDictString = fs.readFileSync(this.userDictPath).toString();
const userDictlineArr = userDictString.split('\n').filter(s => s);
const dict = new Dict();
dict.insertArr(dictlineArr)
dict.insertArr(userDictlineArr)
return dict;
}
cut(str, options = {
cutAll: false,
dag: false,
hmm: true,
cutForSearch: false
}) {
this.checkLoad()
const dag = this.getDag(str);
if (options.dag) {
return dag
}
// route记录从后往前的概率
const route = {
[str.length]: {
number: 0,
to: 0
}
}
if (options.cutAll) {
const result = []
// 用一个set让已成词的字不再以单字出现。
// 如:倒闭了 结果为:['倒闭', '了'] ;而不是:['倒', '倒闭', '闭','了']
const hasInWord = new Set();
for (let len = str.length, i = 0; i < len; i++) {
const arr = dag[i];
if (arr.length === 1) {
if (!hasInWord.has(i)) {
result.push(str.slice(i, i + 1))
hasInWord.add(i)
}
} else {
arr.forEach(x => {
if (x !== i) {
result.push(str.slice(i, x + 1))
hasInWord.add(x)
}
})
}
}
return result
} else {
for (let len = str.length, i = len - 1; i > -1; i--) {
const temp = []
const arr = dag[i];
for (let j = 0, len = arr.length; j < len; j++) {
let _i = arr[j];
let _w = str.slice(i, _i + 1);
const _node = this.dict.root[_w]
let num
if(typeof _node === 'object'){ //有这个词为object,有这个前缀为0,否则为undefined
num = _node.number
}
let number = (log(num) || 1) - log(this.dict.total) + route[_i + 1]['number'];
temp.push({
number,
to: _i
})
}
route[i] = temp.sort((a, b) => {
return b.number - a.number
})[0]
}
var arr = Object.keys(route);
const result = []
for (let i = 0, len = arr.length; i < len; i++) {
const arrIdx = arr[i];
const item = route[arrIdx];
if (item.number === 0) {
break;
}
const wordLen = item.to - i + 1;
const word = str.slice(i, i + wordLen);
result.push(word)
i += wordLen - 1;
}
let hmmResult = []
let hmmTemp = ''
for (let i = 0, len = result.length; i < len; i++) {
const str = result[i];
if (i === len - 1 || str.length !== 1) {
if (hmmTemp) {
hmmResult = hmmResult.concat(this.cutHMM(hmmTemp))
hmmTemp = ''
hmmResult.push(str);
} else {
hmmResult.push(str);
}
} else {
hmmTemp += str
}
}
if (options.cutForSearch) {
const searchResult = []
hmmResult.forEach(it => {
const len = it.length
if (len > 2) {
for (let i = 0; i < len - 1; i++) {
const _it = it.slice(i, i + 2)
const _temp = this.dict.root[_it]
if (typeof _temp === 'object') searchResult.push(_it);
}
}
if (len > 3) {
for (let i = 0; i < len - 1; i++) {
const _it = it.slice(i, i + 3)
const _temp = this.dict.root[_it]
if (typeof _temp === 'object') searchResult.push(_it);
}
}
searchResult.push(it);
})
return searchResult;
}
if (options.hmm) {
return hmmResult;
}
return result;
}
}
getDag(str) {
this.checkLoad()
const DAG = {}
for (let i = 0, len = str.length; i < len; i++) {
const w = str[i];
const wNode = this.dict.root[w]
if (!DAG[i]) {
DAG[i] = [i] //每个词都可以是single
}
if (typeof wNode !== 'object') {
// 遇到库中没有的特殊字符,或空格
continue;
}
for (let j = i + 1; j < len; j++) {
const _W = str.slice(i, j + 1);
const _wNode = this.dict.root[_W]
if (typeof _wNode === 'object') {
DAG[i].push(j)
} else if (_wNode === undefined) {
break;
}
}
}
return DAG;
}
cutHMM(str) {
this.checkLoad()
/* 目前正则匹配还不完整,很多没用到,可能有错 */
const re_userdict = /^(.+?)( [0-9]+)?( [a-z]+)?$/
const re_eng = /[a-zA-Z0-9]/
const re_han_default = /([\u4E00-\u9FD5a-zA-Z0-9+#&\._%]+)/
const re_skip_default = /(\r\n|\s)/
const re_han_cut_all = /([\u4E00-\u9FD5]+)/
const re_skip_cut_all = /[^a-zA-Z0-9+#\n]/
const han_reg = /([\u4E00-\u9FD5]+)/;
const seg_reg = /([a-zA-Z0-9]+(?:\.\d)?)|([0-9]+(?:\.\d)?%?)/;
const arr = str.split(re_han_default).filter(x => x);
let result = []
arr.forEach(it => {
it.split(re_han_cut_all).filter(x => x).forEach(i => {
if (i.match(han_reg)) {
result = result.concat(this._cutHMM(i))
} else {
const _arr = i.split(re_skip_default).filter(x => x)
_arr.forEach(i => {
if (i.match(re_han_default)) {
result = result.concat(i)
} else if (re_skip_default.test(i)) {
result = result.concat(i)
} else {
//
for (let xx of i) {
result = result.concat(xx)
}
}
})
}
})
})
return result
}
_cutHMM(str) {
if (str.length === 1) {
return [str]
}
let {
correctProb,
correctPath
} = this._viterbi(str)
const result = []
let temp = ''
correctPath.split('').forEach((type, idx) => {
switch (type) {
case 'B':
temp += str[idx]
break;
case 'M':
temp += str[idx]
break;
case 'E':
result.push(temp + str[idx])
temp = ''
break;
case 'S':
result.push(str[idx])
break;
}
})
return result
}
_viterbi(sentence) {
const V = [{}] //保存概率,一个对象一个字符
let path = {} //保存每种计算过概率的路径
Object.keys(this.startProb).forEach(type => {
const firstW = sentence[0]
const typeProb = this.startProb[type]
const wordProb = this.emitProb[type][firstW];
V[0][type] = typeProb + wordProb
path[type] = [type];
})
for (var i = 1; i < sentence.length; i++) {
V[i] = {}
var word = sentence[i];
let newpath = {}
Object.keys(this.transProb).forEach(curType => {
const prevTypeArr = this.prevTrans[curType]
var temp = { //计算哪个概率高和上个type
prob: -3.14e+100,
prevType: ''
};
prevTypeArr.forEach(prevType => {
const prevProb = V[i - 1][prevType]
const curProb = prevProb + this.transProb[prevType][curType] + this.emitProb[curType][word]
if (temp.prob === -3.14e+100 || curProb > temp.prob) {
temp = {
prob: curProb,
prevType: prevType
}
}
})
V[i][curType] = temp.prob
newpath[curType] = path[temp.prevType] + curType
})
path = newpath
}
const lastProb = V.pop() //最后一个字符计算出的不同type的概率,找出最合适的一个
let correctProb, correctType
lastProb.E > lastProb.S ? (correctProb = lastProb.E, correctType = 'E') : (correctProb = lastProb.S, correctType = 'S')
const correctPath = path[correctType]
return {
correctProb,
correctPath
}
}
cutAll(str) {
this.checkLoad()
return this.cut(str, {
cutAll: true
})
}
cutForSearch(str) {
this.checkLoad()
return this.cut(str, {
cutForSearch: true
})
}
tag(str) {
this.checkLoad()
const arr = this.cut(str)
const resultArr = []
arr.forEach(word => {
const _Node = this.dict.root[word] || {}
resultArr.push({
word,
tag: _Node.tag || 'x'
})
})
return resultArr;
}
insertWord(w, number = 1, tag = 'x') {
this.checkLoad();
this.dict.insertArr([w + ' ' + number + ' ' + tag])
}
}
// var one= process.memoryUsage()
// var start = new Date()
// var sentence = "我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。";
// const myjieba = new Cut()
// const res = myjieba.cut(sentence);
// console.log(res)
// var two = process.memoryUsage()
// console.log(two.heapUsed - one.heapUsed)
// console.log(new Date() - start)
module.exports = Cut