|
| 1 | +""" |
| 2 | +This algorithm tries to find the pattern in given text using Bad Character Heuristic method. |
| 3 | +The bad-character rule considers the character in Text at which there is a mis-match. The next occurrence of that character to the left in Pattern is found, and a shift which brings that occurrence in line with the mismatched occurrence in Text is proposed. 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 |
| 4 | +
|
| 5 | +Complexity : O(n/m) |
| 6 | + n=length of main string |
| 7 | + m=length of pattern string |
| 8 | +""" |
| 9 | + |
| 10 | +class BoyerMooreSearch: |
| 11 | + def __init__(self, text, pattern): |
| 12 | + self.text, self.pattern = text, pattern |
| 13 | + self.textLen, self.patLen = len(text), len(pattern) |
| 14 | + |
| 15 | + def match_In_Pattern(self, char): |
| 16 | + |
| 17 | + """ finds the index of char in pattern in reverse order |
| 18 | +
|
| 19 | + Paremeters : |
| 20 | + char (chr): character to be searched |
| 21 | + |
| 22 | + Returns : |
| 23 | + i (int): index of char from last in pattern |
| 24 | + -1 (int): if char is not found in pattern |
| 25 | + """ |
| 26 | + |
| 27 | + for i in range(self.patLen-1, -1, -1): |
| 28 | + if char == self.pattern[i]: |
| 29 | + return i |
| 30 | + return -1 |
| 31 | + |
| 32 | + |
| 33 | + def misMatch_In_Text(self, currentPos): |
| 34 | + |
| 35 | + """ finds the index of mis-matched character in text when compared with pattern from last |
| 36 | +
|
| 37 | + Paremeters : |
| 38 | + currentPos (int): current index position of text |
| 39 | + |
| 40 | + Returns : |
| 41 | + i (int): index of mismatched char from last in text |
| 42 | + -1 (int): if there is no mis-match between pattern and text block |
| 43 | + """ |
| 44 | + |
| 45 | + for i in range(self.patLen-1, -1, -1): |
| 46 | + if self.pattern[i] != self.text[currentPos + i]: |
| 47 | + return currentPos + i |
| 48 | + return -1 |
| 49 | + |
| 50 | + def bad_Character_Heuristic(self): |
| 51 | + |
| 52 | + """ searches pattern in text and returns index positions """ |
| 53 | + positions = [] |
| 54 | + for i in range(self.textLen - self.patLen + 1): |
| 55 | + misMatch_Index = self.misMatch_In_Text(i) |
| 56 | + if misMatch_Index == -1: |
| 57 | + positions.append(i) |
| 58 | + else: |
| 59 | + match_Index = self.match_In_Pattern(self.text[misMatch_Index]) |
| 60 | + i = misMatch_Index - match_Index #shifting index |
| 61 | + return positions |
| 62 | + |
| 63 | + |
| 64 | +text = "ABAABA" |
| 65 | +pattern = "AB" |
| 66 | +bms = BoyerMooreSearch(text, pattern) |
| 67 | +positions = bms.bad_Character_Heuristic() |
| 68 | +if len(positions) == 0: |
| 69 | + print("No match found") |
| 70 | +else: |
| 71 | + print("Pattern found in following positions: ") |
| 72 | + print(positions) |
| 73 | + |
| 74 | + |
0 commit comments