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 test {

/**

* 将汉字转换为拼音(带声调)

*/

public static String toPinyinWithTone(String chinese) {

return convertChineseToPinyin(chinese, true, false, false);

}

/**

* 将汉字转换为拼音(不带声调)

*/

public static String toPinyinWithoutTone(String chinese) {

return convertChineseToPinyin(chinese, false, false, false);

}

/**

* 将汉字转换为拼音首字母

*/

public static String toPinyinFirstLetter(String chinese) {

return convertChineseToPinyin(chinese, false, true, false);

}

/**

* 将汉字转换为拼音(首字母大写,不带声调)

*/

public static String toPinyinCapitalized(String chinese) {

return convertChineseToPinyin(chinese, false, false, true);

}

/**

* 汉字转换为拼音的核心方法

*

* @param chinese 中文字符串

* @param withTone 是否带声调

* @param firstLetterOnly 是否只取首字母

* @param capitalized 是否首字母大写

* @return 转换后的拼音字符串

*/

private static String convertChineseToPinyin(String chinese, boolean withTone,

boolean firstLetterOnly, boolean capitalized) {

if (chinese == null || chinese.isEmpty()) {

return "";

}

HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

// 设置拼音大小写

format.setCaseType(capitalized ? HanyuPinyinCaseType.UPPERCASE : HanyuPinyinCaseType.LOWERCASE);

// 设置声调格式

format.setToneType(withTone ? HanyuPinyinToneType.WITH_TONE_MARK : HanyuPinyinToneType.WITHOUT_TONE);

// 关键修复:根据是否带声调设置ü的显示方式

format.setVCharType(withTone ? HanyuPinyinVCharType.WITH_U_UNICODE : HanyuPinyinVCharType.WITH_U_AND_COLON);

StringBuilder pinyin = new StringBuilder();

char[] chars = chinese.toCharArray();

for (char c : chars) {

// 如果是汉字

if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {

try {

String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);

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

String py = pinyinArray[0]; // 取第一个拼音

if (firstLetterOnly) {

pinyin.append(py.charAt(0));

} else {

pinyin.append(py);

}

}

} catch (BadHanyuPinyinOutputFormatCombination e) {

e.printStackTrace();

}

} else {

// 非汉字直接添加

pinyin.append(c);

}

pinyin.append(" ");

}

return pinyin.toString();

}

// 测试方法

public static void main(String[] args) {

String chinese = "银行行长";

System.out.println("带声调: " + toPinyinWithTone(chinese));

System.out.println("不带声调: " + toPinyinWithoutTone(chinese));

System.out.println("首字母: " + toPinyinFirstLetter(chinese));

System.out.println("首字母大写: " + toPinyinCapitalized(chinese));

}

}

文件名精灵2025年最新版 机器翻译 AI智能
...
批量修改文件名称、文件夹名称

免费使用 功能无限制

全面接入机器自动翻译功能,主流AI大模型智能改名

方便快捷,支持正则表达式、无需安装功能齐全、支持定制无广告、无插件,放心使用。

免费下载
本站中所有的计算器的计算结果仅供参考,本站对此结果的准确性不承担任何责任,实际数额以银行/保险公司/国家相关机构确认的结果为准。
在线客服QQ:543690914,备案号: 苏ICP备15037649号-27。东海县白塔埠镇佳诚电脑经营部版权所有。