常用RAID简介及软RAID的实现
2016-03-20 22:53
471 查看
pinyin4j的主页:http://pinyin4j.sourceforge.net/
pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制。
pinyin4j是一个支持将中文转换到拼音的Java开源类库。
支持简体中文和繁体中文字符;
支持转换到汉语拼音,通用拼音, 威妥玛拼音(威玛拼法), 注音符号第二式, 耶鲁拼法和国语罗马字;
支持多音字,即可以获取一个中文字符的多种发音;
支持多种字符串输出格式,比如支持Unicode格式的字符ü和声调符号(阴平 "ˉ",阳平"ˊ",上声"ˇ",去声"ˋ")的输出。
特征:
1. 支持多种拼音系统:
汉语拼音
通用拼音
威氏拼音
注音二式
雅礼
国语罗马字
2. 支持多音字
能够根据上下文获得正确的拼音
3. 支持多种拼音格式
大小写
支持unicode ü 、v 和 u
支持数字音调(lü3)、音调符号 (lǚ)和无音调标识(lü)
利用Java开源库把汉字转拼音
最近做的项目,因为下拉框中的项目太多,需要有个过滤的方法。我想的是键入汉语拼音首字母即时过滤下拉内容。
在网上找了好一会,发现了有个专门用来将中文转换成汉语拼音的Java的开源库,Pinyin4j,
下载地址:pinyin4j-2.5.0.zip
基本用法,如下示例
pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制。
pinyin4j是一个支持将中文转换到拼音的Java开源类库。
支持简体中文和繁体中文字符;
支持转换到汉语拼音,通用拼音, 威妥玛拼音(威玛拼法), 注音符号第二式, 耶鲁拼法和国语罗马字;
支持多音字,即可以获取一个中文字符的多种发音;
支持多种字符串输出格式,比如支持Unicode格式的字符ü和声调符号(阴平 "ˉ",阳平"ˊ",上声"ˇ",去声"ˋ")的输出。
特征:
1. 支持多种拼音系统:
汉语拼音
通用拼音
威氏拼音
注音二式
雅礼
国语罗马字
2. 支持多音字
能够根据上下文获得正确的拼音
3. 支持多种拼音格式
大小写
支持unicode ü 、v 和 u
支持数字音调(lü3)、音调符号 (lǚ)和无音调标识(lü)
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 Pinyin { /** * 将汉字转换为全拼 * * @param src * @return String */ public static String getPinYin(String src) { char[] t1 = null; t1 = src.toCharArray(); // System.out.println(t1.length); String[] t2 = new String[t1.length]; // System.out.println(t2.length); // 设置汉字拼音输出的格式 HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); t3.setCaseType(HanyuPinyinCaseType.LOWERCASE); t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); t3.setVCharType(HanyuPinyinVCharType.WITH_V); String t4 = ""; int t0 = t1.length; try { for (int i = 0; i < t0; i++) { // 判断能否为汉字字符 // System.out.println(t1[i]); if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) { t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中 t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后 } else { // 如果不是汉字字符,间接取出字符并连接到字符串t4后 t4 += Character.toString(t1[i]); } } } catch (BadHanyuPinyinOutputFormatCombination e) { // TODO Auto-generated catch block e.printStackTrace(); } return t4; } /** * 提取每个汉字的首字母 * * @param str * @return String */ public static String getPinYinHeadChar(String str) { String convert = ""; for (int j = 0; j < str.length(); j++) { char word = str.charAt(j); // 提取汉字的首字母 String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); if (pinyinArray != null) { convert += pinyinArray[0].charAt(0); } else { convert += word; } } return convert; } /** * 将字符串转换成ASCII码 * * @param cnStr * @return String */ public static String getCnASCII(String cnStr) { StringBuffer strBuf = new StringBuffer(); // 将字符串转换成字节序列 byte[] bGBK = cnStr.getBytes(); for (int i = 0; i < bGBK.length; i++) { // System.out.println(Integer.toHexString(bGBK[i] & 0xff)); // 将每个字符转换成ASCII码 strBuf.append(Integer.toHexString(bGBK[i] & 0xff)); } return strBuf.toString(); } public static void main(String[] args) { String cnStr = "嘅囧誰說壞學生來勼髮視頻裆児"; System.out.println(getPinYin(cnStr)); System.out.println(getPinYinHeadChar(cnStr)); System.out.println(getCnASCII(cnStr)); } }实例:
利用Java开源库把汉字转拼音
最近做的项目,因为下拉框中的项目太多,需要有个过滤的方法。我想的是键入汉语拼音首字母即时过滤下拉内容。
在网上找了好一会,发现了有个专门用来将中文转换成汉语拼音的Java的开源库,Pinyin4j,
下载地址:pinyin4j-2.5.0.zip
基本用法,如下示例
Java代码 /** * 汉字转换位汉语拼音,英文字符不变 * @author xuke * */ public class Cn2Spell { /** * 汉字转换位汉语拼音首字母,英文字符不变 * @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); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] > 128) { try { pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ pinyinName += nameChar[i]; } } return pinyinName; } /** * 汉字转换位汉语拼音,英文字符不变 * @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); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] > 128) { try { pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0]; } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ pinyinName += nameChar[i]; } } return pinyinName; } public static void main(String[] args) { System.out.println(converterToFirstSpell("欢迎来到最棒的Java中文社区")); } } /** * 汉字转换位汉语拼音,英文字符不变 * @author xuke * */ public class Cn2Spell { /** * 汉字转换位汉语拼音首字母,英文字符不变 * @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); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] > 128) { try { pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ pinyinName += nameChar[i]; } } return pinyinName; } /** * 汉字转换位汉语拼音,英文字符不变 * @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); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] > 128) { try { pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0]; } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ pinyinName += nameChar[i]; } } return pinyinName; } public static void main(String[] args) { System.out.println(converterToFirstSpell("欢迎来到最棒的Java中文社区")); } }以上一个是完全转换成汉语拼音的,另一个是只转换首字母的,遇到ASCII字符都不转换,保留。
相关文章推荐
- UVA 514——Rails
- AIDL简介
- 严苛模式解决NetworkOnMainThreadException
- 英文歌曲:Right Here Waiting(此情可待)
- CF651B-Beautiful Paintings
- ABP源码分析七:Setting 以及 Mail
- POJ-1273 Drainage Ditches(最大流)
- 模板(双向链表与队列)
- 《leetCode》:Contains Duplicate
- RAID各级别的特性
- RAID各级别的特性
- LeetCode 219. Contains Duplicate II
- 《leetCode》:Contains Duplicate II
- 1014. Waiting in Line (30)
- CodeForces 272C-Dima and Staircase-线段树区间更新-RMQ
- RAID技术介绍和总结
- CodeForces 209C Trails and Glades(欧拉回路判断+并查集计算联通分量)
- alter table *** add constraint *** 用法
- leetcod--Climbing Stairs
- LeetCode 217. Contains Duplicate