Skip to main content

A python package that uses a compressed prefix tree (aka trie or radix tree) to store a dictionary of 'correct' words and provides suggestions to complete partial words.

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((("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 radix
>>> r = radix.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.0.1.tar.gz (7.9 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.0.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for patrix-0.0.1.tar.gz
Algorithm Hash digest
SHA256 502d55014f8c181989a7f0d0740e4a4bf50388b2ee0fcecf1589a531d5c8e8f5
MD5 1a69c18cbf67094cd4948633e3b68f52
BLAKE2b-256 ca1654ea88cfc1f1b3df814b60de052880b800731131312c3b13816124c78359

See more details on using hashes here.

File details

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

File metadata

  • Download URL: patrix-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 194c7912fefdf497732850c0ff0cfad04fe224faaec13db53d853345b2f46382
MD5 a6493323115858466f13030955057688
BLAKE2b-256 abae4ea81e27c466fc0b4b42ca7935bf5c46161e6a2a07ff0ee28740534b6e1f

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