pinyin4j

最近在倒腾与搜索相关的拼音检查技术,顺便看了一下中文转拼音开源插件pinyin4j的源码,参考资料:http://blog.csdn.net/hfhwfw/archive/2010/11/23/6030816.aspx整理了一下笔记:

pinyin4j是一个支持将简体和繁体中文转换到成拼音的Java开源类库,作者是Li Min ([email protected])。

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

时间: 2024-08-11 01:36:22

pinyin4j的相关文章

Pinyin4j的基本用法

转自:http://www.cnblogs.com/bluestorm/archive/2012/07/23/2605412.html 1. 简单介绍 有时候,需要将汉字编程对应的拼音,以方便数据的处理.比如在Android手机应用的开发上,要查询联系人的姓名,通常都是用拼音进行查询的.比如要查询"曹孟德",就可以输入"cmd",即"曹孟德"三个汉字的拼音"caomengde"各字的首字母.但是怎样才能将"曹孟德&q

[JavaWeb基础] 032.第三方插件pinyin4j的使用

突然发现了一个比较新奇的插件,就是可以把我们输入的汉字,输出它所有的拼音的jar包.下面以代码的形式简单的介绍下这个插件 package com.babybus.sdteam.pinyin4j; import java.util.Scanner; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourcefor

java 中文转拼音之pinyin4j

一.简单介绍 有时候,需要将汉字编程对应的拼音,以方便数据的处理.比如在Android手机应用的开发上,要查询联系人的姓名,通常都是用拼音进行查询的. 比如要查询"曹孟德",就可以输入"cmd",即"曹孟德"三个汉字的拼音"caomengde"各字的首字母.但是怎样才能将"曹孟德"翻译成"caomengde"呢? 很简单的办法就是建立一个大的对照表(比如用关联容器Map),比如<&

终结者:借助pinyin4j相关jar包提取汉字的首字母

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.sourcefor

Android之开源类库Pinyin4j的使用----搜索联系人

Pinyin4j只能算是Java开源类库,但是在Android的应用开发中也经常被用到.大家都用过手机的通讯录,通讯录的搜索联系人的功能,用pinyin4j就可以实现.下面我给大家带来这个例子,使用pinyin4j实现搜索联系人功能. 首先先对pinyin4j做一个简单的介绍: pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换,拼音的输出格式也可以定制,并且支持汉字的多音字.pinyin4j的官方网是:http://pinyin4j.sourceforge.net/ piny

pinyin4j的使用

pinyin4j是一个功能强悍的汉语拼音工具包,主要是从汉语获取各种格式和需求的拼音,功能强悍,下面看看如何使用pinyin4j. import java.util.HashSet;import java.util.Set; import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;import net.sourceforge.pinyin4j.f

pinyin4j使用示例

pinyin4j的主页:http://pinyin4j.sourceforge.net/pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制pinyin4j是一个支持将中文转换到拼音的Java开源类库 1.支持简体中文和繁体中文字符 2.支持转换到汉语拼音,通用拼音, 威妥玛拼音(威玛拼法), 注音符号第二式, 耶鲁拼法和国语罗马字 3.支持多音字,即可以获取一个中文字符的多种发音 4.支持多种字符串输出格式,比如支持Unicode格式的字符ü和声调符号(阴平 "ˉ"

通过pinyin4j将汉字转换为全拼 和 拼音首字母

/** * 汉字转换为拼音 包含多音字,包含生母zh,ch,sh的 */ public void toPinYinAll(){ String initials = "zh,ch,sh"; //返回汉语拼音的全拼 List<String> result = new ArrayList<String>(); //返回汉语拼音的单拼 List<String> firstResult = new ArrayList<String>(); resu

框架 day50 BOS项目 4 批量导入(ocupload插件,pinyin4J)/POI解析Excel/Combobox下拉框/分区组合条件分页查询(ajax)/分区数据导出(Excel)

知识点: 批量导入(ocupload插件,pinyin4J /POI解析Excel(apache POI) /区域分页查询 /Combobox下拉框 /分区组合条件分页查询(ajax) /分区数据导出(Excel下载) BOS项目笔记第4天 1.    区域批量导入功能 *Ajax不支持文件上传. *上传并且不刷新上传页面原理: Target到一个0,0,0的隐藏iframe里,造成一个没有刷新的假象 <form target="myIframe" action="ab

java-汉字转换拼音-pinyin4j.jar

使用pinyin4j将汉字转成拼音,附件为pinyin4j的jar包 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuP