A python package that uses a compressed prefix tree (radix tree) to store a dictionary of known words and suggests completions for partial entries.
Project description
Patrix
A python package that uses a radix tree (aka trie, compressed prefix tree, or compact prefix tree) to store a dictionary of known words and provides suggestions to complete partial words.
It is used in autocomplete systems to provide suggestions to users based on the words they have typed.
Trie example
>>> from patrix import Trie
>>> t = Trie((("trie", 1), ("try", 2), ("tree", 3)))
>>> t.asdict()
{'t': {'r': {'i': {'e': {}}, 'y': {}, 'e': {'e': {}}}}}
Search for a word in the trie:
>>> t.search("tri")
<patrix.trie.TrieNode object at 0x7f952c171c10>
>>> t.search("tri").get_key()
'tri'
>>> t.search("trio") is None
True
Add a new word to the trie:
>>> t.insert("trio", 4)
>>> t.asdict()
{'t': {'r': {'e': {'e': {}}, 'i': {'e': {}, 'o': {}}, 'y': {}}}}
Radix tree example
>>> from patrix import RadixTree
>>> # Entries can be a list of strings or key-value tuples
>>> r = RadixTree(("computer", "compute", ("computing", 1)))
>>> r.asdict()
{'comput': {'e': {'': {}, 'r': {}}, 'ing': {}}}
Display suggestions on how to continue a given query prefix
>>> r.completions("c")
{'comput'}
>>> r.completions("comput")
{'compute', 'computing'}
>>> r.completions("compute") # The word 'compute' here is both a stem and a final word
{'compute', 'computer'}
>>> r.completions("p")
set()
Compression rate
>>> r.total_chars
11
>>> len("computer" + "computing" + "compute")
24
>>> 1 - 11 / 24 # 54% compression rate
0.5416666666666667
>>> r.size # nodes in the tree excluding the root
6
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file patrix-0.2.0.tar.gz.
File metadata
- Download URL: patrix-0.2.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a82e0ec91679e594253e1900b35f465a234f4b9c9a3ae3a2775b1a80fb6bcbf1
|
|
| MD5 |
a62fd70d0ea80c7a103b6ebedfd2c73d
|
|
| BLAKE2b-256 |
ad9ca2718f195800af9d5f035ed91a77bb2523abbd3c05be2ec2a0b37afbc07e
|
File details
Details for the file patrix-0.2.0-py3-none-any.whl.
File metadata
- Download URL: patrix-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
608b291bd81bed2983c88f1375e387e0b1412d4bddb6f99dfafcf309e1cfe69b
|
|
| MD5 |
ad9bacfbd52ba944e7f25f2687b641c9
|
|
| BLAKE2b-256 |
d8cf2cb2cedbbc475e7eb84e9ce7dbaf3108fdda4dc20600cb9bed54e4239a45
|