forked from SAGIRI-kawaii/saya_plugins_collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFA.py
68 lines (60 loc) · 2.12 KB
/
DFA.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
from .Sqlite3Manager import Sqlite3Manager
class DFAUtils:
__filter_words_dict = {}
__skip_char = [' ', '&', '!', '!', '@', '#', '$', '¥', '*', '^', '%', '?', '?', '<', '>', "《", '》']
def __init__(self):
keywords = self.__get_words()
for keyword in keywords:
self.add_keyword(keyword[0])
# print(self.__filter_words_dict)
def filter_judge(self, text: str) -> list:
words = []
for i in range(len(text)):
length = self.check(text, i)
if length > 0:
words.append(text[i:i + length])
return words
def check(self, text: str, begin_index: int) -> int:
flag = False
match_flag_length = 0
node = self.__filter_words_dict
tmp_flag = 0
for i in range(begin_index, len(text)):
char = text[i]
if char in self.__skip_char:
tmp_flag += 1
continue
node = node.get(char)
if node:
match_flag_length += 1
tmp_flag += 1
if node.get("is_end"):
flag = True
else:
break
if tmp_flag < 2 or not flag:
tmp_flag = 0
return tmp_flag
def add_keyword(self, keyword: str):
node = self.__filter_words_dict
for i in range(len(keyword)):
char = keyword[i]
if char in node.keys():
node = node.get(char)
node["is_end"] = False
else:
node[char] = {"is_end": True if i == len(keyword) - 1 else False}
node = node[char]
def replace_filter_word(self, text: str) -> str:
filter_words = self.filter_judge(text)
for word in filter_words:
text = text.replace(word, "*" * len(word))
return text
def __get_words(self) -> tuple:
conn = Sqlite3Manager.get_instance().get_conn()
cursor = conn.cursor()
sql = f"SELECT keyword FROM keywords"
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return result