Skip to main content
File added late

1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.

WordTrie

Example

Create a new trie:

from wordtrie import WordTrie
trie = WordTrie()

Words and their values are added to the trie with the add() method:

trie.add("She", 1)
trie.add("sea", 2)

Exact matches in the trie are found with the match() method:

print(trie.match("She"))
# 1
print(trie.match("sells"))
# None
print(trie.match("She sells"))
# None

All matches in a stream of words can be found with the search() method:

print(trie.search("She sells sea shells by the sea shore."))
# [1, 2, 2]

Phrases can be added too and will be split into a list of words:

trie.add("sea shells", 3)
# same as trie.add(["sea", "shells"], 3)

Matching is greedy and will match the maximal length phrase:

print(trie.match("sea shells"))
# 3
print(trie.search("She sells sea shells by the sea shore."))
# [1, 3, 2]

In addition to the values, you can return the trie nodes that were matched with return_nodes=True:

print(trie.search("She sells sea shells by the sea shore.", return_nodes=True))
# [(['She'], 1), (['sea', 'shells'], 3), (['sea'], 2)]

The trie can be written to a JSON file with:

trie.to_json("sea.json")
# {
#   "She": {
#     "#": 1
#   },
#   "sea": {
#     "#": 2,
#     "shells": {
#       "#": 3
#     }
#   }
# }

Or restored from a JSON file with:

trie.from_json("sea.json")
print(trie.match("sea"))
# 2

The reserved key # is used to store the value in the JSON structure. You can still add a word that starts with # to the trie, and it will be protected with an additional prepended #:

trie.add("#She", 4)
trie.to_json("sea.json")
# {
#    "##She": {
# ...
print(trie.match("#She"))
# 4

When a node does not yet exist in the trie, the value specified in the add() method is used as the initial value. If the node already exists, then an aggregator function can be called to modify the value based on the old and new values. The default aggregator is to replace the old value with the new value. However, a custom aggregator can be defined as a function with signature aggregator(old, new) and passed to the add() call:

def sum_aggregator(old, new):
    return old + new
trie.add("She", 100, aggregator=sum_aggregator)
print(trie.search("She sells sea shells by the sea shore."))
# [101, 3, 2]

Testing

Run the example above as a basic regression test with:

# grep "^    " README.md | sed 's/    //' | python

Metadata

Release files for wordtrie 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

File added late

1 file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release files before installing.

Source distribution (sdist)

Source distribution for wordtrie 0.0.4
File Size Uploaded
wordtrie-0.0.4.zip 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wordtrie 0.0.4
File Interpreter ABI Platform
wordtrie-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / wordtrie-0.0.4.zip

Download URL wordtrie-0.0.4.zip
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
252538279b8a552f223234e5946be585794a62eb0fceac5d3790df2c3b39c43d
BLAKE2b-256 checksum
How to use checksums
5ad20dfe2f2ce432d0726eec1dfafa9a3fed93859752006692ff7aa6aaf54730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.10

Release files / wordtrie-0.0.4-py3-none-any.whl

File added late

This file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release file before installing.

Download URL wordtrie-0.0.4-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2d0dbae051bcafe12f42f6e176f17ca27a3b12c23f0c3af9eb5766407f3947e
BLAKE2b-256 checksum
How to use checksums
48b7a7305c755b56f558d6fa7466f0742bf5f0a7be0c0fcae629fb2d14b237b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page