-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdfa.js
157 lines (130 loc) · 4.21 KB
/
dfa.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
const fs = require('fs');
// create sw hash
let map = {};
class SWF {
constructor () {}
/**
* create tree node
* @param {String,sensitive words} word
* @param {Number, if flag===1, done} flag
* @param {Object, childs} nodes
* @return {Object, TNode} node
*/
createTNode (word, flag, nodes) {
let node = {};
node.word = word || '';
flag < 0 ? node.flag = 0 : node.flag = 1;
node.nodes = nodes || {};
return node;
}
/**
* add sw in the tree
* @param {String, sensitive words} sentences
*/
addWord (sentences) {
if(!sentences) return;
const len = sentences.length;
sentences = sentences.toLowerCase();
// init rootNode
const rootNode = sentences.charAt(0);
// just one word
if(len===1){
map[rootNode] = this.createTNode(rootNode,1,{});
return ;
}
if(!map[rootNode])
map[rootNode] = this.createTNode(rootNode,0,{});
let _map = map[rootNode];
for(let i=1; i<len; i++) {
let _pre = sentences.charAt(i-1),
_cur = sentences.charAt(i),
_node = this.createTNode(_cur,i-len+1,{});
_map.nodes[_cur] = _node;
_map = _map.nodes[_cur];
}
}
builtSWL (filePath) {
// if users would't built their own libs
filePath === void 0 ? filePath = './sw.txt' : 1==1;
const dataBuf = fs.readFileSync(filePath);
// Buffer convert to Array , if linux, please use next line
// const datas = data.toString().split('\n');
const datas = dataBuf.toString().split('\r\n');
datas.forEach((val)=>{
this.addWord(val);
});
}
/**
* filter and replace it by "*"
* @param {String, your sentence} sentence
* if return -1, mean input error
*/
filter (sentence) {
if(!sentence) return -1;
const len = sentence.length;
const m = map;
// [a-zA-Z] convert to [a-z]
sentence = sentence.toLowerCase().split('');
for(let i=0; i<len; i++) {
let w = sentence[i];
if(m[w]) {
// state transition
let curState = m[w];
for(let j=i+1; j<=len; ) {
// next word
let nw = sentence[j];
//only one word
if (curState.nodes === {}){
sentence[i] = '*';
}
if(curState.nodes[nw]) {
// if it matches a sensitive word
if(curState.nodes[nw].flag === 1) {
// replace
for(i;i<=j;i++)
sentence[i] = '*';
// continue to judge until i equals len
i = j;
break;
}else{
curState = curState.nodes[nw];
j++;
}
}else {
break;
}
}
}
}
return sentence.join('');
}
}
// example
// let test = new SWF();
// test.builtSWL();
// let res = test.filter('fuck');
// console.log(res);
// test
/**
* there are 1065 words and 972 words can be reconized accuary: 91.27%
* filter one word use 1ms
* if a sentence contains 5654 words , it will use 3ms
* filter 1065 sentences use 6ms
*/
// let data = fs.readFileSync('./sw.txt').toString().split('\r\n'),
// str = '';
// for(let i=0, len=data.length; i<len; i++)
// str += data[i];
// let test = new SWF();
// test.builtSWL();
// // let counter = 0;
// let start = Date.now();
// // data.forEach((val)=>{
// // let res = test.filter(val);
// // if(res[0]!=='*')
// // counter++;
// // });
// // console.log('all: ',data.length,'| err: ', counter, '| acc: ',(data.length-counter)/data.length);
// let res = test.filter(str);
// let end = Date.now();
// console.log('it spend: ',end-start,'ms');