2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1999-09-07 07:19:21 adam
8 * Work on character mapping. Implemented replace rules.
10 * Revision 1.15 1999/05/26 07:49:14 adam
13 * Revision 1.14 1999/02/19 10:37:40 adam
16 * Revision 1.13 1999/02/18 15:01:04 adam
17 * Structure=key uses register type 0.
19 * Revision 1.12 1999/02/12 13:29:25 adam
20 * Implemented position-flag for registers.
22 * Revision 1.11 1998/10/13 20:09:19 adam
23 * Changed call to readconf_line.
25 * Revision 1.10 1998/06/23 15:33:37 adam
26 * Added feature to specify sort criteria in query (type 7 specifies
29 * Revision 1.9 1998/04/02 14:35:30 adam
30 * First version of Zebra that works with compiled ASN.1.
32 * Revision 1.8 1998/03/05 08:42:44 adam
33 * Minor changes to zebramap data structures. Query mapping rules changed.
35 * Revision 1.7 1998/02/10 12:03:07 adam
38 * Revision 1.6 1998/01/29 13:36:01 adam
39 * Structure word-list, free-form-text and document-text all
40 * trigger ranked search.
42 * Revision 1.5 1997/11/19 10:22:14 adam
43 * Bug fix (introduced by previous commit).
45 * Revision 1.4 1997/11/18 10:05:08 adam
46 * Changed character map facility so that admin can specify character
47 * mapping files for each register type, w, p, etc.
49 * Revision 1.3 1997/11/17 15:35:26 adam
50 * Bug fix. Relation=relevance wasn't observed.
52 * Revision 1.2 1997/10/31 12:39:30 adam
53 * Changed log message.
55 * Revision 1.1 1997/10/27 14:33:06 adam
56 * Moved towards generic character mapping depending on "structure"
57 * field in abstract syntax file. Fixed a few memory leaks. Fixed
58 * bug with negative integers when doing searches with relational
70 #define ZEBRA_MAP_TYPE_SORT 1
71 #define ZEBRA_MAP_TYPE_INDEX 2
77 struct zm_token *next;
94 const char *maptab_name;
95 struct zebra_map *next;
96 struct zm_token *replace_tokens;
102 struct zebra_map *map_list;
103 char temp_map_str[2];
104 const char *temp_map_ptr[2];
105 struct zebra_map **lookup_array;
106 WRBUF wrbuf_1, wrbuf_2;
109 void zebra_maps_close (ZebraMaps zms)
111 struct zebra_map *zm = zms->map_list;
115 chrmaptab_destroy (zm->maptab);
118 wrbuf_free (zms->wrbuf_1, 1);
119 wrbuf_free (zms->wrbuf_2, 1);
120 nmem_destroy (zms->nmem);
124 static void zebra_map_read (ZebraMaps zms, const char *name)
131 struct zebra_map **zm = 0, *zp;
133 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
135 logf(LOG_WARN|LOG_ERRNO, "%s", name);
138 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
140 if (!yaz_matchstr (argv[0], "index") && argc == 2)
146 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
147 (*zm)->reg_id = argv[1][0];
148 (*zm)->maptab_name = NULL;
149 (*zm)->maptab = NULL;
150 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
151 (*zm)->completeness = 0;
152 (*zm)->positioned = 1;
153 (*zm)->replace_tokens = 0;
155 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
161 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
162 (*zm)->reg_id = argv[1][0];
163 (*zm)->maptab_name = NULL;
164 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
165 (*zm)->u.sort.entry_size = 80;
166 (*zm)->maptab = NULL;
167 (*zm)->completeness = 0;
168 (*zm)->positioned = 0;
170 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
172 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
174 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
176 (*zm)->completeness = atoi (argv[1]);
178 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
180 (*zm)->positioned = atoi (argv[1]);
182 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
184 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
185 (*zm)->u.sort.entry_size = atoi (argv[1]);
187 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
189 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
191 token->next = (*zm)->replace_tokens;
192 (*zm)->replace_tokens = token;
193 dp = token->token_from = nmem_strdup (zms->nmem, cp = argv[1]);
202 *dp++ = zebra_prim(&cp);
208 dp = token->token_to = nmem_strdup (zms->nmem, cp = argv[2]);
217 *dp++ = zebra_prim(&cp);
229 for (zp = zms->map_list; zp; zp = zp->next)
230 zms->lookup_array[zp->reg_id] = zp;
233 static void zms_map_handle (void *p, const char *name, const char *value)
235 ZebraMaps zms = (ZebraMaps) p;
237 zebra_map_read (zms, value);
240 ZebraMaps zebra_maps_open (Res res)
242 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
245 zms->nmem = nmem_create ();
246 zms->tabpath = nmem_strdup (zms->nmem,
247 res_get_def (res, "profilePath", "."));
248 zms->map_list = NULL;
250 zms->temp_map_str[0] = '\0';
251 zms->temp_map_str[1] = '\0';
253 zms->temp_map_ptr[0] = zms->temp_map_str;
254 zms->temp_map_ptr[1] = NULL;
256 zms->lookup_array = (struct zebra_map**)
257 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
258 for (i = 0; i<256; i++)
259 zms->lookup_array[i] = 0;
260 if (!res || !res_trav (res, "index", zms, zms_map_handle))
261 zebra_map_read (zms, "default.idx");
263 zms->wrbuf_1 = wrbuf_alloc();
264 zms->wrbuf_2 = wrbuf_alloc();
268 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
270 return zms->lookup_array[reg_id];
273 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
275 struct zebra_map *zm = zebra_map_get (zms, reg_id);
278 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
279 logf (LOG_WARN, "Unknown register type: %c", reg_id);
282 zm->maptab_name = NULL;
284 zm->type = ZEBRA_MAP_TYPE_INDEX;
285 zm->completeness = 0;
286 zm->next = zms->map_list;
287 zms->map_list = zm->next;
289 zms->lookup_array[zm->reg_id & 255] = zm;
293 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
295 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
296 zm->maptab_name, 0)))
297 logf(LOG_WARN, "Failed to read character table %s",
300 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
305 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
306 const char **from, int len)
310 maptab = zebra_charmap_get (zms, reg_id);
312 return chr_map_input(maptab, from, len);
314 zms->temp_map_str[0] = **from;
317 return zms->temp_map_ptr;
321 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
322 const char *input_str, int input_len,
325 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
334 wrbuf_write (wrbuf, input_str, input_len);
345 out = chr_map_input (maptab, str, len);
348 while (out && *out && **out == *CHR_SPACE)
349 out = chr_map_input (maptab, str, len);
354 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
357 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
360 return chr_map_output (maptab, from, 1);
364 /* ------------------------------------ */
370 Z_AttributeElement **attributeList;
374 static int attr_find (AttrType *src, oid_value *attributeSetP)
376 while (src->major < src->num_attributes)
378 Z_AttributeElement *element;
380 element = src->attributeList[src->major];
381 if (src->type == *element->attributeType)
383 switch (element->which)
385 case Z_AttributeValue_numeric:
387 if (element->attributeSet && attributeSetP)
391 attrset = oid_getentbyoid (element->attributeSet);
392 *attributeSetP = attrset->value;
394 return *element->value.numeric;
396 case Z_AttributeValue_complex:
397 if (src->minor >= element->value.complex->num_list ||
398 element->value.complex->list[src->minor]->which !=
399 Z_StringOrNumeric_numeric)
402 if (element->attributeSet && attributeSetP)
406 attrset = oid_getentbyoid (element->attributeSet);
407 *attributeSetP = attrset->value;
409 return *element->value.complex->list[src->minor-1]->u.numeric;
419 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
422 src->attributeList = zapt->attributes->attributes;
423 src->num_attributes = zapt->attributes->num_attributes;
425 src->attributeList = zapt->attributeList;
426 src->num_attributes = zapt->num_attributes;
433 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
435 src->attributeList = list->attributes;
436 src->num_attributes = list->num_attributes;
442 /* ------------------------------------ */
444 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
446 struct zebra_map *zm = zebra_map_get (zms, reg_id);
448 return zm->completeness;
452 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
454 struct zebra_map *zm = zebra_map_get (zms, reg_id);
456 return zm->positioned;
460 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
462 struct zebra_map *zm = zebra_map_get (zms, reg_id);
464 return zm->type == ZEBRA_MAP_TYPE_SORT;
468 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
471 attr_init_AttrList (&use, sortAttributes->list, 1);
473 return attr_find (&use, NULL);
476 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
477 unsigned *reg_id, char **search_type, char **rank_type,
478 int *complete_flag, int *sort_flag)
480 AttrType completeness;
483 AttrType sort_relation;
484 int completeness_value;
487 int sort_relation_value;
489 attr_init_APT (&structure, zapt, 4);
490 attr_init_APT (&completeness, zapt, 6);
491 attr_init_APT (&relation, zapt, 2);
492 attr_init_APT (&sort_relation, zapt, 7);
494 completeness_value = attr_find (&completeness, NULL);
495 structure_value = attr_find (&structure, NULL);
496 relation_value = attr_find (&relation, NULL);
497 sort_relation_value = attr_find (&sort_relation, NULL);
499 if (completeness_value == 2 || completeness_value == 3)
505 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
506 *search_type = "phrase";
508 if (relation_value == 102)
515 switch (structure_value)
517 case 6: /* word list */
518 *search_type = "and-list";
520 case 105: /* free-form-text */
521 *search_type = "or-list";
523 case 106: /* document-text */
524 *search_type = "or-list";
529 case 108: /* string */
530 *search_type = "phrase";
532 case 107: /* local-number */
533 *search_type = "local";
536 case 109: /* numeric string */
538 *search_type = "numeric";
542 *search_type = "phrase";
546 *search_type = "phrase";
554 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
555 const char *input_str, int input_len, WRBUF wrbuf);
557 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
558 const char *input_str, int input_len)
560 struct zebra_map *zm = zebra_map_get (zms, reg_id);
562 wrbuf_rewind(zms->wrbuf_1);
563 wrbuf_write(zms->wrbuf_1, input_str, input_len);
564 if (!zm->replace_tokens)
568 logf (LOG_LOG, "zebra_replace");
569 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
570 wrbuf_buf(zms->wrbuf_1));
574 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
575 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
577 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
578 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
584 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
585 const char *input_str, int input_len, WRBUF wrbuf)
589 struct zebra_map *zm = zebra_map_get (zms, reg_id);
592 for (i = -1; i <= input_len; )
594 struct zm_token *token;
595 char replace_string[128];
599 for (token = zm->replace_tokens; !replace_in && token;
603 int replace_done = 0;
608 if (!token->token_from[j])
613 if (ex_list && strchr (ex_list, token->token_from[j]))
615 if (i+j < 0 || j+i >= input_len)
618 c = tolower(input_str[j+i]);
619 if (token->token_from[j] == '.')
623 replace_string[replace_out++] = c;
627 if (c != token->token_from[j])
631 const char *cp = token->token_to;
633 for (; cp && *cp; cp++)
634 replace_string[replace_out++] = *cp;
641 if (i >= 0 && i < input_len)
642 wrbuf_putc(wrbuf, input_str[i]);
649 wrbuf_write(wrbuf, replace_string, replace_out);
654 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));