-
Notifications
You must be signed in to change notification settings - Fork 2
/
SensitiveWordFilter.py
executable file
·205 lines (187 loc) · 6.3 KB
/
SensitiveWordFilter.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
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
"""
Encoding: utf-8
Author: April
Email: [email protected]
CreateTime: 2020-01-18 1:57
Description: SensitiveWordFilter.py
Version: 1.0
"""
import os
class DFAFilter:
"""
DFA
"""
def __init__(self):
"""
Initializes the sensitive word trie.
"""
self.root = {}
self.word_end = "END"
def insert(self, word):
"""
:function: Inserts a word into the trie.
:param word: str
:return: void
"""
curr_node = self.root
for ch in word:
if ch not in curr_node:
curr_node[ch] = {}
curr_node = curr_node[ch]
curr_node[self.word_end] = True
def readFile(self, file_path):
"""
:function: Read sensitive word files or directories
:param file_path: file path or directory path
:return: void
"""
if os.path.isfile(file_path):
with open(file_path, mode='r', encoding='utf-8') as file_handle:
for word in file_handle:
self.insert(word.strip().lower())
if os.path.isdir(file_path):
for root, dirs, files in os.walk(file_path, topdown=False):
for file in files:
curr_path = os.path.join(root, file)
print(curr_path)
with open(curr_path, 'r', encoding='utf-8') as file_handle:
for word in file_handle:
self.insert(word.strip().lower())
del self.root[self.word_end]
def search(self, word):
"""
:function: Returns if the word is in the trie.
:param word: str
:return: bool
"""
curr_node = self.root
for ch in word.lower():
if ch not in curr_node:
return False
curr_node = curr_node[ch]
# Doesn't end here
if self.word_end not in curr_node:
return False
return True
def startsWith(self, prefix):
"""
:function: Returns if there is any word in the trie that starts with the given prefix.
:param prefix: str
:return: bool
"""
curr_node = self.root
for ch in prefix.lower():
if ch not in curr_node:
return False
curr_node = curr_node[ch]
return True
def checkSentence(self, text):
"""
:function: Returns if the text contains sensitive words.
:param text: a Chinese text
:return: bool
"""
text = text.lower()
index = 0
while index < len(text):
curr_node = self.root.copy()
curr_text = text[index:]
temp_index = 0
count = 0
while temp_index < len(curr_text) and curr_text[temp_index] in curr_node:
curr_node = curr_node[curr_text[temp_index]]
temp_index += 1
count += 1
if self.word_end in curr_node:
# sensitive_word = curr_text[:count]
# print(sensitive_word)
return True
index += 1
return False
def coverSensitive(self, text, cover_str):
"""
:function: Returns a paragraph of Chinese text replacing sensitive words with *
:param text: a Chinese text
:param cover_str: a character that replaces a sensitive word
:return:
"""
text = text.lower()
result = ''
index = 0
len_text = len(text)
while index < len_text:
count = 0
curr_node = self.root.copy()
# temp = ''
while index < len_text and text[index] in curr_node:
curr_node = curr_node[text[index]]
count += 1
index += 1
if count == 0:
temp = text[index]
index += 1
elif self.word_end in curr_node: # and count != 0
temp = cover_str * count
else:
temp = text[index-count:index]
result += temp
return result
class node(object):
def __init__(self):
self.next = {}
self.fail = None
self.isWord = False
self.word = ""
class AcAutomationFilter:
"""
Ac Automation
"""
def __init__(self):
self.root = node()
def insertWord(self, word):
curr_node = self.root
for char in word:
if char not in curr_node.next:
curr_node.next[char] = node()
curr_node = curr_node.next[char]
curr_node.isWord = True
curr_node.word = word
def readFile(self, file_path):
if os.path.isfile(file_path):
with open(file_path, mode='r', encoding='utf-8') as file_handle:
for word in file_handle:
self.insertWord(word.strip().lower())
if os.path.isdir(file_path):
for root, dirs, files in os.walk(file_path, topdown=False):
for file in files:
curr_path = os.path.join(root, file)
print(curr_path)
with open(curr_path, 'r', encoding='utf-8') as file_handle:
for word in file_handle:
self.insertWord(word.strip().lower())
self.root.isWord = False
self.root.word = ''
def checkSentence(self, text):
"""
:function: Returns if the text contains sensitive words.
:param text: a Chinese text
:return: bool
"""
text = text.lower()
result = []
index = 0
while index < len(text):
curr_node = self.root
curr_text = text[index:]
temp_index = 0
while temp_index < len(curr_text) and curr_text[temp_index] in curr_node.next:
curr_node = curr_node.next[curr_text[temp_index]]
temp_index += 1
if curr_node.isWord:
result.append(curr_node.word)
index += 1
# print(result)
if len(result) != 0:
return True
else:
return False