|
@ -11,7 +11,7 @@ class Bm(object): |
|
|
def __init__(self, text: str, pattern: str): |
|
|
def __init__(self, text: str, pattern: str): |
|
|
self.text = text |
|
|
self.text = text |
|
|
self.pattern = pattern |
|
|
self.pattern = pattern |
|
|
self.table = make_km_table(pattern) |
|
|
|
|
|
|
|
|
# self.table = make_km_table(pattern) |
|
|
|
|
|
|
|
|
def decide_slide_width(self, c: str) -> int: |
|
|
def decide_slide_width(self, c: str) -> int: |
|
|
assert len(c) == 1 |
|
|
assert len(c) == 1 |
|
@ -19,5 +19,36 @@ class Bm(object): |
|
|
return -1 |
|
|
return -1 |
|
|
|
|
|
|
|
|
def search(self) -> int: |
|
|
def search(self) -> int: |
|
|
raise Exception("TODO") |
|
|
|
|
|
|
|
|
PATTERN_LENGTH = len(self.pattern) |
|
|
|
|
|
TEXT_LENGTH = len(self.text) |
|
|
|
|
|
|
|
|
|
|
|
END_OF_PATTERN = PATTERN_LENGTH - 1 |
|
|
|
|
|
LAST_CHAR_OF_PATTERN = self.pattern[-1] |
|
|
|
|
|
|
|
|
|
|
|
offset = {c: PATTERN_LENGTH - i -1 for i, c in enumerate(self.pattern[:-1])} |
|
|
|
|
|
print(offset) |
|
|
|
|
|
|
|
|
|
|
|
matches = [] |
|
|
|
|
|
head = END_OF_PATTERN |
|
|
|
|
|
|
|
|
|
|
|
while head < TEXT_LENGTH: |
|
|
|
|
|
print(f'{" " * (head - PATTERN_LENGTH + 1)}{self.pattern}') |
|
|
|
|
|
print(self.text[:head + 1]) |
|
|
|
|
|
print(f'{" " * head}^') |
|
|
|
|
|
|
|
|
|
|
|
if self.text[head] == LAST_CHAR_OF_PATTERN: |
|
|
|
|
|
for i, c in enumerate(reversed(self.pattern)): |
|
|
|
|
|
# print(text[head - i], c) |
|
|
|
|
|
if self.text[head - i] != c: |
|
|
|
|
|
head += offset[self.text[head]] |
|
|
|
|
|
break |
|
|
|
|
|
else: |
|
|
|
|
|
print("!!!") |
|
|
|
|
|
return head - PATTERN_LENGTH + 1 |
|
|
|
|
|
else: |
|
|
|
|
|
try: |
|
|
|
|
|
head += offset[self.text[head]] |
|
|
|
|
|
except KeyError: |
|
|
|
|
|
head += PATTERN_LENGTH |
|
|
|
|
|
print('----------------------------------') |
|
|
return -1 |
|
|
return -1 |