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