:py:mod:`collection.trie` ========================= .. py:module:: collection.trie .. autoapi-nested-parse:: Trie data structure for efficient prefix/suffix matching. Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: collection.trie.Trie .. py:class:: Trie(word_list: collections.abc.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 .. py:attribute:: END_MARKER :value: '' .. py:method:: add(word: str) -> None Add a word to the trie. :param word: the word to add .. py:method:: contains(word: str) -> bool Check whether word is in the trie. :param word: a word :return: True if the word is in the trie, False otherwise .. py:method:: __contains__(word: str) -> bool Check whether word is in the trie. :param word: a word .. py:method:: match(word: str, delimiter: str | None = None) -> bool Check if there is word in the trie which is a prefix/suffix of word. :param word: the word to check :param 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. :return: True if matched, False otherwise