java漢字轉(zhuǎn)拼音工具類分享
本文實(shí)例為大家分享了java漢字轉(zhuǎn)拼音工具類的具體代碼,供大家參考,具體內(nèi)容如下
import com.google.common.base.Strings; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.TreeSet; public class PinyinUtils { private static final Logger logger = LoggerFactory.getLogger(PinyinUtils.class); /** * 單字解析 * * @param str first * @return */ public static String[] convert(String str) { String[] reslut = null; HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat(); hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); try { reslut = PinyinHelper.toHanyuPinyinStringArray(str.charAt(0), hanyuPinyinOutputFormat); TreeSet<String> stringTreeSet = new TreeSet<>(); for (int i = 0; i < reslut.length; i++) { if(reslut.length >=3) { break; } stringTreeSet.add(reslut[i].replace("u:","v")); } reslut = new String[stringTreeSet.size()]; reslut = stringTreeSet.toArray(reslut); } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) { badHanyuPinyinOutputFormatCombination.printStackTrace(); } catch (Exception e) { logger.error("[convert]: ", e); } return reslut; } /** * 詞組解析(全寫) * * @param chs * @return */ public static String getSelling(String chs) { return translate(chs, false); } /** * 漢字轉(zhuǎn)拼音 * * @param chs * @param acronym * @return */ private static String translate(String chs, boolean acronym) { StringBuffer buffer=new StringBuffer(); if (Strings.isNullOrEmpty(chs)) return ""; try { List<List<String>> temps = new ArrayList<>(); int len = chs.length(); int len1 = 0; for (int i = 0; i < len; i++) { List<String> stringList = new ArrayList<>(); String key = chs.charAt(i) + ""; if (key.getBytes().length >= 2) { String[] temp = convert(key); if(temp.length == 0) { continue; } if (temp == null) { stringList.add(""); } else { for (String v : temp) { stringList.add(v); } } } else { stringList.add(key); } temps.add(stringList); len1++; } List<List<String>> t = new ArrayList<>(); for (int i = 0; i < len1; i++) { List<String> currentList = new ArrayList<>(); List<String> stringList = temps.get(i); if (stringList != null) { for (String s : stringList) { if (acronym) { s = s.charAt(0) + ""; } if (i > 0) { List<String> preList = t.get(i - 1); if (preList != null) { for (String s1 : preList) { currentList.add(s1 + s); } } }else{ currentList.add(s); } } } t.add(i, currentList); } if (t.size()>0){ List<String> currentList= t.get(t.size()-1); if (currentList!=null){ for(String current : currentList){ buffer.append(current); buffer.append(""); } } } return buffer.toString(); } catch (Exception e) { logger.error("[getSortLetters]: ", e); return ""; } } /** * 詞組解析(縮寫) * * @param chs * @return */ public static String getSmallSelling(String chs) { return translate(chs, true); } /** * 獲取首字母 * * @return */ public static String getSortLetters(String pingyin) { try { String sortString = pingyin.substring(0, 1).toUpperCase(Locale.getDefault()); // 正則表達(dá)式,判斷首字母是否是英文字母 if (sortString.matches("[A-Z]")) { return sortString.toUpperCase(Locale.getDefault()); } } catch (Exception e) { logger.error("[getSortLetters]: ", e); } return "#"; } public static void main(String [] args) { PinyinUtils p = new PinyinUtils(); System.out.println(p.getSelling("單個(gè)")); System.out.println(p.getSmallSelling("測(cè)試")); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JAVA實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能代碼實(shí)例
- Java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例
- Java漢字轉(zhuǎn)拼音pinyin4j用法詳解
- Java漢字轉(zhuǎn)拼音類庫(kù)Pinyin4j詳細(xì)使用方法與實(shí)例
- Java漢字轉(zhuǎn)成漢語(yǔ)拼音工具類
- Java獲取漢字對(duì)應(yīng)的拼音(全拼或首字母)
- Java 實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音的實(shí)例
- java實(shí)現(xiàn)漢字轉(zhuǎn)拼音
- Java中漢字轉(zhuǎn)拼音pinyin4j用法實(shí)例分析
- Java實(shí)現(xiàn)將漢字轉(zhuǎn)化為漢語(yǔ)拼音的方法
- java 根據(jù)漢字生成拼音全拼或拼音首字母的示例
相關(guān)文章
JAVA面試題之緩存擊穿、緩存穿透、緩存雪崩的三者區(qū)別
當(dāng)服務(wù)器QPS比較高,并且對(duì)數(shù)據(jù)的實(shí)時(shí)性要求不高時(shí),往往會(huì)接入緩存以達(dá)到快速Response、降低數(shù)據(jù)庫(kù)壓力的作用,常用來做緩存的中間件如Redis等。本文主要介紹了JAVA面試時(shí)??嫉木彺鎿舸⒋┩?、雪崩場(chǎng)景三者區(qū)別,有興趣的小伙伴可以看一下2021-11-11spring boot項(xiàng)目fat jar瘦身的實(shí)現(xiàn)
這篇文章主要介紹了spring boot項(xiàng)目fat jar瘦身的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06Spring Boot之搞定mongoTemplate的知識(shí)小結(jié)
這篇文章主要介紹了Spring Boot之搞定mongoTemplate的知識(shí)小結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12EL調(diào)用Java方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
簡(jiǎn)單來說,我們?cè)谝粋€(gè)類中的某個(gè)方法,可以使用EL進(jìn)行調(diào)用,這個(gè)能被EL表達(dá)式調(diào)用的方法稱之為EL函數(shù),但是這種方式必須滿足兩點(diǎn)要求,具體哪兩點(diǎn),大家可以參考下本文2017-07-07一文解決pom.xml報(bào)錯(cuò)Dependency "xxx" not f
我們?cè)谑褂胢aven進(jìn)行jar包管理時(shí)有時(shí)會(huì)遇到pom.xml中報(bào)錯(cuò)Dependency “XXX” not found,所以在本文中將給大家介紹一下pom.xml報(bào)錯(cuò)Dependency "xxx" not found的解決方案,需要的朋友可以參考下2024-01-01關(guān)于@Autowired注解和靜態(tài)方法及new的關(guān)系
這篇文章主要介紹了關(guān)于@Autowired注解和靜態(tài)方法及new的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02springcloud使用profile實(shí)現(xiàn)多環(huán)境配置方式
這篇文章主要介紹了springcloud使用profile實(shí)現(xiàn)多環(huán)境配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03