First version of dictionary lookup with regular expressions and errors.
[idzebra-moved-to-github.git] / dict / dicttest.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dicttest.c,v $
7  * Revision 1.12  1994-10-03 17:23:03  adam
8  * First version of dictionary lookup with regular expressions and errors.
9  *
10  * Revision 1.11  1994/09/28  13:07:09  adam
11  * Use log_mask_str now.
12  *
13  * Revision 1.10  1994/09/26  10:17:24  adam
14  * Minor changes.
15  *
16  * Revision 1.9  1994/09/22  14:43:56  adam
17  * First functional version of lookup with error correction. A 'range'
18  * specified the maximum number of insertions+deletions+substitutions.
19  *
20  * Revision 1.8  1994/09/22  10:43:44  adam
21  * Two versions of depend. Type 1 is the tail-type compatible with
22  * all make programs. Type 2 is the GNU make with include facility.
23  * Type 2 is default. depend rule chooses current rule.
24  *
25  * Revision 1.7  1994/09/19  16:34:26  adam
26  * Depend rule change. Minor changes in dicttest.c
27  *
28  * Revision 1.6  1994/09/16  15:39:12  adam
29  * Initial code of lookup - not tested yet.
30  *
31  * Revision 1.5  1994/09/06  13:05:14  adam
32  * Further development of insertion. Some special cases are
33  * not properly handled yet! assert(0) are put here. The
34  * binary search in each page definitely reduce usr CPU.
35  *
36  * Revision 1.4  1994/09/01  17:49:37  adam
37  * Removed stupid line. Work on insertion in dictionary. Not finished yet.
38  *
39  * Revision 1.3  1994/09/01  17:44:06  adam
40  * depend include change.
41  *
42  * Revision 1.2  1994/08/18  12:40:54  adam
43  * Some development of dictionary. Not finished at all!
44  *
45  * Revision 1.1  1994/08/16  16:26:47  adam
46  * Added dict.
47  *
48  */
49
50 #include <stdlib.h>
51 #include <string.h>
52 #include <stdio.h>
53 #include <ctype.h>
54
55 #include <dict.h>
56
57 char *prog;
58 static Dict dict;
59
60 static int look_hits;
61
62 static int lookup_handle (Dict_char *name)
63 {
64     look_hits++;
65     printf ("%s\n", name);
66     return 0;
67 }
68
69 static int grep_handle (Dict_char *name, char *info)
70 {
71     look_hits++;
72     printf ("%s\n", name);
73     return 0;
74 }
75
76 int main (int argc, char **argv)
77 {
78     const char *name = NULL;
79     const char *inputfile = NULL;
80     const char *base = NULL;
81     int range = -1;
82     int rw = 0;
83     int infosize = 4;
84     int cache = 10;
85     int ret;
86     int unique = 0;
87     char *grep_pattern = NULL;
88     char *arg;
89     int no_of_iterations = 0;
90     int no_of_new = 0, no_of_same = 0, no_of_change = 0;
91     int no_of_hits = 0, no_of_misses = 0;
92
93     
94     prog = argv[0];
95     if (argc < 2)
96     {
97         fprintf (stderr, "usage:\n "
98                  " %s [-r n] [-u] [-g pat] [-s n] [-v n] [-i f] [-w] [-c n]"
99                  " base file\n",
100                  prog);
101         exit (1);
102     }
103     while ((ret = options ("r:ug:s:v:i:wc:", argv, argc, &arg)) != -2)
104     {
105         if (ret == 0)
106         {
107             if (!base)
108                 base = arg;
109             else if (!name)
110                 name = arg;
111             else
112             {
113                 log (LOG_FATAL, "too many files specified\n");
114                 exit (1);
115             }
116         }
117         else if (ret == 'g')
118         {
119             grep_pattern = arg;
120         }
121         else if (ret == 'r')
122         {
123             range = atoi (arg);
124         }
125         else if (ret == 'u')
126         {
127             unique = 1;
128         }
129         else if (ret == 'c')
130         {
131             cache = atoi(arg);
132             if (cache<2)
133                 cache = 2;
134         }
135         else if (ret == 'w')
136             rw = 1;
137         else if (ret == 'i')
138             inputfile = arg;
139         else if (ret == 's')
140         {
141             infosize = atoi(arg);
142         }
143         else if (ret == 'v')
144         {
145             log_init (log_mask_str(arg), prog, NULL);
146         }
147         else
148         {
149             log (LOG_FATAL, "unknown option");
150             exit (1);
151         }
152     }
153     if (!base || !name)
154     {
155         log (LOG_FATAL, "no base and/or dictionary specified");
156         exit (1);
157     }
158     common_resource = res_open (base);
159     if (!common_resource)
160     {
161         log (LOG_FATAL, "cannot open resource `%s'", base);
162         exit (1);
163     }
164     dict = dict_open (name, cache, rw);
165     if (!dict)
166     {
167         log (LOG_FATAL, "dict_open fail of `%s'", name);
168         exit (1);
169     }
170     if (inputfile)
171     {
172         FILE *ipf;
173         char ipf_buf[1024];
174         int line = 1;
175         char infobytes[120];
176         memset (infobytes, 0, 120);
177
178         if (!(ipf = fopen(inputfile, "r")))
179         {
180             log (LOG_FATAL|LOG_ERRNO, "cannot open %s", inputfile);
181             exit (1);
182         }
183         
184         while (fgets (ipf_buf, 1023, ipf))
185         {
186             char *ipf_ptr = ipf_buf;
187             sprintf (infobytes, "%d", line);
188             for (;*ipf_ptr && *ipf_ptr != '\n';ipf_ptr++)
189             {
190                 if (isalpha(*ipf_ptr) || *ipf_ptr == '_')
191                 {
192                     int i = 1;
193                     while (ipf_ptr[i] && (isalnum(ipf_ptr[i]) ||
194                                           ipf_ptr[i] == '_'))
195                         i++;
196                     if (ipf_ptr[i])
197                         ipf_ptr[i++] = '\0';
198                     if (rw)
199                     {
200                         switch(dict_insert (dict, ipf_ptr,
201                                             infosize, infobytes))
202                         {
203                         case 0:
204                             no_of_new++;
205                             break;
206                         case 1:
207                             no_of_change++;
208                         if (unique)
209                             log (LOG_LOG, "%s change\n", ipf_ptr);
210                             break;
211                         case 2:
212                             if (unique)
213                                 log (LOG_LOG, "%s duplicate\n", ipf_ptr);
214                             no_of_same++;
215                             break;
216                         }
217                     }
218                     else if(range < 0)
219                     {
220                         char *cp;
221
222                         cp = dict_lookup (dict, ipf_ptr);
223                         if (cp && *cp)
224                             no_of_hits++;
225                         else
226                             no_of_misses++;
227                     }
228                     else
229                     {
230                         look_hits = 0;
231                         dict_lookup_ec (dict, ipf_ptr, range, lookup_handle);
232                         if (look_hits)
233                             no_of_hits++;
234                         else
235                             no_of_misses++;
236                     }
237                     ++no_of_iterations;
238                     ipf_ptr += (i-1);
239                 }
240             }
241             ++line;
242         }
243         fclose (ipf);
244     }
245     if (rw)
246     {
247         log (LOG_LOG, "Insertions.... %d", no_of_iterations);
248         log (LOG_LOG, "No of new..... %d", no_of_new);
249         log (LOG_LOG, "No of change.. %d", no_of_change);
250         log (LOG_LOG, "No of same.... %d", no_of_same);
251     }
252     else
253     {
254         log (LOG_LOG, "Lookups....... %d", no_of_iterations);
255         log (LOG_LOG, "No of hits.... %d", no_of_hits);
256         log (LOG_LOG, "No of misses.. %d", no_of_misses);
257     }
258     if (grep_pattern)
259     {
260         if (range < 0)
261             range = 0;
262         log (LOG_LOG, "Grepping '%s'", grep_pattern);
263         dict_lookup_grep (dict, grep_pattern, range, grep_handle);
264     }
265     dict_close (dict);
266     res_close (common_resource);
267     return 0;
268 }