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
Patrix provides a radix tree class (aka trie, compressed prefix tree, or compact prefix tree) that behaves mostly like a python dictionary but provides quick and efficient completion suggestions for partial word entries.
This is useful for building autocomplete systems of long lists of known strings that share common prefixes. This is typical for hierarchical naming systems like file paths, IP addresses, or domain names, but it is not limited to those examples.
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': {}}}
>>> s = RadixTree.from_dict({'comput': {'e': {'': {}, 'r': {}}, 'ing': {}}})
>>> s.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()
RadixTree behaves like a python dictionary:
>>> r["computer"] = 1
>>> r["compute"] = 2
>>> r["computing"] = 3
>>> r.pop("compute")
2
>>> "computing" in r
True
>>> r["computing"]
3
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.1.tar.gz.
File metadata
- Download URL: patrix-0.2.1.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c764464d77281c33db96bd73b0ed6109a28ce480ef22f10efed8de20aa641a7e
|
|
| MD5 |
f612e13fecc02ada95aad4164a7a811a
|
|
| BLAKE2b-256 |
e86c430f84d3c352b5e9f7490b0f527538c352eda4c4d5935f4c570c76d11cdb
|
File details
Details for the file patrix-0.2.1-py3-none-any.whl.
File metadata
- Download URL: patrix-0.2.1-py3-none-any.whl
- Upload date:
- Size: 9.2 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 |
dfb5619f6f24268a8357968bc0913b62afe947da90ccacaea2c2e71d64dada1b
|
|
| MD5 |
318d7c6a4d3f3aa69da9e5624cadccdc
|
|
| BLAKE2b-256 |
0d2cd6d6ec6f9ad08f84f829e84499ffacb6082fce02e441a100e5680b06049a
|