Skip to main content

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

pipeline docs python license

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

patrix-0.2.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

patrix-0.2.2-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file patrix-0.2.2.tar.gz.

File metadata

  • Download URL: patrix-0.2.2.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for patrix-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f190aad86e5d1bdbfc867a7a71c46f598f8c5b0587f05e605d66d0d1b8f81056
MD5 1c4f8662a3506df915fe5c827c69e96b
BLAKE2b-256 d68b5e479bdb7d33d0060ab12a8fea1c0b8e9f1abe416df4dbe78879a2732ac0

See more details on using hashes here.

File details

Details for the file patrix-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: patrix-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for patrix-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 57c59eaa18366f518e96fe0c9ca6b84763fe23d9f6ef5b2a84b16af46c9eee00
MD5 4a19e6418db076cd3f88f2d75fbf1812
BLAKE2b-256 cb444833cad38a347d4c6a755b8be31384e67fa933b3fdaf62d8072c1406feb1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page