2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.25 2000-12-05 09:59:10 adam
8 * Work on dict_delete_subtree.
10 * Revision 1.24 2000/09/05 14:04:05 adam
11 * Updates for prefix 'yaz_' for YAZ log functions.
13 * Revision 1.23 2000/07/07 12:49:20 adam
14 * Optimized resultSetInsert{Rank,Sort}.
16 * Revision 1.22 1999/02/02 14:50:19 adam
17 * Updated WIN32 code specific sections. Changed header.
19 * Revision 1.21 1996/10/29 14:00:03 adam
20 * Page size given by DICT_DEFAULT_PAGESIZE in dict.h.
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.
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.
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
35 * Revision 1.17 1995/12/06 17:48:30 adam
36 * Bug fix: delete didn't work.
38 * Revision 1.16 1995/10/09 16:18:31 adam
39 * Function dict_lookup_grep got extra client data parameter.
41 * Revision 1.15 1995/09/04 12:33:31 adam
42 * Various cleanup. YAZ util used instead.
44 * Revision 1.14 1994/10/04 17:46:55 adam
45 * Function options now returns arg with error option.
47 * Revision 1.13 1994/10/04 12:08:05 adam
48 * Some bug fixes and some optimizations.
50 * Revision 1.12 1994/10/03 17:23:03 adam
51 * First version of dictionary lookup with regular expressions and errors.
53 * Revision 1.11 1994/09/28 13:07:09 adam
54 * Use log_mask_str now.
56 * Revision 1.10 1994/09/26 10:17:24 adam
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.
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.
68 * Revision 1.7 1994/09/19 16:34:26 adam
69 * Depend rule change. Minor changes in dicttest.c
71 * Revision 1.6 1994/09/16 15:39:12 adam
72 * Initial code of lookup - not tested yet.
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.
79 * Revision 1.4 1994/09/01 17:49:37 adam
80 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
82 * Revision 1.3 1994/09/01 17:44:06 adam
83 * depend include change.
85 * Revision 1.2 1994/08/18 12:40:54 adam
86 * Some development of dictionary. Not finished at all!
88 * Revision 1.1 1994/08/16 16:26:47 adam
104 static int look_hits;
106 static int grep_handler (char *name, const char *info, void *client)
109 printf ("%s\n", name);
113 static int scan_handler (char *name, const char *info, int pos, void *client)
115 printf ("%s\n", name);
119 int main (int argc, char **argv)
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;
136 char *grep_pattern = NULL;
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;
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",
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");
164 while ((ret = options ("D:Sdr:p:ug:s:v:i:wc:", argv, argc, &arg)) != -2)
174 logf (LOG_FATAL, "too many files specified\n");
214 infosize = atoi(arg);
218 yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
222 logf (LOG_FATAL, "Unknown option '-%s'", arg);
226 if (!config || !name)
228 logf (LOG_FATAL, "no config and/or dictionary specified");
231 my_resource = res_open (config);
234 logf (LOG_FATAL, "cannot open resource `%s'", config);
237 bfs = bfs_create (res_get(my_resource, "register"));
240 logf (LOG_FATAL, "bfs_create fail");
243 dict = dict_open (bfs, name, cache, rw, 0);
246 logf (LOG_FATAL, "dict_open fail of `%s'", name);
255 memset (infobytes, 0, 120);
257 if (!(ipf = fopen(inputfile, "r")))
259 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", inputfile);
263 while (fgets (ipf_buf, 1023, ipf))
265 char *ipf_ptr = ipf_buf;
266 sprintf (infobytes, "%d", line);
267 for (;*ipf_ptr && *ipf_ptr != '\n';ipf_ptr++)
269 if (isalpha(*ipf_ptr) || *ipf_ptr == '_')
272 while (ipf_ptr[i] && (isalnum(ipf_ptr[i]) ||
280 switch (dict_delete (dict, ipf_ptr))
289 switch(dict_insert (dict, ipf_ptr,
290 infosize, infobytes))
298 logf (LOG_LOG, "%s change\n", ipf_ptr);
302 logf (LOG_LOG, "%s duplicate\n", ipf_ptr);
311 cp = dict_lookup (dict, ipf_ptr);
320 dict_lookup_grep (dict, ipf_ptr, range, NULL,
321 &max_pos, srange, grep_handler);
328 if ((no_of_iterations % 10000) == 0)
330 printf ("."); fflush(stdout);
339 if (rw && delete_term)
341 logf (LOG_LOG, "dict_delete_subtree %s", delete_term);
342 dict_delete_subtree (dict, delete_term, 0, 0);
348 logf (LOG_LOG, "Grepping '%s'", grep_pattern);
349 dict_lookup_grep (dict, grep_pattern, range, NULL, &max_pos,
350 srange, grep_handler);
354 logf (LOG_LOG, "Iterations.... %d", no_of_iterations);
357 logf (LOG_LOG, "No of deleted. %d", no_of_deleted);
358 logf (LOG_LOG, "No not found.. %d", no_not_found);
362 logf (LOG_LOG, "No of new..... %d", no_of_new);
363 logf (LOG_LOG, "No of change.. %d", no_of_change);
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);
374 char term_dict[1024];
376 int before = 1000000;
378 logf (LOG_LOG, "dict_scan");
381 dict_scan (dict, term_dict, &before, &after, 0, scan_handler);
385 res_close (my_resource);