-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofanity.js
54 lines (51 loc) · 1.64 KB
/
profanity.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
const mystem = require('./mystem');
const badWords = require('./badwords');
const chars = '@#$%*';
function arraySample(array) {
const index = Math.round(Math.random() * array.length);
return array[index >= array.length ? array.length - 1 : index];
}
function beep(text) {
const first = text[0];
const last = text[text.length-1];
const beepLen = text.length-2;
let result = first;
let lastChar = '';
for (let i = 0; i < beepLen; i++) {
let char = arraySample(chars);
while (char === lastChar) char = arraySample(chars);
result += char;
lastChar = char;
}
result += last;
return result;
}
module.exports = async function(input) {
let isDirty = false;
let analysed;
try {
analysed = await mystem(input);
if (!Array.isArray(analysed)) throw new Error('mystem parse failed');
const words = analysed.map(({text, analysis}) => {
let profane = false;
if (!analysis || !analysis.length) {
if (badWords.includes(text.toLowerCase())) {
profane = true;
isDirty = true;
} else return text;
} else {
for (const {lex} of analysis) {
if (!badWords.includes(lex.toLowerCase())) continue;
profane = true;
isDirty = true;
break;
}
}
return profane ? beep(text) : text;
});
return { text: words.join(''), isDirty };
} catch (err) {
// console.error(err);
return { error: err.message, analysed };
}
};