[pinyin4j] java版汉字转换拼音(大小写)
pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0
pinyin4J 提供PinyinHelper这个静态类对外提供拼音转换的服务,主要有一下方法:
static public String[] toHanyuPinyinStringArray(char ch)
将char(必须为汉字单字)转化为拼音,实用的是通用的格式,如果ch为非汉字,返回null。
输入:重 输出:[zhong4, chong2]
说明重字有两个读音,拼音后面的1,2,3,4 代表的是读音
static public String[] toHanyuPinyinStringArray(char ch,HanyuPinyinOutputFormat outputFormat)
同上,但是这个方法可以设置输出的格式。HanyuPinyinOutputFormat 可以设置拼音大小写、是否后面加读音数字、特殊读音的显示方式,定义如下:
- /**
- * The option indicates that the output of 'ü' is "u:"
- */
- public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");
- /**
- * The option indicates that the output of 'ü' is "v"
- */
- public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")
- /**
- * The option indicates that the output of 'ü' is "ü" in Unicode form
- */
- public static final HanyuPinyinVCharType WITH_U_UNICODE = new HanyuPinyinVCharType("WITH_U_UNICODE");
static public String[] toTongyongPinyinStringArray(char ch)
转换为通用拼音。通用拼音的介绍见:http://zh.wikipedia.org/zh-cn/%E9%80%9A%E7%94%A8%E6%8B%BC%E9%9F%B3
static public String[] toWadeGilesPinyinStringArray(char ch)
转换为威妥玛拼音:http://zh.wikipedia.org/wiki/%E5%A8%81%E5%A6%A5%E7%91%AA%E6%8B%BC%E9%9F%B3
static public String[] toMPS2PinyinStringArray(char ch)
转换为注音符号拼音:http://zh.wikipedia.org/zh-cn/%E6%B3%A8%E9%9F%B3%E7%AC%A6%E8%99%9F
static public String[] toYalePinyinStringArray(char ch)
转换为耶魯拼音:http://zh.wikipedia.org/zh-cn/%E8%80%B6%E9%AD%AF%E6%8B%BC%E9%9F%B3
static public String[] toGwoyeuRomatzyhStringArray(char ch)
转换为国语罗马字:http://zh.wikipedia.org/wiki/%E5%9C%8B%E8%AA%9E%E7%BE%85%E9%A6%AC%E5%AD%97
对于”重“的拼音转换,以上方法分别得到的结果是:
- 汉语拼音:[zhong4, chong2]
- 通用拼音:[jhong4, chong2]
- 威妥玛拼音:[chung4, ch`ung2]
- 注音符号拼音:[jung4, chung2]
- 耶魯拼音:[jung4, chung2]
- 国语罗马字:[jonq, chorng]
好了,有了上面的基础,我们可以封装一个工具类,用来将汉字转换成拼音,这里只使用了汉字拼音。
首先要将pinyin4j加入项目中,如果是maven项目,可以添加引用:
- <span style="white-space:pre"> </span><!-- 增加pinyin4j -->
- <dependency>
- <groupId>com.belerweb</groupId>
- <artifactId>pinyin4j</artifactId>
- <version>2.5.0</version>
- </dependency>
非maven的可以直接将下载好的jar包放入classpath。
然后编写工具类 PinyinTool.java:
- package org.nerve.d3lesson.common.tools;
- 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.Arrays;
- /**
- *
- * Created by zengxm on 2014/12/4.
- */
- public class PinyinTool {
- HanyuPinyinOutputFormat format = null;
- public static enum Type {
- UPPERCASE, //全部大写
- LOWERCASE, //全部小写
- FIRSTUPPER //首字母大写
- }
- public PinyinTool(){
- format = new HanyuPinyinOutputFormat();
- format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
- format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- }
- public String toPinYin(String str) throws BadHanyuPinyinOutputFormatCombination{
- return toPinYin(str, "", Type.UPPERCASE);
- }
- public String toPinYin(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{
- return toPinYin(str, spera, Type.UPPERCASE);
- }
- /**
- * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换
- * 如: 明天 转换成 MINGTIAN
- * @param str
- * @param spera
- * @return
- * @throws BadHanyuPinyinOutputFormatCombination
- */
- public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
- if(str == null || str.trim().length()==0)
- return "";
- if(type == Type.UPPERCASE)
- format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
- else
- format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
- String py = "";
- String temp = "";
- String[] t;
- for(int i=0;i<str.length();i++){
- char c = str.charAt(i);
- if((int)c <= 128)
- py += c;
- else{
- t = PinyinHelper.toHanyuPinyinStringArray(c, format);
- if(t == null)
- py += c;
- else{
- temp = t[0];
- if(type == Type.FIRSTUPPER)
- temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
- py += temp+(i==str.length()-1?"":spera);
- }
- }
- }
- return py.trim();
- }
- }
写个测试用例看看结果:
[pinyin4j] java版汉字转换拼音(大小写)的更多相关文章
- JAVA实现汉字转换为拼音 pinyin4j/JPinyin
在项目中经常会遇到需求用户输入汉字后转换为拼音的场景,比如说通讯录,就会要求按名字首字符发音排序,如果自己写实现这方面的功能是个很好大的工程,还好网上有公开的第三方jar支持转换,结合网上很多前辈的代 ...
- java实现汉字转为拼音
java实现汉字转为拼音: 1.需要导入pinyin4j.jar package com.loo.pinyin; import net.sourceforge.pinyin4j.PinyinHelpe ...
- JAVA实现汉字转拼音
两个工具包都可以实现:pinyin4j/JPinyin pinyin4j 第一个是使用pinyin4j的jar,此jar对多音字语句的处理不太理想 package edu.ws; import net ...
- C#汉字转换拼音技术详解
C#汉字转换拼音技术详解(高性能) 下面将源代码贴出.public static class ChineseToPinYin { private sta ...
- 【干货】JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多音字,有的不支持声调,有的字典文件太大,还比如有时候我仅仅是需要获取汉字拼音首字母却要引入200kb的字 ...
- JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
原文:http://www.cnblogs.com/liuxianan/p/pinyinjs.html 前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多 ...
- JS版汉字与拼音互转终极方案,附简单的JS拼音
前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多音字,有的不支持声调,有的字典文件太大,还比如有时候我仅仅是需要获取汉字拼音首字母却要引入200kb的字 ...
- C#汉字转换拼音技术详解(高性能)
public static class ChineseToPinYin { private static readonly Dictionary<<span class="key ...
- SQL中把汉字转换拼音码
思路:在SQL中创建一个函数fn_GetPy(),函数的输入参数是一个汉字字符串,返回值是拼音码字符串. 创建函数语句: CREATE function fn_GetPy(@str nvarchar( ...
随机推荐
- js 图片点击放大功能
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Best JavaScript Tools for Developers
JavaScript solves multiple purposes; it helps you to create interactive websites, web applications, ...
- 如何用dumpbin.exe检查编译器生成的托管模块所嵌入的信息
开启CMD 运行到dumpbin目录下:D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin 运行命令VCVARS32.BAT,配置环境 ...
- J2EE总结(2)——Servlet/JSP
Servlet/JSP Servlet定义:部署在java的Webserver上的组件.整个java服务端程序都构建在Servlet之上,以多线程方式提 供服务,具有效率高.可扩展,可移植的特点. J ...
- Build MySQL 5.7.4 in RedHat
Install Cmake 1. download cmake source code at http://www.cmake.org/files/v3.1/cmake-3.1.0.tar.gz 2 ...
- iOS基础 - 文本属性Attributes
NSKernAttributeName: 调整字句 kerning 字句调整 NSFontAttributeName : [UIFont systemFontOfSize:_fontSize] 设置字 ...
- D10
=-=今天被dev-c++坑到死..简直 晚上准备怒装liunx.. T1:数论 一开始碰到的是T1的运算符优先问题吧..maybe..但是我加上括号了还是WA啊..后面把式子拆开写才A了..次奥 附 ...
- CLR_Via_C#学习笔记之枚举
CLR_Via_C#学习笔记之枚举 枚举类型(Enum)定义的一组"符号名称/值"配对:因为枚举类型使用程序更容易编写.阅读和维护,而且它是强类型: 枚举是值类型:由System. ...
- [转]解决MySQL出现大量unauthenticated user的问题
最近发现两台MySQL server在中午的时候忽然(很突然的那种)发飙,不断的挂掉.重启mysql也尽是失败,看mysql的errorlog,只能看到类似如下的信息: Forcing close o ...
- MVC AuthorizeAttribute 动态授权
开发中经常会遇到权限功能的设计,而在MVC 下我们便可以使用重写 AuthorizeAttribute 类来实现自定义的权限认证 首先我们的了解 AuthorizeAttribute 下面3个主要的方 ...