汉字转换拼音工具类:
public class CnConvertSpellUtil {
private static final String WORD_BASE_PATH = "/F02-GB2312-to-PuTongHua-PinYin.txt";
private static HashMap<Character,String> spellMap = null;
static {
if (spellMap == null) {
spellMap = new HashMap<Character,String>(22587);
}
initialize();
}
private CnConvertSpellUtil() {
}
private static void initialize() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File(CnConvertSpellUtil.class
.getResource(WORD_BASE_PATH).getPath())));
String lineContent = reader.readLine();
String[] contentAry = new String[2];
while (StringUtils.isNotBlank(lineContent)) {
if (StringUtils.isBlank(lineContent.trim())) {
continue;
}
contentAry = lineContent.split(",");
if(spellMap.containsKey(contentAry[0].charAt(0))){
String value = spellMap.get(contentAry[0].charAt(0));
spellMap.put(contentAry[0].charAt(0), value + "," +contentAry[1]);
}else{
spellMap.put(contentAry[0].charAt(0), contentAry[1]);
}
lineContent = reader.readLine();
}
} catch (FileNotFoundException e) {
throw new ServiceException("加载 "+WORD_BASE_PATH+" 文件失败!", e);
} catch (IOException e) {
throw new ServiceException("读取 "+WORD_BASE_PATH+" 文件失败!", e);
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
throw new ServiceException("关闭文件失败!", e);
}
}
}
}
public static String[] getFullSpell(String cnStr){
if (StringUtils.isBlank(cnStr)) {
return new String[0];
}
char[] chars = cnStr.toCharArray();
String[] retStrs = new String[chars.length];
for (int i = 0; i < chars.length; i++) {
String spell = spellMap.get(chars[i]);
if(StringUtils.isBlank(spell)){
retStrs[i] = String.valueOf(chars[i]);
continue;
}
// retStrs[i] = spell;
retStrs[i] = StringUtils.substringBefore(spell, ",");
}
return retStrs;
}
public static String getFirstSpell(String cnStr){
if (StringUtils.isBlank(cnStr)) {
return cnStr;
}
char[] chars = cnStr.toCharArray();
StringBuffer retBuf = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
String spell = spellMap.get(chars[i]);
if(StringUtils.isBlank(spell)){
retBuf.append(chars[i]);
continue;
}
if(spell.indexOf(",") != -1){
String[] spells = spell.split(",");
String spellsStr = "";
Set<String> firstSpellSet = new HashSet<String>();
for (int j = 0; j < spells.length; j++) {
String firstSpell = spells[j].substring(0,1);
if(firstSpellSet.contains(firstSpell)){
continue;
}
spellsStr = spellsStr + firstSpell + ",";
firstSpellSet.add(firstSpell);
}
if(firstSpellSet.size() > 1){
// retBuf.append("[" + spellsStr.substring(0,spellsStr.length()-1) + "]");
retBuf.append(StringUtils.substringBefore(spellsStr, ","));
}else{
retBuf.append(spellsStr.substring(0,spellsStr.length()-1));
}
}else{
retBuf.append(spell.substring(0,1));
}
}
return retBuf.toString();
}
public static void main(String[] args) {
// System.out.println(((URL)CnToSpell.class.getResource("/F02-GB2312-to-PuTongHua-PinYin.txt"))
// .getPath());
String str = "安放山豆根反乐对的";
String spellAry = CnConvertSpellUtil.getFirstSpell(str);
String[] spellAry2 = CnConvertSpellUtil.getFullSpell(str);
System.out.println(spellAry);
System.out.println(StringUtils.join(spellAry2));
// for (int i = 0; i < spellAry2.length; i++) {
// System.out.print(spellAry2[i]);
// }
}
}
欢迎大家加入QQ群:258298887 共同学习 共同进步