2018-09-04 Java Code Examples for com.ibm.icu.text.Transliterator 汉字转拼音 全角转半角

https://www.programcreek.com/java-api-examples/?api=com.ibm.icu.text.Transliterator

public void testBasicFunctionality() throws Exception {
  checkToken(Transliterator.getInstance("Traditional-Simplified"), 
      "簡化字", "简化字"); 
  checkToken(Transliterator.getInstance("Katakana-Hiragana"), 
      "ヒラガナ", "ひらがな");
  checkToken(Transliterator.getInstance("Fullwidth-Halfwidth"), 
      "アルアノリウ", "アルアノリウ");
  checkToken(Transliterator.getInstance("Any-Latin"), 
      "Αλφαβητικός Κατάλογος", "Alphabētikós Katálogos");
  checkToken(Transliterator.getInstance("NFD; [:Nonspacing Mark:] Remove"), 
      "Alphabētikós Katálogos", "Alphabetikos Katalogos");
  checkToken(Transliterator.getInstance("Han-Latin"),
      "中国", "zhōng guó");
}
 
public void testHalfTextNorm() {
    final Transliterator transliterator = Transliterator.createFromRules("", ":: Any-Latin; ' ' > ; :: Lower; ", Transliterator.FORWARD);
    assertEquals("kyanpasu", transliterator.transliterate("キャンパス"));
    assertEquals("alphabētikóskatálogos", transliterator.transliterate("Αλφαβητικός Κατάλογος"));
    assertEquals("biologičeskom", transliterator.transliterate("биологическом"));

    assertEquals("xièxiè", transliterator.transliterate("謝謝"));
    assertEquals("xièxiè", transliterator.transliterate("谢谢"));

    assertEquals("diànnǎo", transliterator.transliterate("電腦"));
    assertEquals("diànnǎo", transliterator.transliterate("电脑"));
    assertEquals("jìsuànjī", transliterator.transliterate("計算機"));
    assertEquals("jìsuànjī", transliterator.transliterate("计算机"));
}
 
public void testChinese() {
    final Language cmn = Language.lookup("cmn");
    final Transliterator transliterator = Transliterator.createFromRules("", cmn.getDefaultNormalizerRules(), Transliterator.FORWARD);

    assertEquals("xiexie", transliterator.transliterate("謝謝"));
    assertEquals("xiexie", transliterator.transliterate("谢谢"));

    assertEquals("diannao", transliterator.transliterate("電腦"));
    assertEquals("diannao", transliterator.transliterate("电脑"));
    assertEquals("jisuanji", transliterator.transliterate("計算機"));
    assertEquals("jisuanji", transliterator.transliterate("计算机"));

    assertEquals("chengjiu", transliterator.transliterate("成就"));

}
 

你可能感兴趣的:(2018-09-04 Java Code Examples for com.ibm.icu.text.Transliterator 汉字转拼音 全角转半角)