collection.trie

Module Contents

Classes

Trie

Implements a trie data structure.

class collection.trie.Trie(word_list: Iterable[str] | None = None, use_suffix: bool = False, match_delimiter: str = '')

Implements a trie data structure.

This structure can be used to do the following:

  • check if a word match a suffix in a list of suffixes

  • check if a word match a prefix in a list of prefixes

  • check if a word is in a list of words

This is more efficient than iterating other sets or than using a simple regexp that concatenates the list of words/prefixes/suffixes

END_MARKER = ''
add(word: str) None

Add a word to the trie.

Parameters:

word – the word to add

contains(word: str) bool

Check whether word is in the trie.

Parameters:

word – a word

Returns:

True if the word is in the trie, False otherwise

__contains__(word: str) bool
match(word: str, delimiter: str | None = None) bool

Check if there is word in the trie which is a prefix/suffix of word.

Parameters:
  • word – the word to check

  • delimiter – if None use the default delimiter. If delimiter is ‘’ then the function returns True whenever a word in the trie is a prefix or suffix of word. If delimiter is a non empty string then the function returns True if there is a word W in the trie such as W is equal to word or if W is a prefix/suffix of word and that the next/previous character in word is in delimiter.

Returns:

True if matched, False otherwise