1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
29 #include <yaz/timing.h>
42 //#include <unicode/ustring.h>
43 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
46 #define MAX_KEY_SIZE 256
49 uint8_t sort_key[MAX_KEY_SIZE]; // standard C string '\0' terminated
50 char disp_term[MAX_KEY_SIZE]; // standard C utf-8 string
55 int icu_termmap_cmp(const void *vp1, const void *vp2)
57 struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
58 struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
62 cmp = strcmp((const char *)itmp1->sort_key,
63 (const char *)itmp2->sort_key);
70 int test_icu_casemap(const char * locale, char action,
71 const char * src8cstr, const char * chk8cstr)
74 UErrorCode status = U_ZERO_ERROR;
76 struct icu_buf_utf8 * src8 = icu_buf_utf8_create(0);
77 struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
78 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
79 struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
82 int src8cstr_len = strlen(src8cstr);
83 int chk8cstr_len = strlen(chk8cstr);
85 // converting to UTF16
86 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
88 // perform case mapping
89 icu_utf16_casemap(dest16, src16, locale, action, &status);
92 icu_utf16_to_utf8(dest8, dest16, &status);
98 && (dest8->utf8_len == strlen(chk8cstr))
99 && !strcmp(chk8cstr, (const char *) dest8->utf8))
107 printf("original string: '%s' (%d)\n", src8cstr, src8cstr_len);
108 printf("icu_casemap '%s:%c' '%s' (%d)\n",
109 locale, action, dest8->utf8, dest8->utf8_len);
110 printf("expected string: '%s' (%d)\n", chk8cstr, chk8cstr_len);
114 icu_buf_utf8_destroy(src8);
115 icu_buf_utf8_destroy(dest8);
116 icu_buf_utf16_destroy(src16);
117 icu_buf_utf16_destroy(dest16);
125 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
127 void test_icu_I18N_casemap(int argc, char **argv)
133 YAZ_CHECK(test_icu_casemap("en", 'l',
134 "A ReD fOx hunTS sQUirriLs",
135 "a red fox hunts squirrils"));
137 YAZ_CHECK(test_icu_casemap("en", 'u',
138 "A ReD fOx hunTS sQUirriLs",
139 "A RED FOX HUNTS SQUIRRILS"));
141 YAZ_CHECK(test_icu_casemap("en", 'f',
142 "A ReD fOx hunTS sQUirriLs",
143 "a red fox hunts squirrils"));
145 YAZ_CHECK(test_icu_casemap("en", 't',
146 "A ReD fOx hunTS sQUirriLs",
147 "A Red Fox Hunts Squirrils"));
153 YAZ_CHECK(test_icu_casemap("da", 'l',
154 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
155 "åh æble, øs fløde i åen efter blåbærgrøden"));
157 YAZ_CHECK(test_icu_casemap("da", 'u',
158 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
159 "ÅH ÆBLE, ØS FLØDE I ÅEN EFTER BLÅBÆRGRØDEN"));
161 YAZ_CHECK(test_icu_casemap("da", 'f',
162 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
163 "åh æble, øs fløde i åen efter blåbærgrøden"));
165 YAZ_CHECK(test_icu_casemap("da", 't',
166 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
167 "Åh Æble, Øs Fløde I Åen Efter Blåbærgrøden"));
172 YAZ_CHECK(test_icu_casemap("de", 'l',
173 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
174 "zwölf ärgerliche würste rollen über die straße"));
176 YAZ_CHECK(test_icu_casemap("de", 'u',
177 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
178 "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE"));
180 YAZ_CHECK(test_icu_casemap("de", 'f',
181 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
182 "zwölf ärgerliche würste rollen über die strasse"));
184 YAZ_CHECK(test_icu_casemap("de", 't',
185 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
186 "Zwölf Ärgerliche Würste Rollen Über Die Straße"));
191 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
193 int test_icu_sortmap(const char * locale, int src_list_len,
194 const char ** src_list, const char ** chk_list)
198 UErrorCode status = U_ZERO_ERROR;
200 struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0);
201 struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0);
205 struct icu_termmap * list[src_list_len];
207 UCollator *coll = ucol_open(locale, &status);
208 icu_check_status(status);
210 if(U_FAILURE(status))
213 // assigning display terms and sort keys using buf 8 and buf16
214 for( i = 0; i < src_list_len; i++)
217 list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
220 strcpy(list[i]->disp_term, src_list[i]);
222 // transforming to UTF16
223 icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
224 icu_check_status(status);
226 // computing sortkeys
227 icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
228 icu_check_status(status);
230 // assigning sortkeys
231 memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
232 //strncpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
233 //strcpy((char *) list[i]->sort_key, (const char *) buf8->utf8);
238 qsort(list, src_list_len,
239 sizeof(struct icu_termmap *), icu_termmap_cmp);
241 // checking correct sorting
242 for (i = 0; i < src_list_len; i++){
243 if (0 != strcmp(list[i]->disp_term, chk_list[i])){
250 printf("Input str: '%s' : ", locale);
251 for (i = 0; i < src_list_len; i++) {
252 printf(" '%s'", list[i]->disp_term);
255 printf("ICU sort: '%s' : ", locale);
256 for (i = 0; i < src_list_len; i++) {
257 printf(" '%s'", list[i]->disp_term);
258 //printf("(%d|%d)", list[i]->sort_key[0],list[i]->sort_key[1]);
261 printf("Expected: '%s' : ", locale);
262 for (i = 0; i < src_list_len; i++) {
263 printf(" '%s'", chk_list[i]);
270 for( i = 0; i < src_list_len; i++)
276 icu_buf_utf8_destroy(buf8);
277 icu_buf_utf16_destroy(buf16);
283 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
285 void test_icu_I18N_sortmap(int argc, char **argv)
290 const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
291 const char * en_1_cck[6] = {"a", "A", "k", "K", "z", "Z"};
292 YAZ_CHECK(test_icu_sortmap("en", en_1_len, en_1_src, en_1_cck));
293 YAZ_CHECK(test_icu_sortmap("en_AU", en_1_len, en_1_src, en_1_cck));
294 YAZ_CHECK(test_icu_sortmap("en_CA", en_1_len, en_1_src, en_1_cck));
295 YAZ_CHECK(test_icu_sortmap("en_GB", en_1_len, en_1_src, en_1_cck));
296 YAZ_CHECK(test_icu_sortmap("en_US", en_1_len, en_1_src, en_1_cck));
300 const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
301 const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
302 YAZ_CHECK(test_icu_sortmap("da", da_1_len, da_1_src, da_1_cck));
303 YAZ_CHECK(test_icu_sortmap("da_DK", da_1_len, da_1_src, da_1_cck));
307 const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
308 const char * de_1_cck[9] = {"a","ä", "o", "ö", "s", "ß", "t", "u", "ü"};
309 YAZ_CHECK(test_icu_sortmap("de", de_1_len, de_1_src, de_1_cck));
310 YAZ_CHECK(test_icu_sortmap("de_AT", de_1_len, de_1_src, de_1_cck));
311 YAZ_CHECK(test_icu_sortmap("de_DE", de_1_len, de_1_src, de_1_cck));
316 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
321 int test_icu_normalizer(const char * rules8cstr,
322 const char * src8cstr,
323 const char * chk8cstr)
327 UErrorCode status = U_ZERO_ERROR;
329 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
330 struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
331 struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
332 struct icu_normalizer * normalizer
333 = icu_normalizer_create(rules8cstr, 'f', &status);
334 icu_check_status(status);
336 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
337 icu_check_status(status);
339 icu_normalizer_normalize(normalizer, dest16, src16, &status);
340 icu_check_status(status);
342 icu_utf16_to_utf8(dest8, dest16, &status);
343 icu_check_status(status);
346 if(!strcmp((const char *) dest8->utf8,
347 (const char *) chk8cstr))
351 printf("Normalization\n");
352 printf("Rules: '%s'\n", rules8cstr);
353 printf("Input: '%s'\n", src8cstr);
354 printf("Normalized: '%s'\n", dest8->utf8);
355 printf("Expected: '%s'\n", chk8cstr);
359 icu_normalizer_destroy(normalizer);
360 icu_buf_utf16_destroy(src16);
361 icu_buf_utf16_destroy(dest16);
362 icu_buf_utf8_destroy(dest8);
368 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
370 void test_icu_I18N_normalizer(int argc, char **argv)
373 YAZ_CHECK(test_icu_normalizer("[:Punctuation:] Any-Remove",
377 YAZ_CHECK(test_icu_normalizer("[:Control:] Any-Remove",
381 YAZ_CHECK(test_icu_normalizer("[:Decimal_Number:] Any-Remove",
385 YAZ_CHECK(test_icu_normalizer("Lower; [:^Letter:] Remove",
389 YAZ_CHECK(test_icu_normalizer("[:^Number:] Remove",
390 "Monday 15th of April",
393 YAZ_CHECK(test_icu_normalizer("Lower;"
394 "[[:WhiteSpace:][:Punctuation:]] Remove",
399 YAZ_CHECK(test_icu_normalizer("NFD; [:Nonspacing Mark:] Remove; NFC",
400 "à côté de l'alcôve ovoïde",
401 "a cote de l'alcove ovoide"));
406 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
408 int test_icu_tokenizer(const char * locale, char action,
409 const char * src8cstr, int count)
413 UErrorCode status = U_ZERO_ERROR;
414 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
415 struct icu_buf_utf16 * tkn16 = icu_buf_utf16_create(0);
416 struct icu_buf_utf8 * tkn8 = icu_buf_utf8_create(0);
418 //printf("Input: '%s'\n", src8cstr);
420 // transforming to UTF16
421 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
422 icu_check_status(status);
425 struct icu_tokenizer * tokenizer
426 = icu_tokenizer_create(locale, action, &status);
427 icu_check_status(status);
428 YAZ_CHECK(tokenizer);
430 // attach text buffer to tokenizer
431 icu_tokenizer_attach(tokenizer, src16, &status);
432 icu_check_status(status);
433 YAZ_CHECK(tokenizer->bi);
435 // perform work on tokens
436 //printf("Tokens: ");
437 while(icu_tokenizer_next_token(tokenizer, tkn16, &status)){
438 icu_check_status(status);
440 // converting to UTF8
441 icu_utf16_to_utf8(tkn8, tkn16, &status);
443 //printf("token %d %d %d %d '%s'\n",
445 // icu_tokenizer_token_start(tokenizer),
446 // icu_tokenizer_token_end(tokenizer),
447 // icu_tokenizer_token_length(tokenizer),
451 if (count != icu_tokenizer_token_count(tokenizer)){
453 printf("\nTokenizer '%s:%c' Error: \n", locale, action);
454 printf("Input: '%s'\n", src8cstr);
455 printf("Tokens: %d", icu_tokenizer_token_count(tokenizer));
456 printf(", expected: %d\n", count);
459 icu_tokenizer_destroy(tokenizer);
460 icu_buf_utf16_destroy(src16);
461 icu_buf_utf16_destroy(tkn16);
462 icu_buf_utf8_destroy(tkn8);
468 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
470 void test_icu_I18N_tokenizer(int argc, char **argv)
475 = "O Romeo, Romeo! wherefore art thou Romeo?";
477 YAZ_CHECK(test_icu_tokenizer("en", 's', en_str, 2));
478 YAZ_CHECK(test_icu_tokenizer("en", 'l', en_str, 7));
479 YAZ_CHECK(test_icu_tokenizer("en", 'w', en_str, 16));
480 YAZ_CHECK(test_icu_tokenizer("en", 'c', en_str, 41));
485 = "Blåbærtærte. Denne kage stammer fra Finland. "
486 "Den er med blåbær, men alle sommerens forskellige bær kan bruges.";
488 YAZ_CHECK(test_icu_tokenizer("da", 's', da_str, 3));
489 YAZ_CHECK(test_icu_tokenizer("dar", 'l', da_str, 17));
490 YAZ_CHECK(test_icu_tokenizer("da", 'w', da_str, 37));
491 YAZ_CHECK(test_icu_tokenizer("da", 'c', da_str, 110));
496 void test_icu_I18N_chain(int argc, char **argv)
499 = "O Romeo, Romeo! wherefore art thou\t Romeo?";
501 printf("ICU chain:\ninput: '%s'\n", en_str);
503 UErrorCode status = U_ZERO_ERROR;
504 //struct icu_chain_step * step = 0;
505 struct icu_chain * chain = 0;
508 const char * xml_str = "<icu_chain id=\"en:word\" locale=\"en\">"
509 "<normalize rule=\"[:Control:] Any-Remove\"/>"
510 "<tokenize rule=\"l\"/>"
511 "<normalize rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
513 "<casemap rule=\"l\"/>"
519 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
520 xmlNode *xml_node = xmlDocGetRootElement(doc);
524 chain = icu_chain_xml_config(xml_node, &status);
527 chain = icu_chain_create((uint8_t *) "en:word", (uint8_t *) "en");
528 step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize,
529 (const uint8_t *) "[:Control:] Any-Remove",
531 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
532 (const uint8_t *) "s",
534 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
535 (const uint8_t *) "l",
537 step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize,
539 "[[:WhiteSpace:][:Punctuation:]] Any-Remove",
541 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
544 /* step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize, */
545 /* (const uint8_t *) "Lower", */
547 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
548 (const uint8_t *) "l",
550 step = icu_chain_insert_step(chain, ICU_chain_step_type_index,
553 /* step = icu_chain_insert_step(chain, ICU_chain_step_type_sortkey, */
554 /* (const uint8_t *)"", */
562 YAZ_CHECK(icu_chain_assign_cstr(chain, en_str, &status));
564 while (icu_chain_next_token(chain, &status)){
565 printf("%d '%s' '%s'\n",
566 icu_chain_get_token_count(chain),
567 icu_chain_get_norm(chain),
568 icu_chain_get_display(chain));
571 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 7);
574 YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
576 while (icu_chain_next_token(chain, &status)){
577 printf("%d '%s' '%s'\n",
578 icu_chain_get_token_count(chain),
579 icu_chain_get_norm(chain),
580 icu_chain_get_display(chain));
584 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 3);
586 icu_chain_destroy(chain);
590 void test_bug_1140(void)
593 = "O Romeo, Romeo! wherefore art thou\t Romeo?";
595 printf("ICU chain:\ninput: '%s'\n", en_str);
597 UErrorCode status = U_ZERO_ERROR;
598 //struct icu_chain_step * step = 0;
599 struct icu_chain * chain = 0;
601 const char * xml_str = "<icu_chain id=\"en:word\" locale=\"en\">"
603 /* if the first rule is normalize instead. Then it works */
605 "<normalize rule=\"[:Control:] Any-Remove\"/>"
607 "<tokenize rule=\"l\"/>"
608 "<normalize rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
610 "<casemap rule=\"l\"/>"
616 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
617 xmlNode *xml_node = xmlDocGetRootElement(doc);
620 chain = icu_chain_xml_config(xml_node, &status);
625 YAZ_CHECK(icu_chain_assign_cstr(
626 chain, "O Romeo, Romeo! wherefore art thou\t Romeo?",
629 while (icu_chain_next_token(chain, &status))
632 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 7);
634 YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
636 while (icu_chain_next_token(chain, &status)){
637 printf("%d '%s' '%s'\n",
638 icu_chain_get_token_count(chain),
639 icu_chain_get_norm(chain),
640 icu_chain_get_display(chain));
643 /* we expect 'what' 'is' 'this', i.e. 3 tokens */
644 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 3);
646 icu_chain_destroy(chain);
651 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
653 int main(int argc, char **argv)
656 YAZ_CHECK_INIT(argc, argv);
661 //test_icu_I18N_casemap_failures(argc, argv);
662 test_icu_I18N_casemap(argc, argv);
663 test_icu_I18N_sortmap(argc, argv);
664 test_icu_I18N_normalizer(argc, argv);
665 test_icu_I18N_tokenizer(argc, argv);
666 test_icu_I18N_chain(argc, argv);
671 printf("ICU unit tests omitted.\n"
672 "Please install libicu36-dev and icu-doc or similar\n");
681 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
688 * indent-tabs-mode: nil
690 * vim: shiftwidth=4 tabstop=8 expandtab