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