classac_automatic: def__init__(self): self.root = node() defadd(self, word): p = self.root for w in word: if w in p.next: p = p.next[w] else: p.next[w] = node() p = p.next[w] p.isword=True p.word=word defjudge(self): p = self.root temp = [p] whilelen(temp) != 0: d = temp.pop(0) for v in d.next.keys(): p = d if p == self.root: v.fail = self.root else:
while p.fail != Noneand v notin p.fail.next: p = p.fail
if v in p.fail.next: v.fail = p.fail.next[v] else: v.fail = self.root temp.append(v) defsearch(self, content): p = self.root final = [] for w in content:
while p.fail != Noneand w notin p.fail.next: p = p.fail
if w in p.next: p = p.next[w] else: p = self.root if p.isword == True: final.append(p.word) return final
model = ac_automatic() model.add("her") model.add("she") print(model.search("dfadooudashefhe2fdl"))