A basic implementation of word dictionary using the Trie data structure in Python 3.
A trie (derived from retrieval) is a multiway tree data structure used for storing strings over an alphabet. It is used to store a large amount of strings. The pattern matching can be done efficiently using tries.
To clone the repo:
git clone https://github.com/tharunsuresh-code/pytrie.git
Create a new Trie
to use as a word dictionary:
from pytrie import Trie
trie = Trie()
trie.addWord("hello")
trie.addWord("world")
trie.addWord("Harry")
trie.addWord("Potter")
Now you can query the Trie for any string:
>>> trie.search("hello")
True
>>> trie.search("world")
True
>>> trie.search("harry")
False
>>> trie.search("Harry")
True
You can also search for words using wildcard .
character. It replaces for one single character:
>>> trie.search("H.rr.")
True
>>> trie.search("P...")
False
>>> trie.search("P....r")
True
Docs are inspired from PyTrie and datrie. Trie definition from geeksforgeeks.
Licensed under MIT.