Remove useless assert
[yaz-moved-to-github.git] / src / icu_chain.c
index 5354e89..f62e9bb 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2010 Index Data
+ * Copyright (C) 1995-2013 Index Data
  * See the file LICENSE for details.
  */
 
 
 #include <yaz/icu_I18N.h>
 
-#include <yaz/log.h>
+#include <yaz/stemmer.h>
 
+#include <yaz/log.h>
+#include <yaz/nmem.h>
+#include <yaz/nmem_xml.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.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
@@ -41,9 +45,10 @@ 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;  
+       struct icu_tokenizer *tokenizer;
+        yaz_stemmer_p         stemmer;
     } u;
     struct icu_chain_step *previous;
 };
@@ -55,7 +60,7 @@ struct icu_chain
     int sort;
 
     UCollator *coll;
-    
+
     /* linked list of chain steps */
     struct icu_chain_step *csteps;
 };
@@ -65,17 +70,17 @@ int icu_check_status(UErrorCode status)
     if (U_FAILURE(status))
     {
         yaz_log(YLOG_WARN, "ICU: %d %s\n", status, u_errorName(status));
-        return 0;   
+        return 0;
     }
     return 1;
 }
 
-static struct icu_chain_step *icu_chain_step_create(
-    struct icu_chain *chain,  enum icu_chain_step_type type,
+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_step *step = 0;
-    
+
     if (!chain || !type || !rule)
         return 0;
 
@@ -96,7 +101,7 @@ static struct icu_chain_step *icu_chain_step_create(
                                                  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:
@@ -104,9 +109,16 @@ static struct icu_chain_step *icu_chain_step_create(
         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;
     }
+    step->previous = chain->csteps;
+    chain->csteps = step;
+
     return step;
 }
 
@@ -132,6 +144,9 @@ static void icu_chain_step_destroy(struct icu_chain_step *step)
     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;
     }
@@ -146,7 +161,7 @@ struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
     {
         *sp = (struct icu_chain_step *) xmalloc(sizeof(**sp));
         (*sp)->type = old->type;
-        
+
         switch ((*sp)->type)
         {
         case ICU_chain_step_type_display:
@@ -161,6 +176,9 @@ struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
         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;
         }
@@ -174,21 +192,17 @@ struct icu_chain_step *icu_chain_step_clone(struct icu_chain_step *old)
 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;
@@ -209,69 +223,83 @@ void icu_chain_destroy(struct icu_chain *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, 
+struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
                                        int sort,
                                        UErrorCode *status)
 {
     xmlNode *node = 0;
+    int no_errors = 0;
     struct icu_chain *chain = 0;
-   
+    NMEM nmem = 0;
+
     *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 *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;
 
+    nmem = nmem_create();
     for (node = xml_node->children; node; node = node->next)
     {
-        xmlChar *xml_rule;
+        char *rule = 0;
         struct icu_chain_step *step = 0;
+        struct _xmlAttr *attr;
 
+        nmem_reset(nmem);
         if (node->type != XML_ELEMENT_NODE)
             continue;
 
-        xml_rule = xmlGetProp(node, (xmlChar *) "rule");
-
-        yaz_log(YLOG_LOG, "rule=%s", xml_rule);
+        for (attr = node->properties; attr; attr = attr->next)
+        {
+            if (!strcmp((const char *) attr->name, "rule"))
+            {
+                rule = nmem_text_node_cdata(attr->children, nmem);
+            }
+            else
+            {
+                yaz_log(YLOG_WARN, "Unsupported attribute '%s' for "
+                        "element '%s'", attr->name, node->name);
+                no_errors++;
+                continue;
+            }
+        }
+        if (!rule && node->children)
+            rule = nmem_text_node_cdata(node->children, nmem);
 
         if (!strcmp((const char *) node->name, "casemap"))
-            step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap, 
-                                         (const uint8_t *) xml_rule, status);
+            step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
+                                         (const uint8_t *) rule, status);
         else if (!strcmp((const char *) node->name, "transform"))
-            step = icu_chain_insert_step(chain, ICU_chain_step_type_transform, 
-                                         (const uint8_t *) xml_rule, status);
+            step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
+                                         (const uint8_t *) rule, status);
         else if (!strcmp((const char *) node->name, "transliterate"))
-            step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate, 
-                                         (const uint8_t *) xml_rule, status);
+            step = icu_chain_insert_step(chain, ICU_chain_step_type_transliterate,
+                                         (const uint8_t *) rule, status);
         else if (!strcmp((const char *) node->name, "tokenize"))
-            step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize, 
-                                         (const uint8_t *) xml_rule, status);
+            step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
+                                         (const uint8_t *) rule, status);
         else if (!strcmp((const char *) node->name, "display"))
-            step = icu_chain_insert_step(chain, ICU_chain_step_type_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. "
                     "Use transform instead", node->name);
-            step = icu_chain_insert_step(chain, ICU_chain_step_type_transform, 
-                                         (const uint8_t *) xml_rule, status);
+            step = icu_chain_insert_step(chain, ICU_chain_step_type_transform,
+                                         (const uint8_t *) rule, status);
         }
         else if (!strcmp((const char *) node->name, "index")
                  || !strcmp((const char *) node->name, "sortkey"))
@@ -282,36 +310,22 @@ struct icu_chain *icu_chain_xml_config(const xmlNode *xml_node,
         else
         {
             yaz_log(YLOG_WARN, "Unknown element %s", node->name);
-            icu_chain_destroy(chain);
-            return 0;
+            no_errors++;
+            continue;
         }
-        xmlFree(xml_rule);
         if (step && U_FAILURE(*status))
         {
-            icu_chain_destroy(chain);
-            return 0;
+            no_errors++;
+            break;
         }
     }
-    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_step *step = 0;
-    if (!chain || !type || !rule)
+    nmem_destroy(nmem);
+    if (no_errors)
+    {
+        icu_chain_destroy(chain);
         return 0;
-
-    /* create actual chain step with this buffer */
-    step = icu_chain_step_create(chain, type, rule,
-                                 status);
-
-    step->previous = chain->csteps;
-    chain->csteps = step;
-
-    return step;
+    }
+    return chain;
 }
 
 struct icu_iter {
@@ -333,7 +347,6 @@ void icu_utf16_print(struct icu_buf_utf16 *src16)
     struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
     icu_utf16_to_utf8(dst8, src16, &status);
 
-    assert(status != 1234);
     if (U_FAILURE(status))
     {
         printf("failure");
@@ -355,7 +368,7 @@ struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
     else
     {
         struct icu_buf_utf16 *dst = icu_iter_invoke(iter, step->previous, src);
-        
+
         switch (step->type)
         {
         case ICU_chain_step_type_casemap:
@@ -399,6 +412,15 @@ struct icu_buf_utf16 *icu_iter_invoke(yaz_icu_iter_t iter,
             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);
         }
@@ -453,18 +475,17 @@ int icu_iter_next(yaz_icu_iter_t iter)
     else
     {
         /* on first call, iter->input is the input string. Thereafter: 0. */
-        iter->last = icu_iter_invoke(iter, iter->steps ?
-                                     iter->steps : iter->chain->csteps,
-                                     iter->input);
+        assert(iter->steps || !iter->chain->csteps);
+        iter->last = icu_iter_invoke(iter, iter->steps, iter->input);
         iter->input = 0;
-        
+
         if (!iter->last)
             return 0;
 
         iter->token_count++;
 
         if (iter->chain->sort)
-        {        
+        {
             icu_sortkey8_from_utf16(iter->chain->coll,
                                     iter->sort8, iter->last,
                                     &iter->status);
@@ -487,16 +508,16 @@ const char *icu_iter_get_sortkey(yaz_icu_iter_t iter)
 }
 
 const char *icu_iter_get_display(yaz_icu_iter_t iter)
-{ 
-    return icu_buf_utf8_to_cstr(iter->display);   
+{
+    return icu_buf_utf8_to_cstr(iter->display);
 }
 
 int icu_iter_get_token_number(yaz_icu_iter_t iter)
-{ 
+{
     return iter->token_count;
 }
 
-int icu_chain_assign_cstr(struct icu_chain *chain, const char *src8cstr, 
+int icu_chain_assign_cstr(struct icu_chain *chain, const char *src8cstr,
                           UErrorCode *status)
 {
     if (chain->iter)