2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1999-11-30 13:48:04 adam
8 * Improved installation. Updated for inclusion of YAZ header files.
10 * Revision 1.18 1999/10/15 08:27:46 adam
11 * Fixed replace handler. 8-bit fix.
13 * Revision 1.17 1999/09/08 12:13:21 adam
14 * Fixed minor bug "replace"-mappings. Removed some logging messages.
16 * Revision 1.16 1999/09/07 07:19:21 adam
17 * Work on character mapping. Implemented replace rules.
19 * Revision 1.15 1999/05/26 07:49:14 adam
22 * Revision 1.14 1999/02/19 10:37:40 adam
25 * Revision 1.13 1999/02/18 15:01:04 adam
26 * Structure=key uses register type 0.
28 * Revision 1.12 1999/02/12 13:29:25 adam
29 * Implemented position-flag for registers.
31 * Revision 1.11 1998/10/13 20:09:19 adam
32 * Changed call to readconf_line.
34 * Revision 1.10 1998/06/23 15:33:37 adam
35 * Added feature to specify sort criteria in query (type 7 specifies
38 * Revision 1.9 1998/04/02 14:35:30 adam
39 * First version of Zebra that works with compiled ASN.1.
41 * Revision 1.8 1998/03/05 08:42:44 adam
42 * Minor changes to zebramap data structures. Query mapping rules changed.
44 * Revision 1.7 1998/02/10 12:03:07 adam
47 * Revision 1.6 1998/01/29 13:36:01 adam
48 * Structure word-list, free-form-text and document-text all
49 * trigger ranked search.
51 * Revision 1.5 1997/11/19 10:22:14 adam
52 * Bug fix (introduced by previous commit).
54 * Revision 1.4 1997/11/18 10:05:08 adam
55 * Changed character map facility so that admin can specify character
56 * mapping files for each register type, w, p, etc.
58 * Revision 1.3 1997/11/17 15:35:26 adam
59 * Bug fix. Relation=relevance wasn't observed.
61 * Revision 1.2 1997/10/31 12:39:30 adam
62 * Changed log message.
64 * Revision 1.1 1997/10/27 14:33:06 adam
65 * Moved towards generic character mapping depending on "structure"
66 * field in abstract syntax file. Fixed a few memory leaks. Fixed
67 * bug with negative integers when doing searches with relational
75 #include <yaz/yaz-util.h>
79 #define ZEBRA_MAP_TYPE_SORT 1
80 #define ZEBRA_MAP_TYPE_INDEX 2
82 #define ZEBRA_REPLACE_ANY 300
88 struct zm_token *next;
105 const char *maptab_name;
106 struct zebra_map *next;
107 struct zm_token *replace_tokens;
113 struct zebra_map *map_list;
114 char temp_map_str[2];
115 const char *temp_map_ptr[2];
116 struct zebra_map **lookup_array;
117 WRBUF wrbuf_1, wrbuf_2;
120 void zebra_maps_close (ZebraMaps zms)
122 struct zebra_map *zm = zms->map_list;
126 chrmaptab_destroy (zm->maptab);
129 wrbuf_free (zms->wrbuf_1, 1);
130 wrbuf_free (zms->wrbuf_2, 1);
131 nmem_destroy (zms->nmem);
135 static void zebra_map_read (ZebraMaps zms, const char *name)
142 struct zebra_map **zm = 0, *zp;
144 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
146 logf(LOG_WARN|LOG_ERRNO, "%s", name);
149 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
151 if (!yaz_matchstr (argv[0], "index") && argc == 2)
157 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
158 (*zm)->reg_id = argv[1][0];
159 (*zm)->maptab_name = NULL;
160 (*zm)->maptab = NULL;
161 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
162 (*zm)->completeness = 0;
163 (*zm)->positioned = 1;
164 (*zm)->replace_tokens = 0;
166 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
172 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
173 (*zm)->reg_id = argv[1][0];
174 (*zm)->maptab_name = NULL;
175 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
176 (*zm)->u.sort.entry_size = 80;
177 (*zm)->maptab = NULL;
178 (*zm)->completeness = 0;
179 (*zm)->positioned = 0;
180 (*zm)->replace_tokens = 0;
182 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
184 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
186 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
188 (*zm)->completeness = atoi (argv[1]);
190 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
192 (*zm)->positioned = atoi (argv[1]);
194 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
196 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
197 (*zm)->u.sort.entry_size = atoi (argv[1]);
199 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
201 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
202 token->next = (*zm)->replace_tokens;
203 (*zm)->replace_tokens = token;
205 logf (LOG_LOG, "replace %s", argv[1]);
207 token->token_from = 0;
211 int *dp = token->token_from = (int *)
212 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
221 *dp++ = ZEBRA_REPLACE_ANY;
226 *dp++ = zebra_prim(&cp);
228 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
236 char *dp = token->token_to =
237 nmem_malloc (zms->nmem, strlen(cp)+1);
245 *dp++ = zebra_prim(&cp);
256 for (zp = zms->map_list; zp; zp = zp->next)
257 zms->lookup_array[zp->reg_id] = zp;
260 static void zms_map_handle (void *p, const char *name, const char *value)
262 ZebraMaps zms = (ZebraMaps) p;
264 zebra_map_read (zms, value);
267 ZebraMaps zebra_maps_open (Res res)
269 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
272 zms->nmem = nmem_create ();
273 zms->tabpath = nmem_strdup (zms->nmem,
274 res_get_def (res, "profilePath", "."));
275 zms->map_list = NULL;
277 zms->temp_map_str[0] = '\0';
278 zms->temp_map_str[1] = '\0';
280 zms->temp_map_ptr[0] = zms->temp_map_str;
281 zms->temp_map_ptr[1] = NULL;
283 zms->lookup_array = (struct zebra_map**)
284 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
285 for (i = 0; i<256; i++)
286 zms->lookup_array[i] = 0;
287 if (!res || !res_trav (res, "index", zms, zms_map_handle))
288 zebra_map_read (zms, "default.idx");
290 zms->wrbuf_1 = wrbuf_alloc();
291 zms->wrbuf_2 = wrbuf_alloc();
295 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
297 return zms->lookup_array[reg_id];
300 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
302 struct zebra_map *zm = zebra_map_get (zms, reg_id);
305 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
306 logf (LOG_WARN, "Unknown register type: %c", reg_id);
309 zm->maptab_name = NULL;
311 zm->type = ZEBRA_MAP_TYPE_INDEX;
312 zm->completeness = 0;
313 zm->next = zms->map_list;
314 zms->map_list = zm->next;
316 zms->lookup_array[zm->reg_id & 255] = zm;
320 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
322 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
323 zm->maptab_name, 0)))
324 logf(LOG_WARN, "Failed to read character table %s",
327 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
332 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
333 const char **from, int len)
337 maptab = zebra_charmap_get (zms, reg_id);
339 return chr_map_input(maptab, from, len);
341 zms->temp_map_str[0] = **from;
344 return zms->temp_map_ptr;
348 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
349 const char *input_str, int input_len,
352 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
361 wrbuf_write (wrbuf, input_str, input_len);
372 out = chr_map_input (maptab, str, len);
375 while (out && *out && **out == *CHR_SPACE)
376 out = chr_map_input (maptab, str, len);
381 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
384 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
387 return chr_map_output (maptab, from, 1);
391 /* ------------------------------------ */
397 Z_AttributeElement **attributeList;
401 static int attr_find (AttrType *src, oid_value *attributeSetP)
403 while (src->major < src->num_attributes)
405 Z_AttributeElement *element;
407 element = src->attributeList[src->major];
408 if (src->type == *element->attributeType)
410 switch (element->which)
412 case Z_AttributeValue_numeric:
414 if (element->attributeSet && attributeSetP)
418 attrset = oid_getentbyoid (element->attributeSet);
419 *attributeSetP = attrset->value;
421 return *element->value.numeric;
423 case Z_AttributeValue_complex:
424 if (src->minor >= element->value.complex->num_list ||
425 element->value.complex->list[src->minor]->which !=
426 Z_StringOrNumeric_numeric)
429 if (element->attributeSet && attributeSetP)
433 attrset = oid_getentbyoid (element->attributeSet);
434 *attributeSetP = attrset->value;
436 return *element->value.complex->list[src->minor-1]->u.numeric;
446 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
449 src->attributeList = zapt->attributes->attributes;
450 src->num_attributes = zapt->attributes->num_attributes;
452 src->attributeList = zapt->attributeList;
453 src->num_attributes = zapt->num_attributes;
460 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
462 src->attributeList = list->attributes;
463 src->num_attributes = list->num_attributes;
469 /* ------------------------------------ */
471 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
473 struct zebra_map *zm = zebra_map_get (zms, reg_id);
475 return zm->completeness;
479 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
481 struct zebra_map *zm = zebra_map_get (zms, reg_id);
483 return zm->positioned;
487 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
489 struct zebra_map *zm = zebra_map_get (zms, reg_id);
491 return zm->type == ZEBRA_MAP_TYPE_SORT;
495 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
498 attr_init_AttrList (&use, sortAttributes->list, 1);
500 return attr_find (&use, NULL);
503 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
504 unsigned *reg_id, char **search_type, char **rank_type,
505 int *complete_flag, int *sort_flag)
507 AttrType completeness;
510 AttrType sort_relation;
511 int completeness_value;
514 int sort_relation_value;
516 attr_init_APT (&structure, zapt, 4);
517 attr_init_APT (&completeness, zapt, 6);
518 attr_init_APT (&relation, zapt, 2);
519 attr_init_APT (&sort_relation, zapt, 7);
521 completeness_value = attr_find (&completeness, NULL);
522 structure_value = attr_find (&structure, NULL);
523 relation_value = attr_find (&relation, NULL);
524 sort_relation_value = attr_find (&sort_relation, NULL);
526 if (completeness_value == 2 || completeness_value == 3)
532 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
533 *search_type = "phrase";
535 if (relation_value == 102)
542 switch (structure_value)
544 case 6: /* word list */
545 *search_type = "and-list";
547 case 105: /* free-form-text */
548 *search_type = "or-list";
550 case 106: /* document-text */
551 *search_type = "or-list";
556 case 108: /* string */
557 *search_type = "phrase";
559 case 107: /* local-number */
560 *search_type = "local";
563 case 109: /* numeric string */
565 *search_type = "numeric";
569 *search_type = "phrase";
573 *search_type = "phrase";
581 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
582 const char *input_str, int input_len, WRBUF wrbuf);
584 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
585 const char *input_str, int input_len)
587 struct zebra_map *zm = zebra_map_get (zms, reg_id);
589 wrbuf_rewind(zms->wrbuf_1);
590 wrbuf_write(zms->wrbuf_1, input_str, input_len);
591 if (!zm->replace_tokens)
595 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
596 wrbuf_buf(zms->wrbuf_1));
600 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
601 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
603 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
604 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
610 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
611 const char *input_str, int input_len, WRBUF wrbuf)
615 struct zebra_map *zm = zebra_map_get (zms, reg_id);
618 for (i = -1; i <= input_len; )
620 struct zm_token *token;
621 char replace_string[128];
625 for (token = zm->replace_tokens; !replace_in && token;
629 int replace_done = 0;
634 if (!token->token_from[j])
639 if (ex_list && strchr (ex_list, token->token_from[j]))
641 if (i+j < 0 || j+i >= input_len)
644 c = input_str[j+i] & 255;
645 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
649 replace_string[replace_out++] = c;
653 if (c != token->token_from[j])
659 const char *cp = token->token_to;
661 for (; cp && *cp; cp++)
662 replace_string[replace_out++] = *cp;
669 if (i >= 0 && i < input_len)
670 wrbuf_putc(wrbuf, input_str[i]);
677 wrbuf_write(wrbuf, replace_string, replace_out);
682 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));