-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMP.py
214 lines (146 loc) · 5.67 KB
/
KMP.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
206
207
208
209
210
211
212
213
214
import docx
import time
import random
def KMPSearch(pat, txt):
M = len(pat)
N = len(txt)
# create lps[] that will hold the longest prefix suffix
# values for pattern
lps = [0]*M
j = 0 # index for pat[]
# Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps) # calculate longest prefix suffix values are stored in lps
i = 0 # index for txt[]
indices = [] # initialize list of indices
while i < N:
if pat[j] == txt[i]:
i += 1 # proceed to the next characters
j += 1
if j == M: #complete match is found
indices.append(i-j) # pattern found, add index to list
j = lps[j-1]
# mismatch after j matches
elif i < N and pat[j] != txt[i]: #avoid unnecesary comparisons by skipping ahead
# Do not match lps[0..lps[j-1]] characters,
# they will match anyway
if j != 0:
j = lps[j-1]
else:
i += 1
return indices # return list of indices where pattern is found
def computeLPSArray(pat, M, lps):
len = 0 # length of the previous longest prefix suffix
lps[0] = 0
i = 1 #second index of the pattern, first index doesnT have any proper suffixes
# the loop calculates lps[i] for i = 1 to M-1
while i < M:
if pat[i] == pat[len]: #means we found a longer proper suffix, meaning the suffix that is not equal to the entire string itself
len += 1 # upgrade the length of the suffix
lps[i] = len
i += 1 # next position
else: #current suffix is not a proper suffix anymore
# This is tricky. Consider the example.
# AAACAAAA and i = 7. The idea is similar
# to search step.
if len != 0:
len = lps[len-1]
# Also, note that we do not increment i here
else:
lps[i] = 0
i += 1
# Open the Word file
#doc = docx.Document(r"C:\Users\lenovo\Downloads\Raven.docx")
#text = " "
# Iterate over paragraphs in the document
#for para in doc.paragraphs:
# # Print the text of each paragraph
# text += para.text
doc = docx.Document(r"C:\Users\lenovo\Downloads\Raven.docx")
text = '\n'.join([para.text for para in doc.paragraphs])
#small pattern
pat_small = "silence"
start_time = time.time()
indices_small = KMPSearch(pat_small,text)
elapsed_time_small = time.time() - start_time
print(f"Indices of '{pat_small}' found using KMP algorithm: {indices_small}") #it says none
print(f"Elapsed time for small pattern: {elapsed_time_small:.6f} seconds")
#large pattern
pat_large = "Ah, distinctly I remember it was in the bleak December"
start_time = time.time()
indices_large = KMPSearch(pat_large,text)
elapsed_time_large = time.time() - start_time
print(f"Indices of large pattern found using KMP algorithm: {indices_large}")
print(f"Elapsed time for large pattern: {elapsed_time_large:.6f} seconds")
import docx
import time
import random
def KMPSearch(pat, txt):
M = len(pat)
N = len(txt)
# create lps[] that will hold the longest prefix suffix
# values for pattern
lps = [0]*M
j = 0 # index for pat[]
# Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps)
i = 0 # index for txt[]
indices = [] # initialize list of indices
while i < N:
if pat[j] == txt[i]:
i += 1
j += 1
if j == M:
indices.append(i-j) # pattern found, add index to list
j = lps[j-1]
# mismatch after j matches
elif i < N and pat[j] != txt[i]:
# Do not match lps[0..lps[j-1]] characters,
# they will match anyway
if j != 0:
j = lps[j-1]
else:
i += 1
return indices # return list of indices where pattern is found
def computeLPSArray(pat, M, lps):
len = 0 # length of the previous longest prefix suffix
lps[0] = 0
i = 1
# the loop calculates lps[i] for i = 1 to M-1
while i < M:
if pat[i] == pat[len]:
len += 1
lps[i] = len
i += 1
else:
# This is tricky. Consider the example.
# AAACAAAA and i = 7. The idea is similar
# to search step.
if len != 0:
len = lps[len-1]
# Also, note that we do not increment i here
else:
lps[i] = 0
i += 1
# Open the Word file
#doc = docx.Document(r"C:\Users\lenovo\Downloads\Raven.docx")
#text = " "
# Iterate over paragraphs in the document
#for para in doc.paragraphs:
# # Print the text of each paragraph
# text += para.text
doc = docx.Document(r"C:\Users\lenovo\Downloads\Raven.docx")
text = '\n'.join([para.text for para in doc.paragraphs])
#small pattern
pat_small = "silence"
start_time = time.time()
indices_small = KMPSearch(pat_small,text)
elapsed_time_small = time.time() - start_time
print(f"Indices of '{pat_small}' found using KMP algorithm: {indices_small}") #it says none
print(f"Elapsed time for small pattern: {elapsed_time_small:.6f} seconds")
#large pattern
pat_large = "Ah, distinctly I remember it was in the bleak December"
start_time = time.time()
indices_large = KMPSearch(pat_large,text)
elapsed_time_large = time.time() - start_time
print(f"Indices of large pattern found using KMP algorithm: {indices_large}")
print(f"Elapsed time for large pattern: {elapsed_time_large:.6f} seconds")