当前位置:网站首页>汉字转拼音
汉字转拼音
2022-08-05 02:32:00 【斧头湖懒客】
一,pinyin4j 工具包
汉字转拼音我们需要使用到pinyin4j这个工具jar包,它可以处理中文转换成拼音(汉语拼音,罗马拼音等)
1,常用类介绍
PinyinHelper 提供了汉字转拼音的主要方法
HanyuPinyinOutputFormat 定义如何输出拼音,
HanyuPinyinCaseType 提供了拼音输出的样式
- LOWERCASE:输出小写,
- UPPERCASE:输出大写
HanyuPinyinToneType 输出音标的设置
- WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常),
- WITH_TONE_NUMBER:1-4数字表示音标,
- WITHOUT_TONE:没有音标
HanyuPinyinVCharType 特殊音标ü的设置(了解下)
- WITH_V:用v表示ü,
- WITH_U_AND_COLON:用"u:"表示ü,
- WITH_U_UNICODE:直接用ü
二,使用pinyin4j
1,引用jar包
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
2,写一个工具类
工具类中的两个方法可以合在一起
/** * @description:汉字转拼音工具类 */
public class HanZiToPinYinUtil {
public static void main(String[] args) {
String allPinYin = getAllPinYin("我是中国人00");
String firstPinYin = getFirstPinYin("我是中国人00");
String allPinYin2 = getAllPinYin("王宇");
System.out.println(allPinYin);
System.out.println(allPinYin2);
System.out.println(firstPinYin);
}
/** * 获取汉字字符串的全拼音 * @param hanZi '我是中国人00' * @return */
public static String getAllPinYin(String hanZi){
//这个类定义了如何输出汉语拼音。
HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//小写形式输出,LOWERCASE:输出小写,UPPERCASE:输出大写
pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);//输出音标设置,WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常),WITH_TONE_NUMBER:1-4数字表示音标,WITHOUT_TONE:没有音标
pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);//特殊音标ü的设置,(WITH_V:用v表示ü,WITH_U_AND_COLON:用"u:"表示ü,WITH_U_UNICODE:直接用ü)
char[] hanziChars = hanZi.trim().toCharArray();//汉字数组,去掉前后多余的空格
StringBuilder stringBuilder = new StringBuilder();
try {
for (int i = 0; i < hanziChars.length; i++) {
String hanziString = Character.toString(hanziChars[i]);//得到单个汉字,例如‘中’
if(hanziString.matches("[\\u4e00-\\u9fa5]")){
//判断是否是汉字
String[] pinYinAll = PinyinHelper.toHanyuPinyinStringArray(hanziChars[i],pinyinOutputFormat);//得到此汉字的所有读音,例如中是一个多音字,其结果就是['zhōng','zhòng']
String oneHanZiPinYin = pinYinAll[0];//直接取一个拼音为汉字的拼音,例如zhōng
stringBuilder.append(oneHanZiPinYin);
}else {
//不是汉字原样输出
stringBuilder.append(hanziString);
}
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
String allPinYin = stringBuilder.toString();//返回中文字符串的全拼,例如:wŏshìzhōngzhòngguórén00
return allPinYin;
}
/** * 获取汉字字符串的首字母拼音 * @param hanZi '我是中国人00' * @return */
public static String getFirstPinYin(String hanZi){
//这个类定义了如何输出汉语拼音。
HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//小写形式输出,LOWERCASE:输出小写,UPPERCASE:输出大写
pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);//输出音标设置,WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常),WITH_TONE_NUMBER:1-4数字表示音标,WITHOUT_TONE:没有音标
pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);//特殊音标ü的设置,(WITH_V:用v表示ü,WITH_U_AND_COLON:用"u:"表示ü,WITH_U_UNICODE:直接用ü)
char[] hanziChars = hanZi.trim().toCharArray();//去掉前后多余的空格,转为汉字数组
StringBuilder stringBuilder = new StringBuilder();
try {
for (int i = 0; i < hanziChars.length; i++) {
String hanziString = Character.toString(hanziChars[i]);//得到单个汉字,例如‘中’
if(hanziString.matches("[\\u4e00-\\u9fa5]")){
//判断是否是汉字
String[] pinYinAll = PinyinHelper.toHanyuPinyinStringArray(hanziChars[i],pinyinOutputFormat);//得到此汉字的所有读音,例如中是一个多音字,其结果就是['zhōng','zhòng']
String firstString = Character.toString(pinYinAll[0].charAt(0));//去第一个拼音的第一个字符为汉字首字母
stringBuilder.append(firstString);
}else {
//不是汉字原样输出
stringBuilder.append(hanziString);
}
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
String allPinYin = stringBuilder.toString();//返回中文字符串的首字母拼音
return allPinYin;
}
}
输出结果
wŏshìzhōngguórén00
wángyŭ
wszgr00
边栏推荐
- How do programmers without objects spend the Chinese Valentine's Day
- [机缘参悟-60]:《兵者,诡道也》-2-孙子兵法解读
- Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
- 短域名绕过及xss相关知识
- C student management system Find student nodes based on student ID
- Hypervisor related knowledge points
- nodeJs--封装路由
- fragment可见性判断
- 关于#sql shell#的问题,如何解决?
- 从零到一快速学会三子棋
猜你喜欢

Quickly learn chess from zero to one

Using OpenVINO to implement the flying paddle version of the PGNet inference program

LeetCode uses the minimum cost to climb the stairs----dp problem

云原生(三十二) | Kubernetes篇之平台存储系统介绍

nodeJs--encapsulate routing

2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!

没有对象的程序员如何过七夕

“嘀哩哩,等灯等灯”,工厂安全生产的提示音

树形查找(二叉查找树)

J9数字货币论:web3的创作者经济是什么?
随机推荐
Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
浅谈数据安全治理与隐私计算
用@Mapper查询oracle的分区情况报错
甘特图来啦,项目管理神器,模板直接用
意识形态的机制
js中try...catch和finally的用法
RAID磁盘阵列
[深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
post-study program
重新审视分布式系统:永远不会有完美的一致性方案……
How to deal with your own shame
从零到一快速学会三子棋
01 【前言 基础使用 核心概念】
HOG feature study notes
CPDA|运营人如何从负基础学会数据分析(SQL)
正则表达式,匹配中间的某一段字符串
nodeJs--封装路由
如何模拟后台API调用场景,很细!
云原生(三十二) | Kubernetes篇之平台存储系统介绍