1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 Index Data
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
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
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
21 \brief Pazpar2 Character set facilities
28 #include <yaz/xmalloc.h>
29 #include <yaz/wrbuf.h>
31 #include <yaz/yaz-version.h>
37 #include "normalize7bit.h"
43 typedef struct pp2_charset_s *pp2_charset_t;
44 static pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node);
45 static pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn);
46 static pp2_charset_t pp2_charset_create_a_to_z(void);
47 static void pp2_charset_destroy(pp2_charset_t pct);
48 static pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct);
51 struct pp2_charset_s {
52 const char *(*token_next_handler)(pp2_relevance_token_t prt);
53 const char *(*get_sort_handler)(pp2_relevance_token_t prt);
54 const char *(*get_display_handler)(pp2_relevance_token_t prt);
56 struct icu_chain * icu_chn;
61 static const char *pp2_relevance_token_null(pp2_relevance_token_t prt);
62 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
63 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt);
64 static const char *pp2_get_display_ascii(pp2_relevance_token_t prt);
67 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
68 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt);
69 static const char *pp2_get_display_icu(pp2_relevance_token_t prt);
72 /* tokenzier handle */
73 struct pp2_relevance_token_s {
74 const char *cp; /* unnormalized buffer we're tokenizing */
75 const char *last_cp; /* pointer to last token we're dealing with */
76 pp2_charset_t pct; /* our main charset handle (type+config) */
77 WRBUF norm_str; /* normized string we return (temporarily) */
78 WRBUF sort_str; /* sort string we return (temporarily) */
84 struct pp2_charset_fact_s {
85 struct pp2_charset_entry *list;
89 struct pp2_charset_entry {
90 struct pp2_charset_entry *next;
96 static int pp2_charset_fact_add(pp2_charset_fact_t pft,
97 pp2_charset_t pct, const char *default_id);
99 pp2_charset_fact_t pp2_charset_fact_create(void)
101 pp2_charset_fact_t pft = xmalloc(sizeof(*pft));
105 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "relevance");
106 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "sort");
107 pp2_charset_fact_add(pft, pp2_charset_create_a_to_z(), "mergekey");
108 pp2_charset_fact_add(pft, pp2_charset_create(0), "facet");
112 void pp2_charset_fact_destroy(pp2_charset_fact_t pft)
116 assert(pft->ref_count >= 1);
118 if (pft->ref_count == 0)
120 struct pp2_charset_entry *pce = pft->list;
123 struct pp2_charset_entry *next = pce->next;
124 pp2_charset_destroy(pce->pct);
134 int pp2_charset_fact_add(pp2_charset_fact_t pft,
135 pp2_charset_t pct, const char *default_id)
137 struct pp2_charset_entry *pce;
139 for (pce = pft->list; pce; pce = pce->next)
140 if (!strcmp(default_id, pce->name))
145 pce = xmalloc(sizeof(*pce));
146 pce->name = xstrdup(default_id);
147 pce->next = pft->list;
152 pp2_charset_destroy(pce->pct);
158 int pp2_charset_fact_define(pp2_charset_fact_t pft,
159 xmlNode *xml_node, const char *default_id)
166 pct = pp2_charset_create_xml(xml_node);
169 id = xmlGetProp(xml_node, (xmlChar*) "id");
171 default_id = (const char *) id;
174 pp2_charset_destroy(pct);
177 r = pp2_charset_fact_add(pft, pct, default_id);
182 void pp2_charset_fact_incref(pp2_charset_fact_t pft)
187 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
190 UErrorCode status = U_ZERO_ERROR;
191 struct icu_chain *chain = 0;
192 while (xml_node && xml_node->type != XML_ELEMENT_NODE)
193 xml_node = xml_node->next;
194 chain = icu_chain_xml_config(xml_node, 1, &status);
195 if (!chain || U_FAILURE(status)){
196 //xmlDocPtr icu_doc = 0;
197 //xmlChar *xmlstr = 0;
199 //xmlDocDumpMemory(icu_doc, size);
201 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
202 "<%s>\n ... \n</%s>",
203 xml_node->name, xml_node->name);
206 return pp2_charset_create(chain);
207 #else // YAZ_HAVE_ICU
208 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
209 "<%s>\n ... \n</%s>",
210 xml_node->name, xml_node->name);
212 "But no ICU support is compiled into the YAZ library.");
214 #endif // YAZ_HAVE_ICU
217 pp2_charset_t pp2_charset_create_a_to_z(void)
219 pp2_charset_t pct = pp2_charset_create(0);
220 pct->token_next_handler = pp2_relevance_token_a_to_z;
224 pp2_charset_t pp2_charset_create(struct icu_chain *icu_chn)
226 pp2_charset_t pct = xmalloc(sizeof(*pct));
228 pct->token_next_handler = pp2_relevance_token_null;
229 pct->get_sort_handler = pp2_get_sort_ascii;
230 pct->get_display_handler = pp2_get_display_ascii;
235 pct->icu_chn = icu_chn;
236 pct->icu_sts = U_ZERO_ERROR;
237 pct->token_next_handler = pp2_relevance_token_icu;
238 pct->get_sort_handler = pp2_get_sort_icu;
239 pct->get_display_handler = pp2_get_display_icu;
241 #endif // YAZ_HAVE_ICU
245 void pp2_charset_destroy(pp2_charset_t pct)
248 icu_chain_destroy(pct->icu_chn);
253 pp2_relevance_token_t pp2_relevance_create(pp2_charset_fact_t pft,
256 struct pp2_charset_entry *pce;
257 for (pce = pft->list; pce; pce = pce->next)
258 if (!strcmp(id, pce->name))
259 return pp2_relevance_tokenize(pce->pct);
263 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct)
265 pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
269 prt->norm_str = wrbuf_alloc();
270 prt->sort_str = wrbuf_alloc();
278 prt->iter = icu_iter_create(pct->icu_chn);
283 void pp2_relevance_first(pp2_relevance_token_t prt,
291 char *pout = firstword;
292 char articles[] = "the den der die des an a "; // must end in space
294 for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
295 *pout++ = tolower(*(unsigned char *)p);
298 if (strstr(articles, firstword))
302 wrbuf_rewind(prt->norm_str);
303 wrbuf_rewind(prt->sort_str);
310 icu_iter_first(prt->iter, buf);
312 #endif // YAZ_HAVE_ICU
315 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
320 icu_iter_destroy(prt->iter);
323 wrbuf_destroy(prt->norm_str);
325 wrbuf_destroy(prt->sort_str);
329 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
332 return (prt->pct->token_next_handler)(prt);
335 const char *pp2_get_sort(pp2_relevance_token_t prt)
337 return prt->pct->get_sort_handler(prt);
340 const char *pp2_get_display(pp2_relevance_token_t prt)
342 return prt->pct->get_display_handler(prt);
345 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
346 /* original tokenizer with our tokenize interface, but we
347 add +1 to ensure no '\0' are in our string (except for EOF)
349 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
351 const char *cp = prt->cp;
354 /* skip white space */
355 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
363 /* now read the term itself */
366 wrbuf_rewind(prt->norm_str);
367 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
369 wrbuf_putc(prt->norm_str, c);
373 return wrbuf_cstr(prt->norm_str);
376 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt)
378 if (prt->last_cp == 0)
382 char *tmp = xstrdup(prt->last_cp);
384 result = normalize7bit_mergekey(tmp);
386 wrbuf_rewind(prt->sort_str);
387 wrbuf_puts(prt->sort_str, result);
389 return wrbuf_cstr(prt->sort_str);
393 static const char *pp2_get_display_ascii(pp2_relevance_token_t prt)
395 if (prt->last_cp == 0)
399 return wrbuf_cstr(prt->norm_str);
403 static const char *pp2_relevance_token_null(pp2_relevance_token_t prt)
405 const char *cp = prt->cp;
407 prt->last_cp = *cp ? cp : 0;
415 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
417 if (icu_iter_next(prt->iter))
419 return icu_iter_get_norm(prt->iter);
424 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt)
426 return icu_iter_get_sortkey(prt->iter);
429 static const char *pp2_get_display_icu(pp2_relevance_token_t prt)
431 return icu_iter_get_display(prt->iter);
434 #endif // YAZ_HAVE_ICU
440 * c-file-style: "Stroustrup"
441 * indent-tabs-mode: nil
443 * vim: shiftwidth=4 tabstop=8 expandtab