2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 2001-01-22 10:42:56 adam
8 * Added numerical sort.
10 * Revision 1.20 2000/03/02 14:35:19 adam
11 * Added structure year and date.
13 * Revision 1.19 1999/11/30 13:48:04 adam
14 * Improved installation. Updated for inclusion of YAZ header files.
16 * Revision 1.18 1999/10/15 08:27:46 adam
17 * Fixed replace handler. 8-bit fix.
19 * Revision 1.17 1999/09/08 12:13:21 adam
20 * Fixed minor bug "replace"-mappings. Removed some logging messages.
22 * Revision 1.16 1999/09/07 07:19:21 adam
23 * Work on character mapping. Implemented replace rules.
25 * Revision 1.15 1999/05/26 07:49:14 adam
28 * Revision 1.14 1999/02/19 10:37:40 adam
31 * Revision 1.13 1999/02/18 15:01:04 adam
32 * Structure=key uses register type 0.
34 * Revision 1.12 1999/02/12 13:29:25 adam
35 * Implemented position-flag for registers.
37 * Revision 1.11 1998/10/13 20:09:19 adam
38 * Changed call to readconf_line.
40 * Revision 1.10 1998/06/23 15:33:37 adam
41 * Added feature to specify sort criteria in query (type 7 specifies
44 * Revision 1.9 1998/04/02 14:35:30 adam
45 * First version of Zebra that works with compiled ASN.1.
47 * Revision 1.8 1998/03/05 08:42:44 adam
48 * Minor changes to zebramap data structures. Query mapping rules changed.
50 * Revision 1.7 1998/02/10 12:03:07 adam
53 * Revision 1.6 1998/01/29 13:36:01 adam
54 * Structure word-list, free-form-text and document-text all
55 * trigger ranked search.
57 * Revision 1.5 1997/11/19 10:22:14 adam
58 * Bug fix (introduced by previous commit).
60 * Revision 1.4 1997/11/18 10:05:08 adam
61 * Changed character map facility so that admin can specify character
62 * mapping files for each register type, w, p, etc.
64 * Revision 1.3 1997/11/17 15:35:26 adam
65 * Bug fix. Relation=relevance wasn't observed.
67 * Revision 1.2 1997/10/31 12:39:30 adam
68 * Changed log message.
70 * Revision 1.1 1997/10/27 14:33:06 adam
71 * Moved towards generic character mapping depending on "structure"
72 * field in abstract syntax file. Fixed a few memory leaks. Fixed
73 * bug with negative integers when doing searches with relational
81 #include <yaz/yaz-util.h>
85 #define ZEBRA_MAP_TYPE_SORT 1
86 #define ZEBRA_MAP_TYPE_INDEX 2
88 #define ZEBRA_REPLACE_ANY 300
94 struct zm_token *next;
111 const char *maptab_name;
112 struct zebra_map *next;
113 struct zm_token *replace_tokens;
119 struct zebra_map *map_list;
120 char temp_map_str[2];
121 const char *temp_map_ptr[2];
122 struct zebra_map **lookup_array;
123 WRBUF wrbuf_1, wrbuf_2;
126 void zebra_maps_close (ZebraMaps zms)
128 struct zebra_map *zm = zms->map_list;
132 chrmaptab_destroy (zm->maptab);
135 wrbuf_free (zms->wrbuf_1, 1);
136 wrbuf_free (zms->wrbuf_2, 1);
137 nmem_destroy (zms->nmem);
141 static void zebra_map_read (ZebraMaps zms, const char *name)
148 struct zebra_map **zm = 0, *zp;
150 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
152 logf(LOG_WARN|LOG_ERRNO, "%s", name);
155 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
157 if (!yaz_matchstr (argv[0], "index") && argc == 2)
163 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
164 (*zm)->reg_id = argv[1][0];
165 (*zm)->maptab_name = NULL;
166 (*zm)->maptab = NULL;
167 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
168 (*zm)->completeness = 0;
169 (*zm)->positioned = 1;
170 (*zm)->replace_tokens = 0;
172 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
178 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
179 (*zm)->reg_id = argv[1][0];
180 (*zm)->maptab_name = NULL;
181 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
182 (*zm)->u.sort.entry_size = 80;
183 (*zm)->maptab = NULL;
184 (*zm)->completeness = 0;
185 (*zm)->positioned = 0;
186 (*zm)->replace_tokens = 0;
188 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
190 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
192 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
194 (*zm)->completeness = atoi (argv[1]);
196 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
198 (*zm)->positioned = atoi (argv[1]);
200 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
202 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
203 (*zm)->u.sort.entry_size = atoi (argv[1]);
205 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
207 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
208 token->next = (*zm)->replace_tokens;
209 (*zm)->replace_tokens = token;
211 logf (LOG_LOG, "replace %s", argv[1]);
213 token->token_from = 0;
217 int *dp = token->token_from = (int *)
218 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
227 *dp++ = ZEBRA_REPLACE_ANY;
232 *dp++ = zebra_prim(&cp);
234 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
242 char *dp = token->token_to =
243 nmem_malloc (zms->nmem, strlen(cp)+1);
251 *dp++ = zebra_prim(&cp);
262 for (zp = zms->map_list; zp; zp = zp->next)
263 zms->lookup_array[zp->reg_id] = zp;
266 static void zms_map_handle (void *p, const char *name, const char *value)
268 ZebraMaps zms = (ZebraMaps) p;
270 zebra_map_read (zms, value);
273 ZebraMaps zebra_maps_open (Res res)
275 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
278 zms->nmem = nmem_create ();
279 zms->tabpath = nmem_strdup (zms->nmem,
280 res_get_def (res, "profilePath", "."));
281 zms->map_list = NULL;
283 zms->temp_map_str[0] = '\0';
284 zms->temp_map_str[1] = '\0';
286 zms->temp_map_ptr[0] = zms->temp_map_str;
287 zms->temp_map_ptr[1] = NULL;
289 zms->lookup_array = (struct zebra_map**)
290 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
291 for (i = 0; i<256; i++)
292 zms->lookup_array[i] = 0;
293 if (!res || !res_trav (res, "index", zms, zms_map_handle))
294 zebra_map_read (zms, "default.idx");
296 zms->wrbuf_1 = wrbuf_alloc();
297 zms->wrbuf_2 = wrbuf_alloc();
301 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
303 return zms->lookup_array[reg_id];
306 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
308 struct zebra_map *zm = zebra_map_get (zms, reg_id);
311 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
312 logf (LOG_WARN, "Unknown register type: %c", reg_id);
315 zm->maptab_name = nmem_strdup (zms->nmem, "@");
317 zm->type = ZEBRA_MAP_TYPE_INDEX;
318 zm->completeness = 0;
319 zm->next = zms->map_list;
320 zms->map_list = zm->next;
322 zms->lookup_array[zm->reg_id & 255] = zm;
326 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
328 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
329 zm->maptab_name, 0)))
330 logf(LOG_WARN, "Failed to read character table %s",
333 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
338 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
339 const char **from, int len)
343 maptab = zebra_charmap_get (zms, reg_id);
345 return chr_map_input(maptab, from, len);
347 zms->temp_map_str[0] = **from;
350 return zms->temp_map_ptr;
354 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
355 const char *input_str, int input_len,
358 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
367 wrbuf_write (wrbuf, input_str, input_len);
378 out = chr_map_input (maptab, str, len);
381 while (out && *out && **out == *CHR_SPACE)
382 out = chr_map_input (maptab, str, len);
387 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
390 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
393 return chr_map_output (maptab, from, 1);
397 /* ------------------------------------ */
403 Z_AttributeElement **attributeList;
407 static int attr_find (AttrType *src, oid_value *attributeSetP)
409 while (src->major < src->num_attributes)
411 Z_AttributeElement *element;
413 element = src->attributeList[src->major];
414 if (src->type == *element->attributeType)
416 switch (element->which)
418 case Z_AttributeValue_numeric:
420 if (element->attributeSet && attributeSetP)
424 attrset = oid_getentbyoid (element->attributeSet);
425 *attributeSetP = attrset->value;
427 return *element->value.numeric;
429 case Z_AttributeValue_complex:
430 if (src->minor >= element->value.complex->num_list ||
431 element->value.complex->list[src->minor]->which !=
432 Z_StringOrNumeric_numeric)
435 if (element->attributeSet && attributeSetP)
439 attrset = oid_getentbyoid (element->attributeSet);
440 *attributeSetP = attrset->value;
442 return *element->value.complex->list[src->minor-1]->u.numeric;
452 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
455 src->attributeList = zapt->attributes->attributes;
456 src->num_attributes = zapt->attributes->num_attributes;
458 src->attributeList = zapt->attributeList;
459 src->num_attributes = zapt->num_attributes;
466 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
468 src->attributeList = list->attributes;
469 src->num_attributes = list->num_attributes;
475 /* ------------------------------------ */
477 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
479 struct zebra_map *zm = zebra_map_get (zms, reg_id);
481 return zm->completeness;
485 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
487 struct zebra_map *zm = zebra_map_get (zms, reg_id);
489 return zm->positioned;
493 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
495 struct zebra_map *zm = zebra_map_get (zms, reg_id);
497 return zm->type == ZEBRA_MAP_TYPE_SORT;
501 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
507 attr_init_AttrList (&use, sortAttributes->list, 1);
508 attr_init_AttrList (&structure, sortAttributes->list, 4);
511 structure_value = attr_find (&structure, 0);
512 if (structure_value == 109)
514 return attr_find (&use, NULL);
517 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
518 unsigned *reg_id, char **search_type, char **rank_type,
519 int *complete_flag, int *sort_flag)
521 AttrType completeness;
524 AttrType sort_relation;
525 int completeness_value;
528 int sort_relation_value;
530 attr_init_APT (&structure, zapt, 4);
531 attr_init_APT (&completeness, zapt, 6);
532 attr_init_APT (&relation, zapt, 2);
533 attr_init_APT (&sort_relation, zapt, 7);
535 completeness_value = attr_find (&completeness, NULL);
536 structure_value = attr_find (&structure, NULL);
537 relation_value = attr_find (&relation, NULL);
538 sort_relation_value = attr_find (&sort_relation, NULL);
540 if (completeness_value == 2 || completeness_value == 3)
546 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
547 *search_type = "phrase";
549 if (relation_value == 102)
556 switch (structure_value)
558 case 6: /* word list */
559 *search_type = "and-list";
561 case 105: /* free-form-text */
562 *search_type = "or-list";
564 case 106: /* document-text */
565 *search_type = "or-list";
570 case 108: /* string */
571 *search_type = "phrase";
573 case 107: /* local-number */
574 *search_type = "local";
577 case 109: /* numeric string */
579 *search_type = "numeric";
583 *search_type = "phrase";
587 *search_type = "phrase";
591 *search_type = "phrase";
595 *search_type = "phrase";
603 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
604 const char *input_str, int input_len, WRBUF wrbuf);
606 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
607 const char *input_str, int input_len)
609 struct zebra_map *zm = zebra_map_get (zms, reg_id);
611 wrbuf_rewind(zms->wrbuf_1);
612 wrbuf_write(zms->wrbuf_1, input_str, input_len);
613 if (!zm || !zm->replace_tokens)
617 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
618 wrbuf_buf(zms->wrbuf_1));
622 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
623 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
625 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
626 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
632 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
633 const char *input_str, int input_len, WRBUF wrbuf)
637 struct zebra_map *zm = zebra_map_get (zms, reg_id);
640 for (i = -1; i <= input_len; )
642 struct zm_token *token;
643 char replace_string[128];
647 for (token = zm->replace_tokens; !replace_in && token;
651 int replace_done = 0;
656 if (!token->token_from[j])
661 if (ex_list && strchr (ex_list, token->token_from[j]))
663 if (i+j < 0 || j+i >= input_len)
666 c = input_str[j+i] & 255;
667 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
671 replace_string[replace_out++] = c;
675 if (c != token->token_from[j])
681 const char *cp = token->token_to;
683 for (; cp && *cp; cp++)
684 replace_string[replace_out++] = *cp;
691 if (i >= 0 && i < input_len)
692 wrbuf_putc(wrbuf, input_str[i]);
699 wrbuf_write(wrbuf, replace_string, replace_out);
704 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));