-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordFilterSystem.js
100 lines (76 loc) · 2.85 KB
/
wordFilterSystem.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
const Filter = require('bad-words');
const levenshtein = require('fast-levenshtein');
let filterNumbersAndSpecialChars = false;
const similarityThreshold = 0.8;
const customBadWords = ['snickers'];
const badWordsFilter = new Filter({ list: customBadWords });
customBadWords.forEach((word) => {
badWordsFilter.addWords(word);
});
const whitelist = ['hello', 'funny', 'picks', 'shine', 'meets', 'where', 'space','ships'];
const filterBadWordsWithLevenshtein = (text) => {
const words = text.split(' ');
const filteredWords = words.filter((word) => {
if (whitelist.includes(word.toLowerCase())) {
return true;
}
let maxSimilarity = 0;
const badWordMatch = badWordsFilter.list.find((badWord) => {
const distance = levenshtein.get(word, badWord);
const similarity = 1 - distance / Math.max(word.length, badWord.length);
maxSimilarity = Math.max(maxSimilarity, similarity);
return similarity >= similarityThreshold;
});
const isFiltered = badWordMatch !== undefined;
if (isFiltered) {
console.log(`Filtered word detected: "${word}" - Bad word match: ${badWordMatch} - Similarity: ${maxSimilarity}`);
}
return !isFiltered;
});
return filteredWords.join(' ');
};
const filterLinks = (text) => {
const urlPattern = /https?:\/\/[^\s]+/g;
return text.replace(urlPattern, '');
};
const filterText = (text) => {
let filteredText = text;
filteredText = filterLinks(filteredText);
if (filterNumbersAndSpecialChars) {
filteredText = filteredText.replace(/[\d\W]/g, '');
}
filteredText = filterBadWordsWithLevenshtein(filteredText);
filteredText = textFilter(filteredText); // Add this line to apply the replacements
return filteredText;
};
// Add the textFilter and escapeRegExp functions here
function textFilter(text) {
const replacements = [
{ key: ":)", value: "Happy Face" },
{ key: ":D", value: "Extremely Happy Face" },
{ key: ":(", value: "Sad Face" },
{ key: ":o", value: "Surprised face" },
{ key: ":z", value: "Tired Face" },
{ key: "B)", value: "Cool Face" },
{ key: ":/", value: "Concerned Face" },
{ key: ";)", value: "Winkie Face" },
{ key: ":p", value: "Winkie Tounge" },
{ key: "R)", value: "YAAARRR" },
{ key: ";p", value: "Tongue" },
{ key: "o_O", value: "uhhhhh" },
{ key: ">(", value: "L O L" },
{ key: "bobheaJMS", value: "John Madden" },
{ key: "clawRM", value: "RIGGED" },
{ key: "useles2Duck", value: "Useless Duck Company" },
{ key: "SnickersHype", value: "hype" },
];
let filteredText = text;
replacements.forEach(({ key, value }) => {
filteredText = filteredText.replace(new RegExp(escapeRegExp(key), "g"), value);
});
return filteredText;
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
module.exports = filterText;