Package: patricia-trie
A pure Python 2.7+ implementation of a PATRICIA trie for effcient matching
of string collections on text.Note that you probably first want to have a look at the Python wrappermarisa-trieor itsPyPi packagebefore using particia-trie; according to simple timeit comparisons, these
wrappers for the C-based MARISA library are about twice as fast as this pure
Python implementation.patricia-triedoes have its merits, however - it is small, clear, and
has a very clean interface that imitates thedictAPI and works with Py3k.Installationpip install patricia-trieUsage>>> T = trie('root', key='value', king='kong') # a root value and two pairs
>>> T['four'] = None # setting new values as in a dict
>>> '' in T # check if the value exits (note: the [empty] root is '')
True
>>> 'kong' in T # existence checks as in a dict
False
>>> T['king'] # get the value for an exact key ... as in a dict
'kong'
>>> T['kong'] # error from non-existing keys (as in a dict)
Traceback (most recent call last):
    ...
KeyError: 'kong'
>>> len(T) # count keys ("terminals") in the tree
4
>>> sorted(T) # plus "traditional stuff": .keys(), .values(), and .items()
['', 'four', 'key', 'king']
>>> # scanning a string S with key(S), value(S), and item(S):
>>> S = 'keys and kewl stuff'
>>> T.key(S) # report the (longest) key that is a prefix of S
'key'
>>> T.value(S, 9) # using offsets; NB: a root value always matches!
'root'
>>> del T[''] # interlude: deleting keys (here, the root)
>>> T.item(S, 9) # raise error if no key is a prefix of S
Traceback (most recent call last):
    ...
KeyError: 'k'
>>> # info: the error string above contains the matched path so far
>>> T.item(S, 9, default=None) # avoid the error by specifying a default
(None, None)
>>> # iterate all matching content with keys(S), values(S), and items(S):
>>> list(T.items(S))
[('key', 'value')]
>>> T.isPrefix('k') # reverse lookup: check if S is a prefix of any key
True
>>> T.isPrefix('kong')
False
>>> sorted(T.iter('k')) # and get all keys that have S as prefix
['key', 'king']Deletingentries is a “half-supported” operation only. The key appears
“removed”, but the trie is not actually changed, only the node state is
changed from terminal to non-terminal. I.e., if you frequently delete keys,
the compaction will become fragmented and less efficient. To mitigate this
effect, make a copy of the trie (using a copy constructor idiom):T = trie(**T)If you are only interested in scanning for thepresenceof keys, but do not
care about mapping a value to each key, usingNoneas the value of your
keys and scanning withkey(S, None, start=i)at every offsetiin the
stringSis perfectly fine (because the return value will be the key
string iff a full match was made andNoneotherwise):>>> T = trie(present=None)
>>> T.key('is absent here', None, start=3) # start scanning at offset 3
>>> T.key('is present here', None, start=3) # start scanning at offset 3
'present'APItrie(*value,**branch)Create a new tree node.Any arguments will be used as thevalueof this node.If keyword arguments are given, they initialize a wholebranch.Note thatNoneis a valid value for a node.trie.isPrefix(prefix)Return True if any key starts withprefix.trie.item(string,start=0,end=None,default=NULL)Return the key, value pair of the longest key that is a prefix ofstring(beginning atstartand ending atend).If no key matches, raise aKeyErroror return theNone,defaultpair if anydefaultvalue was set.trie.items([string[,start[,end]]])Return all key, value pairs (for keys that are a prefix ofstring(beginning atstart(and terminating beforeend))).trie.iter(prefix)Return an iterator over all keys that start withprefix.trie.key(string,start=0,end=None,default=NULL)Return the longest key that is a prefix ofstring(beginning atstartand ending atend).If no key matches, raise aKeyErroror return thedefaultvalue if it was set.trie.keys([string[,start[,end]]])Return all keys (that are a prefix ofstring(beginning atstart(and terminating beforeend))).trie.value(string,start=0,end=None,default=NULL)Return the value of the longest key that is a prefix ofstring(beginning atstartand ending atend).If no key matches, raise aKeyErroror return thedefaultvalue if it was set.trie.values([string[,start[,end]]])Return all values (for keys that are a prefix ofstring(beginning atstart(and terminating beforeend))).HistoryInitial release.Update: Full documentation and corrections.Feature: optional keyword parameters to indicate an offsetstartwhen
scanning a string with the methods key(), keys(), item(), items(), value(),
and values(), so it is not necessary to slice strings for each scan:>>> # Old usage to scan 'string' in 'the string' was:
>>> T.keys('the string'[4:])
>>> # With the new optional keyword parameter:
>>> T.keys('the string', start=4)Important API change: item() now returns key, value pairs even when a
default value is given, usingNoneas the “key”:>>> # Old behaviour was:
>>> T.item('string', default=False)
False
>>> # While now, the same call produces:
>>> T.item('string', default=False)
None, FalseImprovement: Switched from using dictionaries to two-tuple lists
internally (thanks to Pedro Gaio for the suggestion!) to improve the
overall performance a bit (about 20% faster on simple tests).Bugfix: When splitting edges while adding a new key that is shorter than
the current edge, a index error would have occurred.Feature: Added optional keyword parameterendto the methods key(),
keys(), item(), items(), value(), and values(), so it is not necessary to
scan within a window:T.key('string', start=2, end=3, default=None)
T.keys('string', start=2, end=3)Improvement: Switched back to a very efficient internal dictionary
implementation; Runs about two- to three times as fast as the two-tuple
list from update 4 against the simple (and newly added)time_patricia.py“benchmark”.Bugfix: Correct behavior when using a negative start index.
Added a comparison tomarisa-trie- by now, it seems, patricia-trie
is roughly only a factor two slower than the marisa-trie PyPI version
wrapping a C library. Also makes it nice to compare the two usages.Bugfix(15/09/2014): Correct behaviour when using an exactly matching
prefix as query (issue described in #1 by @zachrahan). Also fixes
code-smells (PEP8, code complexity) and a failing test case code.Bugfix(14/12/2014): Added the missing README to PyPI package.
(MANIFEST.in)CopyrightCopyright 2013, Florian Leitner. All rights reserved.LicenseApache License v2