Use icu_iter-functions (YAZ 4.0.2)
[pazpar2-moved-to-github.git] / src / charsets.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 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 <yaz/yaz-version.h>
32 #include <ctype.h>
33 #include <assert.h>
34 #include <string.h>
35
36 #include "charsets.h"
37 #include "normalize7bit.h"
38
39 #if YAZ_HAVE_ICU
40 #include <yaz/icu.h>
41
42 #if YAZ_VERSIONL >= 0x40002
43 /* YAZ 4.0.2 or later as icu_iter */
44 #define ICU_ITER 1
45 #endif
46
47 #endif
48
49 /* charset handle */
50 struct pp2_charset_s {
51     const char *(*token_next_handler)(pp2_relevance_token_t prt);
52     const char *(*get_sort_handler)(pp2_relevance_token_t prt);
53     int ref_count;
54 #if YAZ_HAVE_ICU
55     struct icu_chain * icu_chn;
56     UErrorCode icu_sts;
57 #endif
58 };
59
60 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
61 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt);
62
63 #if YAZ_HAVE_ICU
64 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
65 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt);
66 #endif
67
68 /* tokenzier handle */
69 struct pp2_relevance_token_s {
70     const char *cp;     /* unnormalized buffer we're tokenizing */
71     const char *last_cp;  /* pointer to last token we're dealing with */
72     pp2_charset_t pct;  /* our main charset handle (type+config) */
73     WRBUF norm_str;     /* normized string we return (temporarily) */
74     WRBUF sort_str;     /* sort string we return (temporarily) */
75 #if ICU_ITER
76     yaz_icu_iter_t iter;
77 #endif
78 };
79
80
81 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
82 {
83 #if YAZ_HAVE_ICU
84     UErrorCode status = U_ZERO_ERROR;
85     struct icu_chain *chain = 0;
86     if (xml_node)
87         xml_node = xml_node->children;
88     while (xml_node && xml_node->type != XML_ELEMENT_NODE)
89         xml_node = xml_node->next;
90     chain = icu_chain_xml_config(xml_node, 1, &status);
91     if (!chain || U_FAILURE(status)){
92         //xmlDocPtr icu_doc = 0;
93         //xmlChar *xmlstr = 0;
94                 //int size = 0;
95                 //xmlDocDumpMemory(icu_doc, size);
96         
97         yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
98                 "<%s>\n ... \n</%s>",
99                 xml_node->name, xml_node->name);
100         return 0;
101     }
102     return pp2_charset_create(chain);
103 #else // YAZ_HAVE_ICU
104     yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
105             "<%s>\n ... \n</%s>",
106             xml_node->name, xml_node->name);
107     yaz_log(YLOG_FATAL, 
108             "But no ICU support is compiled into the YAZ library.");
109     return 0;
110 #endif // YAZ_HAVE_ICU
111 }
112
113 void pp2_charset_incref(pp2_charset_t pct)
114 {
115     (pct->ref_count)++;
116 }
117
118 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
119 {
120     pp2_charset_t pct = xmalloc(sizeof(*pct));
121
122     pct->token_next_handler = pp2_relevance_token_a_to_z;
123     pct->get_sort_handler  = pp2_get_sort_ascii;
124     pct->ref_count = 1;
125 #if YAZ_HAVE_ICU
126     pct->icu_chn = 0;
127     if (icu_chn)
128     {
129         pct->icu_chn = icu_chn;
130         pct->icu_sts = U_ZERO_ERROR;
131         pct->token_next_handler = pp2_relevance_token_icu;
132         pct->get_sort_handler = pp2_get_sort_icu;
133     }
134 #endif // YAZ_HAVE_ICU
135     return pct;
136 }
137
138 void pp2_charset_destroy(pp2_charset_t pct)
139 {
140     if (pct)
141     {
142         assert(pct->ref_count >= 1);
143         --(pct->ref_count);
144         if (pct->ref_count == 0)
145         {
146 #if YAZ_HAVE_ICU
147             icu_chain_destroy(pct->icu_chn);
148 #endif
149             xfree(pct);
150         }
151     }
152 }
153
154 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct,
155                                              const char *buf,
156                                              int skip_article)
157 {
158     pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
159
160     assert(pct);
161
162     if (skip_article)
163     {
164         const char *p = buf;
165         char firstword[64];
166         char *pout = firstword;
167         char articles[] = "the den der die des an a "; // must end in space
168         
169         while (*p && !isalnum(*(unsigned char *)p))
170             p++;
171         for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
172             *pout++ = tolower(*(unsigned char *)p);
173         *pout++ = ' ';
174         *pout++ = '\0';
175         if (strstr(articles, firstword))
176             buf = p;
177     }
178
179     prt->norm_str = wrbuf_alloc();
180     prt->sort_str = wrbuf_alloc();
181     prt->cp = buf;
182     prt->last_cp = 0;
183     prt->pct = pct;
184
185 #if YAZ_HAVE_ICU
186 #if ICU_ITER
187     prt->iter = 0;
188 #endif
189     if (pct->icu_chn)
190     {
191 #if ICU_ITER
192         prt->iter = icu_iter_create(pct->icu_chn);
193         icu_iter_first(prt->iter, buf);
194 #else        
195         int ok = 0;
196         pct->icu_sts = U_ZERO_ERROR;
197
198         ok = icu_chain_assign_cstr(pct->icu_chn, buf, &pct->icu_sts);
199 #endif
200         //printf("\nfield ok: %d '%s'\n", ok, buf);
201         prt->pct = pct;
202     }
203 #endif // YAZ_HAVE_ICU
204     return prt;
205 }
206
207
208 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
209 {
210     assert(prt);
211 #if ICU_ITER
212     if (prt->iter)
213         icu_iter_destroy(prt->iter);
214 #endif
215     if(prt->norm_str) 
216         wrbuf_destroy(prt->norm_str);
217     if(prt->sort_str) 
218         wrbuf_destroy(prt->sort_str);
219     xfree(prt);
220 }
221
222 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
223 {
224     assert(prt);
225     return (prt->pct->token_next_handler)(prt);
226 }
227
228 const char *pp2_get_sort(pp2_relevance_token_t prt)
229 {
230     return prt->pct->get_sort_handler(prt);
231 }
232
233 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
234 /* original tokenizer with our tokenize interface, but we
235    add +1 to ensure no '\0' are in our string (except for EOF)
236 */
237 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
238 {
239     const char *cp = prt->cp;
240     int c;
241
242     /* skip white space */
243     while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
244         cp++;
245     if (*cp == '\0')
246     {
247         prt->cp = cp;
248         prt->last_cp = 0;
249         return 0;
250     }
251     /* now read the term itself */
252
253     prt->last_cp = cp;
254     wrbuf_rewind(prt->norm_str);
255     while (*cp && (c = raw_char(tolower(*cp))) >= 0)
256     {
257         wrbuf_putc(prt->norm_str, c);
258         cp++;
259     }
260     prt->cp = cp;
261     return wrbuf_cstr(prt->norm_str);
262 }
263
264 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt)
265 {
266     if (prt->last_cp == 0)
267         return 0;
268     else
269     {
270         char *tmp = xstrdup(prt->last_cp);
271         char *result = 0;
272         result = normalize7bit_mergekey(tmp);
273         
274         wrbuf_rewind(prt->sort_str);
275         wrbuf_puts(prt->sort_str, result);
276         xfree(tmp);
277         return wrbuf_cstr(prt->sort_str);
278     }
279 }
280
281
282 #if YAZ_HAVE_ICU
283 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
284 {
285 #if ICU_ITER
286     if (icu_iter_next(prt->iter))
287     {
288         return icu_iter_get_norm(prt->iter);
289     }
290 #else
291     if (icu_chain_next_token(prt->pct->icu_chn, &prt->pct->icu_sts))
292     {
293         if (U_FAILURE(prt->pct->icu_sts))
294         {
295             return 0;
296         }
297         return icu_chain_token_norm(prt->pct->icu_chn);
298     }
299 #endif
300     return 0;
301 }
302
303 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt)
304 {
305 #if ICU_ITER
306     return icu_iter_get_sortkey(prt->iter);
307 #else
308     return icu_chain_token_sortkey(prt->pct->icu_chn);
309 #endif
310 }
311
312 #endif // YAZ_HAVE_ICU
313
314
315 /*
316  * Local variables:
317  * c-basic-offset: 4
318  * c-file-style: "Stroustrup"
319  * indent-tabs-mode: nil
320  * End:
321  * vim: shiftwidth=4 tabstop=8 expandtab
322  */
323