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
29 //#include <yaz/xmalloc.h>
30 #include <yaz/options.h>
35 #include <unicode/ucnv.h>
36 #include <unicode/ustring.h>
40 /* commando line and config parameters */
41 static struct config_t {
45 struct icu_chain * chain;
52 void print_option_error(const struct config_t *p_config)
54 fprintf(stderr, "Calling error, valid options are :\n");
55 fprintf(stderr, "icu_chain_test\n"
56 " [-c (path/to/config/file.xml)]\n"
57 " [-p (a|c|l|t)] print ICU info \n"
61 "cat hugetextfile.txt | ./icu_chain_test -c config.xml \n"
62 "./icu_chain_test -p c\n"
63 "./icu_chain_test -p l -x\n"
64 "./icu_chain_test -p t -x\n"
66 "Example ICU chain XML configuration file:\n"
67 "<icu_chain id=\"en:word\" locale=\"en\">\n"
68 " <normalize rule=\"[:Control:] Any-Remove\"/>\n"
69 " <tokenize rule=\"l\"/>\n"
70 " <normalize rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>\n"
72 " <casemap rule=\"l\"/>\n"
80 void read_params(int argc, char **argv, struct config_t *p_config)
85 /* set default parameters */
86 p_config->conffile[0] = 0;
87 p_config->print[0] = 0;
88 p_config->xmloutput = 0;
90 p_config->infile = stdin;
91 p_config->outfile = stdout;
93 /* set up command line parameters */
95 while ((ret = options("c:p:x", argv, argc, &arg)) != -2)
100 strcpy(p_config->conffile, arg);
103 strcpy(p_config->print, arg);
106 p_config->xmloutput = 1;
109 print_option_error(p_config);
113 if ((!strlen(p_config->conffile)
114 && !strlen(p_config->print))
118 print_option_error(p_config);
122 /* UConverter *conv; */
123 /* conv = ucnv_open("utf-8", &status); */
124 /* assert(U_SUCCESS(status)); */
127 /* = ucnv_toUChars(conv, ustr16, 1024, */
128 /* (const char *) *xstr8, strlen((const char *) *xstr8), */
133 /* ucnv_fromUChars(conv, */
134 /* (char *) *xstr8, strlen((const char *) *xstr8), */
135 /* ustr16, *ustr16_len, */
137 /* ucnv_close(conv); */
140 static void print_icu_converters(const struct config_t *p_config)
145 count = ucnv_countAvailable();
146 if (p_config->xmloutput)
147 fprintf(config.outfile, "<converters count=\"%d\" default=\"%s\">\n",
148 count, ucnv_getDefaultName());
150 fprintf(config.outfile, "Available ICU converters: %d\n", count);
151 fprintf(config.outfile, "Default ICU Converter is: '%s'\n",
152 ucnv_getDefaultName());
155 for(i=0;i<count;i++){
156 if (p_config->xmloutput)
157 fprintf(config.outfile, "<converter id=\"%s\"/>\n",
158 ucnv_getAvailableName(i));
160 fprintf(config.outfile, "%s ", ucnv_getAvailableName(i));
163 if (p_config->xmloutput)
164 fprintf(config.outfile, "</converters>\n");
166 fprintf(config.outfile, "\n");
169 static void print_icu_transliterators(const struct config_t *p_config)
174 count = utrans_countAvailableIDs();
176 int32_t buf_cap = 128;
179 if (p_config->xmloutput)
180 fprintf(config.outfile, "<transliterators count=\"%d\">\n", count);
182 fprintf(config.outfile, "Available ICU transliterators: %d\n", count);
184 for(i = 0; i <count; i++)
186 utrans_getAvailableID(i, buf, buf_cap);
187 if (p_config->xmloutput)
188 fprintf(config.outfile, "<transliterator id=\"%s\"/>\n", buf);
190 fprintf(config.outfile, " %s", buf);
193 if (p_config->xmloutput){
194 fprintf(config.outfile, "</transliterators>\n");
198 fprintf(config.outfile, "\n\nUnicode Set Patterns:\n"
199 " Pattern Description\n"
200 " Ranges [a-z] The lower case letters a through z\n"
201 " Named Chars [abc123] The six characters a,b,c,1,2 and 3\n"
202 " String [abc{def}] chars a, b and c, and string 'def'\n"
203 " Categories [\\p{Letter}] Perl General Category 'Letter'.\n"
204 " Categories [:Letter:] Posix General Category 'Letter'.\n"
206 " Combination Example\n"
207 " Union [[:Greek:] [:letter:]]\n"
208 " Intersection [[:Greek:] & [:letter:]]\n"
209 " Set Complement [[:Greek:] - [:letter:]]\n"
210 " Complement [^[:Greek:] [:letter:]]\n"
212 "see: http://icu.sourceforge.net/userguide/unicodeSet.html\n"
215 " [:Punctuation:] Any-Remove\n"
216 " [:Cased-Letter:] Any-Upper\n"
217 " [:Control:] Any-Remove\n"
218 " [:Decimal_Number:] Any-Remove\n"
219 " [:Final_Punctuation:] Any-Remove\n"
220 " [:Georgian:] Any-Upper\n"
221 " [:Katakana:] Any-Remove\n"
222 " [:Arabic:] Any-Remove\n"
223 " [:Punctuation:] Remove\n"
224 " [[:Punctuation:]-[.,]] Remove\n"
225 " [:Line_Separator:] Any-Remove\n"
226 " [:Math_Symbol:] Any-Remove\n"
227 " Lower; [:^Letter:] Remove (word tokenization)\n"
228 " [:^Number:] Remove (numeric tokenization)\n"
229 " [:^Katagana:] Remove (remove everything except Katagana)\n"
230 " Lower;[[:WhiteSpace:][:Punctuation:]] Remove (word tokenization)\n"
231 " NFD; [:Nonspacing Mark:] Remove; NFC (removes accents from characters)\n"
232 " [A-Za-z]; Lower(); Latin-Katakana; Katakana-Hiragana (transforms latin and katagana to hiragana)\n"
233 " [[:separator:][:start punctuation:][:initial punctuation:]] Remove \n"
235 "see http://icu.sourceforge.net/userguide/Transform.html\n"
236 " http://www.unicode.org/Public/UNIDATA/UCD.html\n"
237 " http://icu.sourceforge.net/userguide/Transform.html\n"
238 " http://icu.sourceforge.net/userguide/TransformRule.html\n"
242 fprintf(config.outfile, "\n\n");
247 static void print_icu_xml_locales(const struct config_t *p_config)
251 UErrorCode status = U_ZERO_ERROR;
254 int32_t keyword_len = 0;
255 char keyword_str[128];
256 int32_t keyword_str_len = 0;
259 int32_t language_len = 0;
261 int32_t lang_str_len = 0;
264 int32_t script_len = 0;
265 char script_str[128];
266 int32_t script_str_len = 0;
269 int32_t location_len = 0;
270 char location_str[128];
271 int32_t location_str_len = 0;
274 int32_t variant_len = 0;
275 char variant_str[128];
276 int32_t variant_str_len = 0;
279 int32_t name_len = 0;
281 int32_t name_str_len = 0;
284 int32_t localname_len = 0;
285 char localname_str[128];
286 int32_t localname_str_len = 0;
288 count = uloc_countAvailable() ;
290 if (p_config->xmloutput){
292 fprintf(config.outfile, "<locales count=\"%d\" default=\"%s\" collations=\"%d\">\n",
293 count, uloc_getDefault(), ucol_countAvailable());
300 = uloc_getDisplayKeyword(uloc_getAvailable(i), "en",
304 u_strToUTF8(keyword_str, 128, &keyword_str_len,
305 keyword, keyword_len,
310 = uloc_getDisplayLanguage(uloc_getAvailable(i), "en",
314 u_strToUTF8(lang_str, 128, &lang_str_len,
315 language, language_len,
320 = uloc_getDisplayScript(uloc_getAvailable(i), "en",
324 u_strToUTF8(script_str, 128, &script_str_len,
329 = uloc_getDisplayCountry(uloc_getAvailable(i), "en",
333 u_strToUTF8(location_str, 128, &location_str_len,
334 location, location_len,
338 = uloc_getDisplayVariant(uloc_getAvailable(i), "en",
342 u_strToUTF8(variant_str, 128, &variant_str_len,
343 variant, variant_len,
347 = uloc_getDisplayName(uloc_getAvailable(i), "en",
351 u_strToUTF8(name_str, 128, &name_str_len,
356 = uloc_getDisplayName(uloc_getAvailable(i), uloc_getAvailable(i),
360 u_strToUTF8(localname_str, 128, &localname_str_len,
361 localname, localname_len,
365 if (p_config->xmloutput){
366 fprintf(config.outfile, "<locale id=\"%s\"", uloc_getAvailable(i));
367 /* fprintf(config.outfile, " locale=\"%s\"", uloc_getAvailable(i)); */
368 /* if (strlen(keyword_str)) */
369 /* fprintf(config.outfile, " keyword=\"%s\"", keyword_str); */
370 /* if (ucol_getAvailable(i)) */
371 /* fprintf(config.outfile, " collation=\"1\""); */
372 if (strlen(lang_str))
373 fprintf(config.outfile, " language=\"%s\"", lang_str);
374 if (strlen(script_str))
375 fprintf(config.outfile, " script=\"%s\"", script_str);
376 if (strlen(location_str))
377 fprintf(config.outfile, " location=\"%s\"", location_str);
378 if (strlen(variant_str))
379 fprintf(config.outfile, " variant=\"%s\"", variant_str);
380 if (strlen(name_str))
381 fprintf(config.outfile, " name=\"%s\"", name_str);
382 if (strlen(localname_str))
383 fprintf(config.outfile, " localname=\"%s\"", localname_str);
384 fprintf(config.outfile, ">");
385 if (strlen(localname_str))
386 fprintf(config.outfile, "%s", localname_str);
387 fprintf(config.outfile, "</locale>\n");
389 else if (1 == p_config->xmloutput){
390 fprintf(config.outfile, "%s", uloc_getAvailable(i));
391 fprintf(config.outfile, " | ");
392 if (strlen(name_str))
393 fprintf(config.outfile, "%s", name_str);
394 fprintf(config.outfile, " | ");
395 if (strlen(localname_str))
396 fprintf(config.outfile, "%s", localname_str);
397 fprintf(config.outfile, "\n");
400 fprintf(config.outfile, "%s ", uloc_getAvailable(i));
402 if (p_config->xmloutput)
403 fprintf(config.outfile, "</locales>\n");
405 fprintf(config.outfile, "\n");
407 if(U_FAILURE(status)) {
408 fprintf(stderr, "ICU Error: %d %s\n", status, u_errorName(status));
414 static void print_info(const struct config_t *p_config)
416 if (p_config->xmloutput)
417 fprintf(config.outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
420 if ('c' == config.print[0])
421 print_icu_converters(&config);
422 else if ('l' == config.print[0])
423 print_icu_xml_locales(&config);
424 else if ('t' == config.print[0])
425 print_icu_transliterators(&config);
427 print_icu_converters(&config);
428 print_icu_xml_locales(&config);
429 print_icu_transliterators(&config);
432 if (p_config->xmloutput)
433 fprintf(config.outfile, "</icu>\n");
440 static void process_text_file(const struct config_t *p_config)
445 xmlDoc *doc = xmlParseFile(config.conffile);
446 xmlNode *xml_node = xmlDocGetRootElement(doc);
448 long unsigned int token_count = 0;
449 long unsigned int line_count = 0;
451 UErrorCode status = U_ZERO_ERROR;
455 printf("Could not parse XML config file '%s' \n",
461 config.chain = icu_chain_xml_config(xml_node, &status);
463 if (config.chain && U_SUCCESS(status))
466 printf("Could not set up ICU chain from config file '%s' \n",
471 if (p_config->xmloutput)
472 fprintf(config.outfile,
473 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
477 // read input lines for processing
478 while ((line=fgets(linebuf, sizeof(linebuf)-1, config.infile)))
480 success = icu_chain_assign_cstr(config.chain, line, &status);
483 while (success && icu_chain_next_token(config.chain, &status)){
484 if (U_FAILURE(status))
488 if (p_config->xmloutput)
489 fprintf(config.outfile,
490 "<token id=\%lu\" line=\"%lu\""
491 " norm=\"%s\" display=\"%s\"/>\n",
494 icu_chain_get_norm(config.chain),
495 icu_chain_get_display(config.chain));
497 fprintf(config.outfile, "%lu %lu '%s' '%s'\n",
500 icu_chain_get_norm(config.chain),
501 icu_chain_get_display(config.chain));
507 if (p_config->xmloutput)
508 fprintf(config.outfile,
512 icu_chain_destroy(config.chain);
521 int main(int argc, char **argv)
526 read_params(argc, argv, &config);
528 if (config.conffile && strlen(config.conffile))
529 process_text_file(&config);
531 if (config.print && strlen(config.print))
536 printf("ICU not available on your system.\n"
537 "Please install libicu36-dev and icu-doc or similar, "
538 "re-configure and re-compile\n");
550 * indent-tabs-mode: nil
552 * vim: shiftwidth=4 tabstop=8 expandtab