Happy new year
[pazpar2-moved-to-github.git] / src / charsets.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 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 typedef struct pp2_charset_s *pp2_charset_t;
40 static pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node);
41 static pp2_charset_t pp2_charset_create(void);
42 static pp2_charset_t pp2_charset_create_a_to_z(void);
43 static void pp2_charset_destroy(pp2_charset_t pct);
44 static pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct);
45
46 #if YAZ_HAVE_ICU
47 #include <yaz/icu.h>
48 static pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn);
49 #endif
50
51 /* charset handle */
52 struct pp2_charset_s {
53     const char *(*token_next_handler)(pp2_charset_token_t prt);
54     const char *(*get_sort_handler)(pp2_charset_token_t prt);
55     const char *(*get_display_handler)(pp2_charset_token_t prt);
56     void (*get_org_handler)(pp2_charset_token_t ptr,
57                             size_t *start, size_t *len);
58 #if YAZ_HAVE_ICU
59     struct icu_chain * icu_chn;
60     UErrorCode icu_sts;
61 #endif
62 };
63
64 static const char *pp2_charset_token_null(pp2_charset_token_t prt);
65 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt);
66 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt);
67 static const char *pp2_get_display_ascii(pp2_charset_token_t prt);
68 static void pp2_get_org_ascii(pp2_charset_token_t prt,
69                               size_t *start, size_t *len);
70
71 #if YAZ_HAVE_ICU
72 static const char *pp2_charset_token_icu(pp2_charset_token_t prt);
73 static const char *pp2_get_sort_icu(pp2_charset_token_t prt);
74 static const char *pp2_get_display_icu(pp2_charset_token_t prt);
75 static void pp2_get_org_icu(pp2_charset_token_t prt,
76                             size_t *start, size_t *len);
77 #endif
78
79 /* tokenzier handle */
80 struct pp2_charset_token_s {
81     const char *cp;     /* unnormalized buffer we're tokenizing */
82     const char *last_cp;  /* pointer to last token we're dealing with */
83     pp2_charset_t pct;  /* our main charset handle (type+config) */
84     WRBUF norm_str;     /* normized string we return (temporarily) */
85     WRBUF sort_str;     /* sort string we return (temporarily) */
86 #if YAZ_HAVE_ICU
87     yaz_icu_iter_t iter;
88 #endif
89     const char *cp0;
90     size_t start;
91     size_t len;
92 };
93
94 struct pp2_charset_fact_s {
95     struct pp2_charset_entry *list;
96     int ref_count;
97 };
98
99 struct pp2_charset_entry {
100     struct pp2_charset_entry *next;
101     pp2_charset_t pct;
102     char *name;
103 };
104
105
106 static int pp2_charset_fact_add(pp2_charset_fact_t pft,
107                                 pp2_charset_t pct, const char *default_id);
108
109 pp2_charset_fact_t pp2_charset_fact_create(void)
110 {
111     pp2_charset_fact_t pft = xmalloc(sizeof(*pft));
112     pft->list = 0;
113     pft->ref_count = 1;
114
115     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "relevance");
116     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "sort");
117     pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "mergekey");
118     pp2_charset_fact_add(pft, pp2_charset_create(), "facet");
119     return pft;
120 }
121
122 void pp2_charset_fact_destroy(pp2_charset_fact_t pft)
123 {
124     if (pft)
125     {
126         assert(pft->ref_count >= 1);
127         --(pft->ref_count);
128         if (pft->ref_count == 0)
129         {
130             struct pp2_charset_entry *pce = pft->list;
131             while (pce)
132             {
133                 struct pp2_charset_entry *next = pce->next;
134                 pp2_charset_destroy(pce->pct);
135                 xfree(pce->name);
136                 xfree(pce);
137                 pce = next;
138             }
139             xfree(pft);
140         }
141     }
142 }
143
144 int pp2_charset_fact_add(pp2_charset_fact_t pft,
145                          pp2_charset_t pct, const char *default_id)
146 {
147     struct pp2_charset_entry *pce;
148
149     for (pce = pft->list; pce; pce = pce->next)
150         if (!strcmp(default_id, pce->name))
151             break;
152
153     if (!pce)
154     {
155         pce = xmalloc(sizeof(*pce));
156         pce->name = xstrdup(default_id);
157         pce->next = pft->list;
158         pft->list = pce;
159     }
160     else
161     {
162         pp2_charset_destroy(pce->pct);
163     }
164     pce->pct = pct;
165     return 0;
166 }
167
168 int pp2_charset_fact_define(pp2_charset_fact_t pft,
169                             xmlNode *xml_node, const char *default_id)
170 {
171     int r;
172     pp2_charset_t pct;
173     xmlChar *id = 0;
174
175     assert(xml_node);
176     pct = pp2_charset_create_xml(xml_node);
177     if (!pct)
178         return -1;
179     if (!default_id)
180     {
181         id = xmlGetProp(xml_node, (xmlChar*) "id");
182         if (!id)
183         {
184             yaz_log(YLOG_WARN, "Missing id for icu_chain");
185             pp2_charset_destroy(pct);
186             return -1;
187         }
188         default_id = (const char *) id;
189     }
190     r = pp2_charset_fact_add(pft, pct, default_id);
191     if (id)
192         xmlFree(id);
193     return r;
194 }
195
196 void pp2_charset_fact_incref(pp2_charset_fact_t pft)
197 {
198     (pft->ref_count)++;
199 }
200
201 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
202 {
203 #if YAZ_HAVE_ICU
204     UErrorCode status = U_ZERO_ERROR;
205     struct icu_chain *chain = 0;
206     while (xml_node && xml_node->type != XML_ELEMENT_NODE)
207         xml_node = xml_node->next;
208     chain = icu_chain_xml_config(xml_node, 1, &status);
209     if (!chain || U_FAILURE(status)){
210         //xmlDocPtr icu_doc = 0;
211         //xmlChar *xmlstr = 0;
212                 //int size = 0;
213                 //xmlDocDumpMemory(icu_doc, size);
214
215         yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
216                 "<%s>\n ... \n</%s>",
217                 xml_node->name, xml_node->name);
218         return 0;
219     }
220     return pp2_charset_create_icu(chain);
221 #else // YAZ_HAVE_ICU
222     yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
223             "<%s>\n ... \n</%s>",
224             xml_node->name, xml_node->name);
225     yaz_log(YLOG_FATAL,
226             "But no ICU support is compiled into the YAZ library.");
227     return 0;
228 #endif // YAZ_HAVE_ICU
229 }
230
231 pp2_charset_t pp2_charset_create(void)
232 {
233     pp2_charset_t pct = xmalloc(sizeof(*pct));
234
235     pct->token_next_handler = pp2_charset_token_null;
236     pct->get_sort_handler  = pp2_get_sort_ascii;
237     pct->get_display_handler  = pp2_get_display_ascii;
238     pct->get_org_handler = pp2_get_org_ascii;
239 #if YAZ_HAVE_ICU
240     pct->icu_chn = 0;
241 #endif // YAZ_HAVE_ICU
242     return pct;
243 }
244
245 pp2_charset_t pp2_charset_create_a_to_z(void)
246 {
247     pp2_charset_t pct = pp2_charset_create();
248     pct->token_next_handler = pp2_charset_token_a_to_z;
249     return pct;
250 }
251
252 #if YAZ_HAVE_ICU
253 pp2_charset_t pp2_charset_create_icu(struct icu_chain *icu_chn)
254 {
255     pp2_charset_t pct = pp2_charset_create();
256     if (icu_chn)
257     {
258         pct->icu_chn = icu_chn;
259         pct->icu_sts = U_ZERO_ERROR;
260         pct->token_next_handler = pp2_charset_token_icu;
261         pct->get_sort_handler = pp2_get_sort_icu;
262         pct->get_display_handler = pp2_get_display_icu;
263         pct->get_org_handler = pp2_get_org_icu;
264     }
265     return pct;
266 }
267 #endif // YAZ_HAVE_ICU
268
269 void pp2_charset_destroy(pp2_charset_t pct)
270 {
271 #if YAZ_HAVE_ICU
272     icu_chain_destroy(pct->icu_chn);
273 #endif
274     xfree(pct);
275 }
276
277 pp2_charset_token_t pp2_charset_token_create(pp2_charset_fact_t pft,
278                                                const char *id)
279 {
280     struct pp2_charset_entry *pce;
281     for (pce = pft->list; pce; pce = pce->next)
282         if (!strcmp(id, pce->name))
283             return pp2_charset_tokenize(pce->pct);
284     return 0;
285 }
286
287 pp2_charset_token_t pp2_charset_tokenize(pp2_charset_t pct)
288 {
289     pp2_charset_token_t prt = xmalloc(sizeof(*prt));
290
291     assert(pct);
292
293     prt->norm_str = wrbuf_alloc();
294     prt->sort_str = wrbuf_alloc();
295     prt->cp = 0;
296     prt->last_cp = 0;
297     prt->pct = pct;
298
299 #if YAZ_HAVE_ICU
300     prt->iter = 0;
301     if (pct->icu_chn)
302         prt->iter = icu_iter_create(pct->icu_chn);
303 #endif
304     prt->start = 0;
305     prt->len = 0;
306     return prt;
307 }
308
309 void pp2_charset_token_first(pp2_charset_token_t prt,
310                              const char *buf, int skip_article)
311 {
312     if (skip_article)
313     {
314         const char *p = buf;
315         char firstword[64];
316         char *pout = firstword;
317         char articles[] = "the den der die des an a "; // must end in space
318
319         for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
320             *pout++ = tolower(*(unsigned char *)p);
321         *pout++ = ' ';
322         *pout++ = '\0';
323         if (strstr(articles, firstword))
324             buf = p;
325     }
326
327     wrbuf_rewind(prt->norm_str);
328     wrbuf_rewind(prt->sort_str);
329     prt->cp0 = buf;
330     prt->cp = buf;
331     prt->last_cp = 0;
332
333 #if YAZ_HAVE_ICU
334     if (prt->iter)
335     {
336         icu_iter_first(prt->iter, buf);
337     }
338 #endif // YAZ_HAVE_ICU
339 }
340
341 void pp2_charset_token_destroy(pp2_charset_token_t prt)
342 {
343     assert(prt);
344 #if YAZ_HAVE_ICU
345     if (prt->iter)
346         icu_iter_destroy(prt->iter);
347 #endif
348     if(prt->norm_str)
349         wrbuf_destroy(prt->norm_str);
350     if(prt->sort_str)
351         wrbuf_destroy(prt->sort_str);
352     xfree(prt);
353 }
354
355 const char *pp2_charset_token_next(pp2_charset_token_t prt)
356 {
357     assert(prt);
358     return (prt->pct->token_next_handler)(prt);
359 }
360
361 const char *pp2_get_sort(pp2_charset_token_t prt)
362 {
363     return prt->pct->get_sort_handler(prt);
364 }
365
366 const char *pp2_get_display(pp2_charset_token_t prt)
367 {
368     return prt->pct->get_display_handler(prt);
369 }
370
371 void pp2_get_org(pp2_charset_token_t prt, size_t *start, size_t *len)
372 {
373     prt->pct->get_org_handler(prt, start, len);
374 }
375
376
377 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
378 /* original tokenizer with our tokenize interface, but we
379    add +1 to ensure no '\0' are in our string (except for EOF)
380 */
381 static const char *pp2_charset_token_a_to_z(pp2_charset_token_t prt)
382 {
383     const char *cp = prt->cp;
384     int c;
385
386     prt->start = cp - prt->cp0;
387     /* skip white space */
388     while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
389         cp++;
390     if (*cp == '\0')
391     {
392         prt->cp = cp;
393         prt->last_cp = 0;
394         return 0;
395     }
396     /* now read the term itself */
397
398     prt->last_cp = cp;
399     wrbuf_rewind(prt->norm_str);
400     while (*cp && (c = raw_char(tolower(*cp))) >= 0)
401     {
402         wrbuf_putc(prt->norm_str, c);
403         cp++;
404     }
405     prt->len = (cp - prt->cp0) - prt->start;
406     prt->cp = cp;
407     return wrbuf_cstr(prt->norm_str);
408 }
409
410 static const char *pp2_get_sort_ascii(pp2_charset_token_t prt)
411 {
412     if (prt->last_cp == 0)
413         return 0;
414     else
415     {
416         char *tmp = xstrdup(prt->last_cp);
417         char *result = 0;
418         result = normalize7bit_mergekey(tmp);
419
420         wrbuf_rewind(prt->sort_str);
421         wrbuf_puts(prt->sort_str, result);
422         xfree(tmp);
423         return wrbuf_cstr(prt->sort_str);
424     }
425 }
426
427 static const char *pp2_get_display_ascii(pp2_charset_token_t prt)
428 {
429     if (prt->last_cp == 0)
430         return 0;
431     else
432     {
433         return wrbuf_cstr(prt->norm_str);
434     }
435 }
436
437 static void pp2_get_org_ascii(pp2_charset_token_t prt,
438                               size_t *start, size_t *len)
439 {
440     *start = prt->start;
441     *len = prt->len;
442 }
443
444 static const char *pp2_charset_token_null(pp2_charset_token_t prt)
445 {
446     const char *cp = prt->cp;
447
448     prt->last_cp = *cp ? cp : 0;
449     while (*cp)
450         cp++;
451     prt->cp = cp;
452     prt->len = cp - prt->cp0;
453     return prt->last_cp;
454 }
455
456 #if YAZ_HAVE_ICU
457 static const char *pp2_charset_token_icu(pp2_charset_token_t prt)
458 {
459     if (icu_iter_next(prt->iter))
460     {
461         return icu_iter_get_norm(prt->iter);
462     }
463     return 0;
464 }
465
466 static const char *pp2_get_sort_icu(pp2_charset_token_t prt)
467 {
468     return icu_iter_get_sortkey(prt->iter);
469 }
470
471 static const char *pp2_get_display_icu(pp2_charset_token_t prt)
472 {
473     return icu_iter_get_display(prt->iter);
474 }
475
476 static void pp2_get_org_icu(pp2_charset_token_t prt, size_t *start, size_t *len)
477 {
478     icu_iter_get_org_info(prt->iter, start, len);
479 }
480
481 #endif // YAZ_HAVE_ICU
482
483
484 /*
485  * Local variables:
486  * c-basic-offset: 4
487  * c-file-style: "Stroustrup"
488  * indent-tabs-mode: nil
489  * End:
490  * vim: shiftwidth=4 tabstop=8 expandtab
491  */
492