-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdirty_recognize.py
42 lines (36 loc) · 1.11 KB
/
dirty_recognize.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
"""
@author: liucong
@contact: [email protected]
@time: 2020/7/29 15:38
"""
from trie import Trie
class dirty_reg(object):
def __init__(self, path):
self.obj = Trie()
self.build(path)
def insert_new(self, word_list):
word_list = [word.lower() for word in word_list]
self.obj.insert(word_list)
def build(self, path):
f = open(path, "r", encoding="utf-8")
for line in f:
line = line.strip()
if line:
self.insert_new(line)
def enumerateMatchList(self, word_list):
word_list = [word.lower() for word in word_list]
match_list = self.obj.enumerateMatch(word_list)
return match_list
def match(self, query):
al = set()
length = 0
for indx in range(len(query)):
index = indx + length
match_list = self.enumerateMatchList(query[index:])
if match_list == []:
continue
else:
match_list = max(match_list)
length = len("".join(match_list))
al.add(match_list)
return al