ICU chain "facet" honors YAC ICU element <display/>.
[pazpar2-moved-to-github.git] / src / charsets.c
index f867b6f..44ef5fc 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2010 Index Data
+   Copyright (C) 2006-2011 Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -44,6 +44,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 struct pp2_charset_s {
     const char *(*token_next_handler)(pp2_relevance_token_t prt);
     const char *(*get_sort_handler)(pp2_relevance_token_t prt);
+    const char *(*get_display_handler)(pp2_relevance_token_t prt);
     int ref_count;
 #if YAZ_HAVE_ICU
     struct icu_chain * icu_chn;
@@ -51,12 +52,15 @@ struct pp2_charset_s {
 #endif
 };
 
+static const char *pp2_relevance_token_null(pp2_relevance_token_t prt);
 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt);
+static const char *pp2_get_display_ascii(pp2_relevance_token_t prt);
 
 #if YAZ_HAVE_ICU
 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt);
+static const char *pp2_get_display_icu(pp2_relevance_token_t prt);
 #endif
 
 /* tokenzier handle */
@@ -109,12 +113,20 @@ void pp2_charset_incref(pp2_charset_t pct)
     (pct->ref_count)++;
 }
 
-pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
+pp2_charset_t pp2_charset_create_a_to_z(void)
+{
+    pp2_charset_t pct = pp2_charset_create(0);
+    pct->token_next_handler = pp2_relevance_token_a_to_z;
+    return pct;
+}
+
+pp2_charset_t pp2_charset_create(struct icu_chain *icu_chn)
 {
     pp2_charset_t pct = xmalloc(sizeof(*pct));
 
-    pct->token_next_handler = pp2_relevance_token_a_to_z;
+    pct->token_next_handler = pp2_relevance_token_null;
     pct->get_sort_handler  = pp2_get_sort_ascii;
+    pct->get_display_handler  = pp2_get_display_ascii;
     pct->ref_count = 1;
 #if YAZ_HAVE_ICU
     pct->icu_chn = 0;
@@ -124,6 +136,7 @@ pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
         pct->icu_sts = U_ZERO_ERROR;
         pct->token_next_handler = pp2_relevance_token_icu;
         pct->get_sort_handler = pp2_get_sort_icu;
+        pct->get_display_handler = pp2_get_display_icu;
     }
 #endif // YAZ_HAVE_ICU
     return pct;
@@ -176,8 +189,6 @@ void pp2_relevance_first(pp2_relevance_token_t prt,
         char *pout = firstword;
         char articles[] = "the den der die des an a "; // must end in space
         
-        while (*p && !isalnum(*(unsigned char *)p))
-            p++;
         for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
             *pout++ = tolower(*(unsigned char *)p);
         *pout++ = ' ';
@@ -224,6 +235,11 @@ const char *pp2_get_sort(pp2_relevance_token_t prt)
     return prt->pct->get_sort_handler(prt);
 }
 
+const char *pp2_get_display(pp2_relevance_token_t prt)
+{
+    return prt->pct->get_display_handler(prt);
+}
+
 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
 /* original tokenizer with our tokenize interface, but we
    add +1 to ensure no '\0' are in our string (except for EOF)
@@ -272,6 +288,26 @@ static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt)
     }
 }
 
+static const char *pp2_get_display_ascii(pp2_relevance_token_t prt)
+{
+    if (prt->last_cp == 0)
+        return 0;
+    else
+    {
+        return wrbuf_cstr(prt->norm_str);
+    }
+}
+
+static const char *pp2_relevance_token_null(pp2_relevance_token_t prt)
+{
+    const char *cp = prt->cp;
+
+    prt->last_cp = *cp ? cp : 0;
+    while (*cp)
+        cp++;
+    prt->cp = cp;
+    return prt->last_cp;
+}
 
 #if YAZ_HAVE_ICU
 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
@@ -288,6 +324,11 @@ static const char *pp2_get_sort_icu(pp2_relevance_token_t prt)
     return icu_iter_get_sortkey(prt->iter);
 }
 
+static const char *pp2_get_display_icu(pp2_relevance_token_t prt)
+{
+    return icu_iter_get_display(prt->iter);
+}
+
 #endif // YAZ_HAVE_ICU