한자 병 음 변환 (다 음 자 지원)

7515 단어 자바
pom 파일 pinyin4j 도입

    
	    com.belerweb
	    pinyin4j
	    2.5.1
	
package com.toutiao_efficiency.modules.documet_file.service;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

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.exception.BadHanyuPinyinOutputFormatCombination;

public class Pinyin4jUtil {    
    
    /**  
     *             ,      ,            ,     (    :cssc,zssz,zssc,cssz)  
     *   
     * @param chines  
     *                
     * @return     
     */    
    public static String converterToFirstSpell(String chines) {    
        StringBuffer pinyinName = new StringBuffer();    
        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 {    
                    //                
                    String[] strs = PinyinHelper.toHanyuPinyinStringArray(    
                            nameChar[i], defaultFormat);    
                    if (strs != null) {    
                        for (int j = 0; j < strs.length; j++) {    
                            //         
                            pinyinName.append(strs[j].charAt(0));    
                            if (j != strs.length - 1) {    
                                pinyinName.append(",");    
                            }    
                        }    
                    }    
                    // else {    
                    // pinyinName.append(nameChar[i]);    
                    // }    
                } catch (BadHanyuPinyinOutputFormatCombination e) {    
                    e.printStackTrace();    
                }    
            } else {    
                pinyinName.append(nameChar[i]);    
            }    
            pinyinName.append(" ");    
        }    
        // return pinyinName.toString();    
        return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));    
    }    
    
    /**  
     *          ,      ,        
     *      ,     (   :zhongdangcen,zhongdangcan,chongdangcen  
     * ,chongdangshen,zhongdangshen,chongdangcan)  
     *   
     * @param chines  
     *                
     * @return     
     */    
    public static String converterToSpell(String chines) {    
        StringBuffer pinyinName = new StringBuffer();    
        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 {    
                    //                
                    String[] strs = PinyinHelper.toHanyuPinyinStringArray(    
                            nameChar[i], defaultFormat);    
                    if (strs != null) {    
                        for (int j = 0; j < strs.length; j++) {    
                            pinyinName.append(strs[j]);    
                            if (j != strs.length - 1) {    
                                pinyinName.append(",");    
                            }    
                        }    
                    }    
                } catch (BadHanyuPinyinOutputFormatCombination e) {    
                    e.printStackTrace();    
                }    
            } else {    
                pinyinName.append(nameChar[i]);    
            }    
            pinyinName.append(" ");    
        }    
        // return pinyinName.toString();    
        return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));    
    }    
    
    /**  
     *            
     *   
     * @param theStr  
     * @return  
     */    
    private static List> discountTheChinese(String theStr) {    
        //                 
        List> mapList = new ArrayList>();    
        //            ,        
        Map onlyOne = null;    
        String[] firsts = theStr.split(" ");    
        //              
        for (String str : firsts) {    
            onlyOne = new Hashtable();    
            String[] china = str.split(",");    
            //          
            for (String s : china) {    
                Integer count = onlyOne.get(s);    
                if (count == null) {    
                    onlyOne.put(s, new Integer(1));    
                } else {    
                    onlyOne.remove(s);    
                    count++;    
                    onlyOne.put(s, count);    
                }    
            }    
            mapList.add(onlyOne);    
        }    
        return mapList;    
    }    
    
    /**  
     *        ,      (    )  
     *   
     * @return  
     */    
    private static String parseTheChineseByObject(    
            List> list) {    
        Map first = null; //        ,          
        //            
        for (int i = 0; i < list.size(); i++) {    
            //             Map    
            Map temp = new Hashtable();    
            //      ,first      
            if (first != null) {    
                //               ,       
                for (String s : first.keySet()) {    
                    for (String s1 : list.get(i).keySet()) {    
                        String str = s + s1;    
                        temp.put(str, 1);    
                    }    
                }    
                //              
                if (temp != null && temp.size() > 0) {    
                    first.clear();    
                }    
            } else {    
                for (String s : list.get(i).keySet()) {    
                    String str = s;    
                    temp.put(str, 1);    
                }    
            }    
            //                   
            if (temp != null && temp.size() > 0) {    
                first = temp;    
            }    
        }    
        String returnStr = "";    
        if (first != null) {    
            //              
            for (String str : first.keySet()) {    
                returnStr += (str + ",");    
            }    
        }    
        if (returnStr.length() > 0) {    
            returnStr = returnStr.substring(0, returnStr.length() - 1);    
        }    
        return returnStr;    
    }    
    
    public static void main(String[] args) {
    	System.out.println(Pinyin4jUtil.converterToSpell("   "));
	}
}    

좋은 웹페이지 즐겨찾기