2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: yaz-icu.c,v 1.16 2008-01-14 22:58:06 adam Exp $
17 #include <yaz/options.h>
21 #include <unicode/ucnv.h>
22 #include <unicode/ustring.h>
23 #include <unicode/ucol.h>
24 #include <unicode/ubrk.h>
25 #include <unicode/utrans.h>
28 #include <yaz/wrbuf.h>
30 /* commando line and config parameters */
31 static struct config_t {
35 yaz_icu_chain_t chain;
42 void print_option_error(const struct config_t *p_config)
44 fprintf(stderr, "Calling error, valid options are :\n");
45 fprintf(stderr, "yaz-icu\n"
46 " [-c (path/to/config/file.xml)]\n"
47 " [-p (a|c|l|t)] print ICU info \n"
51 "cat hugetextfile.txt | ./yaz-icu -c config.xml \n"
56 "Example ICU chain XML configuration file:\n"
57 "<icu_chain locale=\"en\">\n"
58 " <transform rule=\"[:Control:] Any-Remove\"/>\n"
59 " <tokenize rule=\"l\"/>\n"
60 " <transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>\n"
61 " <casemap rule=\"l\"/>\n"
67 void read_params(int argc, char **argv, struct config_t *p_config)
72 /* set default parameters */
73 p_config->conffile[0] = 0;
74 p_config->print[0] = 0;
75 p_config->xmloutput = 0;
77 p_config->infile = stdin;
78 p_config->outfile = stdout;
80 /* set up command line parameters */
82 while ((ret = options("c:p:x", argv, argc, &arg)) != -2)
87 strcpy(p_config->conffile, arg);
90 strcpy(p_config->print, arg);
93 p_config->xmloutput = 1;
96 print_option_error(p_config);
100 if ((!strlen(p_config->conffile)
101 && !strlen(p_config->print))
105 print_option_error(p_config);
109 /* UConverter *conv; */
110 /* conv = ucnv_open("utf-8", &status); */
111 /* assert(U_SUCCESS(status)); */
114 /* = ucnv_toUChars(conv, ustr16, 1024, */
115 /* (const char *) *xstr8, strlen((const char *) *xstr8), */
120 /* ucnv_fromUChars(conv, */
121 /* (char *) *xstr8, strlen((const char *) *xstr8), */
122 /* ustr16, *ustr16_len, */
124 /* ucnv_close(conv); */
127 static void print_icu_converters(const struct config_t *p_config)
132 count = ucnv_countAvailable();
133 if (p_config->xmloutput)
134 fprintf(config.outfile, "<converters count=\"%d\" default=\"%s\">\n",
135 count, ucnv_getDefaultName());
137 fprintf(config.outfile, "Available ICU converters: %d\n", count);
138 fprintf(config.outfile, "Default ICU Converter is: '%s'\n",
139 ucnv_getDefaultName());
144 if (p_config->xmloutput)
145 fprintf(config.outfile, "<converter id=\"%s\"/>\n",
146 ucnv_getAvailableName(i));
148 fprintf(config.outfile, "%s ", ucnv_getAvailableName(i));
151 if (p_config->xmloutput)
152 fprintf(config.outfile, "</converters>\n");
154 fprintf(config.outfile, "\n");
157 static void print_icu_transliterators(const struct config_t *p_config)
159 int32_t buf_cap = 128;
162 int32_t count = utrans_countAvailableIDs();
164 if (p_config->xmloutput)
165 fprintf(config.outfile, "<transliterators count=\"%d\">\n", count);
167 fprintf(config.outfile, "Available ICU transliterators: %d\n", count);
169 for(i = 0; i <count; i++)
171 utrans_getAvailableID(i, buf, buf_cap);
172 if (p_config->xmloutput)
173 fprintf(config.outfile, "<transliterator id=\"%s\"/>\n", buf);
175 fprintf(config.outfile, " %s", buf);
178 if (p_config->xmloutput)
180 fprintf(config.outfile, "</transliterators>\n");
184 fprintf(config.outfile, "\n\nUnicode Set Patterns:\n"
185 " Pattern Description\n"
186 " Ranges [a-z] The lower case letters a through z\n"
187 " Named Chars [abc123] The six characters a,b,c,1,2 and 3\n"
188 " String [abc{def}] chars a, b and c, and string 'def'\n"
189 " Categories [\\p{Letter}] Perl General Category 'Letter'.\n"
190 " Categories [:Letter:] Posix General Category 'Letter'.\n"
192 " Combination Example\n"
193 " Union [[:Greek:] [:letter:]]\n"
194 " Intersection [[:Greek:] & [:letter:]]\n"
195 " Set Complement [[:Greek:] - [:letter:]]\n"
196 " Complement [^[:Greek:] [:letter:]]\n"
198 "see: http://icu.sourceforge.net/userguide/unicodeSet.html\n"
201 " [:Punctuation:] Any-Remove\n"
202 " [:Cased-Letter:] Any-Upper\n"
203 " [:Control:] Any-Remove\n"
204 " [:Decimal_Number:] Any-Remove\n"
205 " [:Final_Punctuation:] Any-Remove\n"
206 " [:Georgian:] Any-Upper\n"
207 " [:Katakana:] Any-Remove\n"
208 " [:Arabic:] Any-Remove\n"
209 " [:Punctuation:] Remove\n"
210 " [[:Punctuation:]-[.,]] Remove\n"
211 " [:Line_Separator:] Any-Remove\n"
212 " [:Math_Symbol:] Any-Remove\n"
213 " Lower; [:^Letter:] Remove (word tokenization)\n"
214 " [:^Number:] Remove (numeric tokenization)\n"
215 " [:^Katagana:] Remove (remove everything except Katagana)\n"
216 " Lower;[[:WhiteSpace:][:Punctuation:]] Remove (word tokenization)\n"
217 " NFD; [:Nonspacing Mark:] Remove; NFC (removes accents from characters)\n"
218 " [A-Za-z]; Lower(); Latin-Katakana; Katakana-Hiragana (transforms latin and katagana to hiragana)\n"
219 " [[:separator:][:start punctuation:][:initial punctuation:]] Remove \n"
221 "see http://icu.sourceforge.net/userguide/Transform.html\n"
222 " http://www.unicode.org/Public/UNIDATA/UCD.html\n"
223 " http://icu.sourceforge.net/userguide/Transform.html\n"
224 " http://icu.sourceforge.net/userguide/TransformRule.html\n"
228 fprintf(config.outfile, "\n\n");
233 static void print_icu_xml_locales(const struct config_t *p_config)
237 UErrorCode status = U_ZERO_ERROR;
240 int32_t keyword_len = 0;
241 char keyword_str[128];
242 int32_t keyword_str_len = 0;
245 int32_t language_len = 0;
247 int32_t lang_str_len = 0;
250 int32_t script_len = 0;
251 char script_str[128];
252 int32_t script_str_len = 0;
255 int32_t location_len = 0;
256 char location_str[128];
257 int32_t location_str_len = 0;
260 int32_t variant_len = 0;
261 char variant_str[128];
262 int32_t variant_str_len = 0;
265 int32_t name_len = 0;
267 int32_t name_str_len = 0;
270 int32_t localname_len = 0;
271 char localname_str[128];
272 int32_t localname_str_len = 0;
274 count = uloc_countAvailable() ;
276 if (p_config->xmloutput)
278 fprintf(config.outfile, "<locales count=\"%d\" default=\"%s\" collations=\"%d\">\n",
279 count, uloc_getDefault(), ucol_countAvailable());
286 = uloc_getDisplayKeyword(uloc_getAvailable(i), "en",
290 u_strToUTF8(keyword_str, 128, &keyword_str_len,
291 keyword, keyword_len,
296 = uloc_getDisplayLanguage(uloc_getAvailable(i), "en",
300 u_strToUTF8(lang_str, 128, &lang_str_len,
301 language, language_len,
306 = uloc_getDisplayScript(uloc_getAvailable(i), "en",
310 u_strToUTF8(script_str, 128, &script_str_len,
315 = uloc_getDisplayCountry(uloc_getAvailable(i), "en",
319 u_strToUTF8(location_str, 128, &location_str_len,
320 location, location_len,
324 = uloc_getDisplayVariant(uloc_getAvailable(i), "en",
328 u_strToUTF8(variant_str, 128, &variant_str_len,
329 variant, variant_len,
333 = uloc_getDisplayName(uloc_getAvailable(i), "en",
337 u_strToUTF8(name_str, 128, &name_str_len,
342 = uloc_getDisplayName(uloc_getAvailable(i), uloc_getAvailable(i),
346 u_strToUTF8(localname_str, 128, &localname_str_len,
347 localname, localname_len,
351 if (p_config->xmloutput)
353 fprintf(config.outfile, "<locale id=\"%s\"", uloc_getAvailable(i));
354 /* fprintf(config.outfile, " locale=\"%s\"", uloc_getAvailable(i)); */
355 /* if (strlen(keyword_str)) */
356 /* fprintf(config.outfile, " keyword=\"%s\"", keyword_str); */
357 /* if (ucol_getAvailable(i)) */
358 /* fprintf(config.outfile, " collation=\"1\""); */
359 if (strlen(lang_str))
360 fprintf(config.outfile, " language=\"%s\"", lang_str);
361 if (strlen(script_str))
362 fprintf(config.outfile, " script=\"%s\"", script_str);
363 if (strlen(location_str))
364 fprintf(config.outfile, " location=\"%s\"", location_str);
365 if (strlen(variant_str))
366 fprintf(config.outfile, " variant=\"%s\"", variant_str);
367 if (strlen(name_str))
368 fprintf(config.outfile, " name=\"%s\"", name_str);
369 if (strlen(localname_str))
370 fprintf(config.outfile, " localname=\"%s\"", localname_str);
371 fprintf(config.outfile, ">");
372 if (strlen(localname_str))
373 fprintf(config.outfile, "%s", localname_str);
374 fprintf(config.outfile, "</locale>\n");
376 else if (1 == p_config->xmloutput)
378 fprintf(config.outfile, "%s", uloc_getAvailable(i));
379 fprintf(config.outfile, " | ");
380 if (strlen(name_str))
381 fprintf(config.outfile, "%s", name_str);
382 fprintf(config.outfile, " | ");
383 if (strlen(localname_str))
384 fprintf(config.outfile, "%s", localname_str);
385 fprintf(config.outfile, "\n");
388 fprintf(config.outfile, "%s ", uloc_getAvailable(i));
390 if (p_config->xmloutput)
391 fprintf(config.outfile, "</locales>\n");
393 fprintf(config.outfile, "\n");
395 if(U_FAILURE(status))
397 fprintf(stderr, "ICU Error: %d %s\n", status, u_errorName(status));
403 static void print_info(const struct config_t *p_config)
405 if (p_config->xmloutput)
406 fprintf(config.outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
409 if ('c' == config.print[0])
410 print_icu_converters(&config);
411 else if ('l' == config.print[0])
412 print_icu_xml_locales(&config);
413 else if ('t' == config.print[0])
414 print_icu_transliterators(&config);
416 print_icu_converters(&config);
417 print_icu_xml_locales(&config);
418 print_icu_transliterators(&config);
421 if (p_config->xmloutput)
422 fprintf(config.outfile, "</icu>\n");
429 static void process_text_file(const struct config_t *p_config)
434 xmlDoc *doc = xmlParseFile(config.conffile);
435 xmlNode *xml_node = xmlDocGetRootElement(doc);
437 long unsigned int token_count = 0;
438 long unsigned int line_count = 0;
440 UErrorCode status = U_ZERO_ERROR;
445 printf("Could not parse XML config file '%s' \n",
450 config.chain = icu_chain_xml_config(xml_node, 1, &status);
452 if (config.chain && U_SUCCESS(status))
455 printf("Could not set up ICU chain from config file '%s' \n",
460 if (p_config->xmloutput)
461 fprintf(config.outfile,
462 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
466 /* read input lines for processing */
467 while ((line=fgets(linebuf, sizeof(linebuf)-1, config.infile)))
469 success = icu_chain_assign_cstr(config.chain, line, &status);
472 while (success && icu_chain_next_token(config.chain, &status))
474 WRBUF sw = wrbuf_alloc();
475 if (U_FAILURE(status))
478 const char *sortkey = icu_chain_token_sortkey(config.chain);
480 wrbuf_puts_escaped(sw, sortkey);
482 if (p_config->xmloutput)
484 /* should XML encode this. Bug #1902 */
485 fprintf(config.outfile,
486 "<token id=\"%lu\" line=\"%lu\""
487 " norm=\"%s\" display=\"%s\" sortkey=\"%s\"/>\n",
490 icu_chain_token_norm(config.chain),
491 icu_chain_token_display(config.chain),
495 fprintf(config.outfile, "%lu %lu '%s' '%s' '%s'\n",
498 icu_chain_token_norm(config.chain),
499 icu_chain_token_display(config.chain),
507 if (p_config->xmloutput)
508 fprintf(config.outfile,
512 icu_chain_destroy(config.chain);
518 #endif /* YAZ_HAVE_ICU */
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))
534 #else /* YAZ_HAVE_ICU */
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");
541 #endif /* YAZ_HAVE_ICU */
550 * indent-tabs-mode: nil
552 * vim: shiftwidth=4 tabstop=8 expandtab