当前位置:网站首页>汉字转拼音
汉字转拼音
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
边栏推荐
- Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
- C语言日记 9 if的3种语句
- DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
- Flink 1.15.1 集群搭建(StandaloneSession)
- 意识形态的机制
- "Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory
- 02 【开发服务器 资源模块】
- Error: Not a signal or slot declaration
- HOG特征学习笔记
- 后期学习计划
猜你喜欢
Advanced Numbers_Review_Chapter 1: Functions, Limits, Continuity
DAY22:sqli-labs 靶场通关wp(Less01~~Less20)
hypervisor相关的知识点
select 标签自定义样式
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
The 20th day of the special assault version of the sword offer
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
"Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory
使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号
随机推荐
SDC简介
回顾51单片机
What should I do if the self-incrementing id of online MySQL is exhausted?
网络安全与元宇宙:找出薄弱环节
Matlab map with color representation module value size arrow
继承关系下构造方法的访问特点
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
LPQ(局部相位量化)学习笔记
J9数字货币论:web3的创作者经济是什么?
.Net C# 控制台 使用 Win32 API 创建一个窗口
leetcode 15
02 【开发服务器 资源模块】
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
注意潍坊开具发票一般需要注意
02 [Development Server Resource Module]
蚁剑高级模块开发
浅谈数据安全治理与隐私计算
The 2022 EdgeX China Challenge will be grandly opened on August 3
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
Using OpenVINO to implement the flying paddle version of the PGNet inference program