本文整理汇总了Java中net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat类的典型用法代码示例。如果您正苦于以下问题:Java HanyuPinyinOutputFormat类的具体用法?Java HanyuPinyinOutputFormat怎么用?Java HanyuPinyinOutputFormat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HanyuPinyinOutputFormat类属于net.sourceforge.pinyin4j.format包,在下文中一共展示了HanyuPinyinOutputFormat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAllPinYin
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
public List<String> getAllPinYin(char c){
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
Set<String> set = new HashSet<>();
try {
String[] pinYinStringArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
for(String pinYin : pinYinStringArray){
pinYin = pinYin.toLowerCase().replace("ü", "v");
if(StringUtils.isBlank(pinYin)){
continue;
}
set.add(pinYin);
set.add(String.valueOf(pinYin.charAt(0)));
charPinYin.add(pinYin);
if(pinYin.length() > charMaxPinYinLength){
charMaxPinYinLength = pinYin.length();
}
}
}catch (Exception e){
LOGGER.error("获取拼音失败", e);
}
return set.stream().sorted().collect(Collectors.toList());
}
示例2: getSinglePinYing
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
private static String getSinglePinYing(char c) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try {
String[] results = PinyinHelper.toHanyuPinyinStringArray(c, format);
if (results == null) {
// 不是汉字返回空
return null;
} else {
// 因为有可能是多音字
return results[0];
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
return null;
}
}
示例3: getFirstLetterStr
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 获取中文字符串中每个汉字的首字母 例如 “安卓”返回"AZ"。
*
* @param inputString 字符串
* @return 每个汉字的首字母组成的字符串
*/
public static String getFirstLetterStr(String inputString) {
StringBuilder sb = new StringBuilder();
char[] nameChar = inputString.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
sb.append(PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat)[0].charAt(0));
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
sb.append(nameChar[i]);
}
}
return sb.toString();
}
示例4: toPinYin
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 返回一个字的拼音
*/
public static String toPinYin(char hanzi) {
HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
hanyuPinyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
hanyuPinyin.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
String[] pinyinArray = null;
try {
//是否在汉字范围内
if (hanzi >= 0x4e00 && hanzi <= 0x9fa5) {
pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
Log.e(TAG, "toPinYin: hanzi = "+hanzi );
Log.e(TAG, "toPinYin: pinyinArray.toString() = "+pinyinArray.toString() );
}
//将获取到的拼音返回
if (pinyinArray != null && pinyinArray.length > 0) {
return pinyinArray[0];
} else {
Log.e(TAG, "toPinYin: hanzi = "+hanzi );
return "#";
}
}
示例5: converterToFirstSpell
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
public static String converterToFirstSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
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) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
示例6: getPinYin
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 将汉字转换为全拼
*
* @param src 源汉字
* @return String pin yin
*/
public static String getPinYin(String src) {
char[] t1 = src.toCharArray();
String[] t2;
// 设置汉字拼音输出的格式
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
StringBuilder t4 = new StringBuilder();
try {
for (char aT1 : t1) {
// 判断是否为汉字字符
if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(aT1, t3);// 将汉字的几种全拼都存到t2数组中
t4.append(t2[0]);// 取出该汉字全拼的第一种读音并连接到字符串t4后
} else {
// 如果不是汉字字符,直接取出字符并连接到字符串t4后
t4.append(Character.toString(aT1));
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
LOGGER.error("", e);
}
return t4.toString();
}
示例7: ToPinyin
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 汉字转为拼音
* @param chinese
* @return
*/
public static String ToPinyin(String chinese){
String pinyinStr = "";
char[] newChar = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < newChar.length; i++) {
if (newChar[i] > 128) {
try {
pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}else{
pinyinStr += newChar[i];
}
}
return pinyinStr;
}
示例8: converterToSpell
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
public static String converterToSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
示例9: getSinglePinyin
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 字到拼音
* @param zhWord
* @return
*/
public static String getSinglePinyin(char zhWord){
String pinyin[] = new String[0];
if (zhWord=='\0'){ // 判定字符是否为空
return "";
}
if (zhWord=='(' || zhWord==')' || zhWord=='/'){
return "";
}
HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
hanyuPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
try {
pinyin= PinyinHelper.toHanyuPinyinStringArray(zhWord,hanyuPinyinOutputFormat);
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
return pinyin[0];
}
示例10: getFirstLetters
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
public static String getFirstLetters(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();
}
示例11: getPinYin
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 将字符串中的中文转化为拼音,其他字符不变
*
* @param inputString
*
* @return
*/
public static String getPinYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] input = inputString.trim().toCharArray();
String output = "";
try {
for (int i = 0; i < input.length; i++) {
if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
output += temp[0];
} else
output += java.lang.Character.toString(input[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output;
}
示例12: converterToFirstSpell
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 汉字转换位汉语拼音首字母,英文字符不变
*
* @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_TONE);
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) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
示例13: converterToFirstSpell
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 汉字转换位汉语拼音首字母,英文字符不变
*
* @param chines 汉字
* @return 拼音
*/
public static String converterToFirstSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
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) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
示例14: converterToSpell
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
/**
* 汉字转换位汉语拼音,英文字符不变
*
* @param chines 汉字
* @return 拼音
*/
public static String converterToSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
示例15: getPingYin
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; //导入依赖的package包/类
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]);
}
}
return t4;
} catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4;
}