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