汉字转拼音pinyinArray——pinyin4j使用记录

pinyin4j :专门用来将JAVA转为汉字的类库

官方网站:http://pinyin4j.sourceforge.net/

建议,直接下载官方网站类库,查看其zip包里面的DEMO程序


介绍下基本理念:

1,中文可能是多音字,所以转换出来的拼音是个string[]

2,转换出来的格式可以使用 Class:HanyuPinyinOutputFormat 来进行格式化,有三个选项:

 

  • VCharType: output format of character 'ü', which has three options. (ü 显示方式)
    • WITH_U_AND_COLON (default)   (显示成lu:)
    • WITH_V                    (显示成 V)
    • WITH_U_UNICODE             (显示成ü)
  • ToneType: output format of Chinese tones, which has three options. (声调显示方式)
    • WITH_TONE_NUMBER (default)            (用数字代表第几声调)
    • WITHOUT_TONE                    (不显示声调)
    • WITH_TONE_MARK                  (用音标显示声调)
  • CaseType: cases of letters in outputted string, which has two options. (大小写)
    • LOWERCASE (default)
    • UPPERCASE
废话不多说,直接上个JAVA类吧:
package com.yajun.pinyin;

import java.util.ArrayList;
import java.util.List;

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

/**
 * 中文转拼音,不显示音标,不需要声调,全部大写,方便保存数据库,进行后续查询使用
 * 
 * @author Administrator
 */
public class PinYinUtil {

    private static HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
    static {
        // 不需要声调
        outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        // 遇到“ü” 显示成V
        outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        // 所有输出大写
        outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    }

    /**
     * @param chineseCharacter 中文
     * @return 中文拼音,如果多音字,返回多个拼音
     */
    public static String[] getPinyin(char chineseCharacter) {
        String[] pinyinArray = null;
        try {
            pinyinArray = PinyinHelper.toHanyuPinyinStringArray(chineseCharacter, outputFormat);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            // can not happen
            e.printStackTrace();
        }
        List<String> pinyins = new ArrayList<String>();
        if (pinyinArray != null) {
            for (String string : pinyinArray) {
                if (!pinyins.contains(string)) {
                    pinyins.add(string);
                }
            }
        }
        return pinyins.toArray(new String[0]);
    }

    /**
     * @param chineseCharacter 中文
     * @return 拼音首字母
     */
    public static Character[] getInitWord(char chineseCharacter) {
        String[] pinyinArray = getPinyin(chineseCharacter);
        Character[] initWords = getInitWord(pinyinArray);
        return initWords;
    }

    private static Character[] getInitWord(String[] pinyinArray) {
        List<Character> characters = new ArrayList<Character>();
        if (pinyinArray != null && pinyinArray.length > 0) {
            for (String string : pinyinArray) {
                char initWord = string.charAt(0);
                if (!characters.contains(initWord)) {
                    characters.add(initWord);
                }
            }
        }
        return characters.toArray(new Character[0]);
    }

    public static void main(String[] args) {
        // 测试获取拼音首字母
        char chineseCharacter = '和';
        String pinyin[] = PinYinUtil.getPinyin(chineseCharacter);
        if (pinyin != null) {
            for (String string : pinyin) {
                System.out.println(string);
            }
        }
        Character[] initWord = PinYinUtil.getInitWord(chineseCharacter);
        if (initWord != null) {
            for (Character character : initWord) {
                System.out.println(character);
            }
        }
    }
}
 

 

你可能感兴趣的:(.net)