I am implementing an autocomplete feature using a Trie. Using the words list in this link, I am inserting every word into my Trie. I want to reduce the memory used by the Trie without using something too fancy like a directed acyclic word graph.
The Trie is dictionary based to allow for it to be stored in a JSON file. This is my current script:
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)
If this can be done, please help me out with code snippets.