pinyin4j的使用
 
pinyin4j是一个功能强悍的汉语拼音工具包,主要是从汉语获取各种格式和需求的拼音,功能强悍,下面看看如何使用pinyin4j。
 
 
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.io.UnsupportedEncodingException;

/** 
* 拼音工具 
*/ 


public 
class PinyinToolkit {

/** 
         * 获取汉字串拼音首字母,英文字符不变 
         * 
         * @param chinese 汉字串 
         * @return 汉语拼音首字母 
         */ 

        
public 
static String cn2FirstSpell(String chinese) { 

                StringBuffer pybf = 
new StringBuffer(); 

                
char[] arr = chinese.toCharArray(); 

                HanyuPinyinOutputFormat defaultFormat = 
new HanyuPinyinOutputFormat(); 

                defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 

                defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 

                
for (
int i = 0; i < arr.length; i++) { 

                        
if (arr[i] > 128) { 

                                
try { 

                                        String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat); 

                                        
if (_t != 
null) { 

                                                pybf.append(_t[0].charAt(0)); 

                                        } 

                                } 
catch (BadHanyuPinyinOutputFormatCombination e) { 

                                        e.printStackTrace(); 

                                } 

                        } 
else { 

                                pybf.append(arr[i]); 

                        } 

                } 

                
return pybf.toString().replaceAll(
"\\W", "").trim(); 

        }

/** 
         * 获取汉字串拼音,英文字符不变 
         * 
         * @param chinese 汉字串 
         * @return 汉语拼音 
         */ 

        
public 
static String cn2Spell(String chinese) { 

                StringBuffer pybf = 
new StringBuffer(); 

                
char[] arr = chinese.toCharArray(); 

                HanyuPinyinOutputFormat defaultFormat = 
new HanyuPinyinOutputFormat(); 

                defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 

                defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 

                
for (
int i = 0; i < arr.length; i++) { 

                        
if (arr[i] > 128) { 

                                
try { 

                                        pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]); 

                                } 
catch (BadHanyuPinyinOutputFormatCombination e) { 

                                        e.printStackTrace(); 

                                } 

                        } 
else { 

                                pybf.append(arr[i]); 

                        } 

                } 

                
return pybf.toString(); 

        }

public 
static 
void main(String[] args) 
throws UnsupportedEncodingException { 

                String x = 
"中国你好"; 

                System.out.println(cn2FirstSpell(x)); 

                System.out.println(cn2Spell(x)); 

        } 

}

  
在某些系统上可能有字符集的问题,需要做预处理。

pinyin4j的使用的更多相关文章

  1. 通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母

    通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母 例如 我的中国心    ==> wdzgx 我的中国心ya ==> wdzgxya woai我的中国 ==> w ...

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

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

  3. 汉字转拼音(pinyin4j)

    1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...

  4. 【转】java开源类库pinyin4j的使用

    最近CMS系统为了增加查询的匹配率,需要增加拼音检索字段,在网上找到了pinyin4j的java开源类库,提供中文转汉语拼音(并且支持多音字), 呵呵,看了看他的demo,决定就用它了,因为我在实际使 ...

  5. 利用PinYin4j 实现List中的对象按数字,字母, 汉字排序

    要排序的对象: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPi ...

  6. pinyin4j

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

  7. pinyin4j使用示例

    pinyin4j的主页:http://pinyin4j.sourceforge.net/pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制pinyin4j是一个支持将中文转换到拼 ...

  8. 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)

    转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...

  9. 汉语转拼音pinyin4j

    分享一个将汉语转成拼音的工具包:pinyin4j-2.5.0.jar,下载地址:http://download.csdn.net/detail/abc_key/7629141 使用例如以下代码 imp ...

随机推荐

  1. Ch02 从零开始实例学习5

    演练:添加模型 原文链接:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model ...

  2. mmc生产运输投资问题

    本题目,有生产,运输和投资,由于能力不能满足需求, 要扩大生产规模.

  3. svn 标示提示

    原来没有遇到过, 突然发现这次写的项目有几个文件时 这个"表示的" ,死活找不到原因,并且提交,改动 都好烦人,还要锁定什么嘛的. 最后最终知道, 这个意思是  文件的状态为 &q ...

  4. leetcode先刷_Path Sum

    水的问题不解释,具有参数保持部和,当它到达一个叶子节点,推断是否与给予平等. 需要注意的是节点在树中的数目值它可以是正的或负.它不使用,修剪.有仅仅存在罐.因此,关于或代表最终结果的字. bool h ...

  5. 进阶:案例六: Context Menu(静态 与 动态)

    实现: 1.add: 2.delete 3.add2 实现步骤: 1.新建属性display_text 2.创建layout 3.代码部分: add事件: METHOD onactionadd . D ...

  6. FZU 1894 (双端队列)

    Problem 1894 志愿者选拔 Accept: 1166    Submit: 3683 Time Limit: 1500 mSec    Memory Limit : 32768 KB  Pr ...

  7. Perl BEGIN块和END块

    [root@wx03 5]# cat a5.pl END {print cccccccccccc."\n"}; print "aaaaaaaaaaaaa\n"; ...

  8. redhat6.3 jfreechar中文乱码解决途径

    最近使用到jfreechar的项目,在转移到linux上面时出现中文乱码(中文被显示为框框),网上查了一些资料,结合自身系统的特性,总结了一种安装字体的方式.在说字体安装之前首先上个测试的代码吧:we ...

  9. UVA 10795 - A Different Task(递归)

     A Different Task  The (Three peg) Tower of Hanoi problem is a popular one in computer science. Brie ...

  10. delphi 网页提交按钮执行点击事件

    遍历即可实现,下列代码仅供参考: var i: integer; T: OleVariant; begin T := WebBrowser1.Document; do begin if T.all.i ...