xref: /aosp_15_r20/external/marisa-trie/bindings/perl/sample.pl (revision ab8db090fce404b23716c4c9194221ee27efe31c)
1use marisa;
2
3$keyset = new marisa::Keyset;
4$keyset->push_back("cake");
5$keyset->push_back("cookie");
6$keyset->push_back("ice");
7$keyset->push_back("ice-cream");
8
9$trie = new marisa::Trie;
10$trie->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
16$agent = new marisa::Agent;
17
18$agent->set_query("cake");
19$trie->lookup($agent);
20print($agent->query_str(), ": ", $agent->key_id(), "\n");
21
22$agent->set_query("cookie");
23$trie->lookup($agent);
24print($agent->query_str(), ": ", $agent->key_id(), "\n");
25
26$agent->set_query("cockoo");
27if ($trie->lookup(agent)) {
28  print($agent->query_str(), ": not found\n");
29}
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");
35}
36
37$trie->save("sample.dic");
38$trie->load("sample.dic");
39
40$agent->set_query(0);
41$trie->reverse_lookup($agent);
42print($agent->query_id(), ": ", $agent->key_str(), "\n");
43$agent->set_query(1);
44$trie->reverse_lookup($agent);
45print($agent->query_id(), ": ", $agent->key_str(), "\n");
46
47print("2: ", $trie->reverse_lookup(2), "\n");
48print("3: ", $trie->reverse_lookup(3), "\n");
49
50$trie->mmap("sample.dic");
51
52$agent->set_query("ice-cream soda");
53while ($trie->common_prefix_search($agent)) {
54  print($agent->query_str(), ": ", $agent->key_str(), " (",
55        $agent->key_id(), ")\n");
56}
57
58$agent->set_query("ic");
59while ($trie->predictive_search($agent)) {
60  print($agent->query_str(), ": ", $agent->key_str(), " (",
61        $agent->key_id(), ")\n");
62}
63