Java中⽂转拼⾳代码_把中⽂汉字转成拼⾳(java代码)package util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
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.ption.BadHanyuPinyinOutputFormatCombination;public classHanyuPinyinUtil {public static MaphanziToPinyin(String name) {
Map map = new HashMap();
map.put("name", name);returnhanziToPinyin(map);
}public static Map hanziToPinyin(Mapmap) {
Map newMap = null;//参数校验
if (map == null) return null;//map 是否有元素//if(map.isEmpty()) return null;//不存在键名 name
if (!ainsKey("name")) return null;//校验值
String hanzi = ("name");//是否为null 或空⽩字符
if (hanzi == null || im().equals("")) return null;//去除特殊字符只留汉字
while (!hanzi.matches("[\\u4e00-\\u9fa5]+")) {//替换掉⾮中⽂
hanzi = placeAll("[^\\u4e00-\\u9fa5]+", "");
}if (hanzi.equals("")) return null;
newMap= new HashMap();
newMap.put("name", ("name"));//获取 汉字字符串的⾸个中⽂字符的⾸个字母
newMap.put("firstChar", getFirstChar(hanzi));//获取全拼⾳
newMap.put("all", getPinyinAll(hanzi));//获取拼⾳缩写
newMap.put("acronym", getPinyinAcronym(hanzi));returnnewMap;
}//获取汉字字符串的⾸个汉字的⾸字母
private static SetgetFirstChar(String hanzi) {
Set firsts = null;//获取汉字字符串的拼⾳
String[][] pinyins =getHanyuPinyin(hanzi);if (pinyins != null && pinyins.length > 0 && pinyins[0].length > 0) { firsts= new HashSet();
String[] temp= pinyins[0];for(String str : temp) {if (str != null && !im().equals("")) {
firsts.String(str.charAt(0)));
}
}
}returnfirsts;
}//获取中⽂字符串的全拼⾳(多⾳字的不同组合)
private static SetgetPinyinAll(String hanzi) {
Set set = null;
String[][] pinyins=getHanyuPinyin(hanzi);if (pinyins != null && pinyins.length > 0) {
List list = new ArrayList();//获取⾸个汉字的所有拼⾳(多⾳字)
int n = 0;for (int i = 0; i < pinyins[0].length; i++) {
list.add(newStringBuffer(pinyins[n][i]));
}//循环取下个中⽂的拼⾳, 处理多种组合
while (pinyins.length > ++n) {//复制⼀份
List tempList1 = new ArrayList();//确保list⾥的对象不是相同的
for (int i = 0; i < list.size(); i++) {
tempList1.add(new (i).toString()));
}//处理 多个中⽂的拼⾳组合
int n2 = 0;for (int i = 0; i < list.size(); i++) {
<(i).append(pinyins[n][n2]);
}do{//再复制⼀份 确保list⾥的对象不是相同的
List tempList2 = new ArrayList();for (int i = 0; i < tempList1.size(); i++) {
tempList2.add(new (i).toString()));
}//处理 多个中⽂的拼⾳组合
for (int i = 0; i < tempList2.size(); i++) {
<(i).append(pinyins[n][n2]);
}
list.addAll(tempList2);
}while (pinyins[n].length > ++n2);
}set = new TreeSet();for (int i = 0; i < list.size(); i++) {set.(i).toString());
}
}return set;
}//和获取中⽂全拼⾳相同,这⾥只是获取中⽂拼⾳的⾸字母
private static SetgetPinyinAcronym(String hanzi) {
Set set = null;
String[][] pinyins=getHanyuPinyin(hanzi);if (pinyins != null && pinyins.length > 0) {
List list = new ArrayList();int n = 0;for (int i = 0; i < pinyins[0].length; i++) {
list.add(new String(pinyins[n][i].charAt(0))));
}while (pinyins.length > ++n) {
List tempList1 = new ArrayList();for (int i = 0; i < pinyins[0].length; i++) {
tempList1.add(new (i).toString()));
}int n2 = 0;for (int i = 0; i < list.size(); i++) {
<(i).append(pinyins[n][n2].charAt(0));
}do{
List tempList2 = new ArrayList();for (int i = 0; i < tempList1.size(); i++) {
tempList2.add(new (i).toString()));
}for (int i = 0; i < tempList2.size(); i++) {
<(i).append(pinyins[n][n2].charAt(0));
}
list.addAll(tempList2);
}while (pinyins[n].length > ++n2);
}set = new TreeSet();for (int i = 0; i < list.size(); i++) {set.(i).toString());
}
}return set;
}//把汉字转成拼⾳
private staticString[][] getHanyuPinyin(String hanzi) {
String[][] pinyins= null;//⾃定义转成拼⾳后的模板
HanyuPinyinOutputFormat format = newHanyuPinyinOutputFormat();//设置转后的拼⾳为全⼩写字母
replaceall()format.setCaseType(HanyuPinyinCaseType.LOWERCASE);//⽆⾳调
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//特殊拼⾳(ü)⽤(v)替代
format.setVCharType(HanyuPinyinVCharType.WITH_V);//先初始化⼀维数组
pinyins = newString[hanzi.length()][];for (int i = 0; i < hanzi.length(); i++) {char word =hanzi.charAt(i);try{//如果不是汉字会返回null
String[] temp =HanyuPinyinStringArray(word, format);//去除重复
Set set = new HashSet();for (int j = 0; j < temp.length; j++) {set.add(temp[j]);
}//初始化对应的⼆维数组
pinyins[i] = new String[set.size()];int n = 0;for (String str : set) { pinyins[i][n++] =str;
}
}catch(BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}returnpinyins;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论