Integrated ranking algorithm from Liverpool University
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.30 2003-03-26 16:41:48 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24
25 #include <assert.h>
26 #include <ctype.h>
27
28 #include <yaz/yaz-util.h>
29 #include <charmap.h>
30 #include <zebramap.h>
31
32 #define ZEBRA_MAP_TYPE_SORT  1
33 #define ZEBRA_MAP_TYPE_INDEX 2
34
35 #define ZEBRA_REPLACE_ANY  300
36
37 struct zm_token {
38     int *token_from;
39     char *token_to;
40     int token_min;
41     struct zm_token *next;
42 };
43
44 struct zebra_map {
45     unsigned reg_id;
46     int completeness;
47     int positioned;
48     int type;
49     union {
50         struct {
51             int dummy;
52         } index;
53         struct {
54             int entry_size;
55         } sort;
56     } u;
57     chrmaptab maptab;
58     const char *maptab_name;
59     struct zebra_map *next;
60     struct zm_token *replace_tokens;
61 };
62
63 struct zebra_maps {
64     char *tabpath;
65     char *tabroot;
66     NMEM nmem;
67     struct zebra_map *map_list;
68     char temp_map_str[2];
69     const char *temp_map_ptr[2];
70     struct zebra_map **lookup_array;
71     WRBUF wrbuf_1, wrbuf_2;
72 };
73
74 void zebra_maps_close (ZebraMaps zms)
75 {
76     struct zebra_map *zm = zms->map_list;
77     while (zm)
78     {
79         if (zm->maptab)
80             chrmaptab_destroy (zm->maptab);
81         zm = zm->next;
82     }
83     wrbuf_free (zms->wrbuf_1, 1);
84     wrbuf_free (zms->wrbuf_2, 1);
85     nmem_destroy (zms->nmem);
86     xfree (zms);
87 }
88
89 static void zebra_map_read (ZebraMaps zms, const char *name)
90 {
91     FILE *f;
92     char line[512];
93     char *argv[10];
94     int argc;
95     int lineno = 0;
96     struct zebra_map **zm = 0, *zp;
97
98     if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
99     {
100         logf(LOG_WARN|LOG_ERRNO, "%s", name);
101         return ;
102     }
103     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
104     {
105         if (!yaz_matchstr (argv[0], "index") && argc == 2)
106         {
107             if (!zm)
108                 zm = &zms->map_list;
109             else
110                 zm = &(*zm)->next;
111             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
112             (*zm)->reg_id = argv[1][0];
113             (*zm)->maptab_name = NULL;
114             (*zm)->maptab = NULL;
115             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
116             (*zm)->completeness = 0;
117             (*zm)->positioned = 1;
118             (*zm)->replace_tokens = 0;
119         }
120         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
121         {
122             if (!zm)
123                 zm = &zms->map_list;
124             else
125                 zm = &(*zm)->next;
126             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
127             (*zm)->reg_id = argv[1][0];
128             (*zm)->maptab_name = NULL;
129             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
130             (*zm)->u.sort.entry_size = 80;
131             (*zm)->maptab = NULL;
132             (*zm)->completeness = 0;
133             (*zm)->positioned = 0;
134             (*zm)->replace_tokens = 0;
135         }
136         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
137         {
138             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
139         }
140         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
141         {
142             (*zm)->completeness = atoi (argv[1]);
143         }
144         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
145         {
146             (*zm)->positioned = atoi (argv[1]);
147         }
148         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
149         {
150             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
151                 (*zm)->u.sort.entry_size = atoi (argv[1]);
152         }
153         else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
154         {
155             struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
156             token->next = (*zm)->replace_tokens;
157             (*zm)->replace_tokens = token;
158 #if 0
159             logf (LOG_LOG, "replace %s", argv[1]);
160 #endif
161             token->token_from = 0;
162             if (argc >= 2)
163             {
164                 char *cp = argv[1];
165                 int *dp = token->token_from = (int *)
166                     nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
167                 while (*cp)
168                     if (*cp == '$')
169                     {
170                         *dp++ = ' ';
171                         cp++;
172                     }
173                     else if (*cp == '.')
174                     {
175                         *dp++ = ZEBRA_REPLACE_ANY;
176                         cp++;
177                     }
178                     else
179                     {
180                         *dp++ = zebra_prim(&cp);
181 #if 0
182                         logf (LOG_LOG, "  char %2X %c", dp[-1], dp[-1]);
183 #endif
184                     }
185                 *dp = '\0';
186             }
187             if (argc >= 3)
188             {
189                 char *cp = argv[2];
190                 char *dp = token->token_to =
191                     nmem_malloc (zms->nmem, strlen(cp)+1);
192                 while (*cp)
193                     if (*cp == '$')
194                     {
195                         *dp++ = ' ';
196                         cp++;
197                     }
198                     else
199                         *dp++ = zebra_prim(&cp);
200                 *dp = '\0';
201             }
202             else
203                 token->token_to = 0;
204         }
205     }
206     if (zm)
207         (*zm)->next = NULL;
208     yaz_fclose (f);
209
210     for (zp = zms->map_list; zp; zp = zp->next)
211         zms->lookup_array[zp->reg_id] = zp;
212 }
213
214 static void zms_map_handle (void *p, const char *name, const char *value)
215 {
216     ZebraMaps zms = (ZebraMaps) p;
217     
218     zebra_map_read (zms, value);
219 }
220
221 ZebraMaps zebra_maps_open (Res res, const char *base)
222 {
223     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
224     int i;
225
226     zms->nmem = nmem_create ();
227     zms->tabpath = nmem_strdup (zms->nmem,
228                                 res_get_def (res, "profilePath",
229                                              DEFAULT_PROFILE_PATH));
230     zms->tabroot = 0;
231     if (base)
232         zms->tabroot = nmem_strdup (zms->nmem, base);
233     zms->map_list = NULL;
234
235     zms->temp_map_str[0] = '\0';
236     zms->temp_map_str[1] = '\0';
237
238     zms->temp_map_ptr[0] = zms->temp_map_str;
239     zms->temp_map_ptr[1] = NULL;
240
241     zms->lookup_array = (struct zebra_map**)
242         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
243     for (i = 0; i<256; i++)
244         zms->lookup_array[i] = 0;
245     if (!res || !res_trav (res, "index", zms, zms_map_handle))
246         zebra_map_read (zms, "default.idx");
247
248     zms->wrbuf_1 = wrbuf_alloc();
249     zms->wrbuf_2 = wrbuf_alloc();
250
251     return zms;
252 }
253
254 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
255 {
256     return zms->lookup_array[reg_id];
257 }
258
259 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
260 {
261     struct zebra_map *zm = zebra_map_get (zms, reg_id);
262     if (!zm)
263     {
264         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
265         logf (LOG_WARN, "Unknown register type: %c", reg_id);
266
267         zm->reg_id = reg_id;
268         zm->maptab_name = nmem_strdup (zms->nmem, "@");
269         zm->maptab = NULL;
270         zm->type = ZEBRA_MAP_TYPE_INDEX;
271         zm->completeness = 0;
272         zm->next = zms->map_list;
273         zms->map_list = zm->next;
274
275         zms->lookup_array[zm->reg_id & 255] = zm;
276     }
277     if (!zm->maptab)
278     {
279         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
280             return NULL;
281         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
282                                              zm->maptab_name, 0,
283                                              zms->tabroot)))
284             logf(LOG_WARN, "Failed to read character table %s",
285                  zm->maptab_name);
286         else
287             logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
288     }
289     return zm->maptab;
290 }
291
292 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
293                                const char **from, int len)
294 {
295     chrmaptab maptab;
296
297     maptab = zebra_charmap_get (zms, reg_id);
298     if (maptab)
299         return chr_map_input(maptab, from, len);
300     
301     zms->temp_map_str[0] = **from;
302
303     (*from)++;
304     return zms->temp_map_ptr;
305 }
306
307 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
308                               const char **from)
309 {
310     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
311     if (!maptab)
312         return 0;
313     return chr_map_output (maptab, from, 1);
314 }
315
316
317 /* ------------------------------------ */
318
319 typedef struct {
320     int type;
321     int major;
322     int minor;
323     Z_AttributeElement **attributeList;
324     int num_attributes;
325 } AttrType;
326
327 static int attr_find (AttrType *src, oid_value *attributeSetP)
328 {
329     while (src->major < src->num_attributes)
330     {
331         Z_AttributeElement *element;
332
333         element = src->attributeList[src->major];
334         if (src->type == *element->attributeType)
335         {
336             switch (element->which) 
337             {
338             case Z_AttributeValue_numeric:
339                 ++(src->major);
340                 if (element->attributeSet && attributeSetP)
341                 {
342                     oident *attrset;
343
344                     attrset = oid_getentbyoid (element->attributeSet);
345                     *attributeSetP = attrset->value;
346                 }
347                 return *element->value.numeric;
348                 break;
349             case Z_AttributeValue_complex:
350                 if (src->minor >= element->value.complex->num_list ||
351                     element->value.complex->list[src->minor]->which !=  
352                     Z_StringOrNumeric_numeric)
353                     break;
354                 ++(src->minor);
355                 if (element->attributeSet && attributeSetP)
356                 {
357                     oident *attrset;
358
359                     attrset = oid_getentbyoid (element->attributeSet);
360                     *attributeSetP = attrset->value;
361                 }
362                 return *element->value.complex->list[src->minor-1]->u.numeric;
363             default:
364                 assert (0);
365             }
366         }
367         ++(src->major);
368     }
369     return -1;
370 }
371
372 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
373 {
374     src->attributeList = zapt->attributes->attributes;
375     src->num_attributes = zapt->attributes->num_attributes;
376     src->type = type;
377     src->major = 0;
378     src->minor = 0;
379 }
380
381 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
382 {
383     src->attributeList = list->attributes;
384     src->num_attributes = list->num_attributes;
385     src->type = type;
386     src->major = 0;
387     src->minor = 0;
388 }
389
390 /* ------------------------------------ */
391
392 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
393
394     struct zebra_map *zm = zebra_map_get (zms, reg_id);
395     if (zm)
396         return zm->completeness;
397     return 0;
398 }
399
400 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
401 {
402     struct zebra_map *zm = zebra_map_get (zms, reg_id);
403     if (zm)
404         return zm->positioned;
405     return 0;
406 }
407     
408 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
409 {
410     struct zebra_map *zm = zebra_map_get (zms, reg_id);
411     if (zm)
412         return zm->type == ZEBRA_MAP_TYPE_SORT;
413     return 0;
414 }
415
416 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
417                      int *numerical)
418 {
419     AttrType use;
420     AttrType structure;
421     int structure_value;
422     attr_init_AttrList (&use, sortAttributes->list, 1);
423     attr_init_AttrList (&structure, sortAttributes->list, 4);
424
425     *numerical = 0;
426     structure_value = attr_find (&structure, 0);
427     if (structure_value == 109)
428         *numerical = 1;
429     return attr_find (&use, NULL);
430 }
431
432 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
433                      unsigned *reg_id, char **search_type, char *rank_type,
434                      int *complete_flag, int *sort_flag)
435 {
436     AttrType completeness;
437     AttrType structure;
438     AttrType relation;
439     AttrType sort_relation;
440     AttrType weight;
441     AttrType use;
442     int completeness_value;
443     int structure_value;
444     int relation_value;
445     int sort_relation_value;
446     int weight_value;
447     int use_value;
448
449     attr_init_APT (&structure, zapt, 4);
450     attr_init_APT (&completeness, zapt, 6);
451     attr_init_APT (&relation, zapt, 2);
452     attr_init_APT (&sort_relation, zapt, 7);
453     attr_init_APT (&weight, zapt, 9);
454     attr_init_APT (&use, zapt, 1);
455
456     completeness_value = attr_find (&completeness, NULL);
457     structure_value = attr_find (&structure, NULL);
458     relation_value = attr_find (&relation, NULL);
459     sort_relation_value = attr_find (&sort_relation, NULL);
460     weight_value = attr_find (&weight, NULL);
461     use_value = attr_find(&use, NULL);
462
463     if (completeness_value == 2 || completeness_value == 3)
464         *complete_flag = 1;
465     else
466         *complete_flag = 0;
467     *reg_id = 0;
468
469     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
470     *search_type = "phrase";
471     strcpy (rank_type, "void");
472     if (relation_value == 102)
473     {
474         if (weight_value == -1)
475             weight_value = 34;
476         sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
477     }
478     if (relation_value == 103)
479     {
480         *search_type = "always";
481         return 0;
482     }
483     if (*complete_flag)
484         *reg_id = 'p';
485     else
486         *reg_id = 'w';
487     switch (structure_value)
488     {
489     case 6:   /* word list */
490         *search_type = "and-list";
491         break;
492     case 105: /* free-form-text */
493         *search_type = "or-list";
494         break;
495     case 106: /* document-text */
496         *search_type = "or-list";
497         break;  
498     case -1:
499     case 1:   /* phrase */
500     case 2:   /* word */
501     case 108: /* string */ 
502         *search_type = "phrase";
503         break;
504     case 107: /* local-number */
505         *search_type = "local";
506         *reg_id = 0;
507         break;
508     case 109: /* numeric string */
509         *reg_id = 'n';
510         *search_type = "numeric";
511         break;
512     case 104: /* urx */
513         *reg_id = 'u';
514         *search_type = "phrase";
515         break;
516     case 3:   /* key */
517         *reg_id = '0';
518         *search_type = "phrase";
519         break;
520     case 4:  /* year */
521         *reg_id = 'y';
522         *search_type = "phrase";
523         break;
524     case 5:  /* date */
525         *reg_id = 'd';
526         *search_type = "phrase";
527         break;
528     default:
529         return -1;
530     }
531     return 0;
532 }
533
534 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
535                       const char *input_str, int input_len, WRBUF wrbuf);
536
537 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
538                     const char *input_str, int input_len)
539 {
540     struct zebra_map *zm = zebra_map_get (zms, reg_id);
541
542     wrbuf_rewind(zms->wrbuf_1);
543     wrbuf_write(zms->wrbuf_1, input_str, input_len);
544     if (!zm || !zm->replace_tokens)
545         return zms->wrbuf_1;
546   
547 #if 0
548     logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
549           wrbuf_buf(zms->wrbuf_1));
550 #endif
551     for (;;)
552     {
553         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
554                                wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
555             return zms->wrbuf_2;
556         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
557                                wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
558             return zms->wrbuf_1;
559     }
560     return 0;
561 }
562
563 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
564                       const char *input_str, int input_len, WRBUF wrbuf)
565 {
566     int i = -1;
567     int no_replaces = 0;
568     struct zebra_map *zm = zebra_map_get (zms, reg_id);
569
570     wrbuf_rewind(wrbuf);
571     for (i = -1; i <= input_len; )
572     {
573         struct zm_token *token;
574         char replace_string[128];
575         int replace_out;
576         int replace_in = 0;
577
578         for (token = zm->replace_tokens; !replace_in && token;
579              token = token->next)
580         {
581             int j = 0;
582             int replace_done = 0;
583             replace_out = 0;
584             for (;; j++)
585             {
586                 int c;
587                 if (!token->token_from[j])
588                 {
589                     replace_in = j;
590                     break;
591                 }
592                 if (ex_list && strchr (ex_list, token->token_from[j]))
593                     break;
594                 if (i+j < 0 || j+i >= input_len)
595                     c = ' ';
596                 else
597                     c = input_str[j+i] & 255;
598                 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
599                 {
600                     if (c == ' ')
601                         break;
602                     replace_string[replace_out++] = c;
603                 }
604                 else
605                 {
606                     if (c != token->token_from[j])
607                     {
608                         break;
609                     }
610                     if (!replace_done)
611                     {
612                         const char *cp = token->token_to;
613                         replace_done = 1;
614                         for (; cp && *cp; cp++)
615                             replace_string[replace_out++] = *cp;
616                     }
617                 }
618             }
619         }
620         if (!replace_in)
621         {
622             if (i >= 0 && i < input_len)
623                 wrbuf_putc(wrbuf, input_str[i]);
624             i++;
625         }
626         else
627         {
628             no_replaces++;
629             if (replace_out)
630                 wrbuf_write(wrbuf, replace_string, replace_out);
631             i += replace_in;
632         }
633     }
634 #if 0
635     logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));
636 #endif
637     return no_replaces;
638 }