2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 2000-03-02 14:35:19 adam
8 * Added structure year and date.
10 * Revision 1.19 1999/11/30 13:48:04 adam
11 * Improved installation. Updated for inclusion of YAZ header files.
13 * Revision 1.18 1999/10/15 08:27:46 adam
14 * Fixed replace handler. 8-bit fix.
16 * Revision 1.17 1999/09/08 12:13:21 adam
17 * Fixed minor bug "replace"-mappings. Removed some logging messages.
19 * Revision 1.16 1999/09/07 07:19:21 adam
20 * Work on character mapping. Implemented replace rules.
22 * Revision 1.15 1999/05/26 07:49:14 adam
25 * Revision 1.14 1999/02/19 10:37:40 adam
28 * Revision 1.13 1999/02/18 15:01:04 adam
29 * Structure=key uses register type 0.
31 * Revision 1.12 1999/02/12 13:29:25 adam
32 * Implemented position-flag for registers.
34 * Revision 1.11 1998/10/13 20:09:19 adam
35 * Changed call to readconf_line.
37 * Revision 1.10 1998/06/23 15:33:37 adam
38 * Added feature to specify sort criteria in query (type 7 specifies
41 * Revision 1.9 1998/04/02 14:35:30 adam
42 * First version of Zebra that works with compiled ASN.1.
44 * Revision 1.8 1998/03/05 08:42:44 adam
45 * Minor changes to zebramap data structures. Query mapping rules changed.
47 * Revision 1.7 1998/02/10 12:03:07 adam
50 * Revision 1.6 1998/01/29 13:36:01 adam
51 * Structure word-list, free-form-text and document-text all
52 * trigger ranked search.
54 * Revision 1.5 1997/11/19 10:22:14 adam
55 * Bug fix (introduced by previous commit).
57 * Revision 1.4 1997/11/18 10:05:08 adam
58 * Changed character map facility so that admin can specify character
59 * mapping files for each register type, w, p, etc.
61 * Revision 1.3 1997/11/17 15:35:26 adam
62 * Bug fix. Relation=relevance wasn't observed.
64 * Revision 1.2 1997/10/31 12:39:30 adam
65 * Changed log message.
67 * Revision 1.1 1997/10/27 14:33:06 adam
68 * Moved towards generic character mapping depending on "structure"
69 * field in abstract syntax file. Fixed a few memory leaks. Fixed
70 * bug with negative integers when doing searches with relational
78 #include <yaz/yaz-util.h>
82 #define ZEBRA_MAP_TYPE_SORT 1
83 #define ZEBRA_MAP_TYPE_INDEX 2
85 #define ZEBRA_REPLACE_ANY 300
91 struct zm_token *next;
108 const char *maptab_name;
109 struct zebra_map *next;
110 struct zm_token *replace_tokens;
116 struct zebra_map *map_list;
117 char temp_map_str[2];
118 const char *temp_map_ptr[2];
119 struct zebra_map **lookup_array;
120 WRBUF wrbuf_1, wrbuf_2;
123 void zebra_maps_close (ZebraMaps zms)
125 struct zebra_map *zm = zms->map_list;
129 chrmaptab_destroy (zm->maptab);
132 wrbuf_free (zms->wrbuf_1, 1);
133 wrbuf_free (zms->wrbuf_2, 1);
134 nmem_destroy (zms->nmem);
138 static void zebra_map_read (ZebraMaps zms, const char *name)
145 struct zebra_map **zm = 0, *zp;
147 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
149 logf(LOG_WARN|LOG_ERRNO, "%s", name);
152 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
154 if (!yaz_matchstr (argv[0], "index") && argc == 2)
160 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
161 (*zm)->reg_id = argv[1][0];
162 (*zm)->maptab_name = NULL;
163 (*zm)->maptab = NULL;
164 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
165 (*zm)->completeness = 0;
166 (*zm)->positioned = 1;
167 (*zm)->replace_tokens = 0;
169 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
175 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
176 (*zm)->reg_id = argv[1][0];
177 (*zm)->maptab_name = NULL;
178 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
179 (*zm)->u.sort.entry_size = 80;
180 (*zm)->maptab = NULL;
181 (*zm)->completeness = 0;
182 (*zm)->positioned = 0;
183 (*zm)->replace_tokens = 0;
185 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
187 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
189 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
191 (*zm)->completeness = atoi (argv[1]);
193 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
195 (*zm)->positioned = atoi (argv[1]);
197 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
199 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
200 (*zm)->u.sort.entry_size = atoi (argv[1]);
202 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
204 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
205 token->next = (*zm)->replace_tokens;
206 (*zm)->replace_tokens = token;
208 logf (LOG_LOG, "replace %s", argv[1]);
210 token->token_from = 0;
214 int *dp = token->token_from = (int *)
215 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
224 *dp++ = ZEBRA_REPLACE_ANY;
229 *dp++ = zebra_prim(&cp);
231 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
239 char *dp = token->token_to =
240 nmem_malloc (zms->nmem, strlen(cp)+1);
248 *dp++ = zebra_prim(&cp);
259 for (zp = zms->map_list; zp; zp = zp->next)
260 zms->lookup_array[zp->reg_id] = zp;
263 static void zms_map_handle (void *p, const char *name, const char *value)
265 ZebraMaps zms = (ZebraMaps) p;
267 zebra_map_read (zms, value);
270 ZebraMaps zebra_maps_open (Res res)
272 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
275 zms->nmem = nmem_create ();
276 zms->tabpath = nmem_strdup (zms->nmem,
277 res_get_def (res, "profilePath", "."));
278 zms->map_list = NULL;
280 zms->temp_map_str[0] = '\0';
281 zms->temp_map_str[1] = '\0';
283 zms->temp_map_ptr[0] = zms->temp_map_str;
284 zms->temp_map_ptr[1] = NULL;
286 zms->lookup_array = (struct zebra_map**)
287 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
288 for (i = 0; i<256; i++)
289 zms->lookup_array[i] = 0;
290 if (!res || !res_trav (res, "index", zms, zms_map_handle))
291 zebra_map_read (zms, "default.idx");
293 zms->wrbuf_1 = wrbuf_alloc();
294 zms->wrbuf_2 = wrbuf_alloc();
298 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
300 return zms->lookup_array[reg_id];
303 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
305 struct zebra_map *zm = zebra_map_get (zms, reg_id);
308 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
309 logf (LOG_WARN, "Unknown register type: %c", reg_id);
312 zm->maptab_name = nmem_strdup (zms->nmem, "@");
314 zm->type = ZEBRA_MAP_TYPE_INDEX;
315 zm->completeness = 0;
316 zm->next = zms->map_list;
317 zms->map_list = zm->next;
319 zms->lookup_array[zm->reg_id & 255] = zm;
323 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
325 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
326 zm->maptab_name, 0)))
327 logf(LOG_WARN, "Failed to read character table %s",
330 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
335 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
336 const char **from, int len)
340 maptab = zebra_charmap_get (zms, reg_id);
342 return chr_map_input(maptab, from, len);
344 zms->temp_map_str[0] = **from;
347 return zms->temp_map_ptr;
351 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
352 const char *input_str, int input_len,
355 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
364 wrbuf_write (wrbuf, input_str, input_len);
375 out = chr_map_input (maptab, str, len);
378 while (out && *out && **out == *CHR_SPACE)
379 out = chr_map_input (maptab, str, len);
384 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
387 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
390 return chr_map_output (maptab, from, 1);
394 /* ------------------------------------ */
400 Z_AttributeElement **attributeList;
404 static int attr_find (AttrType *src, oid_value *attributeSetP)
406 while (src->major < src->num_attributes)
408 Z_AttributeElement *element;
410 element = src->attributeList[src->major];
411 if (src->type == *element->attributeType)
413 switch (element->which)
415 case Z_AttributeValue_numeric:
417 if (element->attributeSet && attributeSetP)
421 attrset = oid_getentbyoid (element->attributeSet);
422 *attributeSetP = attrset->value;
424 return *element->value.numeric;
426 case Z_AttributeValue_complex:
427 if (src->minor >= element->value.complex->num_list ||
428 element->value.complex->list[src->minor]->which !=
429 Z_StringOrNumeric_numeric)
432 if (element->attributeSet && attributeSetP)
436 attrset = oid_getentbyoid (element->attributeSet);
437 *attributeSetP = attrset->value;
439 return *element->value.complex->list[src->minor-1]->u.numeric;
449 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
452 src->attributeList = zapt->attributes->attributes;
453 src->num_attributes = zapt->attributes->num_attributes;
455 src->attributeList = zapt->attributeList;
456 src->num_attributes = zapt->num_attributes;
463 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
465 src->attributeList = list->attributes;
466 src->num_attributes = list->num_attributes;
472 /* ------------------------------------ */
474 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
476 struct zebra_map *zm = zebra_map_get (zms, reg_id);
478 return zm->completeness;
482 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
484 struct zebra_map *zm = zebra_map_get (zms, reg_id);
486 return zm->positioned;
490 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
492 struct zebra_map *zm = zebra_map_get (zms, reg_id);
494 return zm->type == ZEBRA_MAP_TYPE_SORT;
498 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
501 attr_init_AttrList (&use, sortAttributes->list, 1);
503 return attr_find (&use, NULL);
506 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
507 unsigned *reg_id, char **search_type, char **rank_type,
508 int *complete_flag, int *sort_flag)
510 AttrType completeness;
513 AttrType sort_relation;
514 int completeness_value;
517 int sort_relation_value;
519 attr_init_APT (&structure, zapt, 4);
520 attr_init_APT (&completeness, zapt, 6);
521 attr_init_APT (&relation, zapt, 2);
522 attr_init_APT (&sort_relation, zapt, 7);
524 completeness_value = attr_find (&completeness, NULL);
525 structure_value = attr_find (&structure, NULL);
526 relation_value = attr_find (&relation, NULL);
527 sort_relation_value = attr_find (&sort_relation, NULL);
529 if (completeness_value == 2 || completeness_value == 3)
535 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
536 *search_type = "phrase";
538 if (relation_value == 102)
545 switch (structure_value)
547 case 6: /* word list */
548 *search_type = "and-list";
550 case 105: /* free-form-text */
551 *search_type = "or-list";
553 case 106: /* document-text */
554 *search_type = "or-list";
559 case 108: /* string */
560 *search_type = "phrase";
562 case 107: /* local-number */
563 *search_type = "local";
566 case 109: /* numeric string */
568 *search_type = "numeric";
572 *search_type = "phrase";
576 *search_type = "phrase";
580 *search_type = "phrase";
584 *search_type = "phrase";
592 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
593 const char *input_str, int input_len, WRBUF wrbuf);
595 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
596 const char *input_str, int input_len)
598 struct zebra_map *zm = zebra_map_get (zms, reg_id);
600 wrbuf_rewind(zms->wrbuf_1);
601 wrbuf_write(zms->wrbuf_1, input_str, input_len);
602 if (!zm || !zm->replace_tokens)
606 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
607 wrbuf_buf(zms->wrbuf_1));
611 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
612 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
614 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
615 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
621 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
622 const char *input_str, int input_len, WRBUF wrbuf)
626 struct zebra_map *zm = zebra_map_get (zms, reg_id);
629 for (i = -1; i <= input_len; )
631 struct zm_token *token;
632 char replace_string[128];
636 for (token = zm->replace_tokens; !replace_in && token;
640 int replace_done = 0;
645 if (!token->token_from[j])
650 if (ex_list && strchr (ex_list, token->token_from[j]))
652 if (i+j < 0 || j+i >= input_len)
655 c = input_str[j+i] & 255;
656 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
660 replace_string[replace_out++] = c;
664 if (c != token->token_from[j])
670 const char *cp = token->token_to;
672 for (; cp && *cp; cp++)
673 replace_string[replace_out++] = *cp;
680 if (i >= 0 && i < input_len)
681 wrbuf_putc(wrbuf, input_str[i]);
688 wrbuf_write(wrbuf, replace_string, replace_out);
693 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));