Metadata 'skiparticle works for ICU normalization
[pazpar2-moved-to-github.git] / src / charsets.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** \file charsets.c
21     \brief Pazpar2 Character set facilities
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <yaz/xmalloc.h>
29 #include <yaz/wrbuf.h>
30 #include <yaz/log.h>
31 #include <ctype.h>
32 #include <assert.h>
33 #include <string.h>
34
35 #include "charsets.h"
36 #include "normalize7bit.h"
37
38 #if YAZ_HAVE_ICU
39 #include <yaz/icu.h>
40 #endif
41
42
43 /* charset handle */
44 struct pp2_charset_s {
45     const char *(*token_next_handler)(pp2_relevance_token_t prt);
46     const char *(*get_sort_handler)(pp2_relevance_token_t prt);
47     int ref_count;
48 #if YAZ_HAVE_ICU
49     struct icu_chain * icu_chn;
50     UErrorCode icu_sts;
51 #endif
52 };
53
54 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
55 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt);
56
57 #if YAZ_HAVE_ICU
58 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
59 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt);
60 #endif
61
62 /* tokenzier handle */
63 struct pp2_relevance_token_s {
64     const char *cp;     /* unnormalized buffer we're tokenizing */
65     const char *last_cp;  /* pointer to last token we're dealing with */
66     pp2_charset_t pct;  /* our main charset handle (type+config) */
67     WRBUF norm_str;     /* normized string we return (temporarily) */
68     WRBUF sort_str;     /* sort string we return (temporarily) */
69 };
70
71
72 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
73 {
74 #if YAZ_HAVE_ICU
75     UErrorCode status = U_ZERO_ERROR;
76     struct icu_chain *chain = 0;
77     if (xml_node)
78         xml_node = xml_node->children;
79     while (xml_node && xml_node->type != XML_ELEMENT_NODE)
80         xml_node = xml_node->next;
81     chain = icu_chain_xml_config(xml_node, 1, &status);
82     if (!chain || U_FAILURE(status)){
83         //xmlDocPtr icu_doc = 0;
84         //xmlChar *xmlstr = 0;
85                 //int size = 0;
86                 //xmlDocDumpMemory(icu_doc, size);
87         
88         yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
89                 "<%s>\n ... \n</%s>",
90                 xml_node->name, xml_node->name);
91         return 0;
92     }
93     return pp2_charset_create(chain);
94 #else // YAZ_HAVE_ICU
95     yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
96             "<%s>\n ... \n</%s>",
97             xml_node->name, xml_node->name);
98     yaz_log(YLOG_FATAL, 
99             "But no ICU support is compiled into the YAZ library.");
100     return 0;
101 #endif // YAZ_HAVE_ICU
102 }
103
104 void pp2_charset_incref(pp2_charset_t pct)
105 {
106     (pct->ref_count)++;
107 }
108
109 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
110 {
111     pp2_charset_t pct = xmalloc(sizeof(*pct));
112
113     pct->token_next_handler = pp2_relevance_token_a_to_z;
114     pct->get_sort_handler  = pp2_get_sort_ascii;
115     pct->ref_count = 1;
116 #if YAZ_HAVE_ICU
117     pct->icu_chn = 0;
118     if (icu_chn)
119     {
120         pct->icu_chn = icu_chn;
121         pct->icu_sts = U_ZERO_ERROR;
122         pct->token_next_handler = pp2_relevance_token_icu;
123         pct->get_sort_handler = pp2_get_sort_icu;
124     }
125 #endif // YAZ_HAVE_ICU
126     return pct;
127 }
128
129 void pp2_charset_destroy(pp2_charset_t pct)
130 {
131     if (pct)
132     {
133         assert(pct->ref_count >= 1);
134         --(pct->ref_count);
135         if (pct->ref_count == 0)
136         {
137 #if YAZ_HAVE_ICU
138             icu_chain_destroy(pct->icu_chn);
139 #endif
140             xfree(pct);
141         }
142     }
143 }
144
145 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct,
146                                              const char *buf,
147                                              int skip_article)
148 {
149     pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
150
151     assert(pct);
152
153     if (skip_article)
154     {
155         const char *p = buf;
156         char firstword[64];
157         char *pout = firstword;
158         char articles[] = "the den der die des an a "; // must end in space
159         
160         while (*p && !isalnum(*(unsigned char *)p))
161             p++;
162         for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
163             *pout++ = tolower(*(unsigned char *)p);
164         *pout++ = ' ';
165         *pout++ = '\0';
166         if (strstr(articles, firstword))
167             buf = p;
168     }
169
170     prt->norm_str = wrbuf_alloc();
171     prt->sort_str = wrbuf_alloc();
172     prt->cp = buf;
173     prt->last_cp = 0;
174     prt->pct = pct;
175
176 #if YAZ_HAVE_ICU
177     if (pct->icu_chn)
178     {
179         int ok = 0;
180         pct->icu_sts = U_ZERO_ERROR;
181         ok = icu_chain_assign_cstr(pct->icu_chn, buf, &pct->icu_sts);
182         //printf("\nfield ok: %d '%s'\n", ok, buf);
183         prt->pct = pct;
184     }
185 #endif // YAZ_HAVE_ICU
186     return prt;
187 }
188
189
190 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
191 {
192     assert(prt);
193     if(prt->norm_str) 
194         wrbuf_destroy(prt->norm_str);
195     if(prt->sort_str) 
196         wrbuf_destroy(prt->sort_str);
197     xfree(prt);
198 }
199
200 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
201 {
202     assert(prt);
203     return (prt->pct->token_next_handler)(prt);
204 }
205
206 const char *pp2_get_sort(pp2_relevance_token_t prt)
207 {
208     return prt->pct->get_sort_handler(prt);
209 }
210
211 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
212 /* original tokenizer with our tokenize interface, but we
213    add +1 to ensure no '\0' are in our string (except for EOF)
214 */
215 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
216 {
217     const char *cp = prt->cp;
218     int c;
219
220     /* skip white space */
221     while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
222         cp++;
223     if (*cp == '\0')
224     {
225         prt->cp = cp;
226         prt->last_cp = 0;
227         return 0;
228     }
229     /* now read the term itself */
230
231     prt->last_cp = cp;
232     wrbuf_rewind(prt->norm_str);
233     while (*cp && (c = raw_char(tolower(*cp))) >= 0)
234     {
235         wrbuf_putc(prt->norm_str, c);
236         cp++;
237     }
238     prt->cp = cp;
239     return wrbuf_cstr(prt->norm_str);
240 }
241
242 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt)
243 {
244     if (prt->last_cp == 0)
245         return 0;
246     else
247     {
248         char *tmp = xstrdup(prt->last_cp);
249         char *result = 0;
250         result = normalize7bit_mergekey(tmp);
251         
252         wrbuf_rewind(prt->sort_str);
253         wrbuf_puts(prt->sort_str, result);
254         xfree(tmp);
255         return wrbuf_cstr(prt->sort_str);
256     }
257 }
258
259
260 #if YAZ_HAVE_ICU
261 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
262 {
263     if (icu_chain_next_token(prt->pct->icu_chn, &prt->pct->icu_sts))
264     {
265         if (U_FAILURE(prt->pct->icu_sts))
266         {
267             return 0;
268         }
269         return icu_chain_token_norm(prt->pct->icu_chn);
270     }
271     return 0;
272 }
273
274 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt)
275 {
276     return icu_chain_token_sortkey(prt->pct->icu_chn);
277 }
278
279 #endif // YAZ_HAVE_ICU
280
281
282 /*
283  * Local variables:
284  * c-basic-offset: 4
285  * c-file-style: "Stroustrup"
286  * indent-tabs-mode: nil
287  * End:
288  * vim: shiftwidth=4 tabstop=8 expandtab
289  */
290