2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 2001-11-14 22:06:27 adam
8 * Rank-weight may be controlled via query.
10 * Revision 1.21 2001/01/22 10:42:56 adam
11 * Added numerical sort.
13 * Revision 1.20 2000/03/02 14:35:19 adam
14 * Added structure year and date.
16 * Revision 1.19 1999/11/30 13:48:04 adam
17 * Improved installation. Updated for inclusion of YAZ header files.
19 * Revision 1.18 1999/10/15 08:27:46 adam
20 * Fixed replace handler. 8-bit fix.
22 * Revision 1.17 1999/09/08 12:13:21 adam
23 * Fixed minor bug "replace"-mappings. Removed some logging messages.
25 * Revision 1.16 1999/09/07 07:19:21 adam
26 * Work on character mapping. Implemented replace rules.
28 * Revision 1.15 1999/05/26 07:49:14 adam
31 * Revision 1.14 1999/02/19 10:37:40 adam
34 * Revision 1.13 1999/02/18 15:01:04 adam
35 * Structure=key uses register type 0.
37 * Revision 1.12 1999/02/12 13:29:25 adam
38 * Implemented position-flag for registers.
40 * Revision 1.11 1998/10/13 20:09:19 adam
41 * Changed call to readconf_line.
43 * Revision 1.10 1998/06/23 15:33:37 adam
44 * Added feature to specify sort criteria in query (type 7 specifies
47 * Revision 1.9 1998/04/02 14:35:30 adam
48 * First version of Zebra that works with compiled ASN.1.
50 * Revision 1.8 1998/03/05 08:42:44 adam
51 * Minor changes to zebramap data structures. Query mapping rules changed.
53 * Revision 1.7 1998/02/10 12:03:07 adam
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.
60 * Revision 1.5 1997/11/19 10:22:14 adam
61 * Bug fix (introduced by previous commit).
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.
67 * Revision 1.3 1997/11/17 15:35:26 adam
68 * Bug fix. Relation=relevance wasn't observed.
70 * Revision 1.2 1997/10/31 12:39:30 adam
71 * Changed log message.
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
84 #include <yaz/yaz-util.h>
88 #define ZEBRA_MAP_TYPE_SORT 1
89 #define ZEBRA_MAP_TYPE_INDEX 2
91 #define ZEBRA_REPLACE_ANY 300
97 struct zm_token *next;
114 const char *maptab_name;
115 struct zebra_map *next;
116 struct zm_token *replace_tokens;
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;
129 void zebra_maps_close (ZebraMaps zms)
131 struct zebra_map *zm = zms->map_list;
135 chrmaptab_destroy (zm->maptab);
138 wrbuf_free (zms->wrbuf_1, 1);
139 wrbuf_free (zms->wrbuf_2, 1);
140 nmem_destroy (zms->nmem);
144 static void zebra_map_read (ZebraMaps zms, const char *name)
151 struct zebra_map **zm = 0, *zp;
153 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
155 logf(LOG_WARN|LOG_ERRNO, "%s", name);
158 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
160 if (!yaz_matchstr (argv[0], "index") && argc == 2)
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;
175 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
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;
191 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
193 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
195 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
197 (*zm)->completeness = atoi (argv[1]);
199 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
201 (*zm)->positioned = atoi (argv[1]);
203 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
205 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
206 (*zm)->u.sort.entry_size = atoi (argv[1]);
208 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
210 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
211 token->next = (*zm)->replace_tokens;
212 (*zm)->replace_tokens = token;
214 logf (LOG_LOG, "replace %s", argv[1]);
216 token->token_from = 0;
220 int *dp = token->token_from = (int *)
221 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
230 *dp++ = ZEBRA_REPLACE_ANY;
235 *dp++ = zebra_prim(&cp);
237 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
245 char *dp = token->token_to =
246 nmem_malloc (zms->nmem, strlen(cp)+1);
254 *dp++ = zebra_prim(&cp);
265 for (zp = zms->map_list; zp; zp = zp->next)
266 zms->lookup_array[zp->reg_id] = zp;
269 static void zms_map_handle (void *p, const char *name, const char *value)
271 ZebraMaps zms = (ZebraMaps) p;
273 zebra_map_read (zms, value);
276 ZebraMaps zebra_maps_open (Res res)
278 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
281 zms->nmem = nmem_create ();
282 zms->tabpath = nmem_strdup (zms->nmem,
283 res_get_def (res, "profilePath", "."));
284 zms->map_list = NULL;
286 zms->temp_map_str[0] = '\0';
287 zms->temp_map_str[1] = '\0';
289 zms->temp_map_ptr[0] = zms->temp_map_str;
290 zms->temp_map_ptr[1] = NULL;
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");
299 zms->wrbuf_1 = wrbuf_alloc();
300 zms->wrbuf_2 = wrbuf_alloc();
304 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
306 return zms->lookup_array[reg_id];
309 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
311 struct zebra_map *zm = zebra_map_get (zms, reg_id);
314 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
315 logf (LOG_WARN, "Unknown register type: %c", reg_id);
318 zm->maptab_name = nmem_strdup (zms->nmem, "@");
320 zm->type = ZEBRA_MAP_TYPE_INDEX;
321 zm->completeness = 0;
322 zm->next = zms->map_list;
323 zms->map_list = zm->next;
325 zms->lookup_array[zm->reg_id & 255] = zm;
329 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
331 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
332 zm->maptab_name, 0)))
333 logf(LOG_WARN, "Failed to read character table %s",
336 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
341 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
342 const char **from, int len)
346 maptab = zebra_charmap_get (zms, reg_id);
348 return chr_map_input(maptab, from, len);
350 zms->temp_map_str[0] = **from;
353 return zms->temp_map_ptr;
357 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
358 const char *input_str, int input_len,
361 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
370 wrbuf_write (wrbuf, input_str, input_len);
381 out = chr_map_input (maptab, str, len);
384 while (out && *out && **out == *CHR_SPACE)
385 out = chr_map_input (maptab, str, len);
390 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
393 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
396 return chr_map_output (maptab, from, 1);
400 /* ------------------------------------ */
406 Z_AttributeElement **attributeList;
410 static int attr_find (AttrType *src, oid_value *attributeSetP)
412 while (src->major < src->num_attributes)
414 Z_AttributeElement *element;
416 element = src->attributeList[src->major];
417 if (src->type == *element->attributeType)
419 switch (element->which)
421 case Z_AttributeValue_numeric:
423 if (element->attributeSet && attributeSetP)
427 attrset = oid_getentbyoid (element->attributeSet);
428 *attributeSetP = attrset->value;
430 return *element->value.numeric;
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)
438 if (element->attributeSet && attributeSetP)
442 attrset = oid_getentbyoid (element->attributeSet);
443 *attributeSetP = attrset->value;
445 return *element->value.complex->list[src->minor-1]->u.numeric;
455 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
458 src->attributeList = zapt->attributes->attributes;
459 src->num_attributes = zapt->attributes->num_attributes;
461 src->attributeList = zapt->attributeList;
462 src->num_attributes = zapt->num_attributes;
469 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
471 src->attributeList = list->attributes;
472 src->num_attributes = list->num_attributes;
478 /* ------------------------------------ */
480 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
482 struct zebra_map *zm = zebra_map_get (zms, reg_id);
484 return zm->completeness;
488 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
490 struct zebra_map *zm = zebra_map_get (zms, reg_id);
492 return zm->positioned;
496 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
498 struct zebra_map *zm = zebra_map_get (zms, reg_id);
500 return zm->type == ZEBRA_MAP_TYPE_SORT;
504 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
510 attr_init_AttrList (&use, sortAttributes->list, 1);
511 attr_init_AttrList (&structure, sortAttributes->list, 4);
514 structure_value = attr_find (&structure, 0);
515 if (structure_value == 109)
517 return attr_find (&use, NULL);
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)
524 AttrType completeness;
527 AttrType sort_relation;
529 int completeness_value;
532 int sort_relation_value;
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);
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);
547 if (completeness_value == 2 || completeness_value == 3)
553 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
554 *search_type = "phrase";
555 strcpy (rank_type, "void");
556 if (relation_value == 102)
558 if (weight_value == -1)
560 sprintf (rank_type, "rank,%d", weight_value);
566 switch (structure_value)
568 case 6: /* word list */
569 *search_type = "and-list";
571 case 105: /* free-form-text */
572 *search_type = "or-list";
574 case 106: /* document-text */
575 *search_type = "or-list";
580 case 108: /* string */
581 *search_type = "phrase";
583 case 107: /* local-number */
584 *search_type = "local";
587 case 109: /* numeric string */
589 *search_type = "numeric";
593 *search_type = "phrase";
597 *search_type = "phrase";
601 *search_type = "phrase";
605 *search_type = "phrase";
613 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
614 const char *input_str, int input_len, WRBUF wrbuf);
616 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
617 const char *input_str, int input_len)
619 struct zebra_map *zm = zebra_map_get (zms, reg_id);
621 wrbuf_rewind(zms->wrbuf_1);
622 wrbuf_write(zms->wrbuf_1, input_str, input_len);
623 if (!zm || !zm->replace_tokens)
627 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
628 wrbuf_buf(zms->wrbuf_1));
632 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
633 wrbuf_len(zms->wrbuf_1), 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))
642 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
643 const char *input_str, int input_len, WRBUF wrbuf)
647 struct zebra_map *zm = zebra_map_get (zms, reg_id);
650 for (i = -1; i <= input_len; )
652 struct zm_token *token;
653 char replace_string[128];
657 for (token = zm->replace_tokens; !replace_in && token;
661 int replace_done = 0;
666 if (!token->token_from[j])
671 if (ex_list && strchr (ex_list, token->token_from[j]))
673 if (i+j < 0 || j+i >= input_len)
676 c = input_str[j+i] & 255;
677 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
681 replace_string[replace_out++] = c;
685 if (c != token->token_from[j])
691 const char *cp = token->token_to;
693 for (; cp && *cp; cp++)
694 replace_string[replace_out++] = *cp;
701 if (i >= 0 && i < input_len)
702 wrbuf_putc(wrbuf, input_str[i]);
709 wrbuf_write(wrbuf, replace_string, replace_out);
714 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));