文字转拼音

本文来自:http://xsl2007.iteye.com/blog/992985


文字转拼音 ,需要引入jar包,pinyin4j-2.5.0.jar 已经上传到我的资源库中了

资源库地址:http://download.csdn.net/detail/heqinghua217/9664431


包的api:


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   可以设置拼音大小写、是否后面加读音数字、特殊读音的显示方式,定义如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * The option indicates that the output of 'ü' is "u:" 
  3.  */  
  4. public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");  
  5. /** 
  6.  * The option indicates that the output of 'ü' is "v" 
  7.  */  
  8. public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")  
  9. /** 
  10.  * The option indicates that the output of 'ü' is "ü" in Unicode form 
  11.  */  
  12. 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






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 ChineseToSpell {
	
	// 将汉字转换为全拼  
    public static String getPingYin(String src) {  
  
        char[] t1 = null;  
        t1 = src.toCharArray();  
        String[] t2 = new String[t1.length];  
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
          
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
        String t4 = "";  
        int t0 = t1.length;  
        try {  
            for (int i = 0; i < t0; i++) {  
                // 判断是否为汉字字符  
                if (java.lang.Character.toString(t1[i]).matches(  
                        "[\\u4E00-\\u9FA5]+")) {  
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);  
                    t4 += t2[0];  
                } else  
                    t4 += java.lang.Character.toString(t1[i]);  
            }  
            // System.out.println(t4);  
            return t4;  
        } catch (BadHanyuPinyinOutputFormatCombination e1) {  
            e1.printStackTrace();  
        }  
        return t4;  
    }  
  
    // 返回中文的首字母  
    public static String getPinYinHeadChar(String str) {  
  
        String convert = "";  
        for (int j = 0; j < str.length(); j++) {  
            char word = str.charAt(j);  
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
            if (pinyinArray != null) {  
                convert += pinyinArray[0].charAt(0);  
            } else {  
                convert += word;  
            }  
        }  
        return convert;  
    }  
  
    // 将字符串转移为ASCII码  
    public static String getCnASCII(String cnStr) {  
        StringBuffer strBuf = new StringBuffer();  
        byte[] bGBK = cnStr.getBytes();  
        for (int i = 0; i < bGBK.length; i++) {  
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));  
        }  
        return strBuf.toString();  
    }  
  
    public static void main(String[] args) {  
        System.out.println(getPingYin("新疆维吾尔吾拉脱"));  
        System.out.println(getPinYinHeadChar("何清华"));  
        System.out.println(getCnASCII("张赛赛"));  
    }  

}
</pre><pre code_snippet_id="1949818" snippet_file_name="blog_20161026_1_895164" name="code" class="java">



运行GUI demo

命令行执行cd ${pinyin-install-dir}/lib,进入到pinyin4j-2.5.0.jar所在的目录,敲入java -jar pinyin4j-2.5.0.jar执行,GUI界面如下:

  

    

 

上图是输入汉字“中”,执行Convert to Pinyin后的截图。Format后有三个下拉框,第一个下拉框有三个选项,用来控制生成的拼音声调的显示方式,三个方式及其效果如下(以汉字“中”,选中Formatted hanyu Pinyin选项卡测试)

WITH_TONE_NUMBER(以数字代替声调) :  zhong1  zhong4

WITHOUT_TONE (无声调) :                           zhong   zhong

WITH_TONE_MARK (有声调) :                      zhōng  zhòng

第二个下拉框是碰到unicode 的ü v  u时的显示方式,共有三个方式, 以下是以声调为WITH_TONE_NUMBER方式显示汉字“吕”示例:

WITH_U_AND_COLON : lu:3

WITH_V            lv3

WITH_U_UNICODE :    lü3

第三个下拉框是控制生成的拼音是以大写方式显示还是以小写方式显示,以汉字示例如下:

LOWERCASE guó

UPPERCASE GUÓ

上图的汉字转化成拼音后,有六种显示方式,这是因为pinyin4j支持将汉字转化成六种拼音表示法。其对应关系是:汉语拼音-Hanyu Pinyin,通用拼音-Tongyong Pinyin, 威妥玛拼音(威玛拼法)-Wade-Giles  Pinyin,注音符号第二式-MPSII Pinyin, 耶鲁拼法-Yale Pinyin和国语罗马字-Gwoyeu Romatzyh 

 

4.  字符串转化成拼音Java代码示例

代码:

 

[java:nogutter] view plaincopyprint?
  1. package testcase;  
  2.   
  3. import net.sourceforge.pinyin4j.PinyinHelper;  
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
  7. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
  8. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
  9.   
  10. public class PinYinUtil  
  11.       
  12.     public static String getPinYin(String inputString)  
  13.           
  14.         HanyuPinyinOutputFormat format new HanyuPinyinOutputFormat();  
  15.         format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  16.         format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);  
  17.         format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);  
  18.   
  19.         char[] input inputString.trim().toCharArray();  
  20.         StringBuffer output new StringBuffer("");  
  21.   
  22.         try  
  23.             for (int 0input.length; i++)  
  24.                 if (Character.toString(input[i]).matches("[//u4E00-//u9FA5]+"))  
  25.                     String[] temp PinyinHelper.toHanyuPinyinStringArray(input[i], format);  
  26.                     output.append(temp[0]);  
  27.                     output.append(");  
  28.                 else  
  29.                     output.append(Character.toString(input[i]));  
  30.              
  31.         catch (BadHanyuPinyinOutputFormatCombination e)  
  32.             e.printStackTrace();  
  33.          
  34.         return output.toString();  
  35.      
  36.       
  37.     public static void main(String[] args)  
  38.         String chs "我是中国人! I'm Chinese!" 
  39.         System.out.println(chs);  
  40.         System.out.println(getPinYin(chs));  
  41.      
  42.       
  43.  

运行结果:我是中国人! I'm Chinese!
           wŏ shì zhōng guó rén ! I'm Chinese!

 

附件:

1.各种拼音说明

Yale Pinyin是在第二次世界大战期间由美国军方发明的编码系统,主要为了让在中国地区作战的美军士兵能够快速地熟悉汉语发音,能够向当地人请求帮助,可以说这是一个速成教材,它的目的甚至不是用来互相交流而是使士兵在发音时不会被中国人听错就可以了。

Gwoyeu Romatzyh:即国语罗马字,它是由林语堂提议建立的,在1928年由国民政府大学堂颁布推行。在中国的台湾省这一编码体系得到了保留,但是它就像 Yale一样现在几乎很少有人使用,在1986年,国语罗马字被国语注音符号第二式(MPSII)所取代,在2002年,又被通用拼音(Tongyong Pinyin)取代,成为台湾今天正式的官方汉语音译编码体系。

威妥玛拼音,习惯称作威妥玛拼法或威玛式拼音、韦氏拼音、威翟式拼音,是一套用于拼写中文普通话的罗马拼音系统。19世纪中叶由英国人威妥玛(Thomas Francis Wade)发明,后由翟理斯(Herbert Allen Giles)完成修订,并编入其所撰写的汉英字典。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值