Trie Again: Python Trie implementation optimized for T9 completion
Installation
pip install trie-again
CC='gcc' CFLAGS='-march=native' pip install trie-again
Usage
from trie_again import Trie
from trie_again import CyTrie
trie = Trie()
trie.insert('boy')
trie.extend(['bondage', 'coverage'])
data = {
'bondage': 10,
'coverage': 20,
}
trie.extend(data.keys(), data.values())
print('bondage' in trie)
print(list(trie))
print(list(trie.complete('b')))
T9 Like completion
print(list(trie.complete(['bc', 'o', 'vn'])))
How it works?
b o y
b o n d a g e
c o v e r a g e
^ ^ ^
1 2 3
We use these groups to complete: bc
, o
, vn
. It means that at position 1 it the letter may be b
or c
, at position 2 only o
, at position 3 v
or n
.
Test
poetry run pytest
poetry run pytest --benchmark
Dev
poetry install
poetry run pre-commit install
poetry run black .
poetry run flake8 .
poetry run mypy .
poetry run coverage run -m pytest && poetry run coverage report -m
poetry build -f sdist
Read an article about the trie, friends!
https://blagovdaryu.hashnode.dev/ok-lets-trie-t9-in-python