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