标签:style blog color os io for art ar
/** * 汉字转换为拼音 包含多音字,包含生母zh,ch,sh的 */ public void toPinYinAll(){ String initials = "zh,ch,sh"; //返回汉语拼音的全拼 List<String> result = new ArrayList<String>(); //返回汉语拼音的单拼 List<String> firstResult = new ArrayList<String>(); result.add(""); firstResult.add(""); String text = "我重情长"; char[] t1 = null; t1 = text.toCharArray(); //设置汉字拼音的输出格式 HanyuPinyinOutputFormat hf = new HanyuPinyinOutputFormat(); //设置输出类型为小写 hf.setCaseType(HanyuPinyinCaseType.LOWERCASE); //设置音标类型为无音标 hf.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //设置 吕中u转换为v hf.setVCharType(HanyuPinyinVCharType.WITH_V); StringBuilder sb = new StringBuilder(); for(char c : t1){ //判断是否是汉字 if(Character.toString(c).matches("[\\u4E00-\\u9FA5]+")){ try { String[] t2 = PinyinHelper.toHanyuPinyinStringArray(c, hf); //去掉 多音词中 拼音相同 声调不同的重复拼音 Set<String> set = new HashSet<String>(); set.addAll(Arrays.asList(t2)); t2 = set.toArray(new String[0]); if(t2!=null && t2.length>1){ List<String> temp = new ArrayList<String>(); List<String> firstTemp = new ArrayList<String>(); temp.addAll(result); firstTemp.addAll(firstResult); for(int i=0;i<t2.length;i++){ int n = result.size(); String token = t2[i]; String firstToken = ""+token.charAt(0); if(initials.contains(token.substring(0, 2))){ firstToken = token.substring(0, 2); } if(i==0){ for(int j=0;j<n;j++){ String s = result.get(j); result.remove(j); result.add(j, s+token); String fs = firstResult.get(j); firstResult.remove(j); firstResult.add(j, fs+firstToken); } }else{ for(int j=0;j<n;j++){ String s = temp.get(j); temp.remove(j); temp.add(j, s+token); String fs = firstTemp.get(j); firstTemp.remove(j); firstTemp.add(j, fs+firstToken); } result.addAll(temp); firstResult.addAll(firstTemp); } } }else{ int n = result.size(); for(int i=0;i<n;i++){ String s = result.get(i); result.remove(i); result.add(i, s+t2[0]); String firstToken = ""+t2[0].charAt(0); if(initials.contains(t2[0].substring(0, 2))){ firstToken = t2[0].substring(0, 2); } String fs = firstResult.get(i); firstResult.remove(i); firstResult.add(i, fs+firstToken); } } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else{ int n = result.size(); for(int i=0;i<n;i++){ String s = result.get(i); result.remove(i); result.add(i,s+String.valueOf(c)); } } } System.out.println(result.toString()); System.out.println(firstResult.toString()); }
结果:
[wozhongqingzhang, wochongqingzhang, wozhongqingchang, wochongqingchang]
[wzhqzh, wchqzh, wzhqch, wchqch]
通过pinyin4j将汉字转换为全拼 和 拼音首字母,布布扣,bubuko.com
标签:style blog color os io for art ar
原文地址:http://www.cnblogs.com/a198720/p/3895321.html