+// DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
+
+
+
+
+int test_icu_normalizer(const char * rules8cstr,
+ const char * src8cstr,
+ const char * chk8cstr)
+{
+ int success = 0;
+
+ UErrorCode status = U_ZERO_ERROR;
+
+ struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
+ struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
+ struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
+ struct icu_normalizer * normalizer
+ = icu_normalizer_create(rules8cstr, 'f', &status);
+ icu_check_status(status);
+
+ icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
+ icu_check_status(status);
+
+ icu_normalizer_normalize(normalizer, dest16, src16, &status);
+ icu_check_status(status);
+
+ icu_utf16_to_utf8(dest8, dest16, &status);
+ icu_check_status(status);
+
+
+ if(!strcmp((const char *) dest8->utf8,
+ (const char *) chk8cstr))
+ success = 1;
+ else {
+ success = 0;
+ printf("Normalization\n");
+ printf("Rules: '%s'\n", rules8cstr);
+ printf("Input: '%s'\n", src8cstr);
+ printf("Normalized: '%s'\n", dest8->utf8);
+ printf("Expected: '%s'\n", chk8cstr);
+ }
+
+
+ icu_normalizer_destroy(normalizer);
+ icu_buf_utf16_destroy(src16);
+ icu_buf_utf16_destroy(dest16);
+ icu_buf_utf8_destroy(dest8);
+
+ return success;
+};
+
+
+// DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
+
+void test_icu_I18N_normalizer(int argc, char **argv)
+{
+
+ YAZ_CHECK(test_icu_normalizer("[:Punctuation:] Any-Remove",
+ "Don't shoot!",
+ "Dont shoot"));
+
+ YAZ_CHECK(test_icu_normalizer("[:Control:] Any-Remove",
+ "Don't\n shoot!",
+ "Don't shoot!"));
+
+ YAZ_CHECK(test_icu_normalizer("[:Decimal_Number:] Any-Remove",
+ "This is 4 you!",
+ "This is you!"));
+
+ YAZ_CHECK(test_icu_normalizer("Lower; [:^Letter:] Remove",
+ "Don't shoot!",
+ "dontshoot"));
+
+ YAZ_CHECK(test_icu_normalizer("[:^Number:] Remove",
+ "Monday 15th of April",
+ "15"));
+
+ YAZ_CHECK(test_icu_normalizer("Lower;"
+ "[[:WhiteSpace:][:Punctuation:]] Remove",
+ " word4you? ",
+ "word4you"));
+
+
+ YAZ_CHECK(test_icu_normalizer("NFD; [:Nonspacing Mark:] Remove; NFC",
+ "à côté de l'alcôve ovoïde",
+ "a cote de l'alcove ovoide"));
+
+}
+
+
+// DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
+
+int test_icu_tokenizer(const char * locale, char action,
+ const char * src8cstr, int count)
+{
+ int success = 1;
+
+ UErrorCode status = U_ZERO_ERROR;
+ struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
+ struct icu_buf_utf16 * tkn16 = icu_buf_utf16_create(0);
+ struct icu_buf_utf8 * tkn8 = icu_buf_utf8_create(0);
+
+ //printf("Input: '%s'\n", src8cstr);
+
+ // transforming to UTF16
+ icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
+ icu_check_status(status);
+
+ // set up tokenizer
+ struct icu_tokenizer * tokenizer
+ = icu_tokenizer_create(locale, action, &status);
+ icu_check_status(status);
+ YAZ_CHECK(tokenizer);
+
+ // attach text buffer to tokenizer
+ icu_tokenizer_attach(tokenizer, src16, &status);
+ icu_check_status(status);
+ YAZ_CHECK(tokenizer->bi);
+
+ // perform work on tokens
+ //printf("Tokens: ");
+ while(icu_tokenizer_next_token(tokenizer, tkn16, &status)){
+ icu_check_status(status);
+
+ // converting to UTF8
+ icu_utf16_to_utf8(tkn8, tkn16, &status);
+
+ //printf("token %d %d %d %d '%s'\n",
+ //
+ // icu_tokenizer_token_start(tokenizer),
+ // icu_tokenizer_token_end(tokenizer),
+ // icu_tokenizer_token_length(tokenizer),
+ // tkn8->utf8);
+ }
+
+ if (count != icu_tokenizer_token_count(tokenizer)){
+ success = 0;
+ printf("\nTokenizer '%s:%c' Error: \n", locale, action);
+ printf("Input: '%s'\n", src8cstr);
+ printf("Tokens: %d", icu_tokenizer_token_count(tokenizer));
+ printf(", expected: %d\n", count);
+ }
+
+ icu_tokenizer_destroy(tokenizer);
+ icu_buf_utf16_destroy(src16);
+ icu_buf_utf16_destroy(tkn16);
+ icu_buf_utf8_destroy(tkn8);
+
+ return success;
+}
+
+
+// DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
+
+void test_icu_I18N_tokenizer(int argc, char **argv)
+{
+
+
+ const char * en_str
+ = "O Romeo, Romeo! wherefore art thou Romeo?";
+
+ YAZ_CHECK(test_icu_tokenizer("en", 's', en_str, 2));
+ YAZ_CHECK(test_icu_tokenizer("en", 'l', en_str, 7));
+ YAZ_CHECK(test_icu_tokenizer("en", 'w', en_str, 16));
+ YAZ_CHECK(test_icu_tokenizer("en", 'c', en_str, 41));
+
+
+
+ const char * da_str
+ = "Blåbærtærte. Denne kage stammer fra Finland. "
+ "Den er med blåbær, men alle sommerens forskellige bær kan bruges.";
+
+ YAZ_CHECK(test_icu_tokenizer("da", 's', da_str, 3));
+ YAZ_CHECK(test_icu_tokenizer("dar", 'l', da_str, 17));
+ YAZ_CHECK(test_icu_tokenizer("da", 'w', da_str, 37));
+ YAZ_CHECK(test_icu_tokenizer("da", 'c', da_str, 110));
+
+}
+
+
+void test_icu_I18N_chain(int argc, char **argv)
+{
+ const char * en_str
+ = "O Romeo, Romeo! wherefore art thou\t Romeo?";
+
+ printf("ICU chain:\ninput: '%s'\n", en_str);
+
+ UErrorCode status = U_ZERO_ERROR;
+ //struct icu_chain_step * step = 0;
+ struct icu_chain * chain = 0;
+
+
+ const char * xml_str = "<icu_chain id=\"en:word\" locale=\"en\">"
+ "<normalize rule=\"[:Control:] Any-Remove\"/>"
+ "<tokenize rule=\"l\"/>"
+ "<normalize rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
+ "<display/>"
+ "<casemap rule=\"l\"/>"
+ "<normal/>"
+ "<sort/>"
+ "</icu_chain>";
+
+
+ xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
+ xmlNode *xml_node = xmlDocGetRootElement(doc);
+ YAZ_CHECK(xml_node);
+
+
+ chain = icu_chain_xml_config(xml_node, &status);
+
+#if 0
+ chain = icu_chain_create((uint8_t *) "en:word", (uint8_t *) "en");
+ step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize,
+ (const uint8_t *) "[:Control:] Any-Remove",
+ &status);
+ step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
+ (const uint8_t *) "s",
+ &status);
+ step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
+ (const uint8_t *) "l",
+ &status);
+ step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize,
+ (const uint8_t *)
+ "[[:WhiteSpace:][:Punctuation:]] Any-Remove",
+ &status);
+ step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
+ (const uint8_t *)"",
+ &status);
+/* step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize, */
+/* (const uint8_t *) "Lower", */
+/* &status); */
+ step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
+ (const uint8_t *) "l",
+ &status);
+ step = icu_chain_insert_step(chain, ICU_chain_step_type_norm,
+ (const uint8_t *)"",
+ &status);
+/* step = icu_chain_insert_step(chain, ICU_chain_step_type_sort, */
+/* (const uint8_t *)"", */
+/* &status); */
+
+#endif
+
+ YAZ_CHECK(chain);
+
+ YAZ_CHECK(icu_chain_assign_cstr(chain, en_str, &status));
+
+ while (icu_chain_next_token(chain, &status)){
+ printf("%d '%s' '%s'\n",
+ icu_chain_get_token_count(chain),
+ icu_chain_get_norm(chain),
+ icu_chain_get_display(chain));
+ }
+
+ YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 7);
+
+ icu_chain_destroy(chain);
+ xmlFreeDoc(doc);
+}
+
+
+
+#endif // HAVE_ICU