Pinyin4j的使用研究-开源JAVA中文字符和拼音之间的转换

Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换。拼音输出格式可以定制。

代码实现如下:

1.带声调的方法

 

/** 

* 汉字转换位汉语拼音,英文字符不变  带声调的方法

* @param chines 汉字 

* @return 拼音 

*/

public static String converterToSpellWithTONE(String chines) {

String pinyinName = "";

char[] nameChar = chines.toCharArray();

HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

defaultFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);

defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

for (int i = 0; i < nameChar.length; i++) {

if (nameChar[i] > 128) {

try {

if (PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat) != null

&& PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat).length > 0) {

pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];

}

} catch (BadHanyuPinyinOutputFormatCombination e) {

e.printStackTrace();

}

} else {

pinyinName += nameChar[i];

}

}

return pinyinName;

}


2.不带声调的方法

 

/** 

* 汉字转换位汉语拼音,英文字符不变 

* @param chines 汉字 

* @return 拼音 

*/

public static String converterToSpell(String chines) {

String pinyinName = "";

char[] nameChar = chines.toCharArray();

HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

for (int i = 0; i < nameChar.length; i++) {

if (nameChar[i] > 128) {

try {

if (PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat) != null

&& PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat).length > 0) {

pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];

}

} catch (BadHanyuPinyinOutputFormatCombination e) {

e.printStackTrace();

}

} else {

pinyinName += nameChar[i];

}

}

return pinyinName;

}


3.

 

/** 

* 汉字转换位汉语拼音首字母,英文字符不变 

* @param chines 汉字 

* @return 拼音 

*/

public static String converterToFirstSpell(String chines) {

String pinyinName = "";

char[] nameChar = chines.toCharArray();

HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

for (int i = 0; i < nameChar.length; i++) {

if (nameChar[i] > 128) {

try {

if (PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat) != null

&& PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat).length > 0) {

pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);

}

} catch (BadHanyuPinyinOutputFormatCombination e) {

e.printStackTrace();

}

} else {

pinyinName += nameChar[i];

}

}

return pinyinName;

}


Part Two:详细实现类如下:

 

package test;


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;

/**

 * 

 * pinyin4j使用

 * 

 * @author

 * 

 */

public class PinYinTest {

 public static HanyuPinyinToneType[] toneTypes = new HanyuPinyinToneType[] {

   HanyuPinyinToneType.WITH_TONE_NUMBER,

   HanyuPinyinToneType.WITHOUT_TONE,

   HanyuPinyinToneType.WITH_TONE_MARK };

 public static HanyuPinyinVCharType[] vCharTypes = new HanyuPinyinVCharType[] {

   HanyuPinyinVCharType.WITH_U_AND_COLON, HanyuPinyinVCharType.WITH_V,

   HanyuPinyinVCharType.WITH_U_UNICODE };

 public static HanyuPinyinCaseType[] caseTypes = new HanyuPinyinCaseType[] {

   HanyuPinyinCaseType.LOWERCASE, HanyuPinyinCaseType.UPPERCASE };

 /**

  * 将中文字符转换为相应的数组

  * 

  * @param chineseCharacter

  * @param toneSelection

  * @param vcharSelection

  * @param caseSelection

  */

 private static void updateFormattedText(String chineseCharacter,

   HanyuPinyinToneType toneSelection,

   HanyuPinyinVCharType vcharSelection,

   HanyuPinyinCaseType caseSelection) {

  // 拼音格式化对象

  HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();

  // 设置中文声调

  if (toneSelection != null) {

   if (toneTypes[0] == toneSelection) {

    outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);

   } else if (toneTypes[1] == toneSelection) {

    outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

   } else if (toneTypes[2] == toneSelection) {

    outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);

   }

  }

  // 设置特殊拼音的转换

  if (vcharSelection != null) {

   if (vCharTypes[0] == vcharSelection) {

    outputFormat

      .setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);

   } else if (vCharTypes[1] == vcharSelection) {

    outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

   } else if (vCharTypes[2] == vcharSelection) {

    outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

   }

  }

  // 设置转换的拼音的大小写

  if (caseSelection != null) {

   if (caseTypes[0] == caseSelection) {

    outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

   } else if (caseTypes[1] == caseSelection) {

    outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);

   }

  }

  // 获取字符串

  char[] cnStr = chineseCharacter.toCharArray();

  String msg = "";

  for (char chineseChar : cnStr) {

   // 将字符转换为拼音数组

   String[] pinyinArray = null;

   String myenglish = "";

   try {

  myenglish = PinyinHelper.toHanyuPinyinStringArray(

      chineseChar, outputFormat)[0];

   } catch (BadHanyuPinyinOutputFormatCombination e1) {

    e1.printStackTrace();

   }

//   String outputString = concatPinyinStringArray(pinyinArray);

   System.out.println(myenglish);

   msg += myenglish;

  }

  System.out.println("msg is====" + msg);

 }

 

 private static void updateFormattedText2(String chineseCharacter,

  HanyuPinyinToneType toneSelection,

  HanyuPinyinVCharType vcharSelection,

  HanyuPinyinCaseType caseSelection) {

 // 拼音格式化对象

 HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();

 // 设置中文声调

 if (toneSelection != null) {

  if (toneTypes[0] == toneSelection) {

   outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);

  } else if (toneTypes[1] == toneSelection) {

   outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

  } else if (toneTypes[2] == toneSelection) {

   outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);

  }

 }

 // 设置特殊拼音的转换

 if (vcharSelection != null) {

  if (vCharTypes[0] == vcharSelection) {

   outputFormat

     .setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);

  } else if (vCharTypes[1] == vcharSelection) {

   outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

  } else if (vCharTypes[2] == vcharSelection) {

   outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

  }

 }

 // 设置转换的拼音的大小写

 if (caseSelection != null) {

  if (caseTypes[0] == caseSelection) {

   outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

  } else if (caseTypes[1] == caseSelection) {

   outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);

  }

 }

 // 获取字符串

 char[] cnStr = chineseCharacter.toCharArray();

 for (char chineseChar : cnStr) {

  // 将字符转换为拼音数组

  String[] pinyinArray = null;

//   String test = "";

  try {

  pinyinArray = PinyinHelper.toHanyuPinyinStringArray(

     chineseChar, outputFormat);

  } catch (BadHanyuPinyinOutputFormatCombination e1) {

   e1.printStackTrace();

  }

  String outputString = concatPinyinStringArray(pinyinArray);

  System.out.println(outputString);

 }

}

 /**

  * 测试中文转换拼音方法

  * 

  * @param args

  */

 public static void main(String[] args) {

 updateFormattedText("我是中国人", HanyuPinyinToneType.WITH_TONE_NUMBER,

   HanyuPinyinVCharType.WITH_V,

   HanyuPinyinCaseType.LOWERCASE);

 System.out.println("===================");

 updateFormattedText("握事种果任", HanyuPinyinToneType.WITH_TONE_NUMBER,

   HanyuPinyinVCharType.WITH_U_AND_COLON,

   HanyuPinyinCaseType.LOWERCASE);

 System.out.println("========================");

 updateFormattedText("握事种果任", HanyuPinyinToneType.WITHOUT_TONE,

   HanyuPinyinVCharType.WITH_V,

   HanyuPinyinCaseType.LOWERCASE);

}

 /**

  * 将转换的拼音数组转换为字符串

  * 

  * @param pinyinArray

  * @return

  */

 private static String concatPinyinStringArray(String[] pinyinArray) {

  StringBuffer pinyinStrBuf = new StringBuffer();

  if ((null != pinyinArray) && (pinyinArray.length > 0)) {

   for (int i = 0; i < pinyinArray.length; i++) {

    pinyinStrBuf.append(pinyinArray[i]);

    pinyinStrBuf.append(System.getProperty("line.separator"));

   }

  }

  String outputString = pinyinStrBuf.toString();

  return outputString;

 }

}


下载链接:

你可能感兴趣的:(pinyin4j)