最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)戰(zhàn)之城市多音字處理

 更新時(shí)間:2021年04月28日 15:00:01   作者:litGrey  
這篇文章主要介紹了Java實(shí)戰(zhàn)之城市多音字處理,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下

一、需求

對(duì)城市名稱(chēng)轉(zhuǎn)化為拼音的時(shí)候,當(dāng)遇到多音字城市的時(shí)候,轉(zhuǎn)化拼音就不是我們想要的了。

使用  pinyin4j 無(wú)法直接解決這個(gè)問(wèn)題。 。網(wǎng)上有很多維護(hù)多音字信息的,覺(jué)得麻煩。

如:

長(zhǎng)沙 ====>"zhangsha"  
廈門(mén)===>"shamen"  
重慶===>"zhongqing"

二、導(dǎo)入 jpinyin

版本自選

<!-- https://mvnrepository.com/artifact/com.github.stuxuhai/jpinyin -->
        <dependency>
            <groupId>com.github.stuxuhai</groupId>
            <artifactId>jpinyin</artifactId>
            <version>1.1.8</version>
        </dependency>

三、直接創(chuàng)建工具類(lèi)(不需要其他操作)

import com.github.stuxuhai.jpinyin.ChineseHelper;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
 
/**
 * @Description:
 * @Date: 2021/4/27 16:26
 * @Author: luch
 * @Version: 1.0
 **/
public class ChangeToPinYinJPinYin {
 
 
    /**
     * 轉(zhuǎn)換為有聲調(diào)的拼音字符串
     *
     * @param pinYinStr 漢字
     * @return 有聲調(diào)的拼音字符串
     */
    public static String changeToMarkPinYin(String pinYinStr) {
 
        String tempStr = null;
 
        try {
            tempStr = PinyinHelper.convertToPinyinString(pinYinStr, " ", PinyinFormat.WITH_TONE_MARK);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempStr;
 
    }
 
 
    /**
     * 轉(zhuǎn)換為數(shù)字聲調(diào)字符串
     *
     * @param pinYinStr 需轉(zhuǎn)換的漢字
     * @return 轉(zhuǎn)換完成的拼音字符串
     */
    public static String changeToNumberPinYin(String pinYinStr) {
 
        String tempStr = null;
 
        try {
            tempStr = PinyinHelper.convertToPinyinString(pinYinStr, " ", PinyinFormat.WITH_TONE_NUMBER);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return tempStr;
 
    }
 
    /**
     * 轉(zhuǎn)換為不帶音調(diào)的拼音字符串
     *
     * @param pinYinStr 需轉(zhuǎn)換的漢字
     * @return 拼音字符串
     */
    public static String changeToTonePinYin(String pinYinStr) {
 
        String tempStr = null;
 
        try {
            tempStr = PinyinHelper.convertToPinyinString(pinYinStr, "", PinyinFormat.WITHOUT_TONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempStr;
 
    }
 
    /**
     * 轉(zhuǎn)換為每個(gè)漢字對(duì)應(yīng)拼音首字母字符串
     *
     * @param pinYinStr 需轉(zhuǎn)換的漢字
     * @return 拼音字符串
     */
    public static String changeToGetShortPinYin(String pinYinStr) {
 
        String tempStr = null;
 
        try {
            tempStr = PinyinHelper.getShortPinyin(pinYinStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempStr;
 
    }
 
    /**
     * 檢查漢字是否為多音字
     *
     * @param pinYinStr 需檢查的漢字
     * @return true 多音字,false 不是多音字
     */
    public static boolean checkPinYin(char pinYinStr) {
 
        boolean check = false;
        try {
            check = PinyinHelper.hasMultiPinyin(pinYinStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }
 
    /**
     * 簡(jiǎn)體轉(zhuǎn)換為繁體
     *
     * @param pinYinStr
     * @return
     */
    public static String changeToTraditional(String pinYinStr) {
 
        String tempStr = null;
        try {
            tempStr = ChineseHelper.convertToTraditionalChinese(pinYinStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempStr;
 
    }
 
    /**
     * 繁體轉(zhuǎn)換為簡(jiǎn)體
     *
     * @param pinYinSt
     * @return
     */
    public static String changeToSimplified(String pinYinSt) {
 
        String tempStr = null;
 
        try {
            tempStr = ChineseHelper.convertToSimplifiedChinese(pinYinSt);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return tempStr;
 
    }
 
 
 
}

四、直接測(cè)試代碼

public static void main(String[] args) {
        String str = "長(zhǎng)沙市";
        System.out.println("轉(zhuǎn)換為有聲調(diào)的拼音字符串:"+changeToMarkPinYin(str));
        System.out.println("轉(zhuǎn)換為不帶音調(diào)的拼音字符串:"+changeToTonePinYin(str));
 
        String strFanTi="誰(shuí)是程序員";
        System.out.println("繁體轉(zhuǎn)換為簡(jiǎn)體:"+changeToSimplified(strFanTi));
        System.out.println("重"+"重是否是多音字:"+checkPinYin('重'));
        System.out.println("廈"+"是否是多音字:"+checkPinYin('廈'));
        System.out.println("鼠"+"是否是多音字:"+checkPinYin('鼠'));
    }

輸出結(jié)果,下面不同字的中間是否有空格,這個(gè)可以自己設(shè)置的

五、源碼分析

5.1 字典對(duì)應(yīng)信息

分別是:從上到下分別是

1.繁體-簡(jiǎn)體對(duì)應(yīng)表

2.多音字

3.帶音標(biāo)的拼音

5.2 6個(gè)處理類(lèi)

5.3 處理模式

點(diǎn)開(kāi)

com.github.stuxuhai.jpinyin.PinyinResource#getPinyinResource

有三個(gè)方法分別加載了這幾個(gè)資源

然后通過(guò)

com.github.stuxuhai.jpinyin.ChineseHelper 

 通過(guò)一個(gè)鍵值對(duì)獲取。加載處理而已。

到此這篇關(guān)于Java實(shí)戰(zhàn)之城市多音字處理的文章就介紹到這了,更多相關(guān)java城市多音字處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

鸡西市| 监利县| 惠东县| 瑞金市| 涿鹿县| 巫溪县| 施甸县| 宁都县| 冀州市| 宁陵县| 义乌市| 从化市| 永寿县| 奈曼旗| 邳州市| 淅川县| 合江县| 邓州市| 兰溪市| 淮滨县| 无棣县| 花垣县| 松滋市| 迁安市| 北辰区| 孝义市| 广平县| 云林县| 黄平县| 合川市| 防城港市| 永善县| 通化县| 东方市| 股票| 台北市| 五指山市| 苏尼特右旗| 九龙县| 通山县| 嵩明县|