简繁拼音转换

汉语少不了面对简体繁体、汉字拼音的相互转换。HanLP基于双数组Trie树AC自动机算法提供了性能极高的转换功能。

简繁转换

一句话调用:

System.out.println(HanLP.convertToTraditionalChinese("“以后等你当上皇后,就能买草莓庆祝了”"));
System.out.println(HanLP.convertToSimplifiedChinese("用筆記簿型電腦寫程式HelloWorld"));

拼音转换

一句话就能调用,这里展示了对音调、声母、韵母等的支持:

String text = "重载不是重任";
List<Pinyin> pinyinList = HanLP.convertToPinyinList(text);
System.out.print("原文,");
for (char c : text.toCharArray())
{
    System.out.printf("%c,", c);
}
System.out.println();

System.out.print("拼音(数字音调),");
for (Pinyin pinyin : pinyinList)
{
    System.out.printf("%s,", pinyin);
}
System.out.println();

System.out.print("拼音(符号音调),");
for (Pinyin pinyin : pinyinList)
{
    System.out.printf("%s,", pinyin.getPinyinWithToneMark());
}
System.out.println();

System.out.print("拼音(无音调),");
for (Pinyin pinyin : pinyinList)
{
    System.out.printf("%s,", pinyin.getPinyinWithoutTone());
}
System.out.println();

System.out.print("声调,");
for (Pinyin pinyin : pinyinList)
{
    System.out.printf("%s,", pinyin.getTone());
}
System.out.println();

System.out.print("声母,");
for (Pinyin pinyin : pinyinList)
{
    System.out.printf("%s,", pinyin.getShengmu());
}
System.out.println();

System.out.print("韵母,");
for (Pinyin pinyin : pinyinList)
{
    System.out.printf("%s,", pinyin.getYunmu());
}
System.out.println();

System.out.print("输入法头,");
for (Pinyin pinyin : pinyinList)
{
    System.out.printf("%s,", pinyin.getHead());
}
System.out.println();
  • HanLP不仅支持基础的汉字转拼音,还支持声母、韵母、音调、音标和输入法首字母首声母功能。
  • HanLP能够识别多音字,也能给繁体中文注拼音。
  • 最重要的是,HanLP采用的模式匹配升级到 AhoCorasickDoubleArrayTrie,性能大幅提升,能够提供毫秒级的响应速度!
  • 算法详解: 《汉字转拼音与简繁转换的Java实现》