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