1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 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"
42 struct pp2_charset_s {
43 const char *(*token_next_handler)(pp2_relevance_token_t prt);
44 const char *(*get_sort_handler)(pp2_relevance_token_t prt, int skip);
46 struct icu_chain * icu_chn;
51 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
52 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt, int skip_article);
55 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
56 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt, int skip_article);
59 /* tokenzier handle */
60 struct pp2_relevance_token_s {
61 const char *cp; /* unnormalized buffer we're tokenizing */
62 const char *last_cp; /* pointer to last token we're dealing with */
63 pp2_charset_t pct; /* our main charset handle (type+config) */
64 WRBUF norm_str; /* normized string we return (temporarily) */
65 WRBUF sort_str; /* sort string we return (temporarily) */
69 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
72 UErrorCode status = U_ZERO_ERROR;
73 struct icu_chain *chain = 0;
75 xml_node = xml_node->children;
76 while (xml_node && xml_node->type != XML_ELEMENT_NODE)
77 xml_node = xml_node->next;
78 chain = icu_chain_xml_config(xml_node, &status);
79 if (!chain || U_FAILURE(status)){
80 //xmlDocPtr icu_doc = 0;
81 //xmlChar *xmlstr = 0;
83 //xmlDocDumpMemory(icu_doc, size);
85 yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
87 xml_node->name, xml_node->name);
90 return pp2_charset_create(chain);
92 yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
94 xml_node->name, xml_node->name);
96 "But no ICU support compiled into pazpar2 server.");
98 "Please install libicu36-dev and icu-doc or similar, "
99 "re-configure and re-compile");
105 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
107 pp2_charset_t pct = xmalloc(sizeof(*pct));
109 pct->token_next_handler = pp2_relevance_token_a_to_z;
110 pct->get_sort_handler = pp2_get_sort_ascii;
115 pct->icu_chn = icu_chn;
116 pct->icu_sts = U_ZERO_ERROR;
117 pct->token_next_handler = pp2_relevance_token_icu;
118 pct->get_sort_handler = pp2_get_sort_icu;
124 void pp2_charset_destroy(pp2_charset_t pct)
129 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct,
132 pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
136 prt->norm_str = wrbuf_alloc();
137 prt->sort_str = wrbuf_alloc();
146 pct->icu_sts = U_ZERO_ERROR;
147 ok = icu_chain_assign_cstr(pct->icu_chn, buf, &pct->icu_sts);
148 //printf("\nfield ok: %d '%s'\n", ok, buf);
156 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
160 wrbuf_destroy(prt->norm_str);
162 wrbuf_destroy(prt->sort_str);
166 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
169 return (prt->pct->token_next_handler)(prt);
172 const char *pp2_get_sort(pp2_relevance_token_t prt, int skip)
174 return prt->pct->get_sort_handler(prt, skip);
177 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
178 /* original tokenizer with our tokenize interface, but we
179 add +1 to ensure no '\0' are in our string (except for EOF)
181 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
183 const char *cp = prt->cp;
186 /* skip white space */
187 while (*cp && (c = raw_char(tolower(*(const unsigned char *)cp))) < 0)
195 /* now read the term itself */
198 wrbuf_rewind(prt->norm_str);
199 while (*cp && (c = raw_char(tolower(*cp))) >= 0)
201 wrbuf_putc(prt->norm_str, c);
205 return wrbuf_cstr(prt->norm_str);
208 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt,
211 if (prt->last_cp == 0)
215 char *tmp = xstrdup(prt->last_cp);
217 result = normalize7bit_mergekey(tmp, skip_article);
219 wrbuf_rewind(prt->sort_str);
220 wrbuf_puts(prt->sort_str, result);
222 return wrbuf_cstr(prt->sort_str);
228 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
230 if (icu_chain_next_token(prt->pct->icu_chn, &prt->pct->icu_sts))
232 if (U_FAILURE(prt->pct->icu_sts))
236 return icu_chain_get_norm(prt->pct->icu_chn);
241 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt,
244 return icu_chain_get_sort(prt->pct->icu_chn);
254 * indent-tabs-mode: nil
256 * vim: shiftwidth=4 tabstop=8 expandtab