使用PinYin4j实现汉字转拼音

 

在项目中,可能使用拼音缩写来完成搜索。比如,以商品查询为例,一般的查询会按照商品编号、商品名称等来查询,另外也可以按照商品名称的拼音缩写以autoComplete的方式来完成搜索。

本文给出一个使用PinYin4j实现汉字转拼音的实例:

    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 final class PinYinGenerator {  
       
        private static final String EMPTY = "";  
       
        /** 大写输出 */  
        private static final HanyuPinyinOutputFormat OUTPUT_FORMAT = new HanyuPinyinOutputFormat();  
        static {  
            OUTPUT_FORMAT.setCaseType(HanyuPinyinCaseType.UPPERCASE);  
            OUTPUT_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        }  
       
        /** 
         * 汉字转拼音的方法 
         * 
         * 如好丽友好多鱼(浓香茄汁)33g转换成HAOLIYOUHAODUOYU(NONGXIANGQIEZHI)33g 
         * 
         * @param chineseCharacters 
         * @return 
         * @throws BadHanyuPinyinOutputFormatCombination 
         */  
        @SuppressWarnings("deprecation")  
        public static String formatToPinYin(String chineseCharacters)  
                throws BadHanyuPinyinOutputFormatCombination {  
            if (null == chineseCharacters || EMPTY.equals(chineseCharacters.trim()))  
                return chineseCharacters;  
       
            return PinyinHelper.toHanyuPinyinString(chineseCharacters,  
                    OUTPUT_FORMAT, EMPTY);  
        }  
       
        /** 
         * 汉字转拼音的方法 
         * 
         * 如:好丽友好多鱼(浓香茄汁)33g转换成HLYHDY(NXQZ)33g 
         * 
         * @param chineseCharacters 
         * @return 
         * @throws BadHanyuPinyinOutputFormatCombination 
         */  
        public static String formatAbbrToPinYin(String chineseCharacters)  
                throws BadHanyuPinyinOutputFormatCombination {  
            if (null == chineseCharacters || EMPTY.equals(chineseCharacters.trim()))  
                return chineseCharacters;  
       
            char[] chars = chineseCharacters.toCharArray();  
            StringBuilder sb = new StringBuilder();  
            for (char c : chars) {  
                if (c > 127) {  
                    sb.append(PinyinHelper.toHanyuPinyinStringArray(c,  
                            OUTPUT_FORMAT)[0].toCharArray()[0]);  
                } else {  
                    sb.append(c);  
                }  
            }  
            return sb.toString();  
        }  
       
    }  

 

测试代码和结果如下:

    import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
       
    public class PinYinTest {  
       
        private static final String BANK_NAME = "中国人民银行";  
        private static final String NAME = "好丽友好多鱼(浓香茄汁)33g";  
       
        public static void main(String[] args)  
                throws BadHanyuPinyinOutputFormatCombination {  
       
            System.out.printf("(Full PinYin) Convert %s to %s\n", NAME,  
                    PinYinGenerator.formatToPinYin(NAME));  
       
            System.out.printf("(Abbr PinYin) Convert %s to %s\n", NAME,  
                    PinYinGenerator.formatAbbrToPinYin(NAME));  
       
            System.out.printf("(Full PinYin) Convert %s to %s\n", BANK_NAME,  
                    PinYinGenerator.formatToPinYin(BANK_NAME));  
       
            System.out.printf("(Abbr PinYin) Convert %s to %s\n", BANK_NAME,  
                    PinYinGenerator.formatAbbrToPinYin(BANK_NAME));  
       
        }  
       
    }  

 

更多例子,请访问 : http://thecodesample.com/

你可能感兴趣的:(java,pinyin4j)