Spell fix
[yaz-moved-to-github.git] / src / stemmer.c
index eca3c2a..f221799 100644 (file)
@@ -1,13 +1,19 @@
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2012 Index Data
+ * See the file LICENSE for details.
+ */
 
+/**
+ * \file stemmer.c
+ * \brief Implements stemmer wrapper
+ */
 
 #if HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
 #endif
 
 #if YAZ_HAVE_ICU
 
-#include <yaz/yconfig.h>
-
 #include <yaz/stemmer.h>
 
 #include <yaz/xmalloc.h>
@@ -27,9 +33,7 @@ struct yaz_stemmer_t
     // Required for cloning.
     char *locale;
     char *rule;
-    union {
-        struct sb_stemmer *sb_stemmer;
-    };
+    struct sb_stemmer *sb_stemmer;
 };
 
 const char* yaz_stemmer_lookup_charenc(const char *charenc, const char *rule) {
@@ -45,19 +49,20 @@ yaz_stemmer_p yaz_stemmer_snowball_create(const char *locale, const char *rule,
     const char *algorithm = yaz_stemmer_lookup_algorithm(locale,rule);
     struct sb_stemmer *stemmer = sb_stemmer_new(algorithm, charenc);
     yaz_stemmer_p yaz_stemmer;
-    yaz_log(YLOG_DEBUG, "create snowball stemmer: algoritm %s charenc %s ", algorithm, charenc);
     if (stemmer == 0) {
         *status = U_ILLEGAL_ARGUMENT_ERROR;
-        yaz_log(YLOG_DEBUG, "failed to create stemmer. Creating NOP stemmer");
-
-        return 0;
+        yaz_log(YLOG_FATAL, "yaz_stemmer: Failed to create snowball stemmer from locale %srule %s. Showball: charenc %s algorithm %s ",
+               locale, rule, charenc, algorithm);
+       return 0;
     }
+    yaz_log(YLOG_DEBUG, "created snowball stemmer: algorithm %s charenc %s ", algorithm, charenc);
     yaz_stemmer = xmalloc(sizeof(*yaz_stemmer));
     yaz_stemmer->implementation = yaz_snowball;
+
     yaz_stemmer->locale = xstrdup(locale);
     yaz_stemmer->rule = xstrdup(rule);
     yaz_stemmer->sb_stemmer = stemmer;
-    yaz_log(YLOG_DEBUG, "created snowball stemmer: algoritm %s charenc %s ", algorithm, charenc);
+    yaz_log(YLOG_DEBUG, "created snowball stemmer: algorithm %s charenc %s ", algorithm, charenc);
     return yaz_stemmer;
 }
 
@@ -70,6 +75,8 @@ yaz_stemmer_p yaz_stemmer_create(const char *locale, const char *rule, UErrorCod
 
 yaz_stemmer_p yaz_stemmer_clone(yaz_stemmer_p stemmer) {
     UErrorCode error = U_ZERO_ERROR;
+    if (stemmer == 0)
+      return 0;
     return yaz_stemmer_create(stemmer->locale, stemmer->rule, &error);
 }
 
@@ -86,30 +93,41 @@ void yaz_stemmer_stem(yaz_stemmer_p stemmer, struct icu_buf_utf16 *dst, struct i
                     icu_buf_utf16_copy(dst, src);
                 }
                 else {
-                    const char *cstr = (const char *) sb_symbol;
-                    icu_utf16_from_utf8_cstr(dst, cstr , status);
+
+                    const char *cstr2 = (const char *) sb_symbol;
+                    icu_utf16_from_utf8_cstr(dst, cstr2 , status);
+#if 0
+                   yaz_log(YLOG_DEBUG, "stemming %s to %s ", cstr, cstr2);
+#endif
                 }
             }
-            icu_buf_utf16_destroy(utf8_buf);
+            icu_buf_utf8_destroy(utf8_buf);
             return ;
             break;
         }
-        default: {
+    case yaz_no_operation:
+      yaz_log(YLOG_DEBUG, "Stemmer (No operation) called");
+    default: {
             // Default return the same as given.
             icu_buf_utf16_copy(dst, src);
         }
     }
 }
 
-void yaz_stemmer_destroy(yaz_stemmer_p stemmer) {
-    switch (stemmer->implementation) {
-    case yaz_snowball:
-        sb_stemmer_delete(stemmer->sb_stemmer);
-        break;
-    }
-    xfree(stemmer->locale);
-    xfree(stemmer->rule);
-    xfree(stemmer);
+void yaz_stemmer_destroy(yaz_stemmer_p stemmer)
+{
+  /* Handle no stemmer correctly */
+  if (stemmer == 0)
+    return ;
+
+  switch (stemmer->implementation) {
+  case yaz_snowball:
+    sb_stemmer_delete(stemmer->sb_stemmer);
+    break;
+  }
+  xfree(stemmer->locale);
+  xfree(stemmer->rule);
+  xfree(stemmer);
 }
 
 #endif /* YAZ_HAVE_ICU */