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

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.as_dict()
{'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.as_dict()
{'t': {'r': {'e': {'e': {}}, 'i': {'e': {}, 'o': {}}, 'y': {}}}}

Radix tree example

>>> from patrix import RadixTree
>>> r = RadixTree((("computer", 1), ("compute", 2), ("computing", 3)))
>>> r.as_dict()
{'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

patrix-0.1.2.tar.gz (7.4 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.1.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for patrix-0.1.2.tar.gz
Algorithm Hash digest
SHA256 96c5a148b484dd7457ec6eeaf5c000632f2fb1e1ced4fe2e8248714c5726325a
MD5 06eb0402932599d47592c2032facb706
BLAKE2b-256 45ccaf3e0094070c0992e1276d741a82da507c43663af51a499ce34b90f90f01

See more details on using hashes here.

File details

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

File metadata

  • Download URL: patrix-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.3 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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9ed1f6e61e832992dac0d101e6d3e27dd269f194b66663ffe8ad62aaa17fa6ab
MD5 a5bb9d35dc8fb7aea18d7a49779f1119
BLAKE2b-256 a4f4dc43363d36fbe1d594200dee0d4bbf2f48e3bc3cec9a3bea9a5a8ffdd37a

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