-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_matching.py
141 lines (122 loc) · 3.54 KB
/
pattern_matching.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
# Name: Keval Varia
# Assignment 13: Pattern Matching
# Class: Data Structures
'''
Program Details:
- this program includes three different pattern matching algorithms:
a. Brute Force: where we compare each value of the pattern to that of the text.
b. Boyer Moore: compare each value of the pattern to the while skipping non-existent values using a lastc function.
c. Knutt Morris Pratt: Compare the pattern to the text based off a failure function
- each pattern will return the number of comparisions taken to find the pattern in the text.
'''
#-------------------------------------------------------------------------------
# string to list:
def stringToList(text):
vals = []
for x in range(len(text)):
vals.append(text[x])
return vals
# Brute Force Method
def bruteForce(text, pattern):
for i in range(len(text) - len(pattern) + 1):
j = 0
while (j < len(pattern) and text[i+j] == pattern[j]):
j += 1
if(j == len(pattern)):
return i
return -1
#-------------------------------------------------------------------------------
# Boyer Moore Method
def boyerMoore(text, pattern):
if len(pattern) == 0:
return 0
last = {}
for k in range(len(pattern)):
last[pattern[k]] = k
i = len(pattern) - 1
k = len(pattern) - 1
while i < len(text):
if text[i] == pattern[k]:
if k == 0:
return i
else:
i -= 1
k -= 1
else:
j = last.get(text[i], -1)
i += (len(pattern) - min(k, j+1))
k = len(pattern) - 1
return -1
# Last-C function for Boyer Moore Algorithm
def lastc(pattern, value):
index = -1
for i in range(len(pattern)):
if (pattern[i] == value):
index = i
return index
#-------------------------------------------------------------------------------
# Knutt Morris Pratt
def KMP(text, pattern):
if len(pattern) == 0:
return 0
fail = failFunction(pattern)
j = 0
k = 0
while j < len(text):
if (text[j] == pattern[k]):
if k == len(pattern) - 1:
return j - len(pattern) + 1
j += 1
k += 1
elif k > 0:
k = fail[k-1]
else:
j += 1
return -1
# Failure Function for Knutt Morris Pratt Algorithm
def failFunction(pattern):
fail=[0]*len(pattern)
j=1
k=0
while j<len(pattern):
if(pattern[j]==pattern[k]):
fail[j]=k+1
j+=1
k+=1
elif k>0:
k=fail[k-1]
else:
j+=1
return fail
#-------------------------------------------------------------------------------
def main():
# variable declaration
text = "aaabcaadaabaaa"
pattern = "aabaaa"
# convert strings to list for comparision
listText = stringToList(text)
listPattern = stringToList(pattern)
# print list version of the text
print "\nList:"
print listText
# print list version of the pattern
print "\nPattern:"
print listPattern
# Print number of comparisions for each algorithm
print "\nNumber of Comparisions:"
print ("Brute Force:", (bruteForce(listText, listPattern) + len(listPattern)))
print ("Boyer Moore:", (boyerMoore(listText, listPattern) + 1))
print ("Knutt Morris Pratt:", (KMP(listText, listPattern)))
print "\n"
print "Last-C for Boyer Moore:"
for index, item in enumerate(pattern):
print item, lastc(pattern, item)
print "\n"
print "Failure Function:"
fail = failFunction(listPattern)
for a,b in zip(pattern, fail):
print a, b
print "\n"
#-------------------------------------------------------------------------------
# function call to initialize the program
main()