-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboyerMooreParallelBidirectional2.py
More file actions
143 lines (123 loc) · 4.27 KB
/
Copy pathboyerMooreParallelBidirectional2.py
File metadata and controls
143 lines (123 loc) · 4.27 KB
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
import threading
import time
class BadCharShift(threading.Thread):
def __init__(self, term):
threading.Thread.__init__(self)
self.term = term
def run(self):
self.skipList = self.generateBadCharShift(self.term)
def generateBadCharShift(self,term):
skipList = {}
for i in range(0, len(term)-1):
skipList[term[i]] = len(term)-i-1
return skipList
class SuffixShift(threading.Thread):
def __init__(self,key):
threading.Thread.__init__(self)
self.key = key
def run(self):
self.skipList = self.generateSuffixShift(self.key)
def generateSuffixShift(self,key):
skipList = {}
buffer = ""
for i in range(0, len(key)):
skipList[len(buffer)] = self.findSuffixPosition(key[len(key)-1-i], buffer, key)
buffer = key[len(key)-1-i] + buffer
return skipList
def findSuffixPosition(self,badchar, suffix, full_term):
for offset in range(1, len(full_term)+1)[::-1]:
flag = True
for suffix_index in range(0, len(suffix)):
term_index = offset-len(suffix)-1+suffix_index
if term_index < 0 or suffix[suffix_index] == full_term[term_index]:
pass
else:
flag = False
term_index = offset-len(suffix)-1
if flag and (term_index <= 0 or full_term[term_index-1] != badchar):
return len(full_term)-offset+1
class BmSearch(threading.Thread):
def _init_(self):
threading.Thread.__init__(self)
def run(self):
needle = self.needle
haystack = self.haystack
goodSuffix = self.goodSuffix
badChar = self.badChar
i = 0
while i < len(haystack)-len(needle)+1:
j = len(needle)
while j > 0 and needle[j-1] == haystack[i+j-1]:
j -= 1
if j > 0:
badCharShift = badChar.get(haystack[i+j-1], len(needle))
goodSuffixShift = goodSuffix[len(needle)-j]
if badCharShift > goodSuffixShift:
i += badCharShift
else:
i += goodSuffixShift
else:
self.value = i
return
self.value = -1
return
# Actual Search Algorithm
def BMSearch(haystack, needle):
first = haystack[:int(len(haystack)/2)]
second = haystack[int(len(haystack)/2):]
goodSuffix = None
badChar = None
#setting good suffix and bad character here
try:
thread1 = SuffixShift(needle)
thread2 = BadCharShift(needle)
thread1.start()
thread2.start()
#waiting for the threads to be finished...
thread1.join()
thread2.join()
#checking whether threads have finished
if not(thread1.is_alive() or thread2.is_alive()):
goodSuffix = thread1.skipList
badChar = thread2.skipList
else:
return -1
except Exception as ex:
print("Thread Error ",ex)
return -1
thread1 = BmSearch()
thread1.haystack = haystack
thread1.needle = needle
thread1.badChar = badChar
thread1.goodSuffix = goodSuffix
thread1.start()
thread2 = BmSearch()
thread2.haystack = haystack
thread2.needle = needle
thread2.badChar = badChar
thread2.goodSuffix = goodSuffix
thread2.start()
thread1.join()
thread2.join()
if not (thread1.is_alive() and thread2.is_alive()):
if thread1.value == -1:
if thread2.value != -1:
return thread2.value + len(thread1.haystack)
else :
return -1
else:
return thread1.value
else:
return -1
if __name__ == "__main__":
block = "This is a simple example"
data = open('testData_all_1.txt','r')
text = data.read()
data = open('patterns_all.txt','r')
pattern = data.read()
print(text)
start = time.process_time()
result = BMSearch(text,pattern)
end = time.process_time()
print("pattern found at ",result)
print("User + System Time for the Task in seconds: ",end-start)