2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: yaz-icu.c,v 1.11 2007-11-08 13:35:14 adam Exp $
17 #include <yaz/options.h>
22 #include <unicode/ucnv.h>
23 #include <unicode/ustring.h>
24 #include <unicode/ucol.h>
25 #include <unicode/ubrk.h>
26 #include <unicode/utrans.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 id=\"en:word\" locale=\"en\">\n"
58 " <normalize rule=\"[:Control:] Any-Remove\"/>\n"
59 " <tokenize rule=\"l\"/>\n"
60 " <normalize 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());
142 for(i=0;i<count;i++){
143 if (p_config->xmloutput)
144 fprintf(config.outfile, "<converter id=\"%s\"/>\n",
145 ucnv_getAvailableName(i));
147 fprintf(config.outfile, "%s ", ucnv_getAvailableName(i));
150 if (p_config->xmloutput)
151 fprintf(config.outfile, "</converters>\n");
153 fprintf(config.outfile, "\n");
156 static void print_icu_transliterators(const struct config_t *p_config)
158 int32_t buf_cap = 128;
161 int32_t count = utrans_countAvailableIDs();
163 if (p_config->xmloutput)
164 fprintf(config.outfile, "<transliterators count=\"%d\">\n", count);
166 fprintf(config.outfile, "Available ICU transliterators: %d\n", count);
168 for(i = 0; i <count; i++)
170 utrans_getAvailableID(i, buf, buf_cap);
171 if (p_config->xmloutput)
172 fprintf(config.outfile, "<transliterator id=\"%s\"/>\n", buf);
174 fprintf(config.outfile, " %s", buf);
177 if (p_config->xmloutput){
178 fprintf(config.outfile, "</transliterators>\n");
182 fprintf(config.outfile, "\n\nUnicode Set Patterns:\n"
183 " Pattern Description\n"
184 " Ranges [a-z] The lower case letters a through z\n"
185 " Named Chars [abc123] The six characters a,b,c,1,2 and 3\n"
186 " String [abc{def}] chars a, b and c, and string 'def'\n"
187 " Categories [\\p{Letter}] Perl General Category 'Letter'.\n"
188 " Categories [:Letter:] Posix General Category 'Letter'.\n"
190 " Combination Example\n"
191 " Union [[:Greek:] [:letter:]]\n"
192 " Intersection [[:Greek:] & [:letter:]]\n"
193 " Set Complement [[:Greek:] - [:letter:]]\n"
194 " Complement [^[:Greek:] [:letter:]]\n"
196 "see: http://icu.sourceforge.net/userguide/unicodeSet.html\n"
199 " [:Punctuation:] Any-Remove\n"
200 " [:Cased-Letter:] Any-Upper\n"
201 " [:Control:] Any-Remove\n"
202 " [:Decimal_Number:] Any-Remove\n"
203 " [:Final_Punctuation:] Any-Remove\n"
204 " [:Georgian:] Any-Upper\n"
205 " [:Katakana:] Any-Remove\n"
206 " [:Arabic:] Any-Remove\n"
207 " [:Punctuation:] Remove\n"
208 " [[:Punctuation:]-[.,]] Remove\n"
209 " [:Line_Separator:] Any-Remove\n"
210 " [:Math_Symbol:] Any-Remove\n"
211 " Lower; [:^Letter:] Remove (word tokenization)\n"
212 " [:^Number:] Remove (numeric tokenization)\n"
213 " [:^Katagana:] Remove (remove everything except Katagana)\n"
214 " Lower;[[:WhiteSpace:][:Punctuation:]] Remove (word tokenization)\n"
215 " NFD; [:Nonspacing Mark:] Remove; NFC (removes accents from characters)\n"
216 " [A-Za-z]; Lower(); Latin-Katakana; Katakana-Hiragana (transforms latin and katagana to hiragana)\n"
217 " [[:separator:][:start punctuation:][:initial punctuation:]] Remove \n"
219 "see http://icu.sourceforge.net/userguide/Transform.html\n"
220 " http://www.unicode.org/Public/UNIDATA/UCD.html\n"
221 " http://icu.sourceforge.net/userguide/Transform.html\n"
222 " http://icu.sourceforge.net/userguide/TransformRule.html\n"
226 fprintf(config.outfile, "\n\n");
231 static void print_icu_xml_locales(const struct config_t *p_config)
235 UErrorCode status = U_ZERO_ERROR;
238 int32_t keyword_len = 0;
239 char keyword_str[128];
240 int32_t keyword_str_len = 0;
243 int32_t language_len = 0;
245 int32_t lang_str_len = 0;
248 int32_t script_len = 0;
249 char script_str[128];
250 int32_t script_str_len = 0;
253 int32_t location_len = 0;
254 char location_str[128];
255 int32_t location_str_len = 0;
258 int32_t variant_len = 0;
259 char variant_str[128];
260 int32_t variant_str_len = 0;
263 int32_t name_len = 0;
265 int32_t name_str_len = 0;
268 int32_t localname_len = 0;
269 char localname_str[128];
270 int32_t localname_str_len = 0;
272 count = uloc_countAvailable() ;
274 if (p_config->xmloutput){
276 fprintf(config.outfile, "<locales count=\"%d\" default=\"%s\" collations=\"%d\">\n",
277 count, uloc_getDefault(), ucol_countAvailable());
284 = uloc_getDisplayKeyword(uloc_getAvailable(i), "en",
288 u_strToUTF8(keyword_str, 128, &keyword_str_len,
289 keyword, keyword_len,
294 = uloc_getDisplayLanguage(uloc_getAvailable(i), "en",
298 u_strToUTF8(lang_str, 128, &lang_str_len,
299 language, language_len,
304 = uloc_getDisplayScript(uloc_getAvailable(i), "en",
308 u_strToUTF8(script_str, 128, &script_str_len,
313 = uloc_getDisplayCountry(uloc_getAvailable(i), "en",
317 u_strToUTF8(location_str, 128, &location_str_len,
318 location, location_len,
322 = uloc_getDisplayVariant(uloc_getAvailable(i), "en",
326 u_strToUTF8(variant_str, 128, &variant_str_len,
327 variant, variant_len,
331 = uloc_getDisplayName(uloc_getAvailable(i), "en",
335 u_strToUTF8(name_str, 128, &name_str_len,
340 = uloc_getDisplayName(uloc_getAvailable(i), uloc_getAvailable(i),
344 u_strToUTF8(localname_str, 128, &localname_str_len,
345 localname, localname_len,
349 if (p_config->xmloutput){
350 fprintf(config.outfile, "<locale id=\"%s\"", uloc_getAvailable(i));
351 /* fprintf(config.outfile, " locale=\"%s\"", uloc_getAvailable(i)); */
352 /* if (strlen(keyword_str)) */
353 /* fprintf(config.outfile, " keyword=\"%s\"", keyword_str); */
354 /* if (ucol_getAvailable(i)) */
355 /* fprintf(config.outfile, " collation=\"1\""); */
356 if (strlen(lang_str))
357 fprintf(config.outfile, " language=\"%s\"", lang_str);
358 if (strlen(script_str))
359 fprintf(config.outfile, " script=\"%s\"", script_str);
360 if (strlen(location_str))
361 fprintf(config.outfile, " location=\"%s\"", location_str);
362 if (strlen(variant_str))
363 fprintf(config.outfile, " variant=\"%s\"", variant_str);
364 if (strlen(name_str))
365 fprintf(config.outfile, " name=\"%s\"", name_str);
366 if (strlen(localname_str))
367 fprintf(config.outfile, " localname=\"%s\"", localname_str);
368 fprintf(config.outfile, ">");
369 if (strlen(localname_str))
370 fprintf(config.outfile, "%s", localname_str);
371 fprintf(config.outfile, "</locale>\n");
373 else if (1 == p_config->xmloutput){
374 fprintf(config.outfile, "%s", uloc_getAvailable(i));
375 fprintf(config.outfile, " | ");
376 if (strlen(name_str))
377 fprintf(config.outfile, "%s", name_str);
378 fprintf(config.outfile, " | ");
379 if (strlen(localname_str))
380 fprintf(config.outfile, "%s", localname_str);
381 fprintf(config.outfile, "\n");
384 fprintf(config.outfile, "%s ", uloc_getAvailable(i));
386 if (p_config->xmloutput)
387 fprintf(config.outfile, "</locales>\n");
389 fprintf(config.outfile, "\n");
391 if(U_FAILURE(status)) {
392 fprintf(stderr, "ICU Error: %d %s\n", status, u_errorName(status));
398 static void print_info(const struct config_t *p_config)
400 if (p_config->xmloutput)
401 fprintf(config.outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
404 if ('c' == config.print[0])
405 print_icu_converters(&config);
406 else if ('l' == config.print[0])
407 print_icu_xml_locales(&config);
408 else if ('t' == config.print[0])
409 print_icu_transliterators(&config);
411 print_icu_converters(&config);
412 print_icu_xml_locales(&config);
413 print_icu_transliterators(&config);
416 if (p_config->xmloutput)
417 fprintf(config.outfile, "</icu>\n");
424 static void process_text_file(const struct config_t *p_config)
429 xmlDoc *doc = xmlParseFile(config.conffile);
430 xmlNode *xml_node = xmlDocGetRootElement(doc);
432 long unsigned int token_count = 0;
433 long unsigned int line_count = 0;
435 UErrorCode status = U_ZERO_ERROR;
439 printf("Could not parse XML config file '%s' \n",
444 config.chain = icu_chain_xml_config(xml_node, 0, &status);
446 if (config.chain && U_SUCCESS(status))
449 printf("Could not set up ICU chain from config file '%s' \n",
456 if (p_config->xmloutput)
457 fprintf(config.outfile,
458 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
462 /* read input lines for processing */
463 while ((line=fgets(linebuf, sizeof(linebuf)-1, config.infile)))
465 success = icu_chain_assign_cstr(config.chain, line, &status);
468 while (success && icu_chain_next_token(config.chain, &status)){
469 if (U_FAILURE(status))
473 if (p_config->xmloutput)
474 fprintf(config.outfile,
475 "<token id=\%lu\" line=\"%lu\""
476 " norm=\"%s\" display=\"%s\"/>\n",
479 icu_chain_token_norm(config.chain),
480 icu_chain_token_display(config.chain));
482 fprintf(config.outfile, "%lu %lu '%s' '%s'\n",
485 icu_chain_token_norm(config.chain),
486 icu_chain_token_display(config.chain));
492 if (p_config->xmloutput)
493 fprintf(config.outfile,
497 icu_chain_destroy(config.chain);
503 #endif /* YAZ_HAVE_ICU */
506 int main(int argc, char **argv)
511 read_params(argc, argv, &config);
513 if (config.conffile && strlen(config.conffile))
514 process_text_file(&config);
516 if (config.print && strlen(config.print))
519 #else /* YAZ_HAVE_ICU */
521 printf("ICU not available on your system.\n"
522 "Please install libicu36-dev and icu-doc or similar, "
523 "re-configure and re-compile\n");
526 #endif /* YAZ_HAVE_ICU */
535 * indent-tabs-mode: nil
537 * vim: shiftwidth=4 tabstop=8 expandtab