1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
16 #include <yaz/xmalloc.h>
18 #include <yaz/icu_I18N.h>
20 #include <yaz/stemmer.h>
24 #include <yaz/nmem_xml.h>
30 #include <unicode/ustring.h> /* some more string fcns*/
31 #include <unicode/uchar.h> /* char names */
33 enum icu_chain_step_type {
34 ICU_chain_step_type_none,
35 ICU_chain_step_type_display, /* convert to utf8 display format */
36 ICU_chain_step_type_casemap, /* apply utf16 charmap */
37 ICU_chain_step_type_transform, /* apply utf16 transform */
38 ICU_chain_step_type_tokenize, /* apply utf16 tokenization */
39 ICU_chain_step_type_transliterate, /* apply utf16 tokenization */
40 YAZ_chain_step_type_stemming /* apply utf16 stemming (YAZ) */
45 /* type and action object */
46 enum icu_chain_step_type type;
48 struct icu_casemap *casemap;
49 struct icu_transform *transform;
50 struct icu_tokenizer *tokenizer;
51 yaz_stemmer_p stemmer;
53 struct icu_chain_step *previous;
64 /* linked list of chain steps */
65 struct icu_chain_step *csteps;
68 int icu_check_status(UErrorCode status)
70 if (U_FAILURE(status))
72 yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
78 static struct icu_chain_step *icu_chain_insert_step(
79 struct icu_chain *chain, enum icu_chain_step_type type,
80 const uint8_t *rule, UErrorCode *status)
82 struct icu_chain_step *step = 0;
84 if (!chain || !type || !rule)
87 step = (struct icu_chain_step *) xmalloc(sizeof(*step));
90 /* create auxilary objects */
93 case ICU_chain_step_type_display:
95 case ICU_chain_step_type_casemap:
96 step->u.casemap = icu_casemap_create(rule[0], status);
98 case ICU_chain_step_type_transform:
99 /* rule omitted. Only ID used */
100 step->u.transform = icu_transform_create((const char *) rule, 'f',
103 case ICU_chain_step_type_tokenize:
104 step->u.tokenizer = icu_tokenizer_create(chain->locale,
105 (char) rule[0], status);
107 case ICU_chain_step_type_transliterate:
108 /* we pass a dummy ID to utrans_openU.. */
109 step->u.transform = icu_transform_create("custom", 'f',
110 (const char *) rule, status);
112 case YAZ_chain_step_type_stemming:
113 step->u.stemmer = yaz_stemmer_create(chain->locale,
114 (const char *) rule, status);
119 step->previous = chain->csteps;
120 chain->csteps = step;
126 static void icu_chain_step_destroy(struct icu_chain_step *step)
131 icu_chain_step_destroy(step->previous);
135 case ICU_chain_step_type_display:
137 case ICU_chain_step_type_casemap:
138 icu_casemap_destroy(step->u.casemap);
140 case ICU_chain_step_type_transform:
141 case ICU_chain_step_type_transliterate:
142 icu_transform_destroy(step->u.transform);
144 case ICU_chain_step_type_tokenize:
145 icu_tokenizer_destroy(step->u.tokenizer);
147 case YAZ_chain_step_type_stemming:
148 yaz_stemmer_destroy(step->u.stemmer);
156 struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
158 struct icu_chain_step *step = 0;
159 struct icu_chain_step **sp = &step;
162 *sp = (struct icu_chain_step *) xmalloc(sizeof(**sp));
163 (*sp)->type = old->type;
167 case ICU_chain_step_type_display:
169 case ICU_chain_step_type_casemap:
170 (*sp)->u.casemap = icu_casemap_clone(old->u.casemap);
172 case ICU_chain_step_type_transform:
173 case ICU_chain_step_type_transliterate:
174 (*sp)->u.transform = icu_transform_clone(old->u.transform);
176 case ICU_chain_step_type_tokenize:
177 (*sp)->u.tokenizer = icu_tokenizer_clone(old->u.tokenizer);
179 case YAZ_chain_step_type_stemming:
180 (*sp)->u.stemmer = yaz_stemmer_clone(old->u.stemmer);
182 case ICU_chain_step_type_none:
186 sp = &(*sp)->previous;
192 struct icu_chain *icu_chain_create(const char *locale, int sort,
195 struct icu_chain *chain;
196 UCollator *coll = ucol_open(locale, status);
198 if (U_FAILURE(*status))
201 chain = (struct icu_chain *) xmalloc(sizeof(*chain));
203 chain->locale = xstrdup(locale);
211 void icu_chain_destroy(struct icu_chain *chain)
216 ucol_close(chain->coll);
219 icu_iter_destroy(chain->iter);
220 icu_chain_step_destroy(chain->csteps);
221 xfree(chain->locale);
226 struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
232 struct icu_chain *chain = 0;
235 *status = U_ZERO_ERROR;
237 if (xml_node && xml_node->type == XML_ELEMENT_NODE)
239 xmlChar *xml_locale = xmlGetProp((xmlNode *) xml_node,
240 (xmlChar *) "locale");
243 chain = icu_chain_create((const char *) xml_locale, sort, status);
251 nmem = nmem_create();
252 for (node = xml_node->children; node; node = node->next)
255 struct icu_chain_step *step = 0;
256 struct _xmlAttr *attr;
259 if (node->type != XML_ELEMENT_NODE)
262 for (attr = node->properties; attr; attr = attr->next)
264 if (!strcmp((const char *) attr->name, "rule"))
266 rule = nmem_text_node_cdata(attr->children, nmem);
270 yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
271 "element '%s'", attr->name, node->name);
276 if (!rule && node->children)
277 rule = nmem_text_node_cdata(node->children, nmem);
279 if (!strcmp((const char *) node->name, "casemap"))
280 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
281 (const uint8_t *) rule, status);
282 else if (!strcmp((const char *) node->name, "transform"))
283 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
284 (const uint8_t *) rule, status);
285 else if (!strcmp((const char *) node->name, "transliterate"))
286 step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate,
287 (const uint8_t *) rule, status);
288 else if (!strcmp((const char *) node->name, "tokenize"))
289 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
290 (const uint8_t *) rule, status);
291 else if (!strcmp((const char *) node->name, "display"))
292 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
293 (const uint8_t *) "", status);
294 else if (!strcmp((const char *) node->name, "stemming"))
295 step = icu_chain_insert_step(chain, YAZ_chain_step_type_stemming,
296 (const uint8_t *) rule, status);
297 else if (!strcmp((const char *) node->name, "normalize"))
299 yaz_log(YLOG_WARN, "Element %s is deprecated. "
300 "Use transform instead", node->name);
301 step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
302 (const uint8_t *) rule, status);
304 else if (!strcmp((const char *) node->name, "index")
305 || !strcmp((const char *) node->name, "sortkey"))
307 yaz_log(YLOG_WARN, "Element %s is no longer needed. "
308 "Remove it from the configuration", node->name);
312 yaz_log(YLOG_WARN, "Unknown element %s", node->name);
316 if (step && U_FAILURE(*status))
325 icu_chain_destroy(chain);
332 struct icu_chain *chain;
333 struct icu_buf_utf16 *last;
335 struct icu_buf_utf8 *display;
336 struct icu_buf_utf8 *sort8;
337 struct icu_buf_utf8 *result;
338 struct icu_buf_utf16 *input;
340 struct icu_chain_step *steps;
343 void icu_utf16_print(struct icu_buf_utf16 *src16)
345 UErrorCode status = U_ZERO_ERROR;
347 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
348 icu_utf16_to_utf8(dst8, src16, &status);
350 assert(status != 1234);
351 if (U_FAILURE(status))
357 p = icu_buf_utf8_to_cstr(dst8);
360 icu_buf_utf8_destroy(dst8);
363 struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
364 struct icu_chain_step *step,
365 struct icu_buf_utf16 *src)
371 struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
375 case ICU_chain_step_type_casemap:
378 struct icu_buf_utf16 *src = dst;
380 dst = icu_buf_utf16_create(0);
381 icu_casemap_casemap(step->u.casemap, dst, src, &iter->status,
382 iter->chain->locale);
383 icu_buf_utf16_destroy(src);
386 case ICU_chain_step_type_tokenize:
389 struct icu_buf_utf16 *src = dst;
391 icu_tokenizer_attach(step->u.tokenizer, src, &iter->status);
392 icu_buf_utf16_destroy(src);
394 dst = icu_buf_utf16_create(0);
395 iter->status = U_ZERO_ERROR;
396 if (!icu_tokenizer_next_token(step->u.tokenizer, dst, &iter->status))
398 icu_buf_utf16_destroy(dst);
402 case ICU_chain_step_type_transform:
403 case ICU_chain_step_type_transliterate:
406 struct icu_buf_utf16 *src = dst;
407 dst = icu_buf_utf16_create(0);
408 icu_transform_trans(step->u.transform, dst, src, &iter->status);
409 icu_buf_utf16_destroy(src);
412 case ICU_chain_step_type_display:
414 icu_utf16_to_utf8(iter->display, dst, &iter->status);
416 case YAZ_chain_step_type_stemming:
419 struct icu_buf_utf16 *src = dst;
420 dst = icu_buf_utf16_create(0);
421 yaz_stemmer_stem(step->u.stemmer, dst, src, &iter->status);
422 icu_buf_utf16_destroy(src);
432 yaz_icu_iter_t icu_iter_create(struct icu_chain *chain)
434 yaz_icu_iter_t iter = xmalloc(sizeof(*iter));
436 iter->status = U_ZERO_ERROR;
437 iter->display = icu_buf_utf8_create(0);
438 iter->sort8 = icu_buf_utf8_create(0);
439 iter->result = icu_buf_utf8_create(0);
440 iter->last = 0; /* no last returned string (yet) */
441 iter->steps = icu_chain_step_clone(chain->csteps);
447 void icu_iter_first(yaz_icu_iter_t iter, const char *src8cstr)
450 icu_buf_utf16_destroy(iter->input);
451 iter->input = icu_buf_utf16_create(0);
452 iter->token_count = 0;
453 /* fill and assign input string.. It will be 0 after
455 icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status);
458 void icu_iter_destroy(yaz_icu_iter_t iter)
462 icu_buf_utf8_destroy(iter->display);
463 icu_buf_utf8_destroy(iter->sort8);
464 icu_buf_utf8_destroy(iter->result);
466 icu_buf_utf16_destroy(iter->input);
467 icu_chain_step_destroy(iter->steps);
472 int icu_iter_next(yaz_icu_iter_t iter)
474 if (!iter->input && iter->last == 0)
478 /* on first call, iter->input is the input string. Thereafter: 0. */
479 iter->last = icu_iter_invoke(iter, iter->steps ?
480 iter->steps : iter->chain->csteps,
489 if (iter->chain->sort)
491 icu_sortkey8_from_utf16(iter->chain->coll,
492 iter->sort8, iter->last,
495 icu_utf16_to_utf8(iter->result, iter->last, &iter->status);
496 icu_buf_utf16_destroy(iter->last);
502 const char *icu_iter_get_norm(yaz_icu_iter_t iter)
504 return icu_buf_utf8_to_cstr(iter->result);
507 const char *icu_iter_get_sortkey(yaz_icu_iter_t iter)
509 return icu_buf_utf8_to_cstr(iter->sort8);
512 const char *icu_iter_get_display(yaz_icu_iter_t iter)
514 return icu_buf_utf8_to_cstr(iter->display);
517 int icu_iter_get_token_number(yaz_icu_iter_t iter)
519 return iter->token_count;
522 int icu_chain_assign_cstr(struct icu_chain *chain, const char *src8cstr,
526 icu_iter_destroy(chain->iter);
527 chain->iter = icu_iter_create(chain);
528 icu_iter_first(chain->iter, src8cstr);
532 int icu_chain_next_token(struct icu_chain *chain, UErrorCode *status)
534 *status = U_ZERO_ERROR;
535 return icu_iter_next(chain->iter);
538 int icu_chain_token_number(struct icu_chain *chain)
540 if (chain && chain->iter)
541 return chain->iter->token_count;
545 const char *icu_chain_token_display(struct icu_chain *chain)
548 return icu_iter_get_display(chain->iter);
552 const char *icu_chain_token_norm(struct icu_chain *chain)
555 return icu_iter_get_norm(chain->iter);
559 const char *icu_chain_token_sortkey(struct icu_chain *chain)
562 return icu_iter_get_sortkey(chain->iter);
566 #endif /* YAZ_HAVE_ICU */
571 * c-file-style: "Stroustrup"
572 * indent-tabs-mode: nil
574 * vim: shiftwidth=4 tabstop=8 expandtab