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", 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.1.tar.gz (7.1 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.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: patrix-0.1.1.tar.gz
  • Upload date:
  • Size: 7.1 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.1.tar.gz
Algorithm Hash digest
SHA256 4cf1bec48f4b981e5af299b935e31a07cc574d896bdc55996c8fa37fdba4948a
MD5 5fb1c3730f3d1666368ba53f0e1b1a3c
BLAKE2b-256 14f7bde11391fdde7698cb9737acb568c65f3675dfe4743957a903d1bd9e296b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: patrix-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e593d9710189db85a14fcd4ee11fe878ebd6a55bb27fa3ea7b6dcee68b781a12
MD5 60eeb9a242b3c6fd1509421e8795ec69
BLAKE2b-256 acfc7c6b95bf6e2a8a40b7be9e3a24d12969c8a0588b783a951a14bd6fab1903

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