pinyin4j
最近在倒腾与搜索相关的拼音检查技术,顺便看了一下中文转拼音开源插件pinyin4j的源码,参考资料:http://blog.csdn.net/hfhwfw/archive/2010/11/23/6030816.aspx整理了一下笔记:
pinyin4j是一个支持将简体和繁体中文转换到成拼音的Java开源类库,作者是Li Min (xmlerlimin@gmail.com)。
1. pinyin4j的官方下载地址:http://sourceforge.net/projects/pinyin4j/files/,目前最新的版本是2.5.0
2. 下载解压后的目录结构及说明如下
- doc : pinyin4j的api文档
- lib : pinyin4j的jar包
- src : pinyin4j的源代码
- CHANGELOG.txt : pinyin4j的版本更新日志
- COPYING.txt : LICENSE说明
- README.txt : pinyin4j的概要介绍
3. 源码解析
net/sourceforge/pinyin4j
ChineseToPinyinResource:读取/pinyindb/unicode_to_hanyu_pinyin.txt
GwoyeuRomatzyhResource:读取/pinyindb/pinyin_gwoeu_mapping.xml
GwoyeuRomatzyhTranslator:汉语拼音转换为Gwoyeu拼音
PinyinRomanizationResource:读取/pinyindb/pinyin_mapping.xml
PinyinRomanizationType:定义汉语拼音的六种类型(pinyin4j支持将汉字转化成六种拼音表示法。其对应关系是:汉语拼音-Hanyu Pinyin,通用拼音-Tongyong Pinyin, 威妥玛拼音(威玛拼法)-Wade-Giles Pinyin, 注音符号第二式-MPSII Pinyin, 耶鲁拼法-Yale Pinyin和国语罗马字-Gwoyeu Romatzyh)
PinyinRomanizationTranslator:拼音转换,convertRomanizationSystem(源拼音字符串,源拼音类型,目标拼音类型)
PinyinFormatter:汉语拼音格式化(如:根据提供的格式格式化拼音字符串;注音标等方法)
PinyinHelper:音标格式化方法类(六种拼音类型的获取方法等)
ResourceHelper:从classpath路径下读取文件流BufferedInputStream
TextHelper:获取汉语拼音中拼音或音调数字
extractToneNumber返回音调数字,如输入:luan4 返回:4
extractPinyinString返回汉语拼音前的拼音,如输入:luan4 返回:luan
/format
HanyuPinyinCaseType:定义汉语拼音大小写类型(控制生成的拼音是以大写方式显示还是以小写方式显示)
- LOWERCASE :guó
- UPPERCASE :GUÓ
HanyuPinyinToneType:定义汉语拼音声调类型
- WITH_TONE_NUMBER(以数字代替声调) : zhong1 zhong4
- WITHOUT_TONE (无声调) : zhong zhong
- WITH_TONE_MARK (有声调) : zhōng zhòng
HanyuPinyinVCharType:定义汉语拼音字符u的类型(碰到unicode 的ü 、v 和 u时的显示方式)
- WITH_U_AND_COLON : lu:3
- WITH_V : lv3
- WITH_U_UNICODE : lü3
HanyuPinyinOutputFormat:拼音格式类型构造类
/exception 异常类
BadHanyuPinyinOutputFormatCombination:拼音格式化组合错误异常,如一下组合:
LOWERCASE-WITH_V-WITH_TONE_MARK LOWERCASE-WITH_U_UNICODE-WITH_TONE_MARK
UPPERCASE-WITH_V-WITH_TONE_MARK UPPERCASE-WITH_U_UNICODE-WITH_TONE_MARK
其主要思路:
1)先通过汉字字符的unicode编码从unicode_to_hanyu_pinyin.txt找到对应的带声调数字的拼音
2)根据给定的输出格式化要求对该带声调的数字拼音进行格式化处理
3)在各个拼音之间可以相互转换:
根据pinyin_mapping.xml可以找到汉语拼音对应其他四种格式,如:
<item>
<Hanyu>a</Hanyu>
<Wade>a</Wade>
<MPSII>a</MPSII>
<Yale>a</Yale>
<Tongyong>a</Tongyong>
</item>
根据pinin_gwoyeu_mapping.xml可以找出汉语拼音与Gwoyeu不同声调对应的格式,如:
<item>
<Hanyu>a</Hanyu>
<Gwoyeu_I>a</Gwoyeu_I>
<Gwoyeu_II>ar</Gwoyeu_II>
<Gwoyeu_III>aa</Gwoyeu_III>
<Gwoyeu_IV>ah</Gwoyeu_IV>
<Gwoyeu_V>.a</Gwoyeu_V>
</item>
4. 字符串转化成拼音Java代码示例
代码:
import java.util.HashSet;
import java.util.Set;
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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class Pinyin4j {
public static Set<String> getPinyin(String src){
if(src != null && !src.trim().equalsIgnoreCase("")){
char[] srcChar;
srcChar = src.toCharArray();
//汉语拼音格式输出类
HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
//输出设置,大小写,音标方式等
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); //小写
hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //无音调
hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); //'¨¹' is "v"
String[][] temp = new String[src.length()][];
for(int i=0;i<srcChar.length;i++){
char c = srcChar[i];
//是中文或者a-z或者A-Z转换拼音(我的需求,是保留中文或者a-z或者A-Z)
if(String.valueOf(c).matches("[//u4E00-//u9FA5]+")){ //中文字符
try{
temp[i] = PinyinHelper.toHanyuPinyinStringArray(srcChar[i],hanYuPinOutputFormat);
}catch(BadHanyuPinyinOutputFormatCombination e){
e.printStackTrace();
}
}else if(((int)c>=65&&(int)c<=90)||((int)c>=97&&(int)c<=122)){ //英文字母
temp[i] = new String[]{String.valueOf(srcChar[i])};
}else{ //其他字符
temp[i] = new String[]{""};
}
}
String[] pinyinArray = ExChange(temp);
Set<String> pinyinSet = new HashSet<String>();
for(int i=0;i<pinyinArray.length;i++){
pinyinSet.add(pinyinArray[i]);
}
return pinyinSet;
}
return null;
}
/**
* 字符串集合转换字符串(逗号分隔)
* @param stringSet
* @return
*/
public static String makeStringByStringSet(Set<String> stringSet,String separator){
StringBuilder str = new StringBuilder();
int i=0;
for(String s :stringSet){
if(i == stringSet.size() - 1){
str.append(s);
}else{
str.append(s+separator);
}
i++;
}
return str.toString().toLowerCase();
}
private static String[] ExChange(String[][] strJaggedArray) {
String[][] temp = DoExchange(strJaggedArray);
return temp[0];
}
private static String[][] DoExchange(String[][] strJaggedArray) {
int len = strJaggedArray.length;
if(len >= 2){
int len1 = strJaggedArray[0].length;
int len2 = strJaggedArray[1].length;
int newlen = len1*len2;
String[] temp = new String[newlen];
int index = 0;
for(int i=0;i<len1;i++){
for(int j=0;j<len2;j++){
temp[index] = strJaggedArray[0][i]+strJaggedArray[1][j];
index++;
}
}
String[][] newArray = new String[len-1][];
for(int i=2;i<len;i++){
newArray[i-1] = strJaggedArray[i];
}
newArray[0] = temp;
return DoExchange(newArray);
}else{
return strJaggedArray;
}
}
/**
* 只转换汉字为拼音,其他字符不变
* @param src
* @return
*/
public static String getPinyinWithMark(String src){
if(src != null && !src.trim().equalsIgnoreCase("")){
char[] srcChar;
srcChar = src.toCharArray();
//汉语拼音格式输出类
HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
//输出设置,大小写,音标方式等
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); //小写
hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); //无音调
hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); //'¨¹' is "v"
StringBuffer output = new StringBuffer();
//String[][] temp = new String[src.length()][];
for(int i=0;i<srcChar.length;i++){
char c = srcChar[i];
//是中文转换拼音(我的需求,是保留中文)
if(String.valueOf(c).matches("[//u4E00-//u9FA5]+")){ //中文字符
try{
String[] temp = PinyinHelper.toHanyuPinyinStringArray(srcChar[i],hanYuPinOutputFormat);
output.append(temp[0]);
output.append(" ");
}catch(BadHanyuPinyinOutputFormatCombination e){
e.printStackTrace();
}
}else{ //其他字符
output.append(String.valueOf(srcChar[i]));
}
}
return output.toString();
}
return null;
}
/**
* 只转换汉字为拼音,其他字符不变
* @param src
* @return
*/
public static String getPinyinWithMark2(String inputString){
//汉语拼音格式输出类
HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
//输出设置,大小写,音标方式等
hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); //小写
hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); //有音调
hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); //'¨¹' is "u:"
char[] input = inputString.trim().toCharArray();
StringBuffer output = new StringBuffer();
for(int i=0;i<input.length;i++){
//是中文转换拼音(我的需求,是保留中文)
if(Character.toString(input[i]).matches("[//u4E00-//u9FA5]+")){ //中文字符
try{
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i],hanYuPinOutputFormat);
output.append(temp[0]);
output.append(" ");
}catch(BadHanyuPinyinOutputFormatCombination e){
e.printStackTrace();
}
}else{ //其他字符
output.append(Character.toString(input[i]));
}
}
return output.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
String str = "我是中国人! I'm Chinese!";
//System.out.println(makeStringByStringSet(getPinyin(str)," "));
System.out.println(getPinyinWithMark2(str)+'??');
}
}
附件:
1.各种拼音说明
Yale Pinyin是在第二次世界大战期间由美国军方发明的编码系统,主要为了让在中国地区作战的美军士兵能够快速地熟悉汉语发音,能够向当地人请求帮助,可以说这是一个速成教材,它的目的甚至不是用来互相交流而是使士兵在发音时不会被中国人听错就可以了。
Gwoyeu Romatzyh:即国语罗马字,它是由林语堂提议建立的,在1928年由国民政府大学堂颁布推行。在中国的台湾省这一编码体系得到了保留,但是它就像 Yale一样现在几乎很少有人使用,在1986年,国语罗马字被国语注音符号第二式(MPSII)所取代,在2002年,又被通用拼音(Tongyong Pinyin)取代,成为台湾今天正式的官方汉语音译编码体系。
威妥玛拼音,习惯称作威妥玛拼法或威玛式拼音、韦氏拼音、威翟式拼音,是一套用于拼写中文普通话的罗马拼音系统。19世纪中叶由英国人威妥玛(Thomas Francis Wade)发明,后由翟理斯(Herbert Allen Giles)完成修订,并编入其所撰写的汉英字典。
参考资料:
1. pinyin4j的官方资料 http://pinyin4j.sourceforge.net/
2. 汉语言的罗马化 http://icookies.spaces.live.com/blog/cns!2CC37E2F87FB3864!170.entry
3. Wiki: 威妥玛拼音(维基百科) http://wapedia.mobi/zh/%E5%A8%81%E5%A6%A5%E7%8E%9B%E6%8B%BC%E9%9F%B3
pinyin4j的更多相关文章
- 通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母
通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母 例如 我的中国心 ==> wdzgx 我的中国心ya ==> wdzgxya woai我的中国 ==> w ...
- java-汉字转换拼音-pinyin4j.jar
使用pinyin4j将汉字转成拼音,附件为pinyin4j的jar包 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefo ...
- 汉字转拼音(pinyin4j)
1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...
- 【转】java开源类库pinyin4j的使用
最近CMS系统为了增加查询的匹配率,需要增加拼音检索字段,在网上找到了pinyin4j的java开源类库,提供中文转汉语拼音(并且支持多音字), 呵呵,看了看他的demo,决定就用它了,因为我在实际使 ...
- 利用PinYin4j 实现List中的对象按数字,字母, 汉字排序
要排序的对象: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPi ...
- pinyin4j使用示例
pinyin4j的主页:http://pinyin4j.sourceforge.net/pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制pinyin4j是一个支持将中文转换到拼 ...
- 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)
转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...
- 汉语转拼音pinyin4j
分享一个将汉语转成拼音的工具包:pinyin4j-2.5.0.jar,下载地址:http://download.csdn.net/detail/abc_key/7629141 使用例如以下代码 imp ...
- pinyin4j的使用
pinyin4j的使用 pinyin4j是一个功能强悍的汉语拼音工具包,主要是从汉语获取各种格式和需求的拼音,功能强悍,下面看看如何使用pinyin4j. import net.sourc ...
随机推荐
- Python-Day7 面向对象进阶/异常处理/Socket
一.面向对象高级语法部分 1.静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里 ...
- erp与电子商务集成的结构图
集约化采购管理系统和电子商务平台统一规划.统一设计,通过系统之间的安全接口全面集成,进而实现资源共享和数据共享,企业内外部系统运作的一体化,建立企业同上.下游合作伙伴的电子数据交互,从而提高电子商务的 ...
- C#二维数组及其本质(转)
C#中二维数组包含两类:二维数组和数据矩阵.(这是我个人分类法,我认为比较能反映本质). 如上图,是二维数组,横向为第一维度,纵向为第二维度,不同维度可以有不同长度. 如果去掉元素7,那么上图也可能是 ...
- Oracle定义常量和变量
1.定义变量 变量指的就是可变化的量,程序运行过程中可以随时改变其数据存储结构 标准语法格式:<变量名><数据类型>[(长度):=<初始值>] 示例: declar ...
- ios开发之NavBar和TarBar使用技巧
1 改变NavBar颜色:选中Navigation Bar 的Tint属性.选中颜色. 2 隐藏“back”按钮: self.navigationItem.hidesBackButton = YE ...
- 笔记本电脑连接wifi有时候会自动断网提示有限的访问权限解决办法
解决办法如下: [设备管理器],找到[网络适配器]第一项,右键属性
- NopCommerce——源代码的组织,以及系统的架构
近来使用NopCommerce进行开发,仿照源码的Demo也能做出看上去还蛮高端大气上档次的系统出来,现下准备深入学习学习.首先从官方的Documentation开始看起,先来一篇官网文章的翻译(园里 ...
- SVN 安装配置
1,软件下载 到官方网站的下载二进制安装文件,来到二进制包下载部分,找到 Windows NT, 2000, XP and 2003部分,然后选择Apache 2.2 或者 Apache 2.4,这样 ...
- 母函数入门笔记(施工中…
定义:对于一个数列,它的母函数(即生成函数)为 为了对这个准确求值,我们设 举一个简单的例子 例1 对于数列 他的生成函数为 ,那么应用一下等比数列求和公式 这里由于 所以当时 那么 例 ...
- Memcached 安装及配置
下载Memcached.exe 保存到c:\memcached 运行command: 输入 c:\memcached\memcached.exe -d install 回车,安装memcached s ...