>>> A = Automaton(["what", "hat", "ver", "er"]) >>> A.search_in("whatever, err ... , wherever") {'what': [0], 'hat': [1], 'ver': [5, 25], 'er': [6, 10, 22, 26]}
(self, string: str)
| 65 | ) |
| 66 | |
| 67 | def search_in(self, string: str) -> dict[str, list[int]]: |
| 68 | """ |
| 69 | >>> A = Automaton(["what", "hat", "ver", "er"]) |
| 70 | >>> A.search_in("whatever, err ... , wherever") |
| 71 | {'what': [0], 'hat': [1], 'ver': [5, 25], 'er': [6, 10, 22, 26]} |
| 72 | """ |
| 73 | result: dict = {} # returns a dict with keywords and list of its occurrences |
| 74 | current_state = 0 |
| 75 | for i in range(len(string)): |
| 76 | while ( |
| 77 | self.find_next_state(current_state, string[i]) is None |
| 78 | and current_state != 0 |
| 79 | ): |
| 80 | current_state = self.adlist[current_state]["fail_state"] |
| 81 | next_state = self.find_next_state(current_state, string[i]) |
| 82 | if next_state is None: |
| 83 | current_state = 0 |
| 84 | else: |
| 85 | current_state = next_state |
| 86 | for key in self.adlist[current_state]["output"]: |
| 87 | if key not in result: |
| 88 | result[key] = [] |
| 89 | result[key].append(i - len(key) + 1) |
| 90 | return result |
| 91 | |
| 92 | |
| 93 | if __name__ == "__main__": |
nothing calls this directly
no test coverage detected