import ahocorasick
def build_actree(wordlist):
actree = ahocorasick.Automaton()
for index, word in enumerate(wordlist):
actree.add_word(word, (index, word))
actree.make_automaton()
return actree
if __name__ == '__main__':
wordlist = ['中国', '中山大学', '广州市', '清华大学', '北京市']
sent = '中山大学是一所位于广州市的综合性院校。'
actree = build_actree(wordlist=wordlist)
for i in actree.iter(sent):
print(i)
输出词表中的词在字符串中的结束位置、词在词表中的位置、词:
(3, (1, '中山大学'))
(11, (2, '广州市'))