/AhoCorasickAutomation

Python version Aho-Corasic Automaton.

Primary LanguagePython

Aho-Corasick Automaton

Python version Aho-Corasic Automaton.

1. Usage

Please refer to demo.py.

1.1 Create tree from list

from trie import Trie

words = ["北京", "故宫", "北京故宫", "**", "紫禁城"]
tree = Trie().create_trie_from_list(words)
text = "北京故宫是**明清两代的皇家宫殿,旧称紫禁城。"
matchs = tree.parse_text(text)
print(matchs)

↓↓

>>> [0:2=北京, 0:4=北京故宫, 2:4=故宫, 5:7=**, 19:22=紫禁城]

1.2 Create tree from dict

from trie import Trie

words = {"北京": "GPE", "故宫": "LOC", "北京故宫": "LOC", "紫禁城": "LOC"}
tree = Trie().create_trie_from_dict(words)
text = "北京故宫是**明清两代的皇家宫殿,旧称紫禁城。"
matchs = tree.parse_text(text)
print(matchs)

↓↓

>>> [0:2=北京/GPE, 0:4=北京故宫/LOC, 2:4=故宫/LOC, 19:22=紫禁城/LOC]

2. References