nltk.Trie.as_dict¶
-
Trie.
as_dict
(d=None)[source]¶ Convert
defaultdict
to commondict
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 ofdict
and thus can be converted to a simpledict
usingdict()
, in our case it’s a nesteddefaultdict
, so here’s a quick trick to provide to us thedict
representation of theTrie
withoutdefaultdict(<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 havedict(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