MCPcopy
hub / github.com/TheAlgorithms/Python / search_in

Method search_in

strings/aho_corasick.py:67–90  ·  view source on GitHub ↗

>>> 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)

Source from the content-addressed store, hash-verified

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
93if __name__ == "__main__":

Callers

nothing calls this directly

Calls 2

find_next_stateMethod · 0.95
appendMethod · 0.45

Tested by

no test coverage detected