Work on character mapping. Implemented replace rules.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /*
2  * Copyright (C) 1994-1999, Index Data 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebramap.c,v $
7  * Revision 1.16  1999-09-07 07:19:21  adam
8  * Work on character mapping. Implemented replace rules.
9  *
10  * Revision 1.15  1999/05/26 07:49:14  adam
11  * C++ compilation.
12  *
13  * Revision 1.14  1999/02/19 10:37:40  adam
14  * Minor fix.
15  *
16  * Revision 1.13  1999/02/18 15:01:04  adam
17  * Structure=key uses register type 0.
18  *
19  * Revision 1.12  1999/02/12 13:29:25  adam
20  * Implemented position-flag for registers.
21  *
22  * Revision 1.11  1998/10/13 20:09:19  adam
23  * Changed call to readconf_line.
24  *
25  * Revision 1.10  1998/06/23 15:33:37  adam
26  * Added feature to specify sort criteria in query (type 7 specifies
27  * sort flags).
28  *
29  * Revision 1.9  1998/04/02 14:35:30  adam
30  * First version of Zebra that works with compiled ASN.1.
31  *
32  * Revision 1.8  1998/03/05 08:42:44  adam
33  * Minor changes to zebramap data structures. Query mapping rules changed.
34  *
35  * Revision 1.7  1998/02/10 12:03:07  adam
36  * Implemented Sort.
37  *
38  * Revision 1.6  1998/01/29 13:36:01  adam
39  * Structure word-list, free-form-text and document-text all
40  * trigger ranked search.
41  *
42  * Revision 1.5  1997/11/19 10:22:14  adam
43  * Bug fix (introduced by previous commit).
44  *
45  * Revision 1.4  1997/11/18 10:05:08  adam
46  * Changed character map facility so that admin can specify character
47  * mapping files for each register type, w, p, etc.
48  *
49  * Revision 1.3  1997/11/17 15:35:26  adam
50  * Bug fix. Relation=relevance wasn't observed.
51  *
52  * Revision 1.2  1997/10/31 12:39:30  adam
53  * Changed log message.
54  *
55  * Revision 1.1  1997/10/27 14:33:06  adam
56  * Moved towards generic character mapping depending on "structure"
57  * field in abstract syntax file. Fixed a few memory leaks. Fixed
58  * bug with negative integers when doing searches with relational
59  * operators.
60  *
61  */
62
63 #include <assert.h>
64 #include <ctype.h>
65
66 #include <yaz-util.h>
67 #include <charmap.h>
68 #include <zebramap.h>
69
70 #define ZEBRA_MAP_TYPE_SORT  1
71 #define ZEBRA_MAP_TYPE_INDEX 2
72
73 struct zm_token {
74     char *token_from;
75     char *token_to;
76     int token_min;
77     struct zm_token *next;
78 };
79
80 struct zebra_map {
81     unsigned reg_id;
82     int completeness;
83     int positioned;
84     int type;
85     union {
86         struct {
87             int dummy;
88         } index;
89         struct {
90             int entry_size;
91         } sort;
92     } u;
93     chrmaptab maptab;
94     const char *maptab_name;
95     struct zebra_map *next;
96     struct zm_token *replace_tokens;
97 };
98
99 struct zebra_maps {
100     char *tabpath;
101     NMEM nmem;
102     struct zebra_map *map_list;
103     char temp_map_str[2];
104     const char *temp_map_ptr[2];
105     struct zebra_map **lookup_array;
106     WRBUF wrbuf_1, wrbuf_2;
107 };
108
109 void zebra_maps_close (ZebraMaps zms)
110 {
111     struct zebra_map *zm = zms->map_list;
112     while (zm)
113     {
114         if (zm->maptab)
115             chrmaptab_destroy (zm->maptab);
116         zm = zm->next;
117     }
118     wrbuf_free (zms->wrbuf_1, 1);
119     wrbuf_free (zms->wrbuf_2, 1);
120     nmem_destroy (zms->nmem);
121     xfree (zms);
122 }
123
124 static void zebra_map_read (ZebraMaps zms, const char *name)
125 {
126     FILE *f;
127     char line[512];
128     char *argv[10];
129     int argc;
130     int lineno = 0;
131     struct zebra_map **zm = 0, *zp;
132
133     if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
134     {
135         logf(LOG_WARN|LOG_ERRNO, "%s", name);
136         return ;
137     }
138     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
139     {
140         if (!yaz_matchstr (argv[0], "index") && argc == 2)
141         {
142             if (!zm)
143                 zm = &zms->map_list;
144             else
145                 zm = &(*zm)->next;
146             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
147             (*zm)->reg_id = argv[1][0];
148             (*zm)->maptab_name = NULL;
149             (*zm)->maptab = NULL;
150             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
151             (*zm)->completeness = 0;
152             (*zm)->positioned = 1;
153             (*zm)->replace_tokens = 0;
154         }
155         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
156         {
157             if (!zm)
158                 zm = &zms->map_list;
159             else
160                 zm = &(*zm)->next;
161             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
162             (*zm)->reg_id = argv[1][0];
163             (*zm)->maptab_name = NULL;
164             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
165             (*zm)->u.sort.entry_size = 80;
166             (*zm)->maptab = NULL;
167             (*zm)->completeness = 0;
168             (*zm)->positioned = 0;
169         }
170         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
171         {
172             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
173         }
174         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
175         {
176             (*zm)->completeness = atoi (argv[1]);
177         }
178         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
179         {
180             (*zm)->positioned = atoi (argv[1]);
181         }
182         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
183         {
184             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
185                 (*zm)->u.sort.entry_size = atoi (argv[1]);
186         }
187         else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
188         {
189             struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
190             char *cp, *dp;
191             token->next = (*zm)->replace_tokens;
192             (*zm)->replace_tokens = token;
193             dp = token->token_from = nmem_strdup (zms->nmem, cp = argv[1]);
194             while (*cp)
195             {
196                 if (*cp == '$')
197                 {
198                     *dp++ = ' ';
199                     cp++;
200                 }
201                 else
202                     *dp++ = zebra_prim(&cp);
203             }
204             *dp = '\0';
205
206             if (argc >= 3)
207             {
208                 dp = token->token_to = nmem_strdup (zms->nmem, cp = argv[2]);
209                 while (*cp)
210                 {
211                     if (*cp == '$')
212                     {
213                         *dp++ = ' ';
214                         cp++;
215                     }
216                     else
217                         *dp++ = zebra_prim(&cp);
218                 }
219                 *dp = '\0';
220             }
221             else
222                 token->token_to = 0;
223         }
224     }
225     if (zm)
226         (*zm)->next = NULL;
227     fclose (f);
228
229     for (zp = zms->map_list; zp; zp = zp->next)
230         zms->lookup_array[zp->reg_id] = zp;
231 }
232
233 static void zms_map_handle (void *p, const char *name, const char *value)
234 {
235     ZebraMaps zms = (ZebraMaps) p;
236     
237     zebra_map_read (zms, value);
238 }
239
240 ZebraMaps zebra_maps_open (Res res)
241 {
242     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
243     int i;
244
245     zms->nmem = nmem_create ();
246     zms->tabpath = nmem_strdup (zms->nmem,
247                                 res_get_def (res, "profilePath", "."));
248     zms->map_list = NULL;
249
250     zms->temp_map_str[0] = '\0';
251     zms->temp_map_str[1] = '\0';
252
253     zms->temp_map_ptr[0] = zms->temp_map_str;
254     zms->temp_map_ptr[1] = NULL;
255
256     zms->lookup_array = (struct zebra_map**)
257         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
258     for (i = 0; i<256; i++)
259         zms->lookup_array[i] = 0;
260     if (!res || !res_trav (res, "index", zms, zms_map_handle))
261         zebra_map_read (zms, "default.idx");
262
263     zms->wrbuf_1 = wrbuf_alloc();
264     zms->wrbuf_2 = wrbuf_alloc();
265     return zms;
266 }
267
268 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
269 {
270     return zms->lookup_array[reg_id];
271 }
272
273 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
274 {
275     struct zebra_map *zm = zebra_map_get (zms, reg_id);
276     if (!zm)
277     {
278         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
279         logf (LOG_WARN, "Unknown register type: %c", reg_id);
280
281         zm->reg_id = reg_id;
282         zm->maptab_name = NULL;
283         zm->maptab = NULL;
284         zm->type = ZEBRA_MAP_TYPE_INDEX;
285         zm->completeness = 0;
286         zm->next = zms->map_list;
287         zms->map_list = zm->next;
288
289         zms->lookup_array[zm->reg_id & 255] = zm;
290     }
291     if (!zm->maptab)
292     {
293         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
294             return NULL;
295         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
296                                              zm->maptab_name, 0)))
297             logf(LOG_WARN, "Failed to read character table %s",
298                  zm->maptab_name);
299         else
300             logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
301     }
302     return zm->maptab;
303 }
304
305 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
306                                const char **from, int len)
307 {
308     chrmaptab maptab;
309
310     maptab = zebra_charmap_get (zms, reg_id);
311     if (maptab)
312         return chr_map_input(maptab, from, len);
313     
314     zms->temp_map_str[0] = **from;
315
316     (*from)++;
317     return zms->temp_map_ptr;
318 }
319
320 #if 0
321 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
322                              const char *input_str, int input_len,
323                              WRBUF wrbuf)
324 {
325     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
326     int len[4];
327     char *str[3];
328     int input_i = 0;
329     int first = 1;
330     const char **out;
331         
332     if (!maptab)
333     {
334         wrbuf_write (wrbuf, input_str, input_len);
335         return -1;
336     }
337     str[0] = " ";
338     len[0] = 1;
339     str[1] = input_str;
340     len[1] = input_len;
341     str[2] = " ";
342     len[2] = 1;
343     len[3] = -1;
344     
345     out = chr_map_input (maptab, str, len);
346     while (len[1] > 0)
347     {
348         while (out && *out && **out == *CHR_SPACE)
349             out = chr_map_input (maptab, str, len);
350     }
351 }
352 #endif
353
354 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
355                               const char **from)
356 {
357     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
358     if (!maptab)
359         return 0;
360     return chr_map_output (maptab, from, 1);
361 }
362
363
364 /* ------------------------------------ */
365
366 typedef struct {
367     int type;
368     int major;
369     int minor;
370     Z_AttributeElement **attributeList;
371     int num_attributes;
372 } AttrType;
373
374 static int attr_find (AttrType *src, oid_value *attributeSetP)
375 {
376     while (src->major < src->num_attributes)
377     {
378         Z_AttributeElement *element;
379
380         element = src->attributeList[src->major];
381         if (src->type == *element->attributeType)
382         {
383             switch (element->which) 
384             {
385             case Z_AttributeValue_numeric:
386                 ++(src->major);
387                 if (element->attributeSet && attributeSetP)
388                 {
389                     oident *attrset;
390
391                     attrset = oid_getentbyoid (element->attributeSet);
392                     *attributeSetP = attrset->value;
393                 }
394                 return *element->value.numeric;
395                 break;
396             case Z_AttributeValue_complex:
397                 if (src->minor >= element->value.complex->num_list ||
398                     element->value.complex->list[src->minor]->which !=  
399                     Z_StringOrNumeric_numeric)
400                     break;
401                 ++(src->minor);
402                 if (element->attributeSet && attributeSetP)
403                 {
404                     oident *attrset;
405
406                     attrset = oid_getentbyoid (element->attributeSet);
407                     *attributeSetP = attrset->value;
408                 }
409                 return *element->value.complex->list[src->minor-1]->u.numeric;
410             default:
411                 assert (0);
412             }
413         }
414         ++(src->major);
415     }
416     return -1;
417 }
418
419 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
420 {
421 #ifdef ASN_COMPILED
422     src->attributeList = zapt->attributes->attributes;
423     src->num_attributes = zapt->attributes->num_attributes;
424 #else
425     src->attributeList = zapt->attributeList;
426     src->num_attributes = zapt->num_attributes;
427 #endif
428     src->type = type;
429     src->major = 0;
430     src->minor = 0;
431 }
432
433 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
434 {
435     src->attributeList = list->attributes;
436     src->num_attributes = list->num_attributes;
437     src->type = type;
438     src->major = 0;
439     src->minor = 0;
440 }
441
442 /* ------------------------------------ */
443
444 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
445
446     struct zebra_map *zm = zebra_map_get (zms, reg_id);
447     if (zm)
448         return zm->completeness;
449     return 0;
450 }
451
452 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
453 {
454     struct zebra_map *zm = zebra_map_get (zms, reg_id);
455     if (zm)
456         return zm->positioned;
457     return 0;
458 }
459     
460 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
461 {
462     struct zebra_map *zm = zebra_map_get (zms, reg_id);
463     if (zm)
464         return zm->type == ZEBRA_MAP_TYPE_SORT;
465     return 0;
466 }
467
468 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
469 {
470     AttrType use;
471     attr_init_AttrList (&use, sortAttributes->list, 1);
472
473     return attr_find (&use, NULL);
474 }
475
476 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
477                      unsigned *reg_id, char **search_type, char **rank_type,
478                      int *complete_flag, int *sort_flag)
479 {
480     AttrType completeness;
481     AttrType structure;
482     AttrType relation;
483     AttrType sort_relation;
484     int completeness_value;
485     int structure_value;
486     int relation_value;
487     int sort_relation_value;
488
489     attr_init_APT (&structure, zapt, 4);
490     attr_init_APT (&completeness, zapt, 6);
491     attr_init_APT (&relation, zapt, 2);
492     attr_init_APT (&sort_relation, zapt, 7);
493
494     completeness_value = attr_find (&completeness, NULL);
495     structure_value = attr_find (&structure, NULL);
496     relation_value = attr_find (&relation, NULL);
497     sort_relation_value = attr_find (&sort_relation, NULL);
498
499     if (completeness_value == 2 || completeness_value == 3)
500         *complete_flag = 1;
501     else
502         *complete_flag = 0;
503     *reg_id = 0;
504
505     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
506     *search_type = "phrase";
507     *rank_type = "void";
508     if (relation_value == 102)
509         *rank_type = "rank";
510     
511     if (*complete_flag)
512         *reg_id = 'p';
513     else
514         *reg_id = 'w';
515     switch (structure_value)
516     {
517     case 6:   /* word list */
518         *search_type = "and-list";
519         break;
520     case 105: /* free-form-text */
521         *search_type = "or-list";
522         break;
523     case 106: /* document-text */
524         *search_type = "or-list";
525         break;  
526     case -1:
527     case 1:   /* phrase */
528     case 2:   /* word */
529     case 108: /* string */ 
530         *search_type = "phrase";
531         break;
532     case 107: /* local-number */
533         *search_type = "local";
534         *reg_id = 0;
535         break;
536     case 109: /* numeric string */
537         *reg_id = 'n';
538         *search_type = "numeric";
539         break;
540     case 104: /* urx */
541         *reg_id = 'u';
542         *search_type = "phrase";
543         break;
544     case 3:   /* key */
545         *reg_id = '0';
546         *search_type = "phrase";
547         break;
548     default:
549         return -1;
550     }
551     return 0;
552 }
553
554 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
555                       const char *input_str, int input_len, WRBUF wrbuf);
556
557 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
558                     const char *input_str, int input_len)
559 {
560     struct zebra_map *zm = zebra_map_get (zms, reg_id);
561
562     wrbuf_rewind(zms->wrbuf_1);
563     wrbuf_write(zms->wrbuf_1, input_str, input_len);
564     if (!zm->replace_tokens)
565         return zms->wrbuf_1;
566    
567 #if 0 
568     logf (LOG_LOG, "zebra_replace");
569     logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
570           wrbuf_buf(zms->wrbuf_1));
571 #endif
572     for (;;)
573     {
574         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
575                                wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
576             return zms->wrbuf_2;
577         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
578                                wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
579             return zms->wrbuf_1;
580     }
581     return 0;
582 }
583
584 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
585                       const char *input_str, int input_len, WRBUF wrbuf)
586 {
587     int i = -1;
588     int no_replaces = 0;
589     struct zebra_map *zm = zebra_map_get (zms, reg_id);
590
591     wrbuf_rewind(wrbuf);
592     for (i = -1; i <= input_len; )
593     {
594         struct zm_token *token;
595         char replace_string[128];
596         int replace_out;
597         int replace_in = 0;
598
599         for (token = zm->replace_tokens; !replace_in && token;
600              token = token->next)
601         {
602             int j = 0;
603             int replace_done = 0;
604             replace_out = 0;
605             for (;; j++)
606             {
607                 int c;
608                 if (!token->token_from[j])
609                 {
610                     replace_in = j;
611                     break;
612                 }
613                 if (ex_list && strchr (ex_list, token->token_from[j]))
614                     break;
615                 if (i+j < 0 || j+i >= input_len)
616                     c = ' ';
617                 else
618                     c = tolower(input_str[j+i]);
619                 if (token->token_from[j] == '.')
620                 {
621                     if (c == ' ')
622                         break;
623                     replace_string[replace_out++] = c;
624                 }
625                 else
626                 {
627                     if (c != token->token_from[j])
628                         break;
629                     if (!replace_done)
630                     {
631                         const char *cp = token->token_to;
632                         replace_done = 1;
633                         for (; cp && *cp; cp++)
634                             replace_string[replace_out++] = *cp;
635                     }
636                 }
637             }
638         }
639         if (!replace_in)
640         {
641             if (i >= 0 && i < input_len)
642                 wrbuf_putc(wrbuf, input_str[i]);
643             i++;
644         }
645         else
646         {
647             no_replaces++;
648             if (replace_out)
649                 wrbuf_write(wrbuf, replace_string, replace_out);
650             i += replace_in;
651         }
652     }
653 #if 0
654     logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));
655 #endif
656     return no_replaces;
657 }