1 /* $Id: charsets.c,v 1.6 2007-09-10 16:25:50 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
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
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
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
23 \brief Pazpar2 Character set facilities
30 #include <yaz/xmalloc.h>
31 #include <yaz/wrbuf.h>
37 #include "normalize7bit.h"
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);
48 struct icu_chain * icu_chn;
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);
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);
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) */
71 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
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;
82 //xmlDocDumpMemory(icu_doc, size);
84 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
86 xml_node->name, xml_node->name);
89 return pp2_charset_create(chain);
91 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
95 "But no ICU support compiled into pazpar2 server.");
97 "Please install libicu36-dev and icu-doc or similar, "
98 "re-configure and re-compile");
104 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
106 pp2_charset_t pct = xmalloc(sizeof(*pct));
108 pct->token_next_handler = pp2_relevance_token_a_to_z;
109 pct->get_sort_handler = pp2_get_sort_ascii;
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;
123 void pp2_charset_destroy(pp2_charset_t pct)
128 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct,
131 pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
135 prt->norm_str = wrbuf_alloc();
136 prt->sort_str = wrbuf_alloc();
144 pct->icu_sts = U_ZERO_ERROR;
146 ok = icu_chain_assign_cstr(pct->icu_chn, buf, &pct->icu_sts);
147 //printf("\nfield ok: %d '%s'\n", ok, buf);
155 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
159 wrbuf_destroy(prt->norm_str);
161 wrbuf_destroy(prt->sort_str);
165 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
168 return (prt->pct->token_next_handler)(prt);
171 const char *pp2_get_sort(pp2_relevance_token_t prt, int skip)
173 return prt->pct->get_sort_handler(prt, skip);
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)
180 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
182 const char *cp = prt->cp;
185 /* skip white space */
186 while (*cp && (c = raw_char(tolower(*cp))) < 0)
194 /* now read the term itself */
197 wrbuf_rewind(prt->norm_str);
198 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
200 wrbuf_putc(prt->norm_str, c);
204 return wrbuf_cstr(prt->norm_str);
207 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt,
210 if (prt->last_cp == 0)
214 char *tmp = xstrdup(prt->last_cp);
216 result = normalize7bit_mergekey(tmp, skip_article);
218 wrbuf_rewind(prt->sort_str);
219 wrbuf_puts(prt->sort_str, result);
221 return wrbuf_cstr(prt->sort_str);
227 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
229 if (icu_chain_next_token(prt->pct->icu_chn, &prt->pct->icu_sts))
231 if (U_FAILURE(prt->pct->icu_sts))
235 return icu_chain_get_norm(prt->pct->icu_chn);
240 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt,
243 return icu_chain_get_sort(prt->pct->icu_chn);
253 * indent-tabs-mode: nil
255 * vim: shiftwidth=4 tabstop=8 expandtab