1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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>
35 #include "normalize7bit.h"
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);
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 struct icu_chain *chain = 0;
77 xml_node = xml_node->children;
78 while (xml_node && xml_node->type != XML_ELEMENT_NODE)
79 xml_node = xml_node->next;
80 chain = icu_chain_xml_config(xml_node, 1, &status);
81 if (!chain || U_FAILURE(status)){
82 //xmlDocPtr icu_doc = 0;
83 //xmlChar *xmlstr = 0;
85 //xmlDocDumpMemory(icu_doc, size);
87 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
89 xml_node->name, xml_node->name);
92 return pp2_charset_create(chain);
94 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
96 xml_node->name, xml_node->name);
98 "But no ICU support is compiled into the YAZ library.");
100 #endif // YAZ_HAVE_ICU
103 void pp2_charset_incref(pp2_charset_t pct)
108 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
110 pp2_charset_t pct = xmalloc(sizeof(*pct));
112 pct->token_next_handler = pp2_relevance_token_a_to_z;
113 pct->get_sort_handler = pp2_get_sort_ascii;
119 pct->icu_chn = icu_chn;
120 pct->icu_sts = U_ZERO_ERROR;
121 pct->token_next_handler = pp2_relevance_token_icu;
122 pct->get_sort_handler = pp2_get_sort_icu;
124 #endif // YAZ_HAVE_ICU
128 void pp2_charset_destroy(pp2_charset_t pct)
132 assert(pct->ref_count >= 1);
134 if (pct->ref_count == 0)
137 icu_chain_destroy(pct->icu_chn);
144 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct,
147 pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
151 prt->norm_str = wrbuf_alloc();
152 prt->sort_str = wrbuf_alloc();
161 pct->icu_sts = U_ZERO_ERROR;
162 ok = icu_chain_assign_cstr(pct->icu_chn, buf, &pct->icu_sts);
163 //printf("\nfield ok: %d '%s'\n", ok, buf);
166 #endif // YAZ_HAVE_ICU
171 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
175 wrbuf_destroy(prt->norm_str);
177 wrbuf_destroy(prt->sort_str);
181 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
184 return (prt->pct->token_next_handler)(prt);
187 const char *pp2_get_sort(pp2_relevance_token_t prt, int skip)
189 return prt->pct->get_sort_handler(prt, skip);
192 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
193 /* original tokenizer with our tokenize interface, but we
194 add +1 to ensure no '\0' are in our string (except for EOF)
196 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
198 const char *cp = prt->cp;
201 /* skip white space */
202 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
210 /* now read the term itself */
213 wrbuf_rewind(prt->norm_str);
214 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
216 wrbuf_putc(prt->norm_str, c);
220 return wrbuf_cstr(prt->norm_str);
223 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt,
226 if (prt->last_cp == 0)
230 char *tmp = xstrdup(prt->last_cp);
232 result = normalize7bit_mergekey(tmp, skip_article);
234 wrbuf_rewind(prt->sort_str);
235 wrbuf_puts(prt->sort_str, result);
237 return wrbuf_cstr(prt->sort_str);
243 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
245 if (icu_chain_next_token(prt->pct->icu_chn, &prt->pct->icu_sts))
247 if (U_FAILURE(prt->pct->icu_sts))
251 return icu_chain_token_norm(prt->pct->icu_chn);
256 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt,
259 return icu_chain_token_sortkey(prt->pct->icu_chn);
262 #endif // YAZ_HAVE_ICU
268 * c-file-style: "Stroustrup"
269 * indent-tabs-mode: nil
271 * vim: shiftwidth=4 tabstop=8 expandtab