Java實戰(zhàn)之城市多音字處理
一、需求
對城市名稱轉(zhuǎn)化為拼音的時候,當(dāng)遇到多音字城市的時候,轉(zhuǎn)化拼音就不是我們想要的了。
使用 pinyin4j 無法直接解決這個問題。 。網(wǎng)上有很多維護(hù)多音字信息的,覺得麻煩。
如:
長沙 ====>"zhangsha" 廈門===>"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)建工具類(不需要其他操作)
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)換為每個漢字對應(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; } /** * 簡體轉(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)換為簡體 * * @param pinYinSt * @return */ public static String changeToSimplified(String pinYinSt) { String tempStr = null; try { tempStr = ChineseHelper.convertToSimplifiedChinese(pinYinSt); } catch (Exception e) { e.printStackTrace(); } return tempStr; } }
四、直接測試代碼
public static void main(String[] args) { String str = "長沙市"; System.out.println("轉(zhuǎn)換為有聲調(diào)的拼音字符串:"+changeToMarkPinYin(str)); System.out.println("轉(zhuǎn)換為不帶音調(diào)的拼音字符串:"+changeToTonePinYin(str)); String strFanTi="誰是程序員"; System.out.println("繁體轉(zhuǎn)換為簡體:"+changeToSimplified(strFanTi)); System.out.println("重"+"重是否是多音字:"+checkPinYin('重')); System.out.println("廈"+"是否是多音字:"+checkPinYin('廈')); System.out.println("鼠"+"是否是多音字:"+checkPinYin('鼠')); }
輸出結(jié)果,下面不同字的中間是否有空格,這個可以自己設(shè)置的
五、源碼分析
5.1 字典對應(yīng)信息
分別是:從上到下分別是
1.繁體-簡體對應(yīng)表
2.多音字
3.帶音標(biāo)的拼音
5.2 6個處理類
5.3 處理模式
點開
com.github.stuxuhai.jpinyin.PinyinResource#getPinyinResource
有三個方法分別加載了這幾個資源
然后通過
com.github.stuxuhai.jpinyin.ChineseHelper
通過一個鍵值對獲取。加載處理而已。
到此這篇關(guān)于Java實戰(zhàn)之城市多音字處理的文章就介紹到這了,更多相關(guān)java城市多音字處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java多線程編程之使用thread類創(chuàng)建線程
在Java中創(chuàng)建線程有兩種方法:使用Thread類和使用Runnable接口。在使用Runnable接口時需要建立一個Thread實例2014-01-01基于Java+SpringBoot實現(xiàn)人臉識別搜索
人臉識別搜索技術(shù)作為現(xiàn)代計算機視覺領(lǐng)域的重要研究方向之一,已經(jīng)在多個領(lǐng)域展現(xiàn)出巨大的應(yīng)用潛力,隨著信息技術(shù)的飛速發(fā)展,人臉識別搜索在多個領(lǐng)域得到了廣泛關(guān)注和應(yīng)用,本文旨在探討人臉識別搜索技術(shù)的背景、原理以及其在實際應(yīng)用中的意義和挑戰(zhàn)2023-08-08Spring?Boot中使用Spring?Retry重試框架的操作方法
這篇文章主要介紹了Spring?Retry?在SpringBoot?中的應(yīng)用,介紹了RetryTemplate配置的時候,需要設(shè)置的重試策略和退避策略,需要的朋友可以參考下2022-04-04Java C++題解leetcode 1684統(tǒng)計一致字符串的數(shù)目示例
這篇文章主要為大家介紹了Java C++題解leetcode 1684統(tǒng)計一致字符串的數(shù)目示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Java實現(xiàn)獲取前、后N天日期的函數(shù)分享
本文給大家分享的是使用java實現(xiàn)的獲取當(dāng)前日期前后N天的函數(shù),非常的簡單實用,有需要的小伙伴可以參考下。2015-03-03