java生成拼音

java环境下生成拼音简单示例:

使用pinyin4j-2.5.0.jar包,

/**
*
*/
package com.casco.csmis.synch;

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;

/**
* 拼音处理
*
* @author 60874
*
*/
public class GPinyin {

/**
* 生成汉语拼音<br>
* 小写,不含语调,元音ü用v代替
*
* @param hanyu
* @param nospace true去除空格等无效字符
* @return
*/
public static String genNoToneV(final String hanyu, boolean nospace) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
String copyhanyu = new String(hanyu);
if (nospace) {
copyhanyu = hanyu.trim().replaceAll(” “, “”);
}
char[] input = copyhanyu.toCharArray();
StringBuffer output = new StringBuffer(“”);
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.append(temp[0]);
} else {
output.append(Character.toString(input[i]));
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return output.toString();
}

/**
* 获取汉字串拼音首字母,英文字符不变
*
* @param chinese
* 汉字串
* @return 汉语拼音首字母
*/
public static String getFirstSpell(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[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (temp != null) {
pybf.append(temp[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString().replaceAll(“\\W”, “”).trim().toUpperCase();
}
}

前台javascript可使用pinyin.js(依赖mootools-core-1.5.1-full-compat.js)

示例:

<script src=”<c:url value=”/js/mootools-core-1.5.1-full-compat.js”/>”></script>
<script src=”<c:url value=”/js/pinyin.js”/>”></script>

var person = $(‘#formpersonname’).val();
if(person != null && person.length > 0){
var py = $(‘#formpinyin’).val();
if(py == null || py.length == 0){
var pinyin = new Pinyin();
py = pinyin.getFullChars(person);
py = py.toLowerCase().replace(/\s/g,”);
$(‘#formpinyin’).val(py);
}
}

引用:https://github.com/chinalu/JSPinyin

https://github.com/yccheok/jstock/tree/master/libs/pinyin4j-2.5.0

This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s