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