Java implements the method of converting Chinese characters into hanyu pinyin

  • 2020-04-01 04:33:12
  • OfStack

This article illustrates the method of Java to convert Chinese characters into pinyin. Share with you for your reference, as follows:

Wandering around on the Internet, I came across a very interesting tool named pinyin4j, which can convert Chinese characters into hanyu pinyin. By combining his words with lucene and Chinese word segmentation, I can make a full-text search function similar to Google by typing hanyu pinyin. The code for the implementation is as follows


package pinyin4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class pinyin4jTest {
  public static void main(String argsp[]) {
    try {
      String output = pinyin4jTest.CNToPinyin(" How are you and how are you ", null);
      System.out.println(output);
    } catch (BadHanyuPinyinOutputFormatCombination e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  public static String CNToPinyin(String inputCN, String seg)
      throws BadHanyuPinyinOutputFormatCombination {
    char[] inputArray = inputCN.toCharArray();
    if (seg == null)
      seg = " ";
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);
    String output = "";
    String[] temp = new String[10];
    for (int i = 0; i < inputArray.length; i++) {
      temp = PinyinHelper.toHanyuPinyinStringArray(inputArray[i], format);
      //If the input Chinese character is polyphonic, different pronunciations will be put into temp[], if not polyphonic, only temp[0] has a value
      for (int j = 0; j < temp.length; j++) {
        output += temp[j] + seg;
      }
    }
    return output;
  }
}

I hope this article has been helpful to you in Java programming.


Related articles: