-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtrie-checkpoint.py
34 lines (31 loc) · 1002 Bytes
/
trie-checkpoint.py
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
def build_trie(lexicon):
"""
Build a character-trie from the plain pattern_string -> categories_list
mapping provided by `lexicon`.
Some LIWC patterns end with a `*` to indicate a wildcard match.
"""
trie = {}
for pattern, category_names in lexicon.items():
cursor = trie
for char in pattern:
if char == "*":
cursor["*"] = category_names
break
if char not in cursor:
cursor[char] = {}
cursor = cursor[char]
cursor["$"] = category_names
return trie
def search_trie(trie, token, token_i=0):
"""
Search the given character-trie for paths that match the `token` string.
"""
if "*" in trie:
return trie["*"]
if "$" in trie and token_i == len(token):
return trie["$"]
if token_i < len(token):
char = token[token_i]
if char in trie:
return search_trie(trie[char], token, token_i + 1)
return []