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