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