我正在使用Trie实现自动完成功能。使用this链接中的单词列表,我将每个单词插入到Trie中。我想减少Trie占用的内存,而又不使用像有向非循环字图这样过于花哨的东西。
Trie是基于字典的,允许将其存储在JSON文件中。这是我当前的脚本:
import json
#Make a trie of english words
# The words file can be found at https://github.com/dwyl/english-words
with open('words_dictionary.json', 'r') as file:
words = json.load(file)
_end = '_end_'
trie = {}
def make_trie(words):
root = trie
for word in words:
current = root
for char in word:
if char not in current:
current[char] = {}
current = current[char]
current[_end] = _end
make_trie(words)
with open('word_trie.json', 'w') as outfile:
json.dump(trie, outfile)
如果可以做到这一点,请帮助我提供代码段。