/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2010 Index Data
+ * Copyright (C) 1995-2012 Index Data
* See the file LICENSE for details.
*/
#include <yaz/icu_I18N.h>
+#include <yaz/stemmer.h>
+
#include <yaz/log.h>
#include <yaz/nmem.h>
#include <yaz/nmem_xml.h>
enum icu_chain_step_type {
ICU_chain_step_type_none,
- ICU_chain_step_type_display, /* convert to utf8 display format */
- ICU_chain_step_type_casemap, /* apply utf16 charmap */
- ICU_chain_step_type_transform, /* apply utf16 transform */
- ICU_chain_step_type_tokenize, /* apply utf16 tokenization */
- ICU_chain_step_type_transliterate /* apply utf16 tokenization */
+ ICU_chain_step_type_display, /* convert to utf8 display format */
+ ICU_chain_step_type_casemap, /* apply utf16 charmap */
+ ICU_chain_step_type_transform, /* apply utf16 transform */
+ ICU_chain_step_type_tokenize, /* apply utf16 tokenization */
+ ICU_chain_step_type_transliterate, /* apply utf16 tokenization */
+ YAZ_chain_step_type_stemming /* apply utf16 stemming (YAZ) */
};
struct icu_chain_step
/* type and action object */
enum icu_chain_step_type type;
union {
- struct icu_casemap *casemap;
+ struct icu_casemap *casemap;
struct icu_transform *transform;
struct icu_tokenizer *tokenizer;
+ yaz_stemmer_p stemmer;
} u;
struct icu_chain_step *previous;
};
0, status);
break;
case ICU_chain_step_type_tokenize:
- step->u.tokenizer = icu_tokenizer_create((char *) chain->locale,
+ step->u.tokenizer = icu_tokenizer_create(chain->locale,
(char) rule[0], status);
break;
case ICU_chain_step_type_transliterate:
step->u.transform = icu_transform_create("custom", 'f',
(const char *) rule, status);
break;
+ case YAZ_chain_step_type_stemming:
+ step->u.stemmer = yaz_stemmer_create(chain->locale,
+ (const char *) rule, status);
+ break;
default:
break;
}
case ICU_chain_step_type_tokenize:
icu_tokenizer_destroy(step->u.tokenizer);
break;
+ case YAZ_chain_step_type_stemming:
+ yaz_stemmer_destroy(step->u.stemmer);
+ break;
default:
break;
}
case ICU_chain_step_type_tokenize:
(*sp)->u.tokenizer = icu_tokenizer_clone(old->u.tokenizer);
break;
+ case YAZ_chain_step_type_stemming:
+ (*sp)->u.stemmer = yaz_stemmer_clone(old->u.stemmer);
+ break;
case ICU_chain_step_type_none:
break;
}
struct icu_chain *icu_chain_create(const char *locale, int sort,
UErrorCode *status)
{
- struct icu_chain *chain
- = (struct icu_chain *) xmalloc(sizeof(*chain));
+ struct icu_chain *chain;
+ UCollator *coll = ucol_open(locale, status);
- *status = U_ZERO_ERROR;
+ if (U_FAILURE(*status))
+ return 0;
+ chain = (struct icu_chain *) xmalloc(sizeof(*chain));
chain->iter = 0;
chain->locale = xstrdup(locale);
-
chain->sort = sort;
-
- chain->coll = ucol_open((const char *) chain->locale, status);
-
- if (U_FAILURE(*status))
- return 0;
-
+ chain->coll = coll;
chain->csteps = 0;
return chain;
}
}
-static struct icu_chain_step *icu_chain_insert_step(
- struct icu_chain *chain, enum icu_chain_step_type type,
- const uint8_t *rule, UErrorCode *status);
-
struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
int sort,
UErrorCode *status)
*status = U_ZERO_ERROR;
- if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
- return 0;
-
+ if (xml_node && xml_node->type == XML_ELEMENT_NODE)
{
xmlChar *xml_locale = xmlGetProp((xmlNode *) xml_node,
(xmlChar *) "locale");
-
if (xml_locale)
{
chain = icu_chain_create((const char *) xml_locale, sort, status);
xmlFree(xml_locale);
}
-
}
+
if (!chain)
return 0;
else if (!strcmp((const char *) node->name, "display"))
step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
(const uint8_t *) "", status);
+ else if (!strcmp((const char *) node->name, "stemming"))
+ step = icu_chain_insert_step(chain, YAZ_chain_step_type_stemming,
+ (const uint8_t *) rule, status);
else if (!strcmp((const char *) node->name, "normalize"))
{
yaz_log(YLOG_WARN, "Element %s is deprecated. "
if (dst)
icu_utf16_to_utf8(iter->display, dst, &iter->status);
break;
+ case YAZ_chain_step_type_stemming:
+ if (dst)
+ {
+ struct icu_buf_utf16 *src = dst;
+ dst = icu_buf_utf16_create(0);
+ yaz_stemmer_stem(step->u.stemmer, dst, src, &iter->status);
+ icu_buf_utf16_destroy(src);
+ }
+ break;
default:
assert(0);
}