xref: /aosp_15_r20/external/marisa-trie/bindings/python/sample.py (revision ab8db090fce404b23716c4c9194221ee27efe31c)
1import marisa
2
3keyset = marisa.Keyset()
4keyset.push_back("cake")
5keyset.push_back("cookie")
6keyset.push_back("ice")
7keyset.push_back("ice-cream")
8
9trie = marisa.Trie()
10trie.build(keyset)
11print("no. keys: %d" % trie.num_keys())
12print("no. tries: %d" % trie.num_tries())
13print("no. nodes: %d" % trie.num_nodes())
14print("size: %d" % trie.io_size())
15
16agent = marisa.Agent()
17
18agent.set_query("cake")
19trie.lookup(agent)
20print("%s: %d" % (agent.query_str(), agent.key_id()))
21
22agent.set_query("cookie")
23trie.lookup(agent)
24print("%s: %d" % (agent.query_str(), agent.key_id()))
25
26agent.set_query("cockoo")
27if not trie.lookup(agent):
28  print("%s: not found" % agent.query_str())
29
30print("ice: %d" % trie.lookup("ice"))
31print("ice-cream: %d" % trie.lookup("ice-cream"))
32if trie.lookup("ice-age") == marisa.INVALID_KEY_ID:
33  print("ice-age: not found")
34
35trie.save("sample.dic")
36trie.load("sample.dic")
37
38agent.set_query(0)
39trie.reverse_lookup(agent)
40print("%d: %s" % (agent.query_id(), agent.key_str()))
41
42agent.set_query(1)
43trie.reverse_lookup(agent)
44print("%d: %s" % (agent.query_id(), agent.key_str()))
45
46print("2: %s" % trie.reverse_lookup(2))
47print("3: %s" % trie.reverse_lookup(3))
48
49trie.mmap("sample.dic")
50
51agent.set_query("ice-cream soda")
52while trie.common_prefix_search(agent):
53  print("%s: %s (%d)" % (agent.query_str(), agent.key_str(), agent.key_id()))
54
55agent.set_query("ic")
56while trie.predictive_search(agent):
57  print("%s: %s (%d)" % (agent.query_str(), agent.key_str(), agent.key_id()))
58