• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1require "marisa"
2
3keyset = Marisa::Keyset.new
4keyset.push_back("cake")
5keyset.push_back("cookie")
6keyset.push_back("ice")
7keyset.push_back("ice-cream")
8
9trie = Marisa::Trie.new
10trie.build(keyset)
11print("no. keys: ", trie.num_keys(), "\n")
12print("no. tries: ", trie.num_tries(), "\n")
13print("no. nodes: ", trie.num_nodes(), "\n")
14print("size: ", trie.io_size(), "\n")
15
16agent = Marisa::Agent.new
17
18agent.set_query("cake")
19trie.lookup(agent)
20print(agent.query_str(), ": ", agent.key_id(), "\n")
21
22agent.set_query("cookie")
23trie.lookup(agent)
24print(agent.query_str(), ": ", agent.key_id(), "\n")
25
26agent.set_query("cockoo")
27if not trie.lookup(agent)
28  print(agent.query_str(), ": not found\n")
29end
30
31print("ice: ", trie.lookup("ice"), "\n")
32print("ice-cream: ", trie.lookup("ice-cream"), "\n")
33if trie.lookup("ice-age") == Marisa::INVALID_KEY_ID
34  print("ice-age: not found\n")
35end
36
37trie.save("sample.dic")
38trie.load("sample.dic")
39
40agent.set_query(0)
41trie.reverse_lookup(agent)
42print(agent.query_id(), ": ", agent.key_str(), "\n")
43
44agent.set_query(1)
45trie.reverse_lookup(agent)
46print(agent.query_id(), ": ", agent.key_str(), "\n")
47
48print("2: ", trie.reverse_lookup(2), "\n")
49print("3: ", trie.reverse_lookup(3), "\n")
50
51trie.mmap("sample.dic")
52
53agent.set_query("ice-cream soda")
54while trie.common_prefix_search(agent)
55  print(agent.query_str(), ": ", agent.key_str(), " (", agent.key_id(), ")\n")
56end
57
58agent.set_query("ic")
59while trie.predictive_search(agent)
60  print(agent.query_str(), ": ", agent.key_str(), " (", agent.key_id(), ")\n")
61end
62