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"
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);
49 struct icu_chain * icu_chn;
54 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
55 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt);
58 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
59 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt);
62 /* tokenzier handle */
63 struct pp2_relevance_token_s {
64 const char *cp; /* unnormalized buffer we're tokenizing */
65 const char *last_cp; /* pointer to last token we're dealing with */
66 pp2_charset_t pct; /* our main charset handle (type+config) */
67 WRBUF norm_str; /* normized string we return (temporarily) */
68 WRBUF sort_str; /* sort string we return (temporarily) */
75 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
78 UErrorCode status = U_ZERO_ERROR;
79 struct icu_chain *chain = 0;
81 xml_node = xml_node->children;
82 while (xml_node && xml_node->type != XML_ELEMENT_NODE)
83 xml_node = xml_node->next;
84 chain = icu_chain_xml_config(xml_node, 1, &status);
85 if (!chain || U_FAILURE(status)){
86 //xmlDocPtr icu_doc = 0;
87 //xmlChar *xmlstr = 0;
89 //xmlDocDumpMemory(icu_doc, size);
91 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
93 xml_node->name, xml_node->name);
96 return pp2_charset_create(chain);
98 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
100 xml_node->name, xml_node->name);
102 "But no ICU support is compiled into the YAZ library.");
104 #endif // YAZ_HAVE_ICU
107 void pp2_charset_incref(pp2_charset_t pct)
112 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
114 pp2_charset_t pct = xmalloc(sizeof(*pct));
116 pct->token_next_handler = pp2_relevance_token_a_to_z;
117 pct->get_sort_handler = pp2_get_sort_ascii;
123 pct->icu_chn = icu_chn;
124 pct->icu_sts = U_ZERO_ERROR;
125 pct->token_next_handler = pp2_relevance_token_icu;
126 pct->get_sort_handler = pp2_get_sort_icu;
128 #endif // YAZ_HAVE_ICU
132 void pp2_charset_destroy(pp2_charset_t pct)
136 assert(pct->ref_count >= 1);
138 if (pct->ref_count == 0)
141 icu_chain_destroy(pct->icu_chn);
148 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct)
150 pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
154 prt->norm_str = wrbuf_alloc();
155 prt->sort_str = wrbuf_alloc();
163 prt->iter = icu_iter_create(pct->icu_chn);
168 void pp2_relevance_first(pp2_relevance_token_t prt,
176 char *pout = firstword;
177 char articles[] = "the den der die des an a "; // must end in space
179 while (*p && !isalnum(*(unsigned char *)p))
181 for (; *p && *p != ' ' && pout - firstword < (sizeof(firstword)-2); p++)
182 *pout++ = tolower(*(unsigned char *)p);
185 if (strstr(articles, firstword))
189 wrbuf_rewind(prt->norm_str);
190 wrbuf_rewind(prt->sort_str);
197 icu_iter_first(prt->iter, buf);
199 #endif // YAZ_HAVE_ICU
202 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
207 icu_iter_destroy(prt->iter);
210 wrbuf_destroy(prt->norm_str);
212 wrbuf_destroy(prt->sort_str);
216 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
219 return (prt->pct->token_next_handler)(prt);
222 const char *pp2_get_sort(pp2_relevance_token_t prt)
224 return prt->pct->get_sort_handler(prt);
227 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
228 /* original tokenizer with our tokenize interface, but we
229 add +1 to ensure no '\0' are in our string (except for EOF)
231 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
233 const char *cp = prt->cp;
236 /* skip white space */
237 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
245 /* now read the term itself */
248 wrbuf_rewind(prt->norm_str);
249 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
251 wrbuf_putc(prt->norm_str, c);
255 return wrbuf_cstr(prt->norm_str);
258 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt)
260 if (prt->last_cp == 0)
264 char *tmp = xstrdup(prt->last_cp);
266 result = normalize7bit_mergekey(tmp);
268 wrbuf_rewind(prt->sort_str);
269 wrbuf_puts(prt->sort_str, result);
271 return wrbuf_cstr(prt->sort_str);
277 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
279 if (icu_iter_next(prt->iter))
281 return icu_iter_get_norm(prt->iter);
286 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt)
288 return icu_iter_get_sortkey(prt->iter);
291 #endif // YAZ_HAVE_ICU
297 * c-file-style: "Stroustrup"
298 * indent-tabs-mode: nil
300 * vim: shiftwidth=4 tabstop=8 expandtab