Java中文字转拼音工具类
java取得汉字拼音(pinyin4j)
java取得汉字拼⾳(pinyin4j)jar包:pinyin4j.jar基本⽤法:String[] pinyin = PinyinHelper.toHanyuPinyinStringArray('重');例如“重”字,该⽅法返回⼀个String类型的数组:"zhong4""chong2"“重”是⼀个多⾳字,该⽅法的返回数组包含这个字的所有读⾳的拼⾳。
每个读⾳最后有个数字就是⾳调(第⼀声第⼆声第三声第四声,这个不⽤解释了)格式化:上⾯是最简单的⼀种获取单个汉字的⽅式,还可以使⽤HanyuPinyinOutputFormat来格式化返回拼⾳的格式。
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();// UPPERCASE:⼤写 (ZHONG)// LOWERCASE:⼩写 (zhong)format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// WITHOUT_TONE:⽆⾳标 (zhong)// WITH_TONE_NUMBER:1-4数字表⽰英标 (zhong4)// WITH_TONE_MARK:直接⽤⾳标符(必须WITH_U_UNICODE否则异常) (zhòng)format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);// WITH_V:⽤v表⽰ü (nv)// WITH_U_AND_COLON:⽤"u:"表⽰ü (nu:)// WITH_U_UNICODE:直接⽤ü (nü)format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);String[] pinyin = PinyinHelper.toHanyuPinyinStringArray('重', format);//toHanyuPinyinStringArray如果传⼊的字符不是汉字不能转换成拼⾳,那么会直接返回null。
java全拼转汉字的方法
java全拼转汉字的方法Java全拼转汉字的方法在Java编程中,有时候我们需要将汉字转换为全拼的拼音形式。
这在一些需要对中文进行处理的应用中非常常见,比如搜索引擎的关键词匹配、拼音输入法等。
本文将介绍一种将汉字转为全拼的方法。
Java中有一个开源库叫做pinyin4j,它是一个汉字转拼音的Java 类库,可以将中文转换为拼音,并且还支持多音字的处理。
现在我们就来学习一下如何使用pinyin4j将汉字转换为全拼拼音。
我们需要引入pinyin4j的依赖。
可以使用Maven来管理项目依赖,只需要在pom.xml文件中添加以下依赖代码即可:```xml<dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency>```引入依赖之后,我们就可以开始使用pinyin4j进行汉字转拼音的操作了。
下面是一个简单的示例代码:```javaimport net.sourceforge.pinyin4j.PinyinHelper;public class PinyinConverter {public static String convertToPinyin(String chinese) {StringBuilder pinyin = new StringBuilder();char[] chars = chinese.toCharArray();for (char c : chars) {String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c);if (pinyins != null && pinyins.length > 0) {pinyin.append(pinyins[0]);} else {pinyin.append(c);}}return pinyin.toString();}public static void main(String[] args) {String chinese = "中文转拼音";String pinyin = convertToPinyin(chinese);System.out.println(pinyin);}}```在上面的示例代码中,我们定义了一个名为PinyinConverter的类,其中包含了一个名为convertToPinyin的静态方法。
Java中文字转拼音工具类
Java中文字转拼音工具类2020-03-24 09:49:09有时,我们需要将中文汉字转为拼音存储,例如用于检索时,直接输入拼音或者首字母也能检索出我们想要的结果,这时我们便需要将汉字转拼音•1、首先我们在pom.xml文件中引入需要的maven依赖com.belerweb pinyin4j 2.5.1•2、汉字转拼音工具类代码如b Pinyin4jUtil.java:import net.sourcefdrge.pinyin4j.PinyinHelper; importnet.sourceforge.pinyin4j.format.HanyuPinyinCaseType; importnet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType; importnet.sourceforge.pinyin4j.format.HanyuPinyinVCharType; importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatComb ination;*-中文转化为拼音工具类-(5)author lizhou-*/ public class Pinyin4jUtil {•获得汉语拼音首字母大写•(5)param chines 汉字•(S)return */ public static String getAlpha2UpperCase(String chines){ String pinyinName = ”"; charQ nameChar =chines.toCharArray0; HanyuPinyinOutputFormat defaultFormat =newHanyuPinyinOutputFormatO;defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_T ONE); for (inti = 0; i < nameChar.length; i++) {if [nameChar[i] > 128) {try{ pinyinName +=PinyinHelper. to HanyuPinyinStr ingArray (nameChar [i],defaultFormat)[0].charAt(0);} catch(BadHanyuPinyinOutputFormatCombination e) { System-outprintlnC*获得汉语拼音首字母异常:}" + e.getMessageO);} } else {pinyinName +=nameChar[i];} } return pinyinName;}•将字符串中的中文转化为拼音,英文字符不变•@param inputstring 汉字•^return */ public static String getPingYinfString inputstring) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormatO;format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneTypefHanyuPinyinT oneType.WITH OUT_T ONE);format.setVCharType(HanyuPinyinVCharType.WITH_V); String output = if(null != inputstring && 0 < inputString.lengthO && !M nuir\equals(inputString)) { char[] input =inputstring.trim0.toCharArray0; try { for (int i = 0; i < inputlength;i++) {if (Character.toString(input[i]).matches(,,[\u4E00-\u9FA5]+<3){StringQ temp =PinyinHelpe 匚toHanyuPinyinStringArray(input[i], format); output +=temp[0];} else { output += Character.toString(input[i]);} } } catch(BadHanyuPinyinOutputFormatCombination e) {System.out.printlnC,将字符串中的中文转化为拼音,英文字符不变异常:"+ e.getMessageO);} } else { return } return output; }••汉字转换位汉语拼音首字母,英文字符不变•(5)param chines 汉字•©return 拼音 */ public static String converterToFirstSpell(String chines) { String pinyinName = ”"; char[] nameChar =chines.toCharArrayO; HanyuPinyinOutputFormat defaultFormat =newHanyuPinyinOutputFormatO;defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_T ONE); for (int i = 0; i < nameChar.length; i++) {if [nameCharp] > 128) {try { pinyinName +=PinyinHelpe 匚toHanyuPinyinStringArrayCnameCharp], defaultFormat)[0].charAt(0);} catch (BadHanyuPinyinOutputFormatCombination e){System.out.printlnC,汉字转换位汉语拼音首字母,英文字符不变异常:"+ e.getMessageO); }} else { pinyinName += nameChar[i]; } } returnpinyinName; }•汉字转换位汉语拼音首字母,英文字符不变(小写)•(5)param chines 汉字•^return 拼音 */ public static String converterToFirstSpellSmal(String chines) { String pinyinName =charQ nameChar = chines.toCharArrayO;HanyuPinyinOutputFormat defaultFormat = newHanyuPinyinOutputFormatO;defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_T ONE); for (int i = 0; i < nameChar.length; i++) {if [nameChar[i] > 128) {try {if(Character.toString(nameChar[i]).matchesC,[\u4E00-\u9FA5]+<3) {pinyinName+=PinyinHelper.toHanyuPinyinStringArray(nameChar[i],defaultFormat)[0].charAt(0);} else { pinyinName += nameChar[i];} } catch(BadHanyuPinyinOutputFormatCombination e) {System.out.printlnC,汉字转换位汉语拼音首字母,英文字符不变异常:"+ e.getMessageO); }} else{ pinyinName += nameChar[i];} } return pinyinName; } }如您在阅读中发现不足,欢迎留言!!!。
汉字转化为拼音程序代码
原理,使用Dictionary技术1.添加索引2.遍历词典<%Set d = CreateObject("Scripting.Dictionary")d.add "a",-20319d.add "ai",-20317d.add "an",-20304d.add "ang",-20295d.add "ao",-20292d.add "ba",-20283d.add "bai",-20265d.add "ban",-20257d.add "bang",-20242d.add "bao",-20230d.add "bei",-20051d.add "ben",-20036d.add "beng",-20032d.add "bi",-20026d.add "bian",-20002d.add "biao",-19990d.add "bie",-19986d.add "bin",-19982d.add "bing",-19976d.add "bo",-19805d.add "bu",-19784d.add "ca",-19775d.add "cai",-19774d.add "can",-19763d.add "cang",-19756d.add "cao",-19751d.add "ce",-19746d.add "ceng",-19741d.add "cha",-19739d.add "chai",-19728d.add "chan",-19725d.add "chang",-19715d.add "chao",-19540d.add "che",-19531d.add "chen",-19525d.add "cheng",-19515d.add "chi",-19500d.add "chong",-19484d.add "chu",-19467 d.add "chuai",-19289 d.add "chuan",-19288 d.add "chuang",-19281 d.add "chui",-19275 d.add "chun",-19270 d.add "chuo",-19263 d.add "ci",-19261d.add "cong",-19249 d.add "cou",-19243 d.add "cu",-19242d.add "cuan",-19238 d.add "cui",-19235 d.add "cun",-19227 d.add "cuo",-19224 d.add "da",-19218d.add "dai",-19212 d.add "dan",-19038 d.add "dang",-19023 d.add "dao",-19018 d.add "de",-19006d.add "deng",-19003 d.add "di",-18996d.add "dian",-18977 d.add "diao",-18961 d.add "die",-18952 d.add "ding",-18783 d.add "diu",-18774 d.add "dong",-18773 d.add "dou",-18763 d.add "du",-18756d.add "duan",-18741 d.add "dui",-18735 d.add "dun",-18731 d.add "duo",-18722 d.add "e",-18710d.add "en",-18697d.add "er",-18696d.add "fa",-18526d.add "fan",-18518 d.add "fang",-18501 d.add "fei",-18490 d.add "fen",-18478d.add "fo",-18448 d.add "fou",-18447 d.add "fu",-18446 d.add "ga",-18239 d.add "gai",-18237 d.add "gan",-18231 d.add "gang",-18220 d.add "gao",-18211 d.add "ge",-18201 d.add "gei",-18184 d.add "gen",-18183 d.add "geng",-18181 d.add "gong",-18012 d.add "gou",-17997 d.add "gu",-17988 d.add "gua",-17970 d.add "guai",-17964 d.add "guan",-17961 d.add "guang",-17950 d.add "gui",-17947 d.add "gun",-17931 d.add "guo",-17928 d.add "ha",-17922 d.add "hai",-17759 d.add "han",-17752 d.add "hang",-17733 d.add "hao",-17730 d.add "he",-17721 d.add "hei",-17703 d.add "hen",-17701 d.add "heng",-17697 d.add "hong",-17692 d.add "hou",-17683 d.add "hu",-17676 d.add "hua",-17496 d.add "huai",-17487 d.add "huan",-17482 d.add "huang",-17468 d.add "hui",-17454 d.add "hun",-17433 d.add "huo",-17427 d.add "ji",-17417d.add "jia",-17202d.add "jiang",-16983 d.add "jiao",-16970 d.add "jie",-16942 d.add "jin",-16915 d.add "jing",-16733 d.add "jiong",-16708 d.add "jiu",-16706 d.add "ju",-16689 d.add "juan",-16664 d.add "jue",-16657 d.add "jun",-16647 d.add "ka",-16474 d.add "kai",-16470 d.add "kan",-16465 d.add "kang",-16459 d.add "kao",-16452 d.add "ke",-16448 d.add "ken",-16433 d.add "keng",-16429 d.add "kong",-16427 d.add "kou",-16423 d.add "ku",-16419 d.add "kua",-16412 d.add "kuai",-16407 d.add "kuan",-16403 d.add "kuang",-16401 d.add "kui",-16393 d.add "kun",-16220 d.add "kuo",-16216 d.add "la",-16212 d.add "lai",-16205 d.add "lan",-16202 d.add "lang",-16187 d.add "lao",-16180 d.add "le",-16171 d.add "lei",-16169 d.add "leng",-16158 d.add "li",-16155d.add "lia",-15959 d.add "lian",-15958 d.add "liang",-15944 d.add "liao",-15933 d.add "lie",-15920d.add "ling",-15903 d.add "liu",-15889 d.add "long",-15878 d.add "lou",-15707 d.add "lu",-15701 d.add "lv",-15681 d.add "luan",-15667 d.add "lue",-15661 d.add "lun",-15659 d.add "luo",-15652 d.add "ma",-15640 d.add "mai",-15631 d.add "man",-15625 d.add "mang",-15454 d.add "mao",-15448 d.add "me",-15436 d.add "mei",-15435 d.add "men",-15419 d.add "meng",-15416 d.add "mi",-15408 d.add "mian",-15394 d.add "miao",-15385 d.add "mie",-15377 d.add "min",-15375 d.add "ming",-15369 d.add "miu",-15363 d.add "mo",-15362 d.add "mou",-15183 d.add "mu",-15180 d.add "na",-15165 d.add "nai",-15158 d.add "nan",-15153 d.add "nang",-15150 d.add "nao",-15149 d.add "ne",-15144 d.add "nei",-15143 d.add "nen",-15141 d.add "neng",-15140 d.add "ni",-15139 d.add "nian",-15128 d.add "niang",-15121 d.add "niao",-15119 d.add "nie",-15117d.add "ning",-15109 d.add "niu",-14941 d.add "nong",-14937 d.add "nu",-14933 d.add "nv",-14930 d.add "nuan",-14929 d.add "nue",-14928 d.add "nuo",-14926 d.add "o",-14922 d.add "ou",-14921 d.add "pa",-14914 d.add "pai",-14908 d.add "pan",-14902 d.add "pang",-14894 d.add "pao",-14889 d.add "pei",-14882 d.add "pen",-14873 d.add "peng",-14871 d.add "pi",-14857 d.add "pian",-14678 d.add "piao",-14674 d.add "pie",-14670 d.add "pin",-14668 d.add "ping",-14663 d.add "po",-14654 d.add "pu",-14645 d.add "qi",-14630 d.add "qia",-14594 d.add "qian",-14429 d.add "qiang",-14407 d.add "qiao",-14399 d.add "qie",-14384 d.add "qin",-14379 d.add "qing",-14368 d.add "qiong",-14355 d.add "qiu",-14353 d.add "qu",-14345 d.add "quan",-14170 d.add "que",-14159 d.add "qun",-14151 d.add "ran",-14149 d.add "rang",-14145 d.add "rao",-14140d.add "ren",-14135 d.add "reng",-14125 d.add "ri",-14123d.add "rong",-14122 d.add "rou",-14112 d.add "ru",-14109d.add "ruan",-14099 d.add "rui",-14097 d.add "run",-14094 d.add "ruo",-14092 d.add "sa",-14090d.add "sai",-14087 d.add "san",-14083 d.add "sang",-13917 d.add "sao",-13914 d.add "se",-13910d.add "sen",-13907 d.add "seng",-13906 d.add "sha",-13905 d.add "shai",-13896 d.add "shan",-13894 d.add "shang",-13878 d.add "shao",-13870 d.add "she",-13859 d.add "shen",-13847 d.add "sheng",-13831 d.add "shi",-13658 d.add "shou",-13611 d.add "shu",-13601 d.add "shua",-13406 d.add "shuai",-13404 d.add "shuan",-13400 d.add "shuang",-13398 d.add "shui",-13395 d.add "shun",-13391 d.add "shuo",-13387 d.add "si",-13383d.add "song",-13367 d.add "sou",-13359 d.add "su",-13356d.add "suan",-13343 d.add "sui",-13340 d.add "sun",-13329d.add "ta",-13318 d.add "tai",-13147 d.add "tan",-13138 d.add "tang",-13120 d.add "tao",-13107 d.add "te",-13096 d.add "teng",-13095 d.add "ti",-13091 d.add "tian",-13076 d.add "tiao",-13068 d.add "tie",-13063 d.add "ting",-13060 d.add "tong",-12888 d.add "tou",-12875 d.add "tu",-12871 d.add "tuan",-12860 d.add "tui",-12858 d.add "tun",-12852 d.add "tuo",-12849 d.add "wa",-12838 d.add "wai",-12831 d.add "wan",-12829 d.add "wang",-12812 d.add "wei",-12802 d.add "wen",-12607 d.add "weng",-12597 d.add "wo",-12594 d.add "wu",-12585 d.add "xi",-12556 d.add "xia",-12359 d.add "xian",-12346 d.add "xiang",-12320 d.add "xiao",-12300 d.add "xie",-12120 d.add "xin",-12099 d.add "xing",-12089 d.add "xiong",-12074 d.add "xiu",-12067 d.add "xu",-12058 d.add "xuan",-12039 d.add "xue",-11867 d.add "xun",-11861 d.add "ya",-11847d.add "yang",-11798 d.add "yao",-11781 d.add "ye",-11604d.add "yi",-11589d.add "yin",-11536 d.add "ying",-11358 d.add "yo",-11340d.add "yong",-11339 d.add "you",-11324 d.add "yu",-11303d.add "yuan",-11097 d.add "yue",-11077 d.add "yun",-11067 d.add "za",-11055d.add "zai",-11052 d.add "zan",-11045 d.add "zang",-11041 d.add "zao",-11038 d.add "ze",-11024d.add "zei",-11020 d.add "zen",-11019 d.add "zeng",-11018 d.add "zha",-11014 d.add "zhai",-10838 d.add "zhan",-10832 d.add "zhang",-10815 d.add "zhao",-10800 d.add "zhe",-10790 d.add "zhen",-10780 d.add "zheng",-10764 d.add "zhi",-10587 d.add "zhong",-10544 d.add "zhou",-10533 d.add "zhu",-10519 d.add "zhua",-10331 d.add "zhuai",-10329 d.add "zhuan",-10328 d.add "zhuang",-10322 d.add "zhui",-10315 d.add "zhun",-10309 d.add "zhuo",-10307 d.add "zi",-10296d.add "zong",-10281d.add "zu",-10270d.add "zuan",-10262d.add "zui",-10260d.add "zun",-10256d.add "zuo",-10254function g(num)if num>0 and num<160 theng=chr(num)elseif num<-20319 or num>-10247 then g=""elsea=d.Itemsb=d.keysfor i=d.count-1 to 0 step -1if a(i)<=num then exit fornextg=b(i)end ifend ifend functionfunction c(str)c=""for i=1 to len(str)c=c&g(asc(mid(str,i,1)))nextend functionresponse.write c(request("hz")) %><form method=post>请在此处输入中文:<input >。
将汉字转换为汉语拼音java实现
将汉字转换为汉语拼⾳java实现有的时候我们需要将中⽂转换成汉语拼⾳,将汉字转换成汉语拼⾳其实挺简单。
⾸先我找到了⼀个资源,是⼀个XML⽂件:,这个⽂件中每个汉字作为⼀个节点,该节点下有该汉字对应的汉语拼⾳,我的⽅法就是读取这个⽂件。
import java.util.List;import org.dom4j.Element;import com.yesoft.jdbcgeneric.constant.ConstantEnum;public class HzChangeToPy {/*** 将中⽂字符串转换成汉语拼⾳* @param wenzi* @return*/public String toPinYin(String wenzi){//XMLReader是⼀个读取xml⽂件的⼯具类,getRootElement()是将读取的xml⽂件内容返回为Element类型,这⾥我们解析xml使⽤的是dom4j。
XMLReader xmlReader = new XMLReader(ConstantEnum.PINYINXML.toString());Element commonElement = xmlReader.getRootElement();//Element wenziElement = commonElement.element("NewDataSet");String pyStr = "";List<Element> wenziList = commonElement.elements();String[] str = chai(wenzi);for(int i = 0;i < str.length;i++){for (Element el : wenziList) {String name = el.elementText("wenzi");if (name.equals(str[i])) {pyStr += el.elementText("zimu");}}}return pyStr;}/*** 将中⽂字符串拆解为单个字的数组* @param wenzi* @return*/public String[] chai(String wenzi){int length = wenzi.length();String[] str = new String[length];for(int i = 0;i<length;i++){str[i] = wenzi.substring(i,i+1);}return str;}public static void main(String[] args){HzChangeToPy py = new HzChangeToPy();String srt = py.toPinYin("张三");System.out.println(srt);}}这样张三就能转换成zhangsan了。
java实现将汉字转为拼音
java实现将汉字转为拼⾳ 有时候为了⽅便操作程序的开发,需要将汉字转为拼⾳等操作。
下⾯这个是⾃⼰结合⽹上的资料,加上⾃⼰在公司项⽬中的亲⾃实践。
完整的实现了将汉字转为拼⾳的操作。
这个Demo只是负责将其转换,在main⽅法中测试,在实际需要中,只需要调⽤这个类中的⽅法即可。
本⼈也是在学习中总结,欢迎各位⼤神拍砖指教,本⼈邮箱:it_red@。
转载本博客时请在⽂章明显位置标明⽂章出处()。
⾸先贴出测试结果: 测试参数: 汉字转换为拼⾳ 汉字转换为拼⾳ main测试⽅法的代码:1public static void main(String[] args) {2 System.out.println(ToFirstChar("汉字转换为拼⾳").toUpperCase()); //转为⾸字母⼤写3 System.out.println(ToPinyin("汉字转换为拼⾳"));4 } 本功能的实现时利⽤java开源库,开发此程序需要⼀个jar包。
本⼈⽤的是。
⽹上可以直接下载,也可以在其官⽹进⾏下载。
在此不祥述。
如果实在不乐意,可以将进⾏这个jar包的下载。
贴出实现该Demo的源码:1package com.red.test;23import net.sourceforge.pinyin4j.PinyinHelper;4import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;5import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;6import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;7import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;89/**10 * 汉字转换为拼⾳11 * @author Red12*/13public class PinyinDemo {14/**15 * 测试main⽅法16 * @param args17*/18public static void main(String[] args) {19 System.out.println(ToFirstChar("汉字转换为拼⾳").toUpperCase()); //转为⾸字母⼤写20 System.out.println(ToPinyin("汉字转换为拼⾳"));21 }22/**23 * 获取字符串拼⾳的第⼀个字母24 * @param chinese25 * @return26*/27public static String ToFirstChar(String chinese){28 String pinyinStr = "";29char[] newChar = chinese.toCharArray(); //转为单个字符30 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();31 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);32 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);33for (int i = 0; i < newChar.length; i++) {34if (newChar[i] > 128) {35try {36 pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);37 } catch (BadHanyuPinyinOutputFormatCombination e) {38 e.printStackTrace();39 }40 }else{41 pinyinStr += newChar[i];42 }43 }44return pinyinStr;45 }4647/**48 * 汉字转为拼⾳49 * @param chinese50 * @return51*/52public static String ToPinyin(String chinese){53 String pinyinStr = "";54char[] newChar = chinese.toCharArray();55 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();56 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);57 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);58for (int i = 0; i < newChar.length; i++) {59if (newChar[i] > 128) {60try {61 pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];62 } catch (BadHanyuPinyinOutputFormatCombination e) {63 e.printStackTrace();64 }65 }else{66 pinyinStr += newChar[i];67 }68 }69return pinyinStr;70 }71 }作者:itRed出处:版权声明:本⽂版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段说明,且在⽂章明显位置给出原⽂链接,否则保留追究法律责任的权利。
用JAVA实现汉字转拼音缩写
用JAVA实现汉字转拼音缩写网上看到别人写的,感觉挺强,以前还以为要实现输拼音缩写查询的话需要新增字段,如此以来就方便很多了。
public class aaaaaaa {/*** 汉字转拼音缩写* @param str //要转换的汉字字符串* @return String //拼音缩写*/public static String getPYString(String str){String tempStr = "";for(int i=0; i<str.length(); i++) {char c = str.charAt(i);if((int)c >= 33 && (int)c <=126) {//字母和符号原样保留tempStr += String.valueOf(c);}else {//累加拼音声母tempStr += getPYChar( String.valueOf(c) );}}return tempStr;}/*** 取单个字符的拼音声母* @param c //要转换的单个汉字* @return String 拼音声母*/public static String getPYChar(String c)byte[] array = new byte[2];array = String.valueOf(c).getBytes();int i = (short)(array[0] - '\0' + 256) * 256 + ((short)(array[1] - '\0' + 256));if ( i < 0xB0A1) return "*";if ( i < 0xB0C5) return "a";if ( i < 0xB2C1) return "b";if ( i < 0xB4EE) return "c";if ( i < 0xB6EA) return "d";if ( i < 0xB7A2) return "e";if ( i < 0xB8C1) return "f";if ( i < 0xB9FE) return "g";if ( i < 0xBBF7) return "h";if ( i < 0xBFA6) return "j";if ( i < 0xC0AC) return "k";if ( i < 0xC2E8) return "l";if ( i < 0xC4C3) return "m";if ( i < 0xC5B6) return "n";if ( i < 0xC5BE) return "o";if ( i < 0xC6DA) return "p";if ( i < 0xC8BB) return "q";if ( i < 0xC8F6) return "r";if ( i < 0xCBFA) return "s";if ( i < 0xCDDA) return "t";if ( i < 0xCEF4) return "w";if ( i < 0xD1B9) return "x";if ( i < 0xD4D1) return "y";if ( i < 0xD7FA) return "z";return "*";public static void main(String []g){System.out.println(getPYString("中国,,;$#@&avc")); }}。
中文转拼音——精选推荐
中⽂转拼⾳以下这个类是个⼯具类,⽤来实现中⽂转拼⾳:public class LanguageTransform{/// <summary>/// 汉字转换成全拼的拼⾳/// </summary>/// <param name="Chstr">汉字字符串</param>/// <returns>转换后的拼⾳字符串</returns>public string StrConvertToPinyin(string Chstr){return GetPinyin(Chstr);}/// <summary>/// 汉字转换成全拼的拼⾳/// </summary>/// <param name="Chstr">汉字字符串</param>/// <param name="IsLoweCase">是否⼩写(默认⼩写即false)</param>/// <param name="Interval">拼⾳之间的间隔(默认⽆间隔)</param>/// <returns>转换后的拼⾳字符串</returns>public string StrConvertToPinyin(string Chstr, bool IsLoweCase = false, string Interval = "") {return GetPinyin(Chstr, IsLoweCase, Interval);}#region 获取拼⾳/// <summary>/// 获取拼⾳/// </summary>/// <param name="Chstr">汉字字符串</param>/// <param name="IsLoweCase">是否⼩写(默认⼩写即false)</param>/// <param name="Interval">拼⾳之间的间隔(默认⽆间隔)</param>/// <returns>转换后的拼⾳字符串</returns>private string GetPinyin(string Chstr, bool IsLoweCase = false, string Interval = ""){Regex reg = new Regex("^[\u4e00-\u9fa5]$");//验证是否输⼊汉字byte[] arr = new byte[2];string pystr = "";int asc = 0, M1 = 0, M2 = 0;char[] mChar = Chstr.ToCharArray();//获取汉字对应的字符数组for (int j = 0; j < mChar.Length; j++){//如果输⼊的是汉字if (reg.IsMatch(mChar[j].ToString())){arr = System.Text.Encoding.Default.GetBytes(mChar[j].ToString());M1 = (short)(arr[0]);M2 = (short)(arr[1]);asc = M1 * 256 + M2 - 65536;if (asc > 0 && asc < 160){pystr += mChar[j];}else{switch (asc){case -9254:pystr += "Zhen"; break;case -8985:pystr += "Qian"; break;case -5463:pystr += "Jia"; break;case -8274:pystr += "Ge"; break;case -5448:pystr += "Ga"; break;case -5447:pystr += "La"; break;case -4649:pystr += "Chen"; break;case -5436:pystr += "Mao"; break;case -5213:pystr += "Mao"; break;case -3597:pystr += "Die"; break;case -5659:pystr += "Tian"; break;default:for (int i = (getValue.Length - 1); i >= 0; i--){if (getValue[i] <= asc) //判断汉字的拼⾳区编码是否在指定范围内{pystr += getName[i] + Interval;//如果不超出范围则获取对应的拼⾳break;}}break;}}}else //如果不是汉字{pystr += mChar[j].ToString();//如果不是汉字则返回}}if (IsLoweCase)return pystr.ToLower();//返回获取到的汉字拼⾳elsereturn pystr;}#endregion#region 拼⾳区编码数组//定义拼⾳区编码数组private static int[] getValue = new int[]{-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036, -20032,-20026,-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763, -19756,-19751,-19746,-19741,-19739,-19728,-19725,-19715,-19540,-19531,-19525,-19515, -19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,-19261,-19249, -19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006, -19003,-18996,-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735, -18731,-18722,-18710,-18697,-18696,-18526,-18518,-18501,-18490,-18478,-18463,-18448, -18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183, -18181,-18012, -17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752, -17733,-17730,-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482, -17468,-17454,-17433,-17427,-17417,-17202,-17185,-16983,-16970,-16942,-16915,-16733, -16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,-16452,-16448, -16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216, -16212,-16205,-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944, -15933,-15920,-15915,-15903,-15889,-15878,-15707,-15701,-15681,-15667,-15661,-15659, -15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,-15408,-15394, -15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150, -15149,-15144,-15143,-15141,-15140,-15139,-15128,-15121,-15119,-15117,-15110,-15109, -14941,-14937,-14933,-14930,-14929,-14928,-14926,-14922,-14921,-14914,-14908,-14902, -14894,-14889,-14882,-14873,-14871,-14857,-14678,-14674,-14670,-14668,-14663,-14654, -14645,-14630,-14594,-14429,-14407,-14399,-14384,-14379,-14368,-14355,-14353,-14345, -14170,-14159,-14151,-14149,-14145,-14140,-14137,-14135,-14125,-14123,-14122,-14112, -14109,-14099,-14097,-14094,-14092,-14090,-14087,-14083,-13917,-13914,-13910,-13907, -13906,-13905,-13896,-13894,-13878,-13870,-13859,-13847,-13831,-13658,-13611,-13601,-13406,-13404,-13400,-13398,-13395,-13391,-13387,-13383,-13367,-13359,-13356,-13343, -13340,-13329,-13326,-13318,-13147,-13138,-13120,-13107,-13096,-13095,-13091,-13076, -13068,-13063,-13060,-12888,-12875,-12871,-12860,-12858,-12852,-12849,-12838,-12831, -12829,-12812,-12802,-12607,-12597,-12594,-12585,-12556,-12359,-12346,-12320,-12300, -12120,-12099,-12089,-12074,-12067,-12058,-12039,-11867,-11861,-11847,-11831,-11798, -11781,-11604,-11589,-11536,-11358,-11340,-11339,-11324,-11303,-11097,-11077,-11067, -11055,-11052,-11045,-11041,-11038,-11024,-11020,-11019,-11018,-11014,-10838,-10832, -10815,-10800,-10790,-10780,-10764,-10587,-10544,-10533,-10519,-10331,-10329,-10328, -10322,-10315,-10309,-10307,-10296,-10281,-10274,-10270,-10262,-10260,-10256,-10254 };//定义拼⾳数组private static string[] getName = new string[]{ "A","Ai","An","Ang","Ao","Ba","Bai","Ban","Bang","Bao","Bei","Ben", "Beng","Bi","Bian","Biao","Bie","Bin","Bing","Bo","Bu","Ba","Cai","Can", "Cang","Cao","Ce","Ceng","Cha","Chai","Chan","Chang","Chao","Che","Chen","Cheng", "Chi","Chong","Chou","Chu","Chuai","Chuan","Chuang","Chui","Chun","Chuo","Ci","Cong", "Cou","Cu","Cuan","Cui","Cun","Cuo","Da","Dai","Dan","Dang","Dao","De", "Deng","Di","Dian","Diao","Die","Ding","Diu","Dong","Dou","Du","Duan","Dui", "Dun","Duo","E","En","Er","Fa","Fan","Fang","Fei","Fen","Feng","Fo", "Fou","Fu","Ga","Gai","Gan","Gang","Gao","Ge","Gei","Gen","Geng","Gong", "Gou","Gu","Gua","Guai","Guan","Guang","Gui","Gun","Guo","Ha","Hai","Han", "Hang","Hao","He","Hei","Hen","Heng","Hong","Hou","Hu","Hua","Huai","Huan", "Huang","Hui","Hun","Huo","Ji","Jia","Jian","Jiang","Jiao","Jie","Jin","Jing", "Jiong","Jiu","Ju","Juan","Jue","Jun","Ka","Kai","Kan","Kang","Kao","Ke", "Ken","Keng","Kong","Kou","Ku","Kua","Kuai","Kuan","Kuang","Kui","Kun","Kuo", "La","Lai","Lan","Lang","Lao","Le","Lei","Leng","Li","Lia","Lian","Liang", "Liao","Lie","Lin","Ling","Liu","Long","Lou","Lu","Lv","Luan","Lue","Lun", "Luo","Ma","Mai","Man","Mang","Mao","Me","Mei","Men","Meng","Mi","Mian", "Miao","Mie","Min","Ming","Miu","Mo","Mou","Mu","Na","Nai","Nan","Nang", "Nao","Ne","Nei","Nen","Neng","Ni","Nian","Niang","Niao","Nie","Nin","Ning", "Niu","Nong","Nu","Nv","Nuan","Nue","Nuo","O","Ou","Pa","Pai","Pan", "Pang","Pao","Pei","Pen","Peng","Pi","Pian","Piao","Pie","Pin","Ping","Po", "Pu","Qi","Qia","Qian","Qiang","Qiao","Qie","Qin","Qing","Qiong","Qiu","Qu", "Quan","Que","Qun","Ran","Rang","Rao","Re","Ren","Reng","Ri","Rong","Rou", "Ru","Ruan","Rui","Run","Ruo","Sa","Sai","San","Sang","Sao","Se","Sen", "Seng","Sha","Shai","Shan","Shang","Shao","She","Shen","Sheng","Shi","Shou","Shu", "Shua","Shuai","Shuan","Shuang","Shui","Shun","Shuo","Si","Song","Sou","Su","Suan", "Sui","Sun","Suo","Ta","Tai","Tan","Tang","Tao","Te","Teng","Ti","Tian", "Tiao","Tie","Ting","Tong","Tou","Tu","Tuan","Tui","Tun","Tuo","Wa","Wai", "Wan","Wang","Wei","Wen","Weng","Wo","Wu","Xi","Xia","Xian","Xiang","Xiao", "Xie","Xin","Xing","Xiong","Xiu","Xu","Xuan","Xue","Xun","Ya","Yan","Yang", "Yao","Ye","Yi","Yin","Ying","Yo","Yong","You","Yu","Yuan","Yue","Yun","Za", "Zai","Zan","Zang","Zao","Ze","Zei","Zen","Zeng","Zha","Zhai","Zhan", "Zhang","Zhao","Zhe","Zhen","Zheng","Zhi","Zhong","Zhou","Zhu","Zhua","Zhuai","Zhuan", "Zhuang","Zhui","Zhun","Zhuo","Zi","Zong","Zou","Zu","Zuan","Zui","Zun","Zuo"};#endregion}。
Javascript实现汉字和拼音互转的终极方案
Javascript实现汉字和拼⾳互转的终极⽅案前⾔中⽂汉字和拼⾳互转是很多地⽅都会遇到的,本⽂是精⼼整理并修改了⽹上⼏种常见的字典⽂件并简单封装了⼀下可以直接拿来⽤的⼯具库。
有需要的下⾯来⼀起看看。
汉字转拼⾳:拼⾳转汉字:汉字与拼⾳相关知识普及汉字范围⼀般认为Unicode编码中的汉字范围是 /^[\u2E80-\u9FFF]+$/ (11904-40959) ,但是其中有很多不是汉字,或者说是可以读的汉字,本⽂⽤到的⼏个字典⽂件的汉字范围均是/^[\u4E00-\u9FA5]+$/,也就是(19968-40869),另外还有⼀个单独的汉字〇,其Unicode位置是12295。
拼⾳组合汉字有21个声母:b, p, m, f, d, t, n, l, g, k, h, j, q, x, zh, ch, sh, r, z, c, s,24个韵母,其中单韵母有6个:a, o, e, i, u, v, 复韵母有18个:ai , ei, ui , ao, ou, iu , ie, ve, er, an , en , in, un , vn , ang, eng, ing , ong,假设声母和韵母两两组合的话,会有24X21=504种组合,实际情况是有些组合是没有意义的,⽐如bv, gie, ve等,去除这部分后,还剩余412种。
拼⾳字典⽂件按照字典⽂件的⼤⼩从⼩到⼤依次介绍。
字典⼀:拼⾳⾸字母该字典⽂件的内容⼤致如下:/*** 拼⾳⾸字母字典⽂件*/var pinyin_dict_firstletter = {};pinyin_dict_firstletter.all = "YDYQSXMWZSSXJBYMGCCZQPSSQBYCDSCDQLDYLYBSSJG...";pinyin_dict_firstletter.polyphone = {"19969":"DZ","19975":"WM","19988":"QJ","20048":"YL",...};该数据字典将Unicode字符中 4E00 (19968)- 9FA5 (40869)共计20902个汉字的拼⾳⾸字母拼接在⼀起得到⼀个很长的字符串,然后再将有多⾳字的汉字(共计370个多⾳字)单独列出来。
java获取汉字拼音或拼音首字母
Java获取汉字拼音或拼音首字母Posted on 2012-04-18 21:58 myfavorite阅读(1251) 评论(0)编辑收藏在进行模糊查询时,经常用到使用汉字拼音或者首字母进行匹配查询。
以下是获取汉字拼音或者首字母的java代码实现package com;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 GetPinyin {/*** 得到全拼** @param src* @return*/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 (ng.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);t4 += t2[0];} else {t4 += ng.Character.toString(t1[i]);}}return t4;} catch (BadHanyuPinyinOutputFormatCombination e1) {e1.printStackTrace();}return t4;}/*** 得到中文首字母** @param str* @return*/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码** @param cnStr* @return*/public static String getCnASCII(String cnStr) {StringBuffer strBuf = new StringBuffer();byte[] bGBK = cnStr.getBytes();for (int i = 0; i < bGBK.length; i++) {// System.out.println(Integer.toHexString(bGBK[i]&0xff));strBuf.append(Integer.toHexString(bGBK[i] & 0xff));}return strBuf.toString();}public static void main(String[] args) {String cnStr = "戬浜";System.out.println(getPingYin(cnStr));System.out.println(getPinYinHeadChar(cnStr)); }}。
汉语转拼音之pinyin4j - Java综合 - Java - JavaEye论坛
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
StringBuffer tempPinying = new StringBuffer();
// 如果是中文
if (src > 128) {
}
public static String hanziToPinyin(String hanzi){
return hanziToPinyin(hanzi," ");
srcStr[i] = charToPinyin(srcChar[i], isPolyphone,
separator);
}
return srcStr;
}
*/
public static String hanziToPinyin(String hanzi,String separator){
// 创建汉语拼音处理类
HanyuPinyinOutputFormat defaultFormat = new
if (isPolyphone && null != separator) {
for (int i = 0; i < strs.length; i++) {
tempPinying.append(strs[i]);
defaultFormat);
汉字转拼音首字母
91. * @author wyh
92. * @param strJaggedArray
93. * @return
94. */
95. private static String[][] DoExchange(String[][] strJaggedArray){
85. String[][] temp = DoExchange(strJaggedArray);
86. return temp[0];
87. }
88.
89. /**
51.
52. String[][] temp = new String[src.length()][];
53. for(int i=0;i<srcChar.length;i++){
54. char c = srcChar[i];
106. Index ++;
107. }
108. }
109. String[][] newArray = new String[len-1][];
41. if(src!=null && !src.trim().equalsIgnoreCase("")){
42. char[] srcChar ;
43. srcChar=src.toCharArray();
44. //汉语拼音格式输出类
6.import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
7.import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
java pinyinutil 原理
java pinyinutil 原理
1. 加载拼音数据:PinyinUtil 需要加载一个包含汉字和对应拼音的拼音库或对照表。
这些数据通常以文件形式存储,PinyinUtil 在初始化时将读取并解析这些数据。
2. 分析输入字符:当调用 PinyinUtil 的转换方法时,它会接受一个中文字符串作为输入。
首先,它会逐个分析输入字符串的每个字符。
3. 匹配汉字的拼音:对于每个中文字符,PinyinUtil 会查找拼音库或对照表中与之对应的拼音。
这通常是通过字符的 Unicode 编码进行匹配的。
4. 构建拼音结果:一旦找到了汉字的拼音,PinyinUtil 将该拼音添加到最终的拼音结果中。
5. 处理多音字:由于中文中存在多音字,即一个汉字对应多个发音的情况,PinyinUtil 通常提供了处理多音字的策略,例如只选择第一个发音、选择常用发音等。
6. 输出拼音结果:经过上述步骤,PinyinUtil 将最终的拼音结果返回给调用者。
java写的能将汉字翻译成拼音的类
if(num>0&&num<160){
return String.valueOf((char)num);
}
else if(num<-20319||num>-10247){
return "";
}
else{
if(ht.size()==0){
b();
ht.put("-19006","de");
ht.put("-19003","deng");
ht.put("-18996","di");
ht.put("-18977","dian");
ht.put("-18961","diao");
ht.put("-18952","die");
ht.put("-18783","ding");
这个是为什么呢?附上我找到的一个例子。
<%@ page language="java" import="java.util.*" contentType="text/html" %>
<%!
Hashtable ht=new Hashtable(10);
String g(Integer n){
ht.put("-19784","bu");
ht.put("-19775","ca");
JAVA实现汉字转全拼或首字母
spellMap.put("chu", -19467);
spellMap.put("chuai", -19289);
spellMap.put("chuan", -19288);
spellMap.put("chen", -19525);
spellMap.put("cheng", -19515);
spellMap.put("chi", -19500);
spellMap.put("chong", -19484);
spellMap = Collections.synchronizedMap(new LinkedHashMap<String, Integer>(396)) ;
uncommonWordsMap = Collections.synchronizedMap(new LinkedHashMap<Character, String>(200)) ;
spellMap.put("’an", -20304);
spellMap.put("’ang", -20295);
spellMap.put("’ao", -20292);
spellMap.put("ba", -20283);
JAVA中文转换
/*** 取中文字符的拼音首字母* @author*/public class GB2Alpha {/*** 字母Z使用了两个标签,这里有27个值,i, u, v都不做声母, 跟随前面的字母*/private char[] chartable ={'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈','击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然','撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'};private char[] alphatable ={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};private int[] table = new int[27];/*** 初始化*/{for (int i = 0; i < 27; ++i) {table = gbValue(chartable);}}public GB2Alpha() {}/*** 主函数,输入字符,得到他的声母,英文字母返回对应的大写字母,其他非简体汉字返回'0' */public char Char2Alpha(char ch) {if (ch >= 'a' && ch <= 'z')return (char) (ch - 'a' + 'A');if (ch >= 'A' && ch <= 'Z')return ch;int gb = gbValue(ch);if (gb < table[0])return '0';int i;for (i = 0; i < 26; ++i) {if (match(i, gb))break;}if (i >= 26)return '0';elsereturn alphatable;}/*** 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串* @param SourceStr* @return*/public String String2Alpha(String SourceStr) {String Result = "";int StrLength = SourceStr.length();int i;try {for (i = 0; i < StrLength; i++) {Result += Char2Alpha(SourceStr.charAt(i));}} catch (Exception e) {Result = "";}return Result;}private boolean match(int i, int gb) {if (gb < table)return false;int j = i + 1;//字母Z使用了两个标签while (j < 26 && (table == table))++j;if (j == 26)return gb <= table;elsereturn gb < table;}/*** 取出汉字的编码*/private int gbValue(char ch) {String str = new String();str += ch;try {byte[] bytes = str.getBytes("GB2312");if (bytes.length < 2)return 0;return (bytes[0] << 8 & 0xff00) + (bytes[1] &0xff);} catch (Exception e) {return 0;}}public static void main(String[] args) {GB2Alpha obj1 = new GB2Alpha();System.out.println(obj1.String2Alpha("小华"));return;}}。
【Java】汉字转为拼音
在Java中将汉字转化为拼音,需要用到大名鼎鼎的jar包,pinyin4j 下载导入,或直接复制依赖 maven地址: pom.xml:
【Java】汉字转为拼音
<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version>
}
</dependency>
使用பைடு நூலகம்例:
import net.sourceforge.pinyin4j.PinyinHelper;
import java.util.Scanner;
public class PinyinTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); // 这里为什么用字符串数组接受返回的拼音呢,因为中文有多音字,输入“长”,会返回zhang3和chang2,(2,3表示第几声) String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(str.charAt(0)); // str.charAt(0) 第一个汉字 for (String py : pinyin) { System.out.println(py); } }
pinyin4j汉语转拼音-fuliang-JavaEye技术网站
pinyin4j汉语转拼音-fuliang-JavaEye技术网站pinyin4j 汉语转拼音文章分类:Java编程pinyin4j提供了汉语转拼音的一个开源库,支持多种拼音系统:汉语拼音、通用拼音、威氏拼音、注音二式、雅礼、国语罗马字。
但当前版本貌似不支持根据上下文来识别多音字的功能。
写个小例子试了一下,多音字简单的取第一个读音:Java代码1.package edu.jlu.fuliang;2.3.import net.sourceforge.pinyin4j.PinyinHelper;4.import net.sourceforge.pinyin4j.format.HanyuPinyinCaseT ype;5.import net.sourceforge.pinyin4j.format.HanyuPinyinOutp utFormat;6.import net.sourceforge.pinyin4j.format.HanyuPinyinToneT ype;7.import net.sourceforge.pinyin4j.format.HanyuPinyinVCha rType;8.import net.sourceforge.pinyin4j.format.exception.BadHan yuPinyinOutputFormatCombination;9.10.public class Pinyin4jTest {11.public static String getPinYin(String src) {12.StringBuilder pinyinBuf = new StringBuilder();13.HanyuPinyinOutputFormat outputFormat = new Hany uPinyinOutputFormat();14.outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);15.outputFormat.setT oneType(HanyuPinyinT oneType.WIT HOUT_TONE);16.outputFormat.setVCharType(HanyuPinyinVCharType. WITH_V);17.18.try {19.for (int i = 0; i < src.length(); i++) {20.String[] pinYins = PinyinHelper.toHanyuPinyinStringAr ray(src.charAt(i),21.outputFormat);22.if (pinYins != null && pinYins.length > 0) {// 汉语23.pinyinBuf.append(pinYins[0]);24.} else {// 非汉语25.pinyinBuf.append(src.charAt(i));26.}27.}28.} catch (BadHanyuPinyinOutputFormatCombination e) {29. e.printStackTrace();30.}31.return pinyinBuf.toString();32.}33.34.public static void main(String[] args) {35.System.out.println(getPinYin("Hello,欢迎来到长春"));36.}37.}输出Hello,huanyinglaidaozhangchun。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java中文字转拼音工具类
2020-03-24 09:49:09
有时,我们需要将中文汉字转为拼音存储,例如用于检索时,直接输入拼音或者首字母也能检索出我们想要的结果,这时我们便需要将汉字转拼音
•1、首先我们在 pom.xml 文件中引入需要的 maven 依赖
com.belerweb pinyin4j 2.5.1
•2、汉字转拼音工具类代码如下 Pinyin4jUtil.java:
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.BadHanyuPinyinOutputFormatComb ination;
/**
–中文转化为拼音工具类
–@author lizhou
–*/ public class Pinyin4jUtil {
/**
•获得汉语拼音首字母大写
•@param chines 汉字
•@return */ public static String getAlpha2UpperCase(String
chines) { String pinyinNam e = "“; char[] nameChar =
chines.toCharArray(); HanyuPinyinOutputFormat defaultFormat
= new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_T
ONE); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] >
128) { try { pinyinName +=
PinyinHelper.toHanyuPinyinStringArray(nameChar[i],
defaultFormat)[0].charAt(0); } catch
(BadHanyuPinyinOutputFormatCombination e)
{ System.out.println(”获得汉语拼音首字母异常:}" +
e.getMessage()); } } else { pinyinName += nameChar[i]; } } return
pinyinName; }
/**
•将字符串中的中文转化为拼音,英文字符不变
•@param inputString 汉字
•@return */ public static String getPingYin(String inputString) { HanyuPinyinOutputFormat format = new
HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V); String
output = "“; if (null != inputString && 0 < inputString.length()
&& !”null“.equals(inputString)) { char[] inpu t =
inputString.trim().toCharArray(); try { for (int i = 0; i <
input.length; i++) { if
(Character.toString(input[i]).matches(”[\u4E00-\u9FA5]+“))
{ String[] temp =
PinyinHelper.toHanyuPinyinStringArray(input[i], format); output += temp[0]; } else { output += Character.toString(input[i]); } } }
catch (BadHanyuPinyinOutputFormatCombination e)
{ System.out.println(”将字符串中的中文转化为拼音,英文字符不
变异常 :" + e.getMessage()); } } else { return ""; } return output; } /**
•汉字转换位汉语拼音首字母,英文字符不变
•@param chines 汉字
•@return 拼音 */ public static String converterToFirstSpell(String chines) { String pinyinName = "“; char[] nameChar =
chines.toCharArray(); HanyuPinyinOutputFormat defaultFormat
= new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_T
ONE); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] >
128) { try { pinyinName +=
PinyinHelper.toHanyuPinyinStringArray(nameChar[i],
defaultFormat)[0].charAt(0); } catch
(BadHanyuPinyinOutputFormatCombination e)
{ System.out.println(”汉字转换位汉语拼音首字母,英文字符不
变异常 :" + e.getMessage()); } } else { pinyinName +=
nameChar[i]; } } return pinyinName; }
/**
•汉字转换位汉语拼音首字母,英文字符不变(小写)
•@param chines 汉字
•@return 拼音 */ public static String
converterToFirstSpellSmal(String chines) { String pinyinName =
"“; char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new
HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_T
ONE); for (int i = 0; i < nameChar.length; i++) { if (nameChar[i] >
128) { try { if
(Character.toString(nameChar[i]).matches(”[\u4E00-\u9FA5]+“))
{ pinyinName +=
PinyinHelper.toHanyuPinyinStringArray(nameChar[i],
defaultFormat)[0].charAt(0); } else { pinyinName +=
nameChar[i]; } } catch
(BadHanyuPinyinOutputFormatCombination e)
{ System.out.println(”汉字转换位汉语拼音首字母,英文字符不
变异常:" + e.getMessage()); } } else { pinyinName +=
nameChar[i]; } } return pinyinName; } }
如您在阅读中发现不足,欢迎留言!!!。