2019-07-02 12:19:31 +00:00
|
|
|
"""
|
|
|
|
The algorithm finds the pattern in given text using following rule.
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
The bad-character rule considers the mismatched character in Text.
|
|
|
|
The next occurrence of that character to the left in Pattern is found,
|
2019-07-02 12:19:31 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
If the mismatched character occurs to the left in Pattern,
|
|
|
|
a shift is proposed that aligns text block and pattern.
|
2019-07-02 12:19:31 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
If the mismatched character does not occur to the left in Pattern,
|
|
|
|
a shift is proposed that moves the entirety of Pattern past
|
|
|
|
the point of mismatch in the text.
|
2019-07-02 12:19:31 +00:00
|
|
|
|
|
|
|
If there no mismatch then the pattern matches with text block.
|
|
|
|
|
|
|
|
Time Complexity : O(n/m)
|
|
|
|
n=length of main string
|
|
|
|
m=length of pattern string
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class BoyerMooreSearch:
|
|
|
|
def __init__(self, text, pattern):
|
|
|
|
self.text, self.pattern = text, pattern
|
|
|
|
self.textLen, self.patLen = len(text), len(pattern)
|
|
|
|
|
|
|
|
def match_in_pattern(self, char):
|
2020-09-10 08:31:26 +00:00
|
|
|
"""finds the index of char in pattern in reverse order
|
2019-07-02 12:19:31 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
Parameters :
|
2019-07-02 12:19:31 +00:00
|
|
|
char (chr): character to be searched
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2019-07-02 12:19:31 +00:00
|
|
|
Returns :
|
|
|
|
i (int): index of char from last in pattern
|
2020-05-22 06:10:11 +00:00
|
|
|
-1 (int): if char is not found in pattern
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2019-07-02 12:19:31 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
for i in range(self.patLen - 1, -1, -1):
|
2019-07-02 12:19:31 +00:00
|
|
|
if char == self.pattern[i]:
|
|
|
|
return i
|
|
|
|
return -1
|
|
|
|
|
|
|
|
def mismatch_in_text(self, currentPos):
|
2020-06-16 08:09:19 +00:00
|
|
|
"""
|
|
|
|
find the index of mis-matched character in text when compared with pattern
|
|
|
|
from last
|
2019-07-02 12:19:31 +00:00
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
Parameters :
|
2019-07-02 12:19:31 +00:00
|
|
|
currentPos (int): current index position of text
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2019-07-02 12:19:31 +00:00
|
|
|
Returns :
|
|
|
|
i (int): index of mismatched char from last in text
|
2020-01-18 12:24:33 +00:00
|
|
|
-1 (int): if there is no mismatch between pattern and text block
|
2019-07-02 12:19:31 +00:00
|
|
|
"""
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
for i in range(self.patLen - 1, -1, -1):
|
2019-07-02 12:19:31 +00:00
|
|
|
if self.pattern[i] != self.text[currentPos + i]:
|
|
|
|
return currentPos + i
|
|
|
|
return -1
|
|
|
|
|
|
|
|
def bad_character_heuristic(self):
|
2019-10-05 05:14:13 +00:00
|
|
|
# searches pattern in text and returns index positions
|
2019-07-02 12:19:31 +00:00
|
|
|
positions = []
|
|
|
|
for i in range(self.textLen - self.patLen + 1):
|
|
|
|
mismatch_index = self.mismatch_in_text(i)
|
|
|
|
if mismatch_index == -1:
|
|
|
|
positions.append(i)
|
|
|
|
else:
|
|
|
|
match_index = self.match_in_pattern(self.text[mismatch_index])
|
2019-10-05 05:14:13 +00:00
|
|
|
i = (
|
|
|
|
mismatch_index - match_index
|
|
|
|
) # shifting index lgtm [py/multiple-definition]
|
2019-07-02 12:19:31 +00:00
|
|
|
return positions
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-07-02 12:19:31 +00:00
|
|
|
text = "ABAABA"
|
2019-10-05 05:14:13 +00:00
|
|
|
pattern = "AB"
|
2019-07-02 12:19:31 +00:00
|
|
|
bms = BoyerMooreSearch(text, pattern)
|
|
|
|
positions = bms.bad_character_heuristic()
|
|
|
|
|
|
|
|
if len(positions) == 0:
|
|
|
|
print("No match found")
|
|
|
|
else:
|
|
|
|
print("Pattern found in following positions: ")
|
|
|
|
print(positions)
|