使用Pinyin4j進(jìn)行拼音分詞的方法
使用maven引入相關(guān)的jar
<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version> </dependency>
創(chuàng)建Pinyin4jUtil
package com.os.core.util.solr;
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.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
/**
* 漢語(yǔ)拼音工具類
* Created by PengSongHe on 2017/2/9 0009.
*/
public class Pinyin4jUtil {
public static void main(String[] args) {
String str = "測(cè)試";
String pinyin = Pinyin4jUtil.converterToSpell(str);
System.out.println(str + " pin yin :" + pinyin);
pinyin = Pinyin4jUtil.converterToFirstSpell(str);
System.out.println(str + " short pin yin :" + pinyin);
}
/**
* 漢字轉(zhuǎn)換位漢語(yǔ)拼音首字母,英文字符不變,特殊字符丟失 支持多音字,生成方式如(長(zhǎng)沙市長(zhǎng):cssc,zssz,zssc,cssz)
*
* @param chines 漢字
* @return 拼音
*/
public static String converterToFirstSpell(String chines) {
StringBuffer pinyinName = new StringBuffer();
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
// 取得當(dāng)前漢字的所有全拼
String[] strs = PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat);
if (strs != null) {
for (int j = 0; j < strs.length; j++) {
// 取首字母
pinyinName.append(strs[j].charAt(0));
if (j != strs.length - 1) {
pinyinName.append(",");
}
}
}
// else {
// pinyinName.append(nameChar[i]);
// }
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName.append(nameChar[i]);
}
pinyinName.append(" ");
}
// return pinyinName.toString();
return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
}
/**
* 漢字轉(zhuǎn)換位漢語(yǔ)全拼,英文字符不變,特殊字符丟失
* 支持多音字,生成方式如(重當(dāng)參:zhongdangcen,zhongdangcan,chongdangcen
* ,chongdangshen,zhongdangshen,chongdangcan)
*
* @param chines 漢字
* @return 拼音
*/
public static String converterToSpell(String chines) {
StringBuffer pinyinName = new StringBuffer();
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
// 取得當(dāng)前漢字的所有全拼
String[] strs = PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat);
if (strs != null) {
for (int j = 0; j < strs.length; j++) {
pinyinName.append(strs[j]);
if (j != strs.length - 1) {
pinyinName.append(",");
}
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName.append(nameChar[i]);
}
pinyinName.append(" ");
}
// return pinyinName.toString();
return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
}
/**
* 去除多音字重復(fù)數(shù)據(jù)
*
* @param theStr
* @return
*/
private static List<Map<String, Integer>> discountTheChinese(String theStr) {
// 去除重復(fù)拼音后的拼音列表
List<Map<String, Integer>> mapList = new ArrayList<Map<String, Integer>>();
// 用于處理每個(gè)字的多音字,去掉重復(fù)
Map<String, Integer> onlyOne = null;
String[] firsts = theStr.split(" ");
// 讀出每個(gè)漢字的拼音
for (String str : firsts) {
onlyOne = new Hashtable<String, Integer>();
String[] china = str.split(",");
// 多音字處理
for (String s : china) {
Integer count = onlyOne.get(s);
if (count == null) {
onlyOne.put(s, new Integer(1));
} else {
onlyOne.remove(s);
count++;
onlyOne.put(s, count);
}
}
mapList.add(onlyOne);
}
return mapList;
}
/**
* 解析并組合拼音,對(duì)象合并方案(推薦使用)
*
* @return
*/
private static String parseTheChineseByObject(
List<Map<String, Integer>> list) {
Map<String, Integer> first = null; // 用于統(tǒng)計(jì)每一次,集合組合數(shù)據(jù)
// 遍歷每一組集合
for (int i = 0; i < list.size(); i++) {
// 每一組集合與上一次組合的Map
Map<String, Integer> temp = new Hashtable<String, Integer>();
// 第一次循環(huán),first為空
if (first != null) {
// 取出上次組合與此次集合的字符,并保存
for (String s : first.keySet()) {
for (String s1 : list.get(i).keySet()) {
String str = s + s1;
temp.put(str, 1);
}
}
// 清理上一次組合數(shù)據(jù)
if (temp != null && temp.size() > 0) {
first.clear();
}
} else {
for (String s : list.get(i).keySet()) {
String str = s;
temp.put(str, 1);
}
}
// 保存組合數(shù)據(jù)以便下次循環(huán)使用
if (temp != null && temp.size() > 0) {
first = temp;
}
}
String returnStr = "";
if (first != null) {
// 遍歷取出組合字符串
for (String str : first.keySet()) {
returnStr += (str + ",");
}
}
if (returnStr.length() > 0) {
returnStr = returnStr.substring(0, returnStr.length() - 1);
}
return returnStr;
}
}
以上這篇使用Pinyin4j進(jìn)行拼音分詞的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot @ConfigurationProperties使用詳解
這篇文章主要介紹了SpringBoot @ConfigurationProperties使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Java下利用Jackson進(jìn)行JSON解析和序列化示例
本篇文章主要介紹了Java下利用Jackson進(jìn)行JSON解析和序列化示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
springboot多環(huán)境配置文件及自定義配置文件路徑詳解
這篇文章主要介紹了springboot多環(huán)境配置文件及自定義配置文件路徑,文中給大家介紹了classpath的基本概念講解及自定義springboot配置文件路徑的相關(guān)知識(shí),需要的朋友可以參考下2023-02-02
Springboot 整合 Java DL4J 實(shí)現(xiàn)農(nóng)產(chǎn)品質(zhì)量檢測(cè)系統(tǒng)(推薦)
本文詳細(xì)介紹了系統(tǒng)的搭建過(guò)程,包括技術(shù)選型、數(shù)據(jù)處理、模型訓(xùn)練和評(píng)估等關(guān)鍵步驟,系統(tǒng)采用卷積神經(jīng)網(wǎng)絡(luò),對(duì)水果成熟度和缺陷進(jìn)行識(shí)別,有效解決了傳統(tǒng)方法成本高、效率低的問(wèn)題,有助于提升農(nóng)產(chǎn)品檢測(cè)的科技含量和自動(dòng)化水平2024-10-10
SpringSecurity表單配置之登錄成功及頁(yè)面跳轉(zhuǎn)原理解析
這篇文章主要介紹了SpringSecurity表單配置之登錄成功及頁(yè)面跳轉(zhuǎn)原理解析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12
java 運(yùn)行報(bào)錯(cuò)has been compiled by a more recent version of the J
java 運(yùn)行報(bào)錯(cuò)has been compiled by a more recent version of the Java Runtime (class file version 54.0)2021-04-04

