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 */
37 yaz_icu_chain_t chain;
42 void print_option_error(const struct config_t *p_config)
44 fprintf(stderr, "yaz-icu [options] [infile]\n"
46 " -c file XML configuration\n"
47 " -p a|c|l|t Print ICU info \n"
48 " -s Show sort normalization key\n"
49 " -o Show org positions\n"
50 " -x XML output instread of text\n"
53 "cat hugetextfile.txt | ./yaz-icu -c config.xml \n"
58 "Example ICU chain XML configuration file:\n"
59 "<icu_chain locale=\"en\">\n"
60 " <transform rule=\"[:Control:] Any-Remove\"/>\n"
61 " <tokenize rule=\"l\"/>\n"
62 " <transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>\n"
63 " <casemap rule=\"l\"/>\n"
69 void read_params(int argc, char **argv, struct config_t *p_config)
74 /* set default parameters */
75 p_config->conffile[0] = 0;
76 p_config->print[0] = 0;
77 p_config->xmloutput = 0;
78 p_config->sortoutput = 0;
81 p_config->outfile = stdout;
82 p_config->org_output = 0;
84 /* set up command line parameters */
86 while ((ret = options("c:op:sx", argv, argc, &arg)) != -2)
91 strcpy(p_config->conffile, arg);
94 strcpy(p_config->print, arg);
97 p_config->sortoutput = 1;
100 p_config->xmloutput = 1;
103 p_config->org_output = 1;
106 if (p_config->infile)
108 fprintf(stderr, "yaz-icu: only one input file may be given\n");
109 print_option_error(p_config);
111 p_config->infile = fopen(arg, "r");
112 if (!p_config->infile)
114 fprintf(stderr, "yaz-icu: cannot open %s : %s\n",
115 arg, strerror(errno));
120 fprintf(stderr, "yaz_icu: invalid option: %s\n", arg);
121 print_option_error(p_config);
125 if (p_config->infile == 0)
126 p_config->infile = stdin;
128 if (!strlen(p_config->conffile) && !strlen(p_config->print))
129 print_option_error(p_config);
132 static void print_icu_converters(const struct config_t *p_config)
137 count = ucnv_countAvailable();
138 if (p_config->xmloutput)
139 fprintf(p_config->outfile, "<converters count=\"%d\" default=\"%s\">\n",
140 count, ucnv_getDefaultName());
143 fprintf(p_config->outfile, "Available ICU converters: %d\n", count);
144 fprintf(p_config->outfile, "Default ICU Converter is: '%s'\n",
145 ucnv_getDefaultName());
148 for (i = 0; i < count; i++)
150 if (p_config->xmloutput)
151 fprintf(p_config->outfile, "<converter id=\"%s\"/>\n",
152 ucnv_getAvailableName(i));
154 fprintf(p_config->outfile, "%s\n", ucnv_getAvailableName(i));
157 if (p_config->xmloutput)
158 fprintf(p_config->outfile, "</converters>\n");
160 fprintf(p_config->outfile, "\n");
163 static void print_icu_transliterators(const struct config_t *p_config)
166 UEnumeration *en = utrans_openIDs(&status);
167 int32_t count = uenum_count(en, &status);
171 if (p_config->xmloutput)
172 fprintf(p_config->outfile, "<transliterators count=\"%d\">\n", count);
174 fprintf(p_config->outfile, "Available ICU transliterators: %d\n", count);
176 while ((name = uenum_next(en, &length, &status)))
178 if (p_config->xmloutput)
179 fprintf(p_config->outfile, "<transliterator id=\"%s\"/>\n", name);
181 fprintf(p_config->outfile, "%s\n", name);
184 if (p_config->xmloutput)
185 fprintf(p_config->outfile, "</transliterators>\n");
188 fprintf(p_config->outfile, "\n\nUnicode Set Patterns:\n"
189 " Pattern Description\n"
190 " Ranges [a-z] The lower case letters a through z\n"
191 " Named Chars [abc123] The six characters a,b,c,1,2 and 3\n"
192 " String [abc{def}] chars a, b and c, and string 'def'\n"
193 " Categories [\\p{Letter}] Perl General Category 'Letter'.\n"
194 " Categories [:Letter:] Posix General Category 'Letter'.\n"
196 " Combination Example\n"
197 " Union [[:Greek:] [:letter:]]\n"
198 " Intersection [[:Greek:] & [:letter:]]\n"
199 " Set Complement [[:Greek:] - [:letter:]]\n"
200 " Complement [^[:Greek:] [:letter:]]\n"
202 "see: http://icu.sourceforge.net/userguide/unicodeSet.html\n"
205 " [:Punctuation:] Any-Remove\n"
206 " [:Cased-Letter:] Any-Upper\n"
207 " [:Control:] Any-Remove\n"
208 " [:Decimal_Number:] Any-Remove\n"
209 " [:Final_Punctuation:] Any-Remove\n"
210 " [:Georgian:] Any-Upper\n"
211 " [:Katakana:] Any-Remove\n"
212 " [:Arabic:] Any-Remove\n"
213 " [:Punctuation:] Remove\n"
214 " [[:Punctuation:]-[.,]] Remove\n"
215 " [:Line_Separator:] Any-Remove\n"
216 " [:Math_Symbol:] Any-Remove\n"
217 " Lower; [:^Letter:] Remove (word tokenization)\n"
218 " [:^Number:] Remove (numeric tokenization)\n"
219 " [:^Katagana:] Remove (remove everything except Katagana)\n"
220 " Lower;[[:WhiteSpace:][:Punctuation:]] Remove (word tokenization)\n"
221 " NFD; [:Nonspacing Mark:] Remove; NFC (removes accents from characters)\n"
222 " [A-Za-z]; Lower(); Latin-Katakana; Katakana-Hiragana (transforms latin and katagana to hiragana)\n"
223 " [[:separator:][:start punctuation:][:initial punctuation:]] Remove \n"
225 "see http://userguide.icu-project.org/transforms/general\n"
226 " http://www.unicode.org/reports/tr44/\n"
230 fprintf(p_config->outfile, "\n\n");
235 static void print_icu_xml_locales(const struct config_t *p_config)
239 UErrorCode status = U_ZERO_ERROR;
242 int32_t keyword_len = 0;
243 char keyword_str[128];
244 int32_t keyword_str_len = 0;
247 int32_t language_len = 0;
249 int32_t lang_str_len = 0;
252 int32_t script_len = 0;
253 char script_str[128];
254 int32_t script_str_len = 0;
257 int32_t location_len = 0;
258 char location_str[128];
259 int32_t location_str_len = 0;
262 int32_t variant_len = 0;
263 char variant_str[128];
264 int32_t variant_str_len = 0;
267 int32_t name_len = 0;
269 int32_t name_str_len = 0;
272 int32_t localname_len = 0;
273 char localname_str[128];
274 int32_t localname_str_len = 0;
276 count = uloc_countAvailable() ;
278 if (p_config->xmloutput)
280 fprintf(p_config->outfile, "<locales count=\"%d\" default=\"%s\" collations=\"%d\">\n",
281 count, uloc_getDefault(), ucol_countAvailable());
285 fprintf(p_config->outfile, "Available ICU locales: %d\n", count);
286 fprintf(p_config->outfile, "Default locale is: %s\n", uloc_getDefault());
289 for (i = 0; i < count; i++)
293 = uloc_getDisplayKeyword(uloc_getAvailable(i), "en",
297 u_strToUTF8(keyword_str, 128, &keyword_str_len,
298 keyword, keyword_len,
303 = uloc_getDisplayLanguage(uloc_getAvailable(i), "en",
307 u_strToUTF8(lang_str, 128, &lang_str_len,
308 language, language_len,
313 = uloc_getDisplayScript(uloc_getAvailable(i), "en",
317 u_strToUTF8(script_str, 128, &script_str_len,
322 = uloc_getDisplayCountry(uloc_getAvailable(i), "en",
326 u_strToUTF8(location_str, 128, &location_str_len,
327 location, location_len,
331 = uloc_getDisplayVariant(uloc_getAvailable(i), "en",
335 u_strToUTF8(variant_str, 128, &variant_str_len,
336 variant, variant_len,
340 = uloc_getDisplayName(uloc_getAvailable(i), "en",
344 u_strToUTF8(name_str, 128, &name_str_len,
349 = uloc_getDisplayName(uloc_getAvailable(i), uloc_getAvailable(i),
353 u_strToUTF8(localname_str, 128, &localname_str_len,
354 localname, localname_len,
358 if (p_config->xmloutput)
360 fprintf(p_config->outfile, "<locale id=\"%s\"", uloc_getAvailable(i));
361 if (strlen(lang_str))
362 fprintf(p_config->outfile, " language=\"%s\"", lang_str);
363 if (strlen(script_str))
364 fprintf(p_config->outfile, " script=\"%s\"", script_str);
365 if (strlen(location_str))
366 fprintf(p_config->outfile, " location=\"%s\"", location_str);
367 if (strlen(variant_str))
368 fprintf(p_config->outfile, " variant=\"%s\"", variant_str);
369 if (strlen(name_str))
370 fprintf(p_config->outfile, " name=\"%s\"", name_str);
371 if (strlen(localname_str))
372 fprintf(p_config->outfile, " localname=\"%s\"", localname_str);
373 fprintf(p_config->outfile, ">");
374 if (strlen(localname_str))
375 fprintf(p_config->outfile, "%s", localname_str);
376 fprintf(p_config->outfile, "</locale>\n");
378 else if (1 == p_config->xmloutput)
380 fprintf(p_config->outfile, "%s", uloc_getAvailable(i));
381 fprintf(p_config->outfile, " | ");
382 if (strlen(name_str))
383 fprintf(p_config->outfile, "%s", name_str);
384 fprintf(p_config->outfile, " | ");
385 if (strlen(localname_str))
386 fprintf(p_config->outfile, "%s", localname_str);
387 fprintf(p_config->outfile, "\n");
390 fprintf(p_config->outfile, "%s\n", uloc_getAvailable(i));
392 if (p_config->xmloutput)
393 fprintf(p_config->outfile, "</locales>\n");
395 fprintf(p_config->outfile, "\n");
397 if (U_FAILURE(status))
399 fprintf(stderr, "ICU Error: %d %s\n", status, u_errorName(status));
405 static void print_info(const struct config_t *p_config)
407 if (p_config->xmloutput)
408 fprintf(p_config->outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
411 if ('c' == p_config->print[0])
412 print_icu_converters(p_config);
413 else if ('l' == p_config->print[0])
414 print_icu_xml_locales(p_config);
415 else if ('t' == p_config->print[0])
416 print_icu_transliterators(p_config);
418 print_icu_converters(p_config);
419 print_icu_xml_locales(p_config);
420 print_icu_transliterators(p_config);
423 if (p_config->xmloutput)
424 fprintf(p_config->outfile, "</icu>\n");
431 static void process_text_file(struct config_t *p_config)
436 xmlDoc *doc = xmlParseFile(p_config->conffile);
437 xmlNode *xml_node = xmlDocGetRootElement(doc);
439 long unsigned int token_count = 0;
440 long unsigned int line_count = 0;
442 UErrorCode status = U_ZERO_ERROR;
446 printf("Could not parse XML config file '%s' \n",
451 p_config->chain = icu_chain_xml_config(xml_node, 1, &status);
453 if (!p_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(p_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, p_config->infile)))
469 WRBUF sw = wrbuf_alloc();
470 WRBUF cdata = wrbuf_alloc();
471 int success = icu_chain_assign_cstr(p_config->chain, line, &status);
474 while (success && icu_chain_next_token(p_config->chain, &status))
476 if (U_FAILURE(status))
481 const char *sortkey = icu_chain_token_sortkey(p_config->chain);
483 icu_chain_get_org_info(p_config->chain, &start, &len);
485 wrbuf_puts_escaped(sw, sortkey);
487 if (p_config->xmloutput)
489 fprintf(p_config->outfile,
490 "<token id=\"%lu\" line=\"%lu\"",
491 token_count, line_count);
494 wrbuf_xmlputs(cdata, icu_chain_token_norm(p_config->chain));
495 fprintf(p_config->outfile, " norm=\"%s\"",
499 wrbuf_xmlputs(cdata, icu_chain_token_display(p_config->chain));
500 fprintf(p_config->outfile, " display=\"%s\"",
503 if (p_config->sortoutput)
506 wrbuf_xmlputs(cdata, wrbuf_cstr(sw));
507 fprintf(p_config->outfile, " sortkey=\"%s\"",
510 fprintf(p_config->outfile, "/>\n");
514 fprintf(p_config->outfile, "%lu %lu '%s' '%s'",
517 icu_chain_token_norm(p_config->chain),
518 icu_chain_token_display(p_config->chain));
519 if (p_config->sortoutput)
521 fprintf(p_config->outfile, " '%s'", wrbuf_cstr(sw));
523 if (p_config->org_output)
525 fprintf(p_config->outfile, " %ld+%ld",
526 (long) start, (long) len);
528 fprintf(p_config->outfile, "\n");
533 wrbuf_destroy(cdata);
536 if (p_config->xmloutput)
537 fprintf(p_config->outfile,
541 icu_chain_destroy(p_config->chain);
547 #endif /* YAZ_HAVE_ICU */
550 int main(int argc, char **argv)
553 struct config_t config;
555 read_params(argc, argv, &config);
557 if (config.conffile && strlen(config.conffile))
558 process_text_file(&config);
560 if (config.print && strlen(config.print))
564 #else /* YAZ_HAVE_ICU */
566 printf("ICU not available on your system.\n"
567 "Please install libicu-dev and icu-doc or similar, "
568 "re-configure and re-compile\n");
572 #endif /* YAZ_HAVE_ICU */
581 * c-file-style: "Stroustrup"
582 * indent-tabs-mode: nil
584 * vim: shiftwidth=4 tabstop=8 expandtab