Skip to content

Commit c9728b8

Browse files
committed
Change Node.add() to Trie.add_word()
1 parent 5551157 commit c9728b8

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

pythainlp/util/trie.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,27 @@ def __init__(self):
1515
self.end = False
1616
self.children = {}
1717

18-
def add(self, word: str):
19-
cur = self
20-
for ch in word:
21-
child = cur.children.get(ch)
22-
if not child:
23-
child = Trie.Node()
24-
cur.children[ch] = child
25-
cur = child
26-
cur.end = True
27-
2818
def __init__(self, words: Iterable[str]):
2919
self.words = words
3020
self.root = Trie.Node()
3121

3222
for word in words:
33-
self.root.add(word)
23+
self.add_word(word)
24+
25+
def add_word(self, word: str) -> None:
26+
"""
27+
Add a word to the trie.
28+
29+
:param str text: a word
30+
"""
31+
cur = self.root
32+
for ch in word:
33+
child = cur.children.get(ch)
34+
if not child:
35+
child = Trie.Node()
36+
cur.children[ch] = child
37+
cur = child
38+
cur.end = True
3439

3540
def prefixes(self, text: str) -> List[str]:
3641
"""

0 commit comments

Comments
 (0)