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
>>> # Entries can be a list of strings or key-value tuples
>>> r = RadixTree(("computer", "compute", ("computing", 1)))
>>> 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.3.tar.gz (8.5 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.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: patrix-0.1.3.tar.gz
  • Upload date:
  • Size: 8.5 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.3.tar.gz
Algorithm Hash digest
SHA256 ea00c90c0f2fbcfe48487c611b0290aac5a3ec342b154c5f7697377cb563455f
MD5 5db85f274fa5e6cc73e402457ad3d976
BLAKE2b-256 aaef21040e806adff8b1fac58b4b6d299c3b8ad9c21a324639594208aceeb28c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: patrix-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.7 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 470c78c6311880f09d45b4bea4ab0a792e07247f040cee58aae101fa100179f3
MD5 f4824c89d626b38577d697ee8749a539
BLAKE2b-256 6ae93487e92ef24e212c0ea3284033c8849378d94f125b14363929920cae3c87

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