博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pinyin4jUtil 汉字转拼音
阅读量:6069 次
发布时间:2019-06-20

本文共 9449 字,大约阅读时间需要 31 分钟。

hot3.png

import java.util.HashSet;import java.util.Set;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 is a plug-in, you can kind of Chinese characters into phonetic.Multi-tone character,Tone * Detailed view http://pinyin4j.sourceforge.net/ * * @author Administrator * @ClassName: Pinyin4jUtil * @Description: TODO * @author wang_china@foxmail.com * @date Jan 13, 2012 9:28:28 AM */public class Pinyin4jUtil {    /***************************************************************************     * 获取中文汉字拼音 默认输出     * @Name: Pinyin4jUtil.java     * @Description: TODO     * @author: wang_chian@foxmail.com     * @version: Jan 13, 2012 9:54:01 AM     * @param chinese     * @return     */    public static String getPinyin(String chinese) {        return getPinyinZh_CN(makeStringByStringSet(chinese));    }    /***************************************************************************     * 拼音大写输出     *     * @Name: Pinyin4jUtil.java     * @Description: TODO     * @author: wang_chian@foxmail.com     * @version: Jan 13, 2012 9:58:45 AM     * @param chinese     * @return     */    public static String getPinyinToUpperCase(String chinese) {        return getPinyinZh_CN(makeStringByStringSet(chinese)).toUpperCase();    }    /***************************************************************************     * 拼音小写输出     *     * @Name: Pinyin4jUtil.java     * @Description: TODO     * @author: wang_chian@foxmail.com     * @version: Jan 13, 2012 9:58:45 AM     * @param chinese     * @return     */    public static String getPinyinToLowerCase(String chinese) {        return getPinyinZh_CN(makeStringByStringSet(chinese)).toLowerCase();    }    /***************************************************************************     * 首字母大写输出     *     * @Name: Pinyin4jUtil.java     * @Description: TODO     * @author: wang_chian@foxmail.com     * @version: Jan 13, 2012 10:00:54 AM     * @param chinese     * @return     */    public static String getPinyinFirstToUpperCase(String chinese) {        return getPinyin(chinese);    }    /***************************************************************************     * 拼音简拼输出     *     * @Name: Pinyin4jUtil.java     * @Description: TODO     * @author: wang_chian@foxmail.com     * @version: Jan 13, 2012 11:08:15 AM     * @param chinese     * @return     */    public static String getPinyinJianPin(String chinese) {        return getPinyinConvertJianPin(getPinyin(chinese));    }    /***************************************************************************     * 字符集转换     *     * @Name: Pinyin4jUtil.java     * @Description: TODO     * @author: wang_chian@foxmail.com     * @version: Jan 13, 2012 9:34:11 AM     * @param chinese     *            中文汉字     * @throws BadHanyuPinyinOutputFormatCombination     */    public static Set
makeStringByStringSet(String chinese) { char[] chars = chinese.toCharArray(); if (chinese != null && !chinese.trim().equalsIgnoreCase("")) { char[] srcChar = chinese.toCharArray(); String[][] temp = new String[chinese.length()][]; for (int i = 0; i < srcChar.length; i++) { char c = srcChar[i]; // 是中文或者a-z或者A-Z转换拼音 if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) { try { temp[i] = PinyinHelper.toHanyuPinyinStringArray( chars[i], getDefaultOutputFormat()); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else if (((int) c >= 65 && (int) c <= 90) || ((int) c >= 97 && (int) c <= 122)) { temp[i] = new String[] { String.valueOf(srcChar[i]) }; } else { temp[i] = new String[] { "" }; } } String[] pingyinArray = Exchange(temp); Set
zhongWenPinYin = new HashSet
(); for (int i = 0; i < pingyinArray.length; i++) { zhongWenPinYin.add(pingyinArray[i]); } return zhongWenPinYin; } return null; } /*************************************************************************** * Default Format 默认输出格式 * * @Name: Pinyin4jUtil.java * @Description: TODO * @author: wang_chian@foxmail.com * @version: Jan 13, 2012 9:35:51 AM * @return */ public static HanyuPinyinOutputFormat getDefaultOutputFormat() { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写 format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 没有音调数字 format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);// u显示 return format; } /*************************************************************************** * * @Name: Pinyin4jUtil.java * @Description: TODO * @author: wang_chian@foxmail.com * @version: Jan 13, 2012 9:39:54 AM * @param strJaggedArray * @return */ public static String[] Exchange(String[][] strJaggedArray) { String[][] temp = DoExchange(strJaggedArray); return temp[0]; } /*************************************************************************** * * @Name: Pinyin4jUtil.java * @Description: TODO * @author: wang_chian@foxmail.com * @version: Jan 13, 2012 9:39:47 AM * @param strJaggedArray * @return */ private static String[][] DoExchange(String[][] strJaggedArray) { int len = strJaggedArray.length; if (len >= 2) { int len1 = strJaggedArray[0].length; int len2 = strJaggedArray[1].length; int newlen = len1 * len2; String[] temp = new String[newlen]; int Index = 0; for (int i = 0; i < len1; i++) { for (int j = 0; j < len2; j++) { temp[Index] = capitalize(strJaggedArray[0][i]) + capitalize(strJaggedArray[1][j]); Index++; } } String[][] newArray = new String[len - 1][]; for (int i = 2; i < len; i++) { newArray[i - 1] = strJaggedArray[i]; } newArray[0] = temp; return DoExchange(newArray); } else { return strJaggedArray; } } /*************************************************************************** * 首字母大写 * * @Name: Pinyin4jUtil.java * @Description: TODO * @author: wang_chian@foxmail.com * @version: Jan 13, 2012 9:36:18 AM * @param s * @return */ public static String capitalize(String s) { char ch[]; ch = s.toCharArray(); if (ch[0] >= 'a' && ch[0] <= 'z') { ch[0] = (char) (ch[0] - 32); } String newString = new String(ch); return newString; } /*************************************************************************** * 字符串集合转换字符串(逗号分隔) * * @Name: Pinyin4jUtil.java * @Description: TODO * @author: wang_chian@foxmail.com * @version: Jan 13, 2012 9:37:57 AM * @param stringSet * @return */ public static String getPinyinZh_CN(Set
stringSet) { StringBuilder str = new StringBuilder(); int i = 0; for (String s : stringSet) { if (i == stringSet.size() - 1) { str.append(s); } else { str.append(s + ","); } i++; } return str.toString(); } /*************************************************************************** * 获取每个拼音的简称 * * @Name: Pinyin4jUtil.java * @Description: TODO * @author: wang_chian@foxmail.com * @version: Jan 13, 2012 11:05:58 AM * @param chinese * @return */ public static String getPinyinConvertJianPin(String chinese) { String[] strArray = chinese.split(","); String strChar = ""; for (String str : strArray) { char arr[] = str.toCharArray(); // 将字符串转化成char型数组 for (int i = 0; i < arr.length; i++) { if (arr[i] >= 65 && arr[i] < 91) { // 判断是否是大写字母 strChar += new String(arr[i] + ""); } } strChar += ","; } return strChar; } /*************************************************************************** * Test * * @Name: Pinyin4jUtil.java * @Description: TODO * @author: wang_chian@foxmail.com * @version: Jan 13, 2012 9:49:27 AM * @param args */ public static void main(String[] args) { String str = "张三"; System.out.println("小写输出:" + getPinyinToLowerCase(str)); System.out.println("大写输出:" + getPinyinToUpperCase(str)); System.out.println("首字母大写输出:" + getPinyinFirstToUpperCase(str)); System.out.println("简拼输出:" + getPinyinJianPin(str)); }}

 

转载于:https://my.oschina.net/zlp5201/blog/830420

你可能感兴趣的文章
单链表的反转
查看>>
grep 命令总结
查看>>
进程小知识
查看>>
大数据之HDFS命令行基本操作
查看>>
4.17PMP试题每日一题
查看>>
Cookie禁用了Session还可以用吗?
查看>>
【Linux】开源系统监控方案:Cacti
查看>>
linux redhat6.5 构建SSH服务及SSH构建密钥对验证
查看>>
log4j - springmvc 中设置日志文件的路径
查看>>
Linux CentOS 学习第6天(2018年6月12日)
查看>>
Java程序员从阿里、百度面试回来,这些面试题你们会吗?
查看>>
去除报表参数动态下拉列表框中的Null Value
查看>>
你所遗漏的SpringBoot日志管理知识
查看>>
Java架构师学习路线图
查看>>
MySQL数据库复制概论
查看>>
网吧行业遭到DDOS功击该怎么办?
查看>>
正则辅助工具
查看>>
MongoDB:使用explain()和Index Usage(第2部分)调查查询
查看>>
单片机最小系统-基于LPC1114
查看>>
直面升职加薪,最高效学习营——TMBA线上营等你来战!
查看>>