view src/ctrie_test.c @ 15:d11dfc49b559

Seems to be working, but I need to prove the algorithms and also make ctrie_remove compress down, too. See the final output where the standalone 'n' node could be pushed into it's children
author Eris Caffee <discordia@eldalin.com>
date Wed, 03 Oct 2012 01:07:04 -0500
parents ef73b96fdcae
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "list.h"
6 #include "ctrie.h"
8 void print_val(char * key, void * data)
9 {
10 printf("%s\t\t%s\n", key, (char *) data);
11 }
13 int main(int argc, char ** argv)
14 {
15 struct ctrie * ctrie = ctrie_new();
17 char * words[] = { "hello", "interjection (used to express a greeting, answer a telephone, or attract attention.)",
18 "help", "to give or provide what is necessary to accomplish a task or satisfy a need.",
19 "helper", "a person or thing that helps or gives assistance, support, etc",
20 "helping", "the act of a person or thing that helps.",
21 "an", "indefinite article. the form of a before an initial vowel sound.",
22 "a", "indefinite article. Used before an initial consonant sound.",
23 "anne", "a female name",
24 "any", "one, a, an, or some; one or more without specification or identification",
25 "anyway", "in any case; anyhow; nonetheless; regardless",
26 "anna", "a female name",
27 "anyhow", "in any way whatever",
28 "anybody", "any person",
29 "Apple", "A computer manufacturer.",
30 "ABLE", "having necessary power, skill, resources, or qualifications",
31 /*
32 */
33 NULL, NULL
34 };
35 int i = 0;
36 while (words[i])
37 {
38 printf("Adding %s\n", words[i]);
39 ctrie_insert(ctrie, words[i], words[i+1]);
40 i += 2;
41 }
43 ctrie_dump(ctrie);
44 ctrie_walk_keys(ctrie, print_val);
45 ctrie_dump_raw(ctrie);
46 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
48 printf("\n\n\n");
49 ctrie_insert(ctrie, "a", "indefinite article. Used before an initial consonant sound.");
50 ctrie_dump_raw(ctrie);
51 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
54 ////////////////////////////////////////////
55 printf("===================================================\n");
56 char word[100];;
57 strcpy(word, "any");
58 printf("searching for %s\n", word);
60 void * data = ctrie_find(ctrie, word);
61 if (NULL == data)
62 printf("Failed to find 'any'\n");
63 else
64 printf("any: %s\n", (char *) data);
67 ////////////////////////////////////////////
68 printf("===================================================\n");
69 strcpy(word, "an");
70 printf("Looking for completions of '%s':\n", word);
71 struct list * list = ctrie_get_subkeys(ctrie, word, 0);
72 if (NULL == list)
73 printf("Nothing found for '%s'\n", word);
74 else
75 {
76 printf("list size is %zd\n", list_size(list));
77 struct list_iterator * it = list_begin(list);
78 struct list_iterator * next = it;
79 while (NULL != next)
80 {
81 printf("%s\n", (char *) list_value(it));
82 next = list_next(it);
83 }
84 ctrie_free_subkey_list(list);
85 }
87 ////////////////////////////////////////////
88 printf("===================================================\n");
89 strcpy(word, "a");
90 printf("Looking for first 5 completions of '%s':\n", word);
91 list = ctrie_get_subkeys(ctrie, word, 5);
92 if (NULL == list)
93 printf("Nothing found for '%s'\n", word);
94 else
95 {
96 printf("list size is %zd\n", list_size(list));
97 struct list_iterator * it = list_begin(list);
98 struct list_iterator * next = it;
99 while (NULL != next)
100 {
101 printf("%s\n", (char *) list_value(it));
102 next = list_next(it);
103 }
104 ctrie_free_subkey_list(list);
105 }
108 ////////////////////////////////////////////
109 printf("===================================================\n");
110 strcpy(word, "he");
111 printf("Looking for completions of '%s':\n", word);
112 list = ctrie_get_subkeys(ctrie, word, 0);
113 if (NULL == list)
114 printf("Nothing found for '%s'\n", word);
115 else
116 {
117 printf("list size is %zd\n", list_size(list));
118 struct list_iterator * it = list_begin(list);
119 struct list_iterator * next = it;
120 while (NULL != next)
121 {
122 printf("%s\n", (char *) list_value(it));
123 next = list_next(it);
124 }
125 ctrie_free_subkey_list(list);
126 }
129 ////////////////////////////////////////////
130 printf("===================================================\n");
131 strcpy(word, "helpe");
132 printf("Looking for completions of '%s':\n", word);
133 list = ctrie_get_subkeys(ctrie, word, 0);
134 if (NULL == list)
135 printf("Nothing found for '%s'\n", word);
136 else
137 {
138 printf("list size is %zd\n", list_size(list));
139 struct list_iterator * it = list_begin(list);
140 struct list_iterator * next = it;
141 while (NULL != next)
142 {
143 printf("%s\n", (char *) list_value(it));
144 next = list_next(it);
145 }
146 ctrie_free_subkey_list(list);
147 }
150 ////////////////////////////////////////////
151 printf("===================================================\n");
152 strcpy(word, "anybody");
153 printf("remove '%s'\n", word);
154 ctrie_remove(ctrie, word);
156 // remove a word that is not in the dictionary, but is a prefix of other words. Should do nothing.
157 strcpy(word, "ann");
158 printf("remove '%s'\n", word);
159 ctrie_remove(ctrie, word);
161 strcpy(word, "help");
162 printf("remove '%s'\n", word);
163 ctrie_remove(ctrie, word);
165 strcpy(word, "a");
166 printf("remove '%s'\n", word);
167 ctrie_remove(ctrie, word);
169 strcpy(word, "an");
170 printf("remove '%s'\n", word);
171 ctrie_remove(ctrie, word);
173 ctrie_dump_raw(ctrie);
174 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
177 exit(EXIT_SUCCESS);
179 ////////////////////////////////////////////
180 printf("===================================================\n");
181 strcpy(word, "anybody");
182 printf("re-inserting '%s'\n", word);
183 ctrie_insert(ctrie, word, "fake definition");
184 ctrie_dump(ctrie);
185 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
187 exit(EXIT_SUCCESS);
188 }