-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsearch.py
More file actions
31 lines (26 loc) · 757 Bytes
/
search.py
File metadata and controls
31 lines (26 loc) · 757 Bytes
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
def ComputeSp(pattern):
sp = [-1 for _ in range(len(pattern))]
k = -1
for i in range(1, len(pattern) - 1):
while k >= 0 and not pattern[k + 1] == pattern[i]:
k = sp[k]
if pattern[k + 1] == pattern[i]:
k += 1
sp[i] = k
return sp
def kmp(text, pattern):
sp = ComputeSp(pattern)
j = -1
for i in range(len(text)):
while j >= 0 and not pattern[j + 1] == text[i]:
j = sp[j]
if pattern[j + 1] == text[i]:
j += 1
if j == len(pattern) - 1:
return True
def search(textList, keyword):
searched = []
for text in textList:
if kmp(text["processed"], keyword):
searched.append(text)
return searched