[pinyin4j] java版漢字轉換拼音(大小寫)

pinyin4J 是一個可以將漢字轉換成拼音的lib,非常實用,其maven地址爲:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0


pinyin4J 提供PinyinHelper這個靜態類對外提供拼音轉換的服務,主要有一下方法:

static public String[] toHanyuPinyinStringArray(char ch)

將char(必須爲漢字單字)轉化爲拼音,實用的是通用的格式,如果ch爲非漢字,返回null。

輸入:重 輸出:[zhong4, chong2] 說明重字有兩個讀音,拼音後面的1,2,3,4 代表的是讀音


static public String[] toHanyuPinyinStringArray(char ch,HanyuPinyinOutputFormat outputFormat)

同上,但是這個方法可以設置輸出的格式。HanyuPinyinOutputFormat   可以設置拼音大小寫、是否後面加讀音數字、特殊讀音的顯示方式,定義如下:

    /**
     * The option indicates that the output of 'ü' is "u:"
     */
    public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");
    /**
     * The option indicates that the output of 'ü' is "v"
     */
    public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")
    /**
     * The option indicates that the output of 'ü' is "ü" in Unicode form
     */
    public static final HanyuPinyinVCharType WITH_U_UNICODE = new HanyuPinyinVCharType("WITH_U_UNICODE");


static public String[] toTongyongPinyinStringArray(char ch)

轉換爲通用拼音。通用拼音的介紹見:http://zh.wikipedia.org/zh-cn/%E9%80%9A%E7%94%A8%E6%8B%BC%E9%9F%B3


static public String[] toWadeGilesPinyinStringArray(char ch)

轉換爲威妥瑪拼音:http://zh.wikipedia.org/wiki/%E5%A8%81%E5%A6%A5%E7%91%AA%E6%8B%BC%E9%9F%B3


static public String[] toMPS2PinyinStringArray(char ch)

轉換爲注音符號拼音:http://zh.wikipedia.org/zh-cn/%E6%B3%A8%E9%9F%B3%E7%AC%A6%E8%99%9F


static public String[] toYalePinyinStringArray(char ch)

轉換爲耶魯拼音:http://zh.wikipedia.org/zh-cn/%E8%80%B6%E9%AD%AF%E6%8B%BC%E9%9F%B3


static public String[] toGwoyeuRomatzyhStringArray(char ch)

轉換爲國語羅馬字:http://zh.wikipedia.org/wiki/%E5%9C%8B%E8%AA%9E%E7%BE%85%E9%A6%AC%E5%AD%97


對於”重“的拼音轉換,以上方法分別得到的結果是:

漢語拼音:[zhong4, chong2]
通用拼音:[jhong4, chong2]
威妥瑪拼音:[chung4, ch`ung2]
注音符號拼音:[jung4, chung2]
耶魯拼音:[jung4, chung2]
國語羅馬字:[jonq, chorng]

好了,有了上面的基礎,我們可以封裝一個工具類,用來將漢字轉換成拼音,這裏只使用了漢字拼音。

首先要將pinyin4j加入項目中,如果是maven項目,可以添加引用:

<span style="white-space:pre">	</span><!-- 增加pinyin4j -->
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.0</version>
        </dependency>
非maven的可以直接將下載好的jar包放入classpath。

然後編寫工具類 PinyinTool.java:

package org.nerve.d3lesson.common.tools;

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;

import java.util.Arrays;

/**
 *
 * Created by zengxm on 2014/12/4.
 */
public class PinyinTool {
    HanyuPinyinOutputFormat format = null;
    public static enum Type {
        UPPERCASE,              //全部大寫
        LOWERCASE,              //全部小寫
        FIRSTUPPER              //首字母大寫
    }

    public PinyinTool(){
        format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }

    public String toPinYin(String str) throws BadHanyuPinyinOutputFormatCombination{
        return toPinYin(str, "", Type.UPPERCASE);
    }

    public String toPinYin(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{
        return toPinYin(str, spera, Type.UPPERCASE);
    }

    /**
     * 將str轉換成拼音,如果不是漢字或者沒有對應的拼音,則不作轉換
     * 如: 明天 轉換成 MINGTIAN
     * @param str
     * @param spera
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
        if(str == null || str.trim().length()==0)
            return "";
        if(type == Type.UPPERCASE)
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        else
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

        String py = "";
        String temp = "";
        String[] t;
        for(int i=0;i<str.length();i++){
            char c = str.charAt(i);
            if((int)c <= 128)
                py += c;
            else{
                t = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if(t == null)
                    py += c;
                else{
                    temp = t[0];
                    if(type == Type.FIRSTUPPER)
                        temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
                    py += temp+(i==str.length()-1?"":spera);
                }
            }
        }
        return py.trim();
    }
}

寫個測試用例看看結果:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章