1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
import ahocorasick
def iter_results(s):
r = []
for x in A.iter(teststr):
r.append(x)
return r
def find_all_results(s):
r = []
def append(x, s):
r.append((x, s))
A.find_all(s, append)
return r
A = ahocorasick.Automaton()
for word in ("poke", "go", "pokegois", "egoist"):
A.add_word(word, word)
A.make_automaton()
teststr = 'pokego pokego pokegoist'
expected = iter_results(teststr)
findall = find_all_results(teststr)
if findall != expected:
print("expected: %s" % expected)
print("findall : %s" % findall)
assert findall == expected
|