1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 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"
42 #if YAZ_VERSIONL >= 0x40002
43 /* YAZ 4.0.2 or later as icu_iter */
50 struct pp2_charset_s {
51 const char *(*token_next_handler)(pp2_relevance_token_t prt);
52 const char *(*get_sort_handler)(pp2_relevance_token_t prt);
55 struct icu_chain * icu_chn;
60 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
61 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt);
64 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
65 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt);
68 /* tokenzier handle */
69 struct pp2_relevance_token_s {
70 const char *cp; /* unnormalized buffer we're tokenizing */
71 const char *last_cp; /* pointer to last token we're dealing with */
72 pp2_charset_t pct; /* our main charset handle (type+config) */
73 WRBUF norm_str; /* normized string we return (temporarily) */
74 WRBUF sort_str; /* sort string we return (temporarily) */
81 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
84 UErrorCode status = U_ZERO_ERROR;
85 struct icu_chain *chain = 0;
87 xml_node = xml_node->children;
88 while (xml_node && xml_node->type != XML_ELEMENT_NODE)
89 xml_node = xml_node->next;
90 chain = icu_chain_xml_config(xml_node, 1, &status);
91 if (!chain || U_FAILURE(status)){
92 //xmlDocPtr icu_doc = 0;
93 //xmlChar *xmlstr = 0;
95 //xmlDocDumpMemory(icu_doc, size);
97 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
99 xml_node->name, xml_node->name);
102 return pp2_charset_create(chain);
103 #else // YAZ_HAVE_ICU
104 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
105 "<%s>\n ... \n</%s>",
106 xml_node->name, xml_node->name);
108 "But no ICU support is compiled into the YAZ library.");
110 #endif // YAZ_HAVE_ICU
113 void pp2_charset_incref(pp2_charset_t pct)
118 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
120 pp2_charset_t pct = xmalloc(sizeof(*pct));
122 pct->token_next_handler = pp2_relevance_token_a_to_z;
123 pct->get_sort_handler = pp2_get_sort_ascii;
129 pct->icu_chn = icu_chn;
130 pct->icu_sts = U_ZERO_ERROR;
131 pct->token_next_handler = pp2_relevance_token_icu;
132 pct->get_sort_handler = pp2_get_sort_icu;
134 #endif // YAZ_HAVE_ICU
138 void pp2_charset_destroy(pp2_charset_t pct)
142 assert(pct->ref_count >= 1);
144 if (pct->ref_count == 0)
147 icu_chain_destroy(pct->icu_chn);
154 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct,
158 pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
166 char *pout = firstword;
167 char articles[] = "the den der die des an a "; // must end in space
169 while (*p && !isalnum(*(unsigned char *)p))
171 for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
172 *pout++ = tolower(*(unsigned char *)p);
175 if (strstr(articles, firstword))
179 prt->norm_str = wrbuf_alloc();
180 prt->sort_str = wrbuf_alloc();
192 prt->iter = icu_iter_create(pct->icu_chn);
193 icu_iter_first(prt->iter, buf);
196 pct->icu_sts = U_ZERO_ERROR;
198 ok = icu_chain_assign_cstr(pct->icu_chn, buf, &pct->icu_sts);
200 //printf("\nfield ok: %d '%s'\n", ok, buf);
203 #endif // YAZ_HAVE_ICU
208 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
213 icu_iter_destroy(prt->iter);
216 wrbuf_destroy(prt->norm_str);
218 wrbuf_destroy(prt->sort_str);
222 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
225 return (prt->pct->token_next_handler)(prt);
228 const char *pp2_get_sort(pp2_relevance_token_t prt)
230 return prt->pct->get_sort_handler(prt);
233 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
234 /* original tokenizer with our tokenize interface, but we
235 add +1 to ensure no '\0' are in our string (except for EOF)
237 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
239 const char *cp = prt->cp;
242 /* skip white space */
243 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
251 /* now read the term itself */
254 wrbuf_rewind(prt->norm_str);
255 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
257 wrbuf_putc(prt->norm_str, c);
261 return wrbuf_cstr(prt->norm_str);
264 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt)
266 if (prt->last_cp == 0)
270 char *tmp = xstrdup(prt->last_cp);
272 result = normalize7bit_mergekey(tmp);
274 wrbuf_rewind(prt->sort_str);
275 wrbuf_puts(prt->sort_str, result);
277 return wrbuf_cstr(prt->sort_str);
283 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
286 if (icu_iter_next(prt->iter))
288 return icu_iter_get_norm(prt->iter);
291 if (icu_chain_next_token(prt->pct->icu_chn, &prt->pct->icu_sts))
293 if (U_FAILURE(prt->pct->icu_sts))
297 return icu_chain_token_norm(prt->pct->icu_chn);
303 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt)
306 return icu_iter_get_sortkey(prt->iter);
308 return icu_chain_token_sortkey(prt->pct->icu_chn);
312 #endif // YAZ_HAVE_ICU
318 * c-file-style: "Stroustrup"
319 * indent-tabs-mode: nil
321 * vim: shiftwidth=4 tabstop=8 expandtab