57f1119d7c40eb23818cb0db57de8aaa4a21a214
[idzebra-moved-to-github.git] / dict / dicttest.c
1 /*
2  * Copyright (C) 1994-2000, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dicttest.c,v $
7  * Revision 1.25  2000-12-05 09:59:10  adam
8  * Work on dict_delete_subtree.
9  *
10  * Revision 1.24  2000/09/05 14:04:05  adam
11  * Updates for prefix 'yaz_' for YAZ log functions.
12  *
13  * Revision 1.23  2000/07/07 12:49:20  adam
14  * Optimized resultSetInsert{Rank,Sort}.
15  *
16  * Revision 1.22  1999/02/02 14:50:19  adam
17  * Updated WIN32 code specific sections. Changed header.
18  *
19  * Revision 1.21  1996/10/29 14:00:03  adam
20  * Page size given by DICT_DEFAULT_PAGESIZE in dict.h.
21  *
22  * Revision 1.20  1996/03/20 09:35:16  adam
23  * Function dict_lookup_grep got extra parameter, init_pos, which marks
24  * from which position in pattern approximate pattern matching should occur.
25  *
26  * Revision 1.19  1996/02/02  13:43:50  adam
27  * The public functions simply use char instead of Dict_char to represent
28  * search strings. Dict_char is used internally only.
29  *
30  * Revision 1.18  1996/02/01  20:39:52  adam
31  * Bug fix: insert didn't work on 8-bit characters due to unsigned char
32  * compares in dict_strcmp (strcmp) and signed Dict_char. Dict_char is
33  * unsigned now.
34  *
35  * Revision 1.17  1995/12/06  17:48:30  adam
36  * Bug fix: delete didn't work.
37  *
38  * Revision 1.16  1995/10/09  16:18:31  adam
39  * Function dict_lookup_grep got extra client data parameter.
40  *
41  * Revision 1.15  1995/09/04  12:33:31  adam
42  * Various cleanup. YAZ util used instead.
43  *
44  * Revision 1.14  1994/10/04  17:46:55  adam
45  * Function options now returns arg with error option.
46  *
47  * Revision 1.13  1994/10/04  12:08:05  adam
48  * Some bug fixes and some optimizations.
49  *
50  * Revision 1.12  1994/10/03  17:23:03  adam
51  * First version of dictionary lookup with regular expressions and errors.
52  *
53  * Revision 1.11  1994/09/28  13:07:09  adam
54  * Use log_mask_str now.
55  *
56  * Revision 1.10  1994/09/26  10:17:24  adam
57  * Minor changes.
58  *
59  * Revision 1.9  1994/09/22  14:43:56  adam
60  * First functional version of lookup with error correction. A 'range'
61  * specified the maximum number of insertions+deletions+substitutions.
62  *
63  * Revision 1.8  1994/09/22  10:43:44  adam
64  * Two versions of depend. Type 1 is the tail-type compatible with
65  * all make programs. Type 2 is the GNU make with include facility.
66  * Type 2 is default. depend rule chooses current rule.
67  *
68  * Revision 1.7  1994/09/19  16:34:26  adam
69  * Depend rule change. Minor changes in dicttest.c
70  *
71  * Revision 1.6  1994/09/16  15:39:12  adam
72  * Initial code of lookup - not tested yet.
73  *
74  * Revision 1.5  1994/09/06  13:05:14  adam
75  * Further development of insertion. Some special cases are
76  * not properly handled yet! assert(0) are put here. The
77  * binary search in each page definitely reduce usr CPU.
78  *
79  * Revision 1.4  1994/09/01  17:49:37  adam
80  * Removed stupid line. Work on insertion in dictionary. Not finished yet.
81  *
82  * Revision 1.3  1994/09/01  17:44:06  adam
83  * depend include change.
84  *
85  * Revision 1.2  1994/08/18  12:40:54  adam
86  * Some development of dictionary. Not finished at all!
87  *
88  * Revision 1.1  1994/08/16  16:26:47  adam
89  * Added dict.
90  *
91  */
92
93 #include <stdlib.h>
94 #include <string.h>
95 #include <stdio.h>
96 #include <ctype.h>
97
98 #include <dict.h>
99 #include <zebrautl.h>
100
101 char *prog;
102 static Dict dict;
103
104 static int look_hits;
105
106 static int grep_handler (char *name, const char *info, void *client)
107 {
108     look_hits++;
109     printf ("%s\n", name);
110     return 0;
111 }
112
113 static int scan_handler (char *name, const char *info, int pos, void *client)
114 {
115     printf ("%s\n", name);
116     return 0;
117 }
118
119 int main (int argc, char **argv)
120 {
121     Res my_resource = 0;
122     BFiles bfs;
123     const char *name = NULL;
124     const char *inputfile = NULL;
125     const char *config = NULL;
126     const char *delete_term = NULL;
127     int scan_the_thing = 0;
128     int do_delete = 0;
129     int range = -1;
130     int srange = 0;
131     int rw = 0;
132     int infosize = 4;
133     int cache = 10;
134     int ret;
135     int unique = 0;
136     char *grep_pattern = NULL;
137     char *arg;
138     int no_of_iterations = 0;
139     int no_of_new = 0, no_of_same = 0, no_of_change = 0;
140     int no_of_hits = 0, no_of_misses = 0, no_not_found = 0, no_of_deleted = 0;
141     int max_pos;
142     
143     prog = argv[0];
144     if (argc < 2)
145     {
146         fprintf (stderr, "usage:\n "
147                  " %s [-d] [-D t] [-S] [-r n] [-p n] [-u] [-g pat] [-s n] "
148                  "[-v n] [-i f] [-w] [-c n] config file\n\n",
149                  prog);
150         fprintf (stderr, "  -d      delete instead of insert\n");
151         fprintf (stderr, "  -D t    delete subtree instead of insert\n");
152         fprintf (stderr, "  -r n    set regular match range\n");
153         fprintf (stderr, "  -p n    set regular match start range\n");
154         fprintf (stderr, "  -u      report if keys change during insert\n");
155         fprintf (stderr, "  -g p    try pattern n (see -r)\n");
156         fprintf (stderr, "  -s n    set info size to n (instead of 4)\n");
157         fprintf (stderr, "  -v n    set logging level\n");
158         fprintf (stderr, "  -i f    read file with words\n");
159         fprintf (stderr, "  -w      insert/delete instead of lookup\n");
160         fprintf (stderr, "  -c n    cache size (number of pages)\n");
161         fprintf (stderr, "  -S      scan the dictionary\n");
162         exit (1);
163     }
164     while ((ret = options ("D:Sdr:p:ug:s:v:i:wc:", argv, argc, &arg)) != -2)
165     {
166         if (ret == 0)
167         {
168             if (!config)
169                 config = arg;
170             else if (!name)
171                 name = arg;
172             else
173             {
174                 logf (LOG_FATAL, "too many files specified\n");
175                 exit (1);
176             }
177         }
178         else if (ret == 'D')
179         {
180             delete_term = arg;
181         }
182         else if (ret == 'd')
183             do_delete = 1;
184         else if (ret == 'g')
185         {
186             grep_pattern = arg;
187         }
188         else if (ret == 'r')
189         {
190             range = atoi (arg);
191         }
192         else if (ret == 'p')
193         {
194             srange = atoi (arg);
195         }
196         else if (ret == 'u')
197         {
198             unique = 1;
199         }
200         else if (ret == 'c')
201         {
202             cache = atoi(arg);
203             if (cache<2)
204                 cache = 2;
205         }
206         else if (ret == 'w')
207             rw = 1;
208         else if (ret == 'i')
209             inputfile = arg;
210         else if (ret == 'S')
211             scan_the_thing = 1;
212         else if (ret == 's')
213         {
214             infosize = atoi(arg);
215         }
216         else if (ret == 'v')
217         {
218             yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
219         }
220         else
221         {
222             logf (LOG_FATAL, "Unknown option '-%s'", arg);
223             exit (1);
224         }
225     }
226     if (!config || !name)
227     {
228         logf (LOG_FATAL, "no config and/or dictionary specified");
229         exit (1);
230     }
231     my_resource = res_open (config);
232     if (!my_resource)
233     {
234         logf (LOG_FATAL, "cannot open resource `%s'", config);
235         exit (1);
236     }
237     bfs = bfs_create (res_get(my_resource, "register"));
238     if (!bfs)
239     {
240         logf (LOG_FATAL, "bfs_create fail");
241         exit (1);
242     }
243     dict = dict_open (bfs, name, cache, rw, 0);
244     if (!dict)
245     {
246         logf (LOG_FATAL, "dict_open fail of `%s'", name);
247         exit (1);
248     }
249     if (inputfile)
250     {
251         FILE *ipf;
252         char ipf_buf[1024];
253         int line = 1;
254         char infobytes[120];
255         memset (infobytes, 0, 120);
256
257         if (!(ipf = fopen(inputfile, "r")))
258         {
259             logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", inputfile);
260             exit (1);
261         }
262         
263         while (fgets (ipf_buf, 1023, ipf))
264         {
265             char *ipf_ptr = ipf_buf;
266             sprintf (infobytes, "%d", line);
267             for (;*ipf_ptr && *ipf_ptr != '\n';ipf_ptr++)
268             {
269                 if (isalpha(*ipf_ptr) || *ipf_ptr == '_')
270                 {
271                     int i = 1;
272                     while (ipf_ptr[i] && (isalnum(ipf_ptr[i]) ||
273                                           ipf_ptr[i] == '_'))
274                         i++;
275                     if (ipf_ptr[i])
276                         ipf_ptr[i++] = '\0';
277                     if (rw)
278                     {
279                         if (do_delete)
280                             switch (dict_delete (dict, ipf_ptr))
281                             {
282                             case 0:
283                                 no_not_found++;
284                                 break;
285                             case 1:
286                                 no_of_deleted++;
287                             }
288                         else
289                             switch(dict_insert (dict, ipf_ptr,
290                                                 infosize, infobytes))
291                             {
292                             case 0:
293                                 no_of_new++;
294                                 break;
295                             case 1:
296                                 no_of_change++;
297                                 if (unique)
298                                     logf (LOG_LOG, "%s change\n", ipf_ptr);
299                                 break;
300                             case 2:
301                                 if (unique)
302                                     logf (LOG_LOG, "%s duplicate\n", ipf_ptr);
303                                 no_of_same++;
304                                 break;
305                             }
306                     }
307                     else if(range < 0)
308                     {
309                         char *cp;
310
311                         cp = dict_lookup (dict, ipf_ptr);
312                         if (cp && *cp)
313                             no_of_hits++;
314                         else
315                             no_of_misses++;
316                     }
317                     else
318                     {
319                         look_hits = 0;
320                         dict_lookup_grep (dict, ipf_ptr, range, NULL,
321                                           &max_pos, srange, grep_handler);
322                         if (look_hits)
323                             no_of_hits++;
324                         else
325                             no_of_misses++;
326                     }
327                     ++no_of_iterations;
328                     if ((no_of_iterations % 10000) == 0)
329                     {
330                         printf ("."); fflush(stdout);
331                     }
332                     ipf_ptr += (i-1);
333                 }
334             }
335             ++line;
336         }
337         fclose (ipf);
338     }
339     if (rw && delete_term)
340     {
341         logf (LOG_LOG, "dict_delete_subtree %s", delete_term);
342         dict_delete_subtree (dict, delete_term, 0, 0);
343     }
344     if (grep_pattern)
345     {
346         if (range < 0)
347             range = 0;
348         logf (LOG_LOG, "Grepping '%s'", grep_pattern);
349         dict_lookup_grep (dict, grep_pattern, range, NULL, &max_pos,
350                           srange, grep_handler);
351     }
352     if (rw)
353     {
354         logf (LOG_LOG, "Iterations.... %d", no_of_iterations);            
355         if (do_delete)
356         {
357             logf (LOG_LOG, "No of deleted. %d", no_of_deleted);
358             logf (LOG_LOG, "No not found.. %d", no_not_found);
359         }
360         else
361         {
362             logf (LOG_LOG, "No of new..... %d", no_of_new);
363             logf (LOG_LOG, "No of change.. %d", no_of_change);
364         }
365     }
366     else
367     {
368         logf (LOG_LOG, "Lookups....... %d", no_of_iterations);
369         logf (LOG_LOG, "No of hits.... %d", no_of_hits);
370         logf (LOG_LOG, "No of misses.. %d", no_of_misses);
371     }
372     if (scan_the_thing)
373     {
374         char term_dict[1024];
375         
376         int before = 1000000;
377         int after = 1000000;
378         logf (LOG_LOG, "dict_scan");
379         term_dict[0] = 1;
380         term_dict[1] = 0;
381         dict_scan (dict, term_dict, &before, &after, 0, scan_handler);
382     }
383     dict_close (dict);
384     bfs_destroy (bfs);
385     res_close (my_resource);
386     return 0;
387 }