1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
16 #include <yaz/options.h>
20 #include <unicode/ucnv.h>
21 #include <unicode/ustring.h>
22 #include <unicode/ucol.h>
23 #include <unicode/ubrk.h>
24 #include <unicode/utrans.h>
25 #include <unicode/uclean.h>
28 #include <yaz/wrbuf.h>
30 /* commando line and config parameters */
38 yaz_icu_chain_t chain;
43 void print_option_error(const struct config_t *p_config)
45 fprintf(stderr, "yaz-icu [options] [infile]\n"
47 " -c file XML configuration\n"
48 " -p a|c|l|t Print ICU info \n"
49 " -s Show sort normalization key\n"
50 " -o Show org positions\n"
51 " -x XML output instread of text\n"
52 " -C n Perform conversions n times (instead of once)\n"
55 "cat hugetextfile.txt | ./yaz-icu -c config.xml \n"
60 "Example ICU chain XML configuration file:\n"
61 "<icu_chain locale=\"en\">\n"
62 " <transform rule=\"[:Control:] Any-Remove\"/>\n"
63 " <tokenize rule=\"l\"/>\n"
64 " <transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>\n"
65 " <casemap rule=\"l\"/>\n"
71 void read_params(int argc, char **argv, struct config_t *p_config)
76 /* set default parameters */
77 p_config->conffile[0] = 0;
78 p_config->print[0] = 0;
79 p_config->xmloutput = 0;
80 p_config->sortoutput = 0;
83 p_config->outfile = stdout;
84 p_config->org_output = 0;
87 /* set up command line parameters */
89 while ((ret = options("c:C:op:sx", argv, argc, &arg)) != -2)
94 strcpy(p_config->conffile, arg);
97 p_config->count = atoi(arg);
100 strcpy(p_config->print, arg);
103 p_config->sortoutput = 1;
106 p_config->xmloutput = 1;
109 p_config->org_output = 1;
112 if (p_config->infile)
114 fprintf(stderr, "yaz-icu: only one input file may be given\n");
115 print_option_error(p_config);
117 p_config->infile = fopen(arg, "r");
118 if (!p_config->infile)
120 fprintf(stderr, "yaz-icu: cannot open %s : %s\n",
121 arg, strerror(errno));
126 fprintf(stderr, "yaz_icu: invalid option: %s\n", arg);
127 print_option_error(p_config);
131 if (p_config->infile == 0)
132 p_config->infile = stdin;
134 if (!strlen(p_config->conffile) && !strlen(p_config->print))
135 print_option_error(p_config);
138 static void print_icu_converters(const struct config_t *p_config)
143 count = ucnv_countAvailable();
144 if (p_config->xmloutput)
145 fprintf(p_config->outfile, "<converters count=\"%d\" default=\"%s\">\n",
146 count, ucnv_getDefaultName());
149 fprintf(p_config->outfile, "Available ICU converters: %d\n", count);
150 fprintf(p_config->outfile, "Default ICU Converter is: '%s'\n",
151 ucnv_getDefaultName());
154 for (i = 0; i < count; i++)
156 if (p_config->xmloutput)
157 fprintf(p_config->outfile, "<converter id=\"%s\"/>\n",
158 ucnv_getAvailableName(i));
160 fprintf(p_config->outfile, "%s\n", ucnv_getAvailableName(i));
163 if (p_config->xmloutput)
164 fprintf(p_config->outfile, "</converters>\n");
166 fprintf(p_config->outfile, "\n");
169 static void print_icu_transliterators(const struct config_t *p_config)
172 UEnumeration *en = utrans_openIDs(&status);
173 int32_t count = uenum_count(en, &status);
177 if (p_config->xmloutput)
178 fprintf(p_config->outfile, "<transliterators count=\"%d\">\n", count);
180 fprintf(p_config->outfile, "Available ICU transliterators: %d\n", count);
182 while ((name = uenum_next(en, &length, &status)))
184 if (p_config->xmloutput)
185 fprintf(p_config->outfile, "<transliterator id=\"%s\"/>\n", name);
187 fprintf(p_config->outfile, "%s\n", name);
190 if (p_config->xmloutput)
191 fprintf(p_config->outfile, "</transliterators>\n");
194 fprintf(p_config->outfile, "\n\nUnicode Set Patterns:\n"
195 " Pattern Description\n"
196 " Ranges [a-z] The lower case letters a through z\n"
197 " Named Chars [abc123] The six characters a,b,c,1,2 and 3\n"
198 " String [abc{def}] chars a, b and c, and string 'def'\n"
199 " Categories [\\p{Letter}] Perl General Category 'Letter'.\n"
200 " Categories [:Letter:] Posix General Category 'Letter'.\n"
202 " Combination Example\n"
203 " Union [[:Greek:] [:letter:]]\n"
204 " Intersection [[:Greek:] & [:letter:]]\n"
205 " Set Complement [[:Greek:] - [:letter:]]\n"
206 " Complement [^[:Greek:] [:letter:]]\n"
208 "see: http://icu.sourceforge.net/userguide/unicodeSet.html\n"
211 " [:Punctuation:] Any-Remove\n"
212 " [:Cased-Letter:] Any-Upper\n"
213 " [:Control:] Any-Remove\n"
214 " [:Decimal_Number:] Any-Remove\n"
215 " [:Final_Punctuation:] Any-Remove\n"
216 " [:Georgian:] Any-Upper\n"
217 " [:Katakana:] Any-Remove\n"
218 " [:Arabic:] Any-Remove\n"
219 " [:Punctuation:] Remove\n"
220 " [[:Punctuation:]-[.,]] Remove\n"
221 " [:Line_Separator:] Any-Remove\n"
222 " [:Math_Symbol:] Any-Remove\n"
223 " Lower; [:^Letter:] Remove (word tokenization)\n"
224 " [:^Number:] Remove (numeric tokenization)\n"
225 " [:^Katagana:] Remove (remove everything except Katagana)\n"
226 " Lower;[[:WhiteSpace:][:Punctuation:]] Remove (word tokenization)\n"
227 " NFD; [:Nonspacing Mark:] Remove; NFC (removes accents from characters)\n"
228 " [A-Za-z]; Lower(); Latin-Katakana; Katakana-Hiragana (transforms latin and katagana to hiragana)\n"
229 " [[:separator:][:start punctuation:][:initial punctuation:]] Remove \n"
231 "see http://userguide.icu-project.org/transforms/general\n"
232 " http://www.unicode.org/reports/tr44/\n"
236 fprintf(p_config->outfile, "\n\n");
241 static void print_icu_xml_locales(const struct config_t *p_config)
245 UErrorCode status = U_ZERO_ERROR;
248 int32_t keyword_len = 0;
249 char keyword_str[128];
250 int32_t keyword_str_len = 0;
253 int32_t language_len = 0;
255 int32_t lang_str_len = 0;
258 int32_t script_len = 0;
259 char script_str[128];
260 int32_t script_str_len = 0;
263 int32_t location_len = 0;
264 char location_str[128];
265 int32_t location_str_len = 0;
268 int32_t variant_len = 0;
269 char variant_str[128];
270 int32_t variant_str_len = 0;
273 int32_t name_len = 0;
275 int32_t name_str_len = 0;
278 int32_t localname_len = 0;
279 char localname_str[128];
280 int32_t localname_str_len = 0;
282 count = uloc_countAvailable() ;
284 if (p_config->xmloutput)
286 fprintf(p_config->outfile, "<locales count=\"%d\" default=\"%s\" collations=\"%d\">\n",
287 count, uloc_getDefault(), ucol_countAvailable());
291 fprintf(p_config->outfile, "Available ICU locales: %d\n", count);
292 fprintf(p_config->outfile, "Default locale is: %s\n", uloc_getDefault());
295 for (i = 0; i < count; i++)
299 = uloc_getDisplayKeyword(uloc_getAvailable(i), "en",
303 u_strToUTF8(keyword_str, 128, &keyword_str_len,
304 keyword, keyword_len,
309 = uloc_getDisplayLanguage(uloc_getAvailable(i), "en",
313 u_strToUTF8(lang_str, 128, &lang_str_len,
314 language, language_len,
319 = uloc_getDisplayScript(uloc_getAvailable(i), "en",
323 u_strToUTF8(script_str, 128, &script_str_len,
328 = uloc_getDisplayCountry(uloc_getAvailable(i), "en",
332 u_strToUTF8(location_str, 128, &location_str_len,
333 location, location_len,
337 = uloc_getDisplayVariant(uloc_getAvailable(i), "en",
341 u_strToUTF8(variant_str, 128, &variant_str_len,
342 variant, variant_len,
346 = uloc_getDisplayName(uloc_getAvailable(i), "en",
350 u_strToUTF8(name_str, 128, &name_str_len,
355 = uloc_getDisplayName(uloc_getAvailable(i), uloc_getAvailable(i),
359 u_strToUTF8(localname_str, 128, &localname_str_len,
360 localname, localname_len,
364 if (p_config->xmloutput)
366 fprintf(p_config->outfile, "<locale id=\"%s\"", uloc_getAvailable(i));
367 if (strlen(lang_str))
368 fprintf(p_config->outfile, " language=\"%s\"", lang_str);
369 if (strlen(script_str))
370 fprintf(p_config->outfile, " script=\"%s\"", script_str);
371 if (strlen(location_str))
372 fprintf(p_config->outfile, " location=\"%s\"", location_str);
373 if (strlen(variant_str))
374 fprintf(p_config->outfile, " variant=\"%s\"", variant_str);
375 if (strlen(name_str))
376 fprintf(p_config->outfile, " name=\"%s\"", name_str);
377 if (strlen(localname_str))
378 fprintf(p_config->outfile, " localname=\"%s\"", localname_str);
379 fprintf(p_config->outfile, ">");
380 if (strlen(localname_str))
381 fprintf(p_config->outfile, "%s", localname_str);
382 fprintf(p_config->outfile, "</locale>\n");
384 else if (1 == p_config->xmloutput)
386 fprintf(p_config->outfile, "%s", uloc_getAvailable(i));
387 fprintf(p_config->outfile, " | ");
388 if (strlen(name_str))
389 fprintf(p_config->outfile, "%s", name_str);
390 fprintf(p_config->outfile, " | ");
391 if (strlen(localname_str))
392 fprintf(p_config->outfile, "%s", localname_str);
393 fprintf(p_config->outfile, "\n");
396 fprintf(p_config->outfile, "%s\n", uloc_getAvailable(i));
398 if (p_config->xmloutput)
399 fprintf(p_config->outfile, "</locales>\n");
401 fprintf(p_config->outfile, "\n");
403 if (U_FAILURE(status))
405 fprintf(stderr, "ICU Error: %d %s\n", status, u_errorName(status));
411 static void print_info(const struct config_t *p_config)
413 if (p_config->xmloutput)
414 fprintf(p_config->outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
417 if ('c' == p_config->print[0])
418 print_icu_converters(p_config);
419 else if ('l' == p_config->print[0])
420 print_icu_xml_locales(p_config);
421 else if ('t' == p_config->print[0])
422 print_icu_transliterators(p_config);
424 print_icu_converters(p_config);
425 print_icu_xml_locales(p_config);
426 print_icu_transliterators(p_config);
429 if (p_config->xmloutput)
430 fprintf(p_config->outfile, "</icu>\n");
437 static void process_text_file(struct config_t *p_config)
442 xmlDoc *doc = xmlParseFile(p_config->conffile);
443 xmlNode *xml_node = xmlDocGetRootElement(doc);
445 long unsigned int token_count = 0;
446 long unsigned int line_count = 0;
448 UErrorCode status = U_ZERO_ERROR;
452 printf("Could not parse XML config file '%s' \n",
457 p_config->chain = icu_chain_xml_config(xml_node, 1, &status);
459 if (!p_config->chain || !U_SUCCESS(status))
461 printf("Could not set up ICU chain from config file '%s' \n",
466 if (p_config->xmloutput)
467 fprintf(p_config->outfile,
468 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
472 /* read input lines for processing */
473 while ((line=fgets(linebuf, sizeof(linebuf)-1, p_config->infile)))
475 WRBUF sw = wrbuf_alloc();
476 WRBUF cdata = wrbuf_alloc();
478 for (i = 0; i < p_config->count; i++)
480 int success = icu_chain_assign_cstr(p_config->chain, line, &status);
482 while (success && icu_chain_next_token(p_config->chain, &status))
484 if (U_FAILURE(status))
489 const char *sortkey =
490 icu_chain_token_sortkey(p_config->chain);
491 icu_chain_get_org_info(p_config->chain, &start, &len);
493 wrbuf_puts_escaped(sw, sortkey);
497 ; /* only output on first iteration */
498 else if (p_config->xmloutput)
500 fprintf(p_config->outfile,
501 "<token id=\"%lu\" line=\"%lu\"",
502 token_count, line_count);
505 icu_chain_token_norm(p_config->chain));
506 fprintf(p_config->outfile, " norm=\"%s\"",
509 wrbuf_xmlputs(cdata, icu_chain_token_display(p_config->chain));
510 fprintf(p_config->outfile, " display=\"%s\"",
512 if (p_config->sortoutput)
515 wrbuf_xmlputs(cdata, wrbuf_cstr(sw));
516 fprintf(p_config->outfile, " sortkey=\"%s\"",
519 fprintf(p_config->outfile, "/>\n");
523 fprintf(p_config->outfile, "%lu %lu '%s' '%s'",
526 icu_chain_token_norm(p_config->chain),
527 icu_chain_token_display(p_config->chain));
528 if (p_config->sortoutput)
530 fprintf(p_config->outfile, " '%s'", wrbuf_cstr(sw));
532 if (p_config->org_output)
534 fprintf(p_config->outfile, " %ld+%ld",
535 (long) start, (long) len);
537 fprintf(p_config->outfile, "\n");
543 wrbuf_destroy(cdata);
546 if (p_config->xmloutput)
547 fprintf(p_config->outfile,
551 icu_chain_destroy(p_config->chain);
557 #endif /* YAZ_HAVE_ICU */
560 int main(int argc, char **argv)
563 struct config_t config;
567 read_params(argc, argv, &config);
569 if (config.conffile && strlen(config.conffile))
570 process_text_file(&config);
572 if (config.print && strlen(config.print))
577 #else /* YAZ_HAVE_ICU */
579 printf("ICU not available on your system.\n"
580 "Please install libicu-dev and icu-doc or similar, "
581 "re-configure and re-compile\n");
585 #endif /* YAZ_HAVE_ICU */
593 * c-file-style: "Stroustrup"
594 * indent-tabs-mode: nil
596 * vim: shiftwidth=4 tabstop=8 expandtab