nltk.Trie.as_dict

Trie.as_dict(d=None)[source]

Convert defaultdict to common dict representation.

Param:A defaultdict containing strings mapped to nested defaultdicts. This is the structure of the trie. (Default is None)
Type:defaultdict(str -> defaultdict)
Returns:Even though defaultdict is a subclass of dict and thus can be converted to a simple dict using dict(), in our case it’s a nested defaultdict, so here’s a quick trick to provide to us the dict representation of the Trie without defaultdict(<class 'nltk.util.Trie'>, ...
Return type:dict(str -> dict(bool -> None)) Note: there can be an arbitrarily deeply nested dict(str -> dict(str -> dict(..)), but the last level will have dict(str -> dict(bool -> None))
Example:
>>> from nltk.util import Trie
>>> trie = Trie(["abc", "def"])
>>> expected = {'a': {'b': {'c': {True: None}}}, 'd': {'e': {'f': {True: None}}}}
>>> trie.as_dict() == expected
True