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