-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
PersianSwear.py
61 lines (48 loc) · 1.71 KB
/
PersianSwear.py
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
import json
from string import punctuation
class PersianSwear:
def __init__(self):
with open("data.json") as file:
self.data = json.load(file)
self.swear_words = set(self.data["word"])
def ignoreSY(self, text):
translator = str.maketrans("", "", punctuation)
return text.translate(translator)
def filter_words(self, text, symbol="*", ignoreOT=False):
if not self.swear_words:
return text
words = text.split()
filtered_words = []
for word in words:
if word in self.swear_words or (
ignoreOT and self.ignoreSY(word) in self.swear_words
):
filtered_words.append(symbol)
else:
filtered_words.append(word)
return " ".join(filtered_words)
def is_empty(self):
return not self.swear_words
def add_word(self, word):
self.swear_words.add(word)
self.data["word"].append(word)
def remove_word(self, word):
if word in self.swear_words:
self.swear_words.remove(word)
if word in self.data["word"]:
self.data["word"].remove(word)
def is_bad(self, text, ignoreOT=False):
if ignoreOT:
text = self.ignoreSY(text)
text = text.replace("\u200c", "")
return text in self.swear_words
def has_swear(self, text, ignoreOT=False):
if ignoreOT:
text = self.ignoreSY(text)
text = text.replace("\u200c", "")
if not self.swear_words:
return False
words = text.split()
return any(word in self.swear_words for word in words)
def tostring(self):
return " - ".join(self.swear_words)