-
Notifications
You must be signed in to change notification settings - Fork 0
/
gentleman.js
379 lines (304 loc) · 11.6 KB
/
gentleman.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
var fs = require('fs');
//사전데이터들을 배열형태로 저장해서 보관합니다. (json)
var badWords = [];
var normalWords = [];
var softSearchWords = [];
//빠른 비속어단어 확인을 위해 사전에
//단어목록을 한글자씩 조각내놓고 사용합니다.
var parsedBadWords = [];
//유동적인 비속어 목록 관리를 위해 이미 배열에
//특정 단어가 존재하는지를 확인하기위해 해시맵을 사용합니다.
var badWordsMap = {};
var normalWordsMap = {};
var softSearchWordsMap = {};
var Utils = {
escape: (text) => {
return String(text).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
},
replaceAll: (message, search, replace) => {
return message.replace(new RegExp(search, 'gi'), replace);
},
wordToArray: word => {
let wordArray = [];
for (let i = 0; i <= word.length - 1; i++) {
wordArray[i] = word[i];
}
return wordArray;
},
lengthSplit: (message, limit) => {
if (message.length <= limit) return [message];
let fixedMessage = [];
let fullMessageLength = message.length;
let currentLength = 0;
let splitList = [];
while (true) {
if (currentLength == fullMessageLength) {
if (currentLength != 0 && splitList.length != 0) {
fixedMessage.push(splitList.join(''));
splitList = [];
}
break;
}
if (currentLength != 0 && currentLength % limit == 0 && splitList.length != 0) {
fixedMessage.push(splitList.join(''));
splitList = [];
}
splitList.push(message[currentLength]);
currentLength++;
}
return fixedMessage;
},
sortMap: (inputMap) => {
let sortedMap = {};
Object.keys(inputMap).sort().forEach((key) => {
sortedMap[key] = inputMap[key];
});
return sortedMap;
}
};
class Gentleman {
static load(inputBadwords, inputDictionary, inputSoftSearchWords, disableAutoParse) {
badWords = inputBadwords;
normalWords = inputDictionary;
softSearchWords = inputSoftSearchWords;
if (disableAutoParse != false) {
Gentleman.parse(badWords);
Gentleman.mapping();
}
}
static loadFile(badWordsPath, normalWordsPath, softSearchWordsPath) {
let data = {
badWords: require(badWordsPath).badwords,
normalWords: require(normalWordsPath).dictionary,
softSearchWords: require(softSearchWordsPath).badwords
};
Gentleman.load(data.badWords, data.normalWords, data.softSearchWords);
}
static defaultLoad() {
let data = Gentleman.getDefaultData();
Gentleman.load(data.badWords, data.normalWords, data.softSearchWords);
}
static parse() {
parsedBadWords = [];
for (let index in badWords)
parsedBadWords.push(Utils.wordToArray(badWords[index]));
}
static mapping() {
badWordsMap = {};
normalWordsMap = {};
softSearchWordsMap = {};
for (let index in badWords)
badWordsMap[badWords[index]] = true;
for (let index in normalWords)
normalWordsMap[normalWords[index]] = true;
for (let index in softSearchWords)
softSearchWordsMap[softSearchWords[index]] = true;
}
static sortBadWordsMap() {
badWordsMap = Utils.sortMap(badWordsMap);
badWords = [];
for (var index in badWordsMap) badWords.push(index);
}
static sortNormalWordsMap() {
normalWordsMap = Utils.sortMap(normalWordsMap);
normalWords = [];
for (var index in normalWordsMap) normalWords.push(index);
}
static sortSoftSearchWordsMap() {
softSearchWordsMap = Utils.sortMap(softSearchWordsMap);
softSearchWords = [];
for (var index in softSearchWordsMap) softSearchWords.push(index);
}
static sortAll() {
Gentleman.sortBadWordsMap();
Gentleman.sortNormalWordsMap();
Gentleman.sortSoftSearchWordsMap();
}
static getDefaultData() {
return {
badWords: require('./dictionary/bad-words.json').badwords,
normalWords: require('./dictionary/normal-words.json').dictionary,
softSearchWords: require('./dictionary/soft-search-words.json').badwords
};
}
static getLoadedData() {
return {
badWords: badwords,
normalWords: normalWords,
softSearchWords: softSearchWords
}
}
static saveAllData(badWordsPath, normalWordsPath, softSearchWordsPath, isAsync) {
Gentleman.saveBadWordsData(badWordsPath, isAsync);
Gentleman.saveNormalWordsData(normalWordsPath, isAsync);
Gentleman.saveSoftSearchWordsData(softSearchWordsPath, isAsync);
}
static saveBadWordsData(path, isAsync) {
Gentleman.sortBadWordsMap();
let data = JSON.stringify({
badwords: badWords
}, null, 4);
(isAsync === true) ? fs.writeFile(path, data) : fs.writeFileSync(path, data);
}
static saveNormalWordsData(path, isAsync) {
Gentleman.sortNormalWordsMap();
let data = JSON.stringify({
dictionary: normalWords
}, null, 4);
(isAsync === true) ? fs.writeFile(path, data) : fs.writeFileSync(path, data);
}
static saveSoftSearchWordsData(path, isAsync) {
Gentleman.sortSoftSearchWordsMap();
let data = JSON.stringify({
badwords: softSearchWords
}, null, 4);
(isAsync === true) ? fs.writeFile(path, data) : fs.writeFileSync(path, data);
}
static isBad(message) {
return Gentleman.find(message, false).length != 0;
}
static find(message, needMultipleCheck, splitCheck) {
var totalResult = [];
if (splitCheck === undefined) splitCheck = 15;
var messages = (splitCheck != 0) ? Utils.lengthSplit(message, splitCheck) : [message];
for (var index1 = 0; index1 <= messages.length - 1; index1++) {
let currentResult = Gentleman.nativeFind(messages[index1], needMultipleCheck);
if (needMultipleCheck) {
for (var index2 = 0; index2 <= currentResult.length - 1; index2++)
if (currentResult !== null)
totalResult.push(currentResult[index2]);
} else {
if (currentResult !== null)
totalResult.push(currentResult);
}
}
return totalResult;
}
static nativeFind(message, needMultipleCheck) {
let unsafeMessage = message.toLowerCase();
let foundedBadWords = [];
for (let index in normalWords) {
if (unsafeMessage.length == 0) break;
unsafeMessage = Utils.replaceAll(unsafeMessage, normalWords[index], '');
}
//OTHER LANGUAGE BAD WORDS FIND ALGORITHM
for (var otherLangBadWordsIndex in softSearchWords) {
let otherLangBadWord = softSearchWords[otherLangBadWordsIndex];
if (unsafeMessage.search(otherLangBadWord) != -1) {
foundedBadWords.push(otherLangBadWord);
if (!needMultipleCheck) return foundedBadWords;
}
}
//KR BAD WORDS FIND ALGORITHM
for (let index1 in parsedBadWords) {
let badWord = parsedBadWords[index1];
let wordLength = badWord.length;
let findCount = {};
for (let index2 in badWord) {
let badOneCharacter = String(badWord[index2]).toLowerCase();
for (let index3 in unsafeMessage) {
let unsafeOneCharacter = String(unsafeMessage[index3]).toLowerCase();
if (badOneCharacter == unsafeOneCharacter) {
findCount[badOneCharacter] = true;
break;
}
}
if (wordLength == Object.keys(findCount).length) {
if (needMultipleCheck != true) return badWord.join('');
foundedBadWords.push(badWord.join(''));
}
}
}
if (needMultipleCheck != true) return null;
return foundedBadWords;
}
static fix(message, replaceCharacter) {
let fixedMessage = message;
let foundedBadWords = Gentleman.find(message, true);
replaceCharacter = (replaceCharacter === undefined) ? '*' : replaceCharacter;
for (let index1 in foundedBadWords) {
let foundedBadWord = Utils.wordToArray(foundedBadWords[index1]);
for (let index2 in foundedBadWord) {
let foundedBadOneCharacter = foundedBadWord[index2];
fixedMessage = Utils.replaceAll(fixedMessage, foundedBadOneCharacter, replaceCharacter);
}
}
return fixedMessage;
}
static isExistNormalWord(word) {
return (typeof(normalWordsMap[word]) != 'undefined');
}
static addNormalWords(words) {
for (let wordsIndex in words) {
let word = words[wordsIndex];
if (word.length == 0) continue;
if (Gentleman.isExistNormalWord(word)) continue;
normalWordsMap[word] = true;
normalWords.push(word);
}
}
static deleteNormalWords(words) {
for (let wordsIndex in words) {
let word = words[wordsIndex];
if (!Gentleman.isExistNormalWord(word)) continue;
delete(normalWordsMap[word]);
for (let mapIndex = normalWords.length - 1; mapIndex >= 0; mapIndex--) {
if (normalWords[mapIndex] === word) {
normalWords.splice(mapIndex, 1);
break;
}
}
}
}
static isExistBadWord(word) {
return (typeof(badWordsMap[word]) != 'undefined');
}
static addBadWords(words) {
for (let wordsIndex in words) {
let word = words[wordsIndex];
if (word.length == 0) continue;
if (Gentleman.isExistBadWord(word)) continue;
badWordsMap[word] = true;
badWords.push(word);
}
}
static deleteBadWords(words) {
for (let wordsIndex in words) {
let word = words[wordsIndex];
if (!Gentleman.isExistBadWord(word)) continue;
delete(badWordsMap[word]);
for (let mapIndex = badWords.length - 1; mapIndex >= 0; mapIndex--) {
if (badWords[mapIndex] === word) {
badWords.splice(mapIndex, 1);
break;
}
}
}
}
static isExistSoftSearchWord(word) {
return (typeof(softSearchWordsMap[word]) != 'undefined');
}
static addSoftSearchWords(words) {
for (let wordsIndex in words) {
let word = words[wordsIndex];
if (word.length == 0) continue;
if (Gentleman.isExistSoftSearchWord(word)) continue;
softSearchWordsMap[word] = true;
softSearchWords.push(word);
}
}
static deleteSoftSearchWords(words) {
for (let wordsIndex in words) {
let word = words[wordsIndex];
if (!Gentleman.isExistSoftSearchWord(word)) continue;
delete(softSearchWordsMap[word]);
for (let mapIndex = softSearchWords.length - 1; mapIndex >= 0; mapIndex--) {
if (softSearchWords[mapIndex] === word) {
softSearchWords.splice(mapIndex, 1);
break;
}
}
}
}
}
module.exports = Gentleman;