Using the ICU wrapper from YAZ rather than its own.
[pazpar2-moved-to-github.git] / src / charsets.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 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 <ctype.h>
32 #include <assert.h>
33
34 #include "charsets.h"
35 #include "normalize7bit.h"
36
37 #if YAZ_HAVE_ICU
38 #include <yaz/icu.h>
39 #endif
40
41
42 /* charset handle */
43 struct pp2_charset_s {
44     const char *(*token_next_handler)(pp2_relevance_token_t prt);
45     const char *(*get_sort_handler)(pp2_relevance_token_t prt, int skip);
46 #if YAZ_HAVE_ICU
47     struct icu_chain * icu_chn;
48     UErrorCode icu_sts;
49 #endif
50 };
51
52 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
53 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt, int skip_article);
54
55 #if YAZ_HAVE_ICU
56 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
57 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt, int skip_article);
58 #endif
59
60 /* tokenzier handle */
61 struct pp2_relevance_token_s {
62     const char *cp;     /* unnormalized buffer we're tokenizing */
63     const char *last_cp;  /* pointer to last token we're dealing with */
64     pp2_charset_t pct;  /* our main charset handle (type+config) */
65     WRBUF norm_str;     /* normized string we return (temporarily) */
66     WRBUF sort_str;     /* sort string we return (temporarily) */
67 };
68
69
70 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
71 {
72 #if YAZ_HAVE_ICU
73     UErrorCode status = U_ZERO_ERROR;
74     struct icu_chain *chain = 0;
75     if (xml_node)
76         xml_node = xml_node->children;
77     while (xml_node && xml_node->type != XML_ELEMENT_NODE)
78         xml_node = xml_node->next;
79     chain = icu_chain_xml_config(xml_node, 1, &status);
80     if (!chain || U_FAILURE(status)){
81         //xmlDocPtr icu_doc = 0;
82         //xmlChar *xmlstr = 0;
83                 //int size = 0;
84                 //xmlDocDumpMemory(icu_doc, size);
85         
86         yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
87                 "<%s>\n ... \n</%s>",
88                 xml_node->name, xml_node->name);
89         return 0;
90     }
91     return pp2_charset_create(chain);
92 #else // YAZ_HAVE_ICU
93     yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
94             "<%s>\n ... \n</%s>",
95             xml_node->name, xml_node->name);
96     yaz_log(YLOG_FATAL, 
97             "But no ICU support is compiled into the YAZ library.");
98     return 0;
99 #endif // YAZ_HAVE_ICU
100 }
101
102
103 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
104 {
105     pp2_charset_t pct = xmalloc(sizeof(*pct));
106
107     pct->token_next_handler = pp2_relevance_token_a_to_z;
108     pct->get_sort_handler  = pp2_get_sort_ascii;
109 #if YAZ_HAVE_ICU
110     pct->icu_chn = 0;
111     if (icu_chn)
112     {
113         pct->icu_chn = icu_chn;
114         pct->icu_sts = U_ZERO_ERROR;
115         pct->token_next_handler = pp2_relevance_token_icu;
116         pct->get_sort_handler = pp2_get_sort_icu;
117     }
118 #endif // YAZ_HAVE_ICU
119     return pct;
120 }
121
122 void pp2_charset_destroy(pp2_charset_t pct)
123 {
124     xfree(pct);
125 }
126
127 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct,
128                                              const char *buf)
129 {
130     pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
131
132     assert(pct);
133
134     prt->norm_str = wrbuf_alloc();
135     prt->sort_str = wrbuf_alloc();
136     prt->cp = buf;
137     prt->last_cp = 0;
138     prt->pct = pct;
139
140 #if YAZ_HAVE_ICU
141     if (pct->icu_chn)
142     {
143         int ok = 0;
144         pct->icu_sts = U_ZERO_ERROR;
145         ok = icu_chain_assign_cstr(pct->icu_chn, buf, &pct->icu_sts);
146         //printf("\nfield ok: %d '%s'\n", ok, buf);
147         prt->pct = pct;
148     }
149 #endif // YAZ_HAVE_ICU
150     return prt;
151 }
152
153
154 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
155 {
156     assert(prt);
157     if(prt->norm_str) 
158         wrbuf_destroy(prt->norm_str);
159     if(prt->sort_str) 
160         wrbuf_destroy(prt->sort_str);
161     xfree(prt);
162 }
163
164 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
165 {
166     assert(prt);
167     return (prt->pct->token_next_handler)(prt);
168 }
169
170 const char *pp2_get_sort(pp2_relevance_token_t prt, int skip)
171 {
172     return prt->pct->get_sort_handler(prt, skip);
173 }
174
175 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
176 /* original tokenizer with our tokenize interface, but we
177    add +1 to ensure no '\0' are in our string (except for EOF)
178 */
179 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
180 {
181     const char *cp = prt->cp;
182     int c;
183
184     /* skip white space */
185     while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
186         cp++;
187     if (*cp == '\0')
188     {
189         prt->cp = cp;
190         prt->last_cp = 0;
191         return 0;
192     }
193     /* now read the term itself */
194
195     prt->last_cp = cp;
196     wrbuf_rewind(prt->norm_str);
197     while (*cp && (c = raw_char(tolower(*cp))) >= 0)
198     {
199         wrbuf_putc(prt->norm_str, c);
200         cp++;
201     }
202     prt->cp = cp;
203     return wrbuf_cstr(prt->norm_str);
204 }
205
206 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt,
207                                     int skip_article)
208 {
209     if (prt->last_cp == 0)
210         return 0;
211     else
212     {
213         char *tmp = xstrdup(prt->last_cp);
214         char *result = 0;
215         result = normalize7bit_mergekey(tmp, skip_article);
216         
217         wrbuf_rewind(prt->sort_str);
218         wrbuf_puts(prt->sort_str, result);
219         xfree(tmp);
220         return wrbuf_cstr(prt->sort_str);
221     }
222 }
223
224
225 #if YAZ_HAVE_ICU
226 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
227 {
228     if (icu_chain_next_token(prt->pct->icu_chn, &prt->pct->icu_sts))
229     {
230         if (U_FAILURE(prt->pct->icu_sts))
231         {
232             return 0;
233         }
234         return icu_chain_token_norm(prt->pct->icu_chn);
235     }
236     return 0;
237 }
238
239 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt,
240                                     int skip_article)
241 {
242     return icu_chain_token_sortkey(prt->pct->icu_chn);
243 }
244
245 #endif // YAZ_HAVE_ICU
246
247
248 /*
249  * Local variables:
250  * c-basic-offset: 4
251  * indent-tabs-mode: nil
252  * End:
253  * vim: shiftwidth=4 tabstop=8 expandtab
254  */