2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 2001-11-15 08:41:24 adam
8 * Fix for weight (bug introduced by previous commit).
10 * Revision 1.22 2001/11/14 22:06:27 adam
11 * Rank-weight may be controlled via query.
13 * Revision 1.21 2001/01/22 10:42:56 adam
14 * Added numerical sort.
16 * Revision 1.20 2000/03/02 14:35:19 adam
17 * Added structure year and date.
19 * Revision 1.19 1999/11/30 13:48:04 adam
20 * Improved installation. Updated for inclusion of YAZ header files.
22 * Revision 1.18 1999/10/15 08:27:46 adam
23 * Fixed replace handler. 8-bit fix.
25 * Revision 1.17 1999/09/08 12:13:21 adam
26 * Fixed minor bug "replace"-mappings. Removed some logging messages.
28 * Revision 1.16 1999/09/07 07:19:21 adam
29 * Work on character mapping. Implemented replace rules.
31 * Revision 1.15 1999/05/26 07:49:14 adam
34 * Revision 1.14 1999/02/19 10:37:40 adam
37 * Revision 1.13 1999/02/18 15:01:04 adam
38 * Structure=key uses register type 0.
40 * Revision 1.12 1999/02/12 13:29:25 adam
41 * Implemented position-flag for registers.
43 * Revision 1.11 1998/10/13 20:09:19 adam
44 * Changed call to readconf_line.
46 * Revision 1.10 1998/06/23 15:33:37 adam
47 * Added feature to specify sort criteria in query (type 7 specifies
50 * Revision 1.9 1998/04/02 14:35:30 adam
51 * First version of Zebra that works with compiled ASN.1.
53 * Revision 1.8 1998/03/05 08:42:44 adam
54 * Minor changes to zebramap data structures. Query mapping rules changed.
56 * Revision 1.7 1998/02/10 12:03:07 adam
59 * Revision 1.6 1998/01/29 13:36:01 adam
60 * Structure word-list, free-form-text and document-text all
61 * trigger ranked search.
63 * Revision 1.5 1997/11/19 10:22:14 adam
64 * Bug fix (introduced by previous commit).
66 * Revision 1.4 1997/11/18 10:05:08 adam
67 * Changed character map facility so that admin can specify character
68 * mapping files for each register type, w, p, etc.
70 * Revision 1.3 1997/11/17 15:35:26 adam
71 * Bug fix. Relation=relevance wasn't observed.
73 * Revision 1.2 1997/10/31 12:39:30 adam
74 * Changed log message.
76 * Revision 1.1 1997/10/27 14:33:06 adam
77 * Moved towards generic character mapping depending on "structure"
78 * field in abstract syntax file. Fixed a few memory leaks. Fixed
79 * bug with negative integers when doing searches with relational
87 #include <yaz/yaz-util.h>
91 #define ZEBRA_MAP_TYPE_SORT 1
92 #define ZEBRA_MAP_TYPE_INDEX 2
94 #define ZEBRA_REPLACE_ANY 300
100 struct zm_token *next;
117 const char *maptab_name;
118 struct zebra_map *next;
119 struct zm_token *replace_tokens;
125 struct zebra_map *map_list;
126 char temp_map_str[2];
127 const char *temp_map_ptr[2];
128 struct zebra_map **lookup_array;
129 WRBUF wrbuf_1, wrbuf_2;
132 void zebra_maps_close (ZebraMaps zms)
134 struct zebra_map *zm = zms->map_list;
138 chrmaptab_destroy (zm->maptab);
141 wrbuf_free (zms->wrbuf_1, 1);
142 wrbuf_free (zms->wrbuf_2, 1);
143 nmem_destroy (zms->nmem);
147 static void zebra_map_read (ZebraMaps zms, const char *name)
154 struct zebra_map **zm = 0, *zp;
156 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
158 logf(LOG_WARN|LOG_ERRNO, "%s", name);
161 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
163 if (!yaz_matchstr (argv[0], "index") && argc == 2)
169 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
170 (*zm)->reg_id = argv[1][0];
171 (*zm)->maptab_name = NULL;
172 (*zm)->maptab = NULL;
173 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
174 (*zm)->completeness = 0;
175 (*zm)->positioned = 1;
176 (*zm)->replace_tokens = 0;
178 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
184 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
185 (*zm)->reg_id = argv[1][0];
186 (*zm)->maptab_name = NULL;
187 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
188 (*zm)->u.sort.entry_size = 80;
189 (*zm)->maptab = NULL;
190 (*zm)->completeness = 0;
191 (*zm)->positioned = 0;
192 (*zm)->replace_tokens = 0;
194 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
196 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
198 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
200 (*zm)->completeness = atoi (argv[1]);
202 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
204 (*zm)->positioned = atoi (argv[1]);
206 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
208 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
209 (*zm)->u.sort.entry_size = atoi (argv[1]);
211 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
213 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
214 token->next = (*zm)->replace_tokens;
215 (*zm)->replace_tokens = token;
217 logf (LOG_LOG, "replace %s", argv[1]);
219 token->token_from = 0;
223 int *dp = token->token_from = (int *)
224 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
233 *dp++ = ZEBRA_REPLACE_ANY;
238 *dp++ = zebra_prim(&cp);
240 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
248 char *dp = token->token_to =
249 nmem_malloc (zms->nmem, strlen(cp)+1);
257 *dp++ = zebra_prim(&cp);
268 for (zp = zms->map_list; zp; zp = zp->next)
269 zms->lookup_array[zp->reg_id] = zp;
272 static void zms_map_handle (void *p, const char *name, const char *value)
274 ZebraMaps zms = (ZebraMaps) p;
276 zebra_map_read (zms, value);
279 ZebraMaps zebra_maps_open (Res res)
281 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
284 zms->nmem = nmem_create ();
285 zms->tabpath = nmem_strdup (zms->nmem,
286 res_get_def (res, "profilePath", "."));
287 zms->map_list = NULL;
289 zms->temp_map_str[0] = '\0';
290 zms->temp_map_str[1] = '\0';
292 zms->temp_map_ptr[0] = zms->temp_map_str;
293 zms->temp_map_ptr[1] = NULL;
295 zms->lookup_array = (struct zebra_map**)
296 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
297 for (i = 0; i<256; i++)
298 zms->lookup_array[i] = 0;
299 if (!res || !res_trav (res, "index", zms, zms_map_handle))
300 zebra_map_read (zms, "default.idx");
302 zms->wrbuf_1 = wrbuf_alloc();
303 zms->wrbuf_2 = wrbuf_alloc();
307 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
309 return zms->lookup_array[reg_id];
312 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
314 struct zebra_map *zm = zebra_map_get (zms, reg_id);
317 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
318 logf (LOG_WARN, "Unknown register type: %c", reg_id);
321 zm->maptab_name = nmem_strdup (zms->nmem, "@");
323 zm->type = ZEBRA_MAP_TYPE_INDEX;
324 zm->completeness = 0;
325 zm->next = zms->map_list;
326 zms->map_list = zm->next;
328 zms->lookup_array[zm->reg_id & 255] = zm;
332 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
334 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
335 zm->maptab_name, 0)))
336 logf(LOG_WARN, "Failed to read character table %s",
339 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
344 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
345 const char **from, int len)
349 maptab = zebra_charmap_get (zms, reg_id);
351 return chr_map_input(maptab, from, len);
353 zms->temp_map_str[0] = **from;
356 return zms->temp_map_ptr;
360 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
361 const char *input_str, int input_len,
364 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
373 wrbuf_write (wrbuf, input_str, input_len);
384 out = chr_map_input (maptab, str, len);
387 while (out && *out && **out == *CHR_SPACE)
388 out = chr_map_input (maptab, str, len);
393 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
396 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
399 return chr_map_output (maptab, from, 1);
403 /* ------------------------------------ */
409 Z_AttributeElement **attributeList;
413 static int attr_find (AttrType *src, oid_value *attributeSetP)
415 while (src->major < src->num_attributes)
417 Z_AttributeElement *element;
419 element = src->attributeList[src->major];
420 if (src->type == *element->attributeType)
422 switch (element->which)
424 case Z_AttributeValue_numeric:
426 if (element->attributeSet && attributeSetP)
430 attrset = oid_getentbyoid (element->attributeSet);
431 *attributeSetP = attrset->value;
433 return *element->value.numeric;
435 case Z_AttributeValue_complex:
436 if (src->minor >= element->value.complex->num_list ||
437 element->value.complex->list[src->minor]->which !=
438 Z_StringOrNumeric_numeric)
441 if (element->attributeSet && attributeSetP)
445 attrset = oid_getentbyoid (element->attributeSet);
446 *attributeSetP = attrset->value;
448 return *element->value.complex->list[src->minor-1]->u.numeric;
458 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
461 src->attributeList = zapt->attributes->attributes;
462 src->num_attributes = zapt->attributes->num_attributes;
464 src->attributeList = zapt->attributeList;
465 src->num_attributes = zapt->num_attributes;
472 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
474 src->attributeList = list->attributes;
475 src->num_attributes = list->num_attributes;
481 /* ------------------------------------ */
483 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
485 struct zebra_map *zm = zebra_map_get (zms, reg_id);
487 return zm->completeness;
491 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
493 struct zebra_map *zm = zebra_map_get (zms, reg_id);
495 return zm->positioned;
499 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
501 struct zebra_map *zm = zebra_map_get (zms, reg_id);
503 return zm->type == ZEBRA_MAP_TYPE_SORT;
507 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
513 attr_init_AttrList (&use, sortAttributes->list, 1);
514 attr_init_AttrList (&structure, sortAttributes->list, 4);
517 structure_value = attr_find (&structure, 0);
518 if (structure_value == 109)
520 return attr_find (&use, NULL);
523 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
524 unsigned *reg_id, char **search_type, char *rank_type,
525 int *complete_flag, int *sort_flag)
527 AttrType completeness;
530 AttrType sort_relation;
532 int completeness_value;
535 int sort_relation_value;
538 attr_init_APT (&structure, zapt, 4);
539 attr_init_APT (&completeness, zapt, 6);
540 attr_init_APT (&relation, zapt, 2);
541 attr_init_APT (&sort_relation, zapt, 7);
542 attr_init_APT (&weight, zapt, 9);
544 completeness_value = attr_find (&completeness, NULL);
545 structure_value = attr_find (&structure, NULL);
546 relation_value = attr_find (&relation, NULL);
547 sort_relation_value = attr_find (&sort_relation, NULL);
548 weight_value = attr_find (&weight, NULL);
550 if (completeness_value == 2 || completeness_value == 3)
556 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
557 *search_type = "phrase";
558 strcpy (rank_type, "void");
559 if (relation_value == 102)
561 if (weight_value == -1)
563 sprintf (rank_type, "rank,%d", weight_value);
569 switch (structure_value)
571 case 6: /* word list */
572 *search_type = "and-list";
574 case 105: /* free-form-text */
575 *search_type = "or-list";
577 case 106: /* document-text */
578 *search_type = "or-list";
583 case 108: /* string */
584 *search_type = "phrase";
586 case 107: /* local-number */
587 *search_type = "local";
590 case 109: /* numeric string */
592 *search_type = "numeric";
596 *search_type = "phrase";
600 *search_type = "phrase";
604 *search_type = "phrase";
608 *search_type = "phrase";
616 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
617 const char *input_str, int input_len, WRBUF wrbuf);
619 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
620 const char *input_str, int input_len)
622 struct zebra_map *zm = zebra_map_get (zms, reg_id);
624 wrbuf_rewind(zms->wrbuf_1);
625 wrbuf_write(zms->wrbuf_1, input_str, input_len);
626 if (!zm || !zm->replace_tokens)
630 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
631 wrbuf_buf(zms->wrbuf_1));
635 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
636 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
638 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
639 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
645 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
646 const char *input_str, int input_len, WRBUF wrbuf)
650 struct zebra_map *zm = zebra_map_get (zms, reg_id);
653 for (i = -1; i <= input_len; )
655 struct zm_token *token;
656 char replace_string[128];
660 for (token = zm->replace_tokens; !replace_in && token;
664 int replace_done = 0;
669 if (!token->token_from[j])
674 if (ex_list && strchr (ex_list, token->token_from[j]))
676 if (i+j < 0 || j+i >= input_len)
679 c = input_str[j+i] & 255;
680 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
684 replace_string[replace_out++] = c;
688 if (c != token->token_from[j])
694 const char *cp = token->token_to;
696 for (; cp && *cp; cp++)
697 replace_string[replace_out++] = *cp;
704 if (i >= 0 && i < input_len)
705 wrbuf_putc(wrbuf, input_str[i]);
712 wrbuf_write(wrbuf, replace_string, replace_out);
717 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));