2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1999-09-08 12:13:21 adam
8 * Fixed minor bug "replace"-mappings. Removed some logging messages.
10 * Revision 1.16 1999/09/07 07:19:21 adam
11 * Work on character mapping. Implemented replace rules.
13 * Revision 1.15 1999/05/26 07:49:14 adam
16 * Revision 1.14 1999/02/19 10:37:40 adam
19 * Revision 1.13 1999/02/18 15:01:04 adam
20 * Structure=key uses register type 0.
22 * Revision 1.12 1999/02/12 13:29:25 adam
23 * Implemented position-flag for registers.
25 * Revision 1.11 1998/10/13 20:09:19 adam
26 * Changed call to readconf_line.
28 * Revision 1.10 1998/06/23 15:33:37 adam
29 * Added feature to specify sort criteria in query (type 7 specifies
32 * Revision 1.9 1998/04/02 14:35:30 adam
33 * First version of Zebra that works with compiled ASN.1.
35 * Revision 1.8 1998/03/05 08:42:44 adam
36 * Minor changes to zebramap data structures. Query mapping rules changed.
38 * Revision 1.7 1998/02/10 12:03:07 adam
41 * Revision 1.6 1998/01/29 13:36:01 adam
42 * Structure word-list, free-form-text and document-text all
43 * trigger ranked search.
45 * Revision 1.5 1997/11/19 10:22:14 adam
46 * Bug fix (introduced by previous commit).
48 * Revision 1.4 1997/11/18 10:05:08 adam
49 * Changed character map facility so that admin can specify character
50 * mapping files for each register type, w, p, etc.
52 * Revision 1.3 1997/11/17 15:35:26 adam
53 * Bug fix. Relation=relevance wasn't observed.
55 * Revision 1.2 1997/10/31 12:39:30 adam
56 * Changed log message.
58 * Revision 1.1 1997/10/27 14:33:06 adam
59 * Moved towards generic character mapping depending on "structure"
60 * field in abstract syntax file. Fixed a few memory leaks. Fixed
61 * bug with negative integers when doing searches with relational
73 #define ZEBRA_MAP_TYPE_SORT 1
74 #define ZEBRA_MAP_TYPE_INDEX 2
76 #define ZEBRA_REPLACE_ANY 300
82 struct zm_token *next;
99 const char *maptab_name;
100 struct zebra_map *next;
101 struct zm_token *replace_tokens;
107 struct zebra_map *map_list;
108 char temp_map_str[2];
109 const char *temp_map_ptr[2];
110 struct zebra_map **lookup_array;
111 WRBUF wrbuf_1, wrbuf_2;
114 void zebra_maps_close (ZebraMaps zms)
116 struct zebra_map *zm = zms->map_list;
120 chrmaptab_destroy (zm->maptab);
123 wrbuf_free (zms->wrbuf_1, 1);
124 wrbuf_free (zms->wrbuf_2, 1);
125 nmem_destroy (zms->nmem);
129 static void zebra_map_read (ZebraMaps zms, const char *name)
136 struct zebra_map **zm = 0, *zp;
138 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
140 logf(LOG_WARN|LOG_ERRNO, "%s", name);
143 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
145 if (!yaz_matchstr (argv[0], "index") && argc == 2)
151 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
152 (*zm)->reg_id = argv[1][0];
153 (*zm)->maptab_name = NULL;
154 (*zm)->maptab = NULL;
155 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
156 (*zm)->completeness = 0;
157 (*zm)->positioned = 1;
158 (*zm)->replace_tokens = 0;
160 else if (!yaz_matchstr (argv[0], "sort") && 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)->type = ZEBRA_MAP_TYPE_SORT;
170 (*zm)->u.sort.entry_size = 80;
171 (*zm)->maptab = NULL;
172 (*zm)->completeness = 0;
173 (*zm)->positioned = 0;
174 (*zm)->replace_tokens = 0;
176 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
178 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
180 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
182 (*zm)->completeness = atoi (argv[1]);
184 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
186 (*zm)->positioned = atoi (argv[1]);
188 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
190 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
191 (*zm)->u.sort.entry_size = atoi (argv[1]);
193 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
195 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
196 token->next = (*zm)->replace_tokens;
197 (*zm)->replace_tokens = token;
198 token->token_from = 0;
202 int *dp = token->token_from = (int *)
203 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
212 *dp++ = ZEBRA_REPLACE_ANY;
216 *dp++ = zebra_prim(&cp);
222 char *dp = token->token_to =
223 nmem_malloc (zms->nmem, strlen(cp)+1);
231 *dp++ = zebra_prim(&cp);
242 for (zp = zms->map_list; zp; zp = zp->next)
243 zms->lookup_array[zp->reg_id] = zp;
246 static void zms_map_handle (void *p, const char *name, const char *value)
248 ZebraMaps zms = (ZebraMaps) p;
250 zebra_map_read (zms, value);
253 ZebraMaps zebra_maps_open (Res res)
255 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
258 zms->nmem = nmem_create ();
259 zms->tabpath = nmem_strdup (zms->nmem,
260 res_get_def (res, "profilePath", "."));
261 zms->map_list = NULL;
263 zms->temp_map_str[0] = '\0';
264 zms->temp_map_str[1] = '\0';
266 zms->temp_map_ptr[0] = zms->temp_map_str;
267 zms->temp_map_ptr[1] = NULL;
269 zms->lookup_array = (struct zebra_map**)
270 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
271 for (i = 0; i<256; i++)
272 zms->lookup_array[i] = 0;
273 if (!res || !res_trav (res, "index", zms, zms_map_handle))
274 zebra_map_read (zms, "default.idx");
276 zms->wrbuf_1 = wrbuf_alloc();
277 zms->wrbuf_2 = wrbuf_alloc();
281 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
283 return zms->lookup_array[reg_id];
286 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
288 struct zebra_map *zm = zebra_map_get (zms, reg_id);
291 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
292 logf (LOG_WARN, "Unknown register type: %c", reg_id);
295 zm->maptab_name = NULL;
297 zm->type = ZEBRA_MAP_TYPE_INDEX;
298 zm->completeness = 0;
299 zm->next = zms->map_list;
300 zms->map_list = zm->next;
302 zms->lookup_array[zm->reg_id & 255] = zm;
306 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
308 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
309 zm->maptab_name, 0)))
310 logf(LOG_WARN, "Failed to read character table %s",
313 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
318 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
319 const char **from, int len)
323 maptab = zebra_charmap_get (zms, reg_id);
325 return chr_map_input(maptab, from, len);
327 zms->temp_map_str[0] = **from;
330 return zms->temp_map_ptr;
334 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
335 const char *input_str, int input_len,
338 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
347 wrbuf_write (wrbuf, input_str, input_len);
358 out = chr_map_input (maptab, str, len);
361 while (out && *out && **out == *CHR_SPACE)
362 out = chr_map_input (maptab, str, len);
367 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
370 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
373 return chr_map_output (maptab, from, 1);
377 /* ------------------------------------ */
383 Z_AttributeElement **attributeList;
387 static int attr_find (AttrType *src, oid_value *attributeSetP)
389 while (src->major < src->num_attributes)
391 Z_AttributeElement *element;
393 element = src->attributeList[src->major];
394 if (src->type == *element->attributeType)
396 switch (element->which)
398 case Z_AttributeValue_numeric:
400 if (element->attributeSet && attributeSetP)
404 attrset = oid_getentbyoid (element->attributeSet);
405 *attributeSetP = attrset->value;
407 return *element->value.numeric;
409 case Z_AttributeValue_complex:
410 if (src->minor >= element->value.complex->num_list ||
411 element->value.complex->list[src->minor]->which !=
412 Z_StringOrNumeric_numeric)
415 if (element->attributeSet && attributeSetP)
419 attrset = oid_getentbyoid (element->attributeSet);
420 *attributeSetP = attrset->value;
422 return *element->value.complex->list[src->minor-1]->u.numeric;
432 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
435 src->attributeList = zapt->attributes->attributes;
436 src->num_attributes = zapt->attributes->num_attributes;
438 src->attributeList = zapt->attributeList;
439 src->num_attributes = zapt->num_attributes;
446 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
448 src->attributeList = list->attributes;
449 src->num_attributes = list->num_attributes;
455 /* ------------------------------------ */
457 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
459 struct zebra_map *zm = zebra_map_get (zms, reg_id);
461 return zm->completeness;
465 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
467 struct zebra_map *zm = zebra_map_get (zms, reg_id);
469 return zm->positioned;
473 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
475 struct zebra_map *zm = zebra_map_get (zms, reg_id);
477 return zm->type == ZEBRA_MAP_TYPE_SORT;
481 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
484 attr_init_AttrList (&use, sortAttributes->list, 1);
486 return attr_find (&use, NULL);
489 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
490 unsigned *reg_id, char **search_type, char **rank_type,
491 int *complete_flag, int *sort_flag)
493 AttrType completeness;
496 AttrType sort_relation;
497 int completeness_value;
500 int sort_relation_value;
502 attr_init_APT (&structure, zapt, 4);
503 attr_init_APT (&completeness, zapt, 6);
504 attr_init_APT (&relation, zapt, 2);
505 attr_init_APT (&sort_relation, zapt, 7);
507 completeness_value = attr_find (&completeness, NULL);
508 structure_value = attr_find (&structure, NULL);
509 relation_value = attr_find (&relation, NULL);
510 sort_relation_value = attr_find (&sort_relation, NULL);
512 if (completeness_value == 2 || completeness_value == 3)
518 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
519 *search_type = "phrase";
521 if (relation_value == 102)
528 switch (structure_value)
530 case 6: /* word list */
531 *search_type = "and-list";
533 case 105: /* free-form-text */
534 *search_type = "or-list";
536 case 106: /* document-text */
537 *search_type = "or-list";
542 case 108: /* string */
543 *search_type = "phrase";
545 case 107: /* local-number */
546 *search_type = "local";
549 case 109: /* numeric string */
551 *search_type = "numeric";
555 *search_type = "phrase";
559 *search_type = "phrase";
567 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
568 const char *input_str, int input_len, WRBUF wrbuf);
570 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
571 const char *input_str, int input_len)
573 struct zebra_map *zm = zebra_map_get (zms, reg_id);
575 wrbuf_rewind(zms->wrbuf_1);
576 wrbuf_write(zms->wrbuf_1, input_str, input_len);
577 if (!zm->replace_tokens)
581 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
582 wrbuf_buf(zms->wrbuf_1));
586 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
587 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
589 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
590 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
596 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
597 const char *input_str, int input_len, WRBUF wrbuf)
601 struct zebra_map *zm = zebra_map_get (zms, reg_id);
604 for (i = -1; i <= input_len; )
606 struct zm_token *token;
607 char replace_string[128];
611 for (token = zm->replace_tokens; !replace_in && token;
615 int replace_done = 0;
620 if (!token->token_from[j])
625 if (ex_list && strchr (ex_list, token->token_from[j]))
627 if (i+j < 0 || j+i >= input_len)
630 c = tolower(input_str[j+i]);
631 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
635 replace_string[replace_out++] = c;
639 if (c != token->token_from[j])
643 const char *cp = token->token_to;
645 for (; cp && *cp; cp++)
646 replace_string[replace_out++] = *cp;
653 if (i >= 0 && i < input_len)
654 wrbuf_putc(wrbuf, input_str[i]);
661 wrbuf_write(wrbuf, replace_string, replace_out);
666 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));