2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: yaz-icu.c,v 1.12 2007-11-08 18:02:04 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>
29 /* commando line and config parameters */
30 static struct config_t {
34 yaz_icu_chain_t chain;
41 void print_option_error(const struct config_t *p_config)
43 fprintf(stderr, "Calling error, valid options are :\n");
44 fprintf(stderr, "yaz-icu\n"
45 " [-c (path/to/config/file.xml)]\n"
46 " [-p (a|c|l|t)] print ICU info \n"
50 "cat hugetextfile.txt | ./yaz-icu -c config.xml \n"
55 "Example ICU chain XML configuration file:\n"
56 "<icu_chain id=\"en:word\" locale=\"en\">\n"
57 " <normalize rule=\"[:Control:] Any-Remove\"/>\n"
58 " <tokenize rule=\"l\"/>\n"
59 " <normalize rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>\n"
60 " <casemap rule=\"l\"/>\n"
66 void read_params(int argc, char **argv, struct config_t *p_config)
71 /* set default parameters */
72 p_config->conffile[0] = 0;
73 p_config->print[0] = 0;
74 p_config->xmloutput = 0;
76 p_config->infile = stdin;
77 p_config->outfile = stdout;
79 /* set up command line parameters */
81 while ((ret = options("c:p:x", argv, argc, &arg)) != -2)
86 strcpy(p_config->conffile, arg);
89 strcpy(p_config->print, arg);
92 p_config->xmloutput = 1;
95 print_option_error(p_config);
99 if ((!strlen(p_config->conffile)
100 && !strlen(p_config->print))
104 print_option_error(p_config);
108 /* UConverter *conv; */
109 /* conv = ucnv_open("utf-8", &status); */
110 /* assert(U_SUCCESS(status)); */
113 /* = ucnv_toUChars(conv, ustr16, 1024, */
114 /* (const char *) *xstr8, strlen((const char *) *xstr8), */
119 /* ucnv_fromUChars(conv, */
120 /* (char *) *xstr8, strlen((const char *) *xstr8), */
121 /* ustr16, *ustr16_len, */
123 /* ucnv_close(conv); */
126 static void print_icu_converters(const struct config_t *p_config)
131 count = ucnv_countAvailable();
132 if (p_config->xmloutput)
133 fprintf(config.outfile, "<converters count=\"%d\" default=\"%s\">\n",
134 count, ucnv_getDefaultName());
136 fprintf(config.outfile, "Available ICU converters: %d\n", count);
137 fprintf(config.outfile, "Default ICU Converter is: '%s'\n",
138 ucnv_getDefaultName());
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)
179 fprintf(config.outfile, "</transliterators>\n");
183 fprintf(config.outfile, "\n\nUnicode Set Patterns:\n"
184 " Pattern Description\n"
185 " Ranges [a-z] The lower case letters a through z\n"
186 " Named Chars [abc123] The six characters a,b,c,1,2 and 3\n"
187 " String [abc{def}] chars a, b and c, and string 'def'\n"
188 " Categories [\\p{Letter}] Perl General Category 'Letter'.\n"
189 " Categories [:Letter:] Posix General Category 'Letter'.\n"
191 " Combination Example\n"
192 " Union [[:Greek:] [:letter:]]\n"
193 " Intersection [[:Greek:] & [:letter:]]\n"
194 " Set Complement [[:Greek:] - [:letter:]]\n"
195 " Complement [^[:Greek:] [:letter:]]\n"
197 "see: http://icu.sourceforge.net/userguide/unicodeSet.html\n"
200 " [:Punctuation:] Any-Remove\n"
201 " [:Cased-Letter:] Any-Upper\n"
202 " [:Control:] Any-Remove\n"
203 " [:Decimal_Number:] Any-Remove\n"
204 " [:Final_Punctuation:] Any-Remove\n"
205 " [:Georgian:] Any-Upper\n"
206 " [:Katakana:] Any-Remove\n"
207 " [:Arabic:] Any-Remove\n"
208 " [:Punctuation:] Remove\n"
209 " [[:Punctuation:]-[.,]] Remove\n"
210 " [:Line_Separator:] Any-Remove\n"
211 " [:Math_Symbol:] Any-Remove\n"
212 " Lower; [:^Letter:] Remove (word tokenization)\n"
213 " [:^Number:] Remove (numeric tokenization)\n"
214 " [:^Katagana:] Remove (remove everything except Katagana)\n"
215 " Lower;[[:WhiteSpace:][:Punctuation:]] Remove (word tokenization)\n"
216 " NFD; [:Nonspacing Mark:] Remove; NFC (removes accents from characters)\n"
217 " [A-Za-z]; Lower(); Latin-Katakana; Katakana-Hiragana (transforms latin and katagana to hiragana)\n"
218 " [[:separator:][:start punctuation:][:initial punctuation:]] Remove \n"
220 "see http://icu.sourceforge.net/userguide/Transform.html\n"
221 " http://www.unicode.org/Public/UNIDATA/UCD.html\n"
222 " http://icu.sourceforge.net/userguide/Transform.html\n"
223 " http://icu.sourceforge.net/userguide/TransformRule.html\n"
227 fprintf(config.outfile, "\n\n");
232 static void print_icu_xml_locales(const struct config_t *p_config)
236 UErrorCode status = U_ZERO_ERROR;
239 int32_t keyword_len = 0;
240 char keyword_str[128];
241 int32_t keyword_str_len = 0;
244 int32_t language_len = 0;
246 int32_t lang_str_len = 0;
249 int32_t script_len = 0;
250 char script_str[128];
251 int32_t script_str_len = 0;
254 int32_t location_len = 0;
255 char location_str[128];
256 int32_t location_str_len = 0;
259 int32_t variant_len = 0;
260 char variant_str[128];
261 int32_t variant_str_len = 0;
264 int32_t name_len = 0;
266 int32_t name_str_len = 0;
269 int32_t localname_len = 0;
270 char localname_str[128];
271 int32_t localname_str_len = 0;
273 count = uloc_countAvailable() ;
275 if (p_config->xmloutput)
277 fprintf(config.outfile, "<locales count=\"%d\" default=\"%s\" collations=\"%d\">\n",
278 count, uloc_getDefault(), ucol_countAvailable());
285 = uloc_getDisplayKeyword(uloc_getAvailable(i), "en",
289 u_strToUTF8(keyword_str, 128, &keyword_str_len,
290 keyword, keyword_len,
295 = uloc_getDisplayLanguage(uloc_getAvailable(i), "en",
299 u_strToUTF8(lang_str, 128, &lang_str_len,
300 language, language_len,
305 = uloc_getDisplayScript(uloc_getAvailable(i), "en",
309 u_strToUTF8(script_str, 128, &script_str_len,
314 = uloc_getDisplayCountry(uloc_getAvailable(i), "en",
318 u_strToUTF8(location_str, 128, &location_str_len,
319 location, location_len,
323 = uloc_getDisplayVariant(uloc_getAvailable(i), "en",
327 u_strToUTF8(variant_str, 128, &variant_str_len,
328 variant, variant_len,
332 = uloc_getDisplayName(uloc_getAvailable(i), "en",
336 u_strToUTF8(name_str, 128, &name_str_len,
341 = uloc_getDisplayName(uloc_getAvailable(i), uloc_getAvailable(i),
345 u_strToUTF8(localname_str, 128, &localname_str_len,
346 localname, localname_len,
350 if (p_config->xmloutput)
352 fprintf(config.outfile, "<locale id=\"%s\"", uloc_getAvailable(i));
353 /* fprintf(config.outfile, " locale=\"%s\"", uloc_getAvailable(i)); */
354 /* if (strlen(keyword_str)) */
355 /* fprintf(config.outfile, " keyword=\"%s\"", keyword_str); */
356 /* if (ucol_getAvailable(i)) */
357 /* fprintf(config.outfile, " collation=\"1\""); */
358 if (strlen(lang_str))
359 fprintf(config.outfile, " language=\"%s\"", lang_str);
360 if (strlen(script_str))
361 fprintf(config.outfile, " script=\"%s\"", script_str);
362 if (strlen(location_str))
363 fprintf(config.outfile, " location=\"%s\"", location_str);
364 if (strlen(variant_str))
365 fprintf(config.outfile, " variant=\"%s\"", variant_str);
366 if (strlen(name_str))
367 fprintf(config.outfile, " name=\"%s\"", name_str);
368 if (strlen(localname_str))
369 fprintf(config.outfile, " localname=\"%s\"", localname_str);
370 fprintf(config.outfile, ">");
371 if (strlen(localname_str))
372 fprintf(config.outfile, "%s", localname_str);
373 fprintf(config.outfile, "</locale>\n");
375 else if (1 == p_config->xmloutput)
377 fprintf(config.outfile, "%s", uloc_getAvailable(i));
378 fprintf(config.outfile, " | ");
379 if (strlen(name_str))
380 fprintf(config.outfile, "%s", name_str);
381 fprintf(config.outfile, " | ");
382 if (strlen(localname_str))
383 fprintf(config.outfile, "%s", localname_str);
384 fprintf(config.outfile, "\n");
387 fprintf(config.outfile, "%s ", uloc_getAvailable(i));
389 if (p_config->xmloutput)
390 fprintf(config.outfile, "</locales>\n");
392 fprintf(config.outfile, "\n");
394 if(U_FAILURE(status))
396 fprintf(stderr, "ICU Error: %d %s\n", status, u_errorName(status));
402 static void print_info(const struct config_t *p_config)
404 if (p_config->xmloutput)
405 fprintf(config.outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
408 if ('c' == config.print[0])
409 print_icu_converters(&config);
410 else if ('l' == config.print[0])
411 print_icu_xml_locales(&config);
412 else if ('t' == config.print[0])
413 print_icu_transliterators(&config);
415 print_icu_converters(&config);
416 print_icu_xml_locales(&config);
417 print_icu_transliterators(&config);
420 if (p_config->xmloutput)
421 fprintf(config.outfile, "</icu>\n");
428 static void process_text_file(const struct config_t *p_config)
433 xmlDoc *doc = xmlParseFile(config.conffile);
434 xmlNode *xml_node = xmlDocGetRootElement(doc);
436 long unsigned int token_count = 0;
437 long unsigned int line_count = 0;
439 UErrorCode status = U_ZERO_ERROR;
444 printf("Could not parse XML config file '%s' \n",
449 config.chain = icu_chain_xml_config(xml_node, 0, &status);
451 if (config.chain && U_SUCCESS(status))
454 printf("Could not set up ICU chain from config file '%s' \n",
459 if (p_config->xmloutput)
460 fprintf(config.outfile,
461 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
465 /* read input lines for processing */
466 while ((line=fgets(linebuf, sizeof(linebuf)-1, config.infile)))
468 success = icu_chain_assign_cstr(config.chain, line, &status);
471 while (success && icu_chain_next_token(config.chain, &status))
473 if (U_FAILURE(status))
477 if (p_config->xmloutput)
478 fprintf(config.outfile,
479 "<token id=\%lu\" line=\"%lu\""
480 " norm=\"%s\" display=\"%s\"/>\n",
483 icu_chain_token_norm(config.chain),
484 icu_chain_token_display(config.chain));
486 fprintf(config.outfile, "%lu %lu '%s' '%s'\n",
489 icu_chain_token_norm(config.chain),
490 icu_chain_token_display(config.chain));
496 if (p_config->xmloutput)
497 fprintf(config.outfile,
501 icu_chain_destroy(config.chain);
507 #endif /* YAZ_HAVE_ICU */
510 int main(int argc, char **argv)
515 read_params(argc, argv, &config);
517 if (config.conffile && strlen(config.conffile))
518 process_text_file(&config);
520 if (config.print && strlen(config.print))
523 #else /* YAZ_HAVE_ICU */
525 printf("ICU not available on your system.\n"
526 "Please install libicu36-dev and icu-doc or similar, "
527 "re-configure and re-compile\n");
530 #endif /* YAZ_HAVE_ICU */
539 * indent-tabs-mode: nil
541 * vim: shiftwidth=4 tabstop=8 expandtab