net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination Java Examples
The following examples show how to use
net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PinyinHelper.java From elasticsearch-analysis-lc-pinyin with Artistic License 2.0 | 6 votes |
public static String[] convertChineseToPinyin(char ch, boolean withFirstLetter) { String[] pinyinArray = null; try { pinyinArray = net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(ch, format); if (pinyinArray == null) { return new String[0]; } LinkedHashSet<String> pinyinSet = new LinkedHashSet<String>(); for (int idx = 0; idx < pinyinArray.length; idx++) { pinyinSet.add(pinyinArray[idx]); if (withFirstLetter) { pinyinSet.add(String.valueOf(pinyinArray[idx].charAt(0))); } } pinyinArray = pinyinSet.toArray(new String[pinyinSet.size()]); } catch (BadHanyuPinyinOutputFormatCombination ex) { logger.error(ex.getMessage(), ex); pinyinArray = new String[]{String.valueOf(ch)}; } return pinyinArray; }
Example #2
Source File: T9SearchSupport.java From T9Search with Apache License 2.0 | 6 votes |
@Override public String[] getPinyin(char input) { Set<String> set = SET_POOL.acquire(); if (set == null) { set = new HashSet<>(); } try { String[] pys = PinyinHelper.toHanyuPinyinStringArray(input, FORMAT); if (pys == null) return null; // 去除重复 set.addAll(Arrays.asList(pys)); return set.toArray(new String[set.size()]); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } finally { set.clear(); SET_POOL.release(set); } return null; }
Example #3
Source File: PinYinUtil.java From sloth with Apache License 2.0 | 6 votes |
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(); }
Example #4
Source File: HanyuPinyinHelper.java From opscenter with Apache License 2.0 | 6 votes |
/** * 取第一个汉字的第一个字符 @Title: getFirstLetter @Description: TODO @return String @throws */ public static String getFirstLetter(String ChineseLanguage) { char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 输出拼音全部大写 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调 try { String str = String.valueOf(cl_chars[0]); if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母 hanyupinyin = PinyinHelper.toHanyuPinyinStringArray(cl_chars[0], defaultFormat)[0].substring(0, 1); ; } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字 hanyupinyin += cl_chars[0]; } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[0]; } else {// 否则不转换 } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("字符不能转成汉语拼音"); } return hanyupinyin; }
Example #5
Source File: HanyuPinyinHelper.java From opscenter with Apache License 2.0 | 6 votes |
public static String getPinyinString(String ChineseLanguage) { char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部大写 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调 try { for (int i = 0; i < cl_chars.length; i++) { String str = String.valueOf(cl_chars[i]); if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0]; } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字 hanyupinyin += cl_chars[i]; } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[i]; } else {// 否则不转换 } } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("字符不能转成汉语拼音"); } return hanyupinyin; }
Example #6
Source File: HanyuPinyinHelper.java From opscenter with Apache License 2.0 | 6 votes |
public static String getFirstLetters(String ChineseLanguage, HanyuPinyinCaseType caseType) { char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(caseType);// 输出拼音全部大写 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调 try { for (int i = 0; i < cl_chars.length; i++) { String str = String.valueOf(cl_chars[i]); if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1); } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字 hanyupinyin += cl_chars[i]; } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母 hanyupinyin += cl_chars[i]; } else {// 否则不转换 hanyupinyin += cl_chars[i];// 如果是标点符号的话,带着 } } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("字符不能转成汉语拼音"); } return hanyupinyin; }
Example #7
Source File: HanyuPinyinHelper.java From opscenter with Apache License 2.0 | 6 votes |
/** * 将文字转为汉语拼音 * * @param chineselanguage 要转成拼音的中文 */ public String toHanyuPinyin(String ChineseLanguage) { char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调 defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V); try { for (int i = 0; i < cl_chars.length; i++) { if (i == 0) { if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0]; } else {// 如果字符不是中文,则不转换 hanyupinyin += cl_chars[i]; } } else { hanyupinyin += getPinYinHeadChar(String.valueOf(cl_chars[i])); } } } catch (BadHanyuPinyinOutputFormatCombination e) { System.out.println("字符不能转成汉语拼音"); } return hanyupinyin; }
Example #8
Source File: StringHelper.java From openzaly with Apache License 2.0 | 6 votes |
public static String toLatinPinYin(String text) { // format for string HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写 format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); char[] input = StringUtils.trimToEmpty(text).toCharArray(); StringBuffer result = new StringBuffer(); try { for (char ch : input) { if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]+")) { String[] temp = PinyinHelper.toHanyuPinyinStringArray(ch, format); result.append(temp[0]); } else result.append(Character.toString(ch)); } } catch (BadHanyuPinyinOutputFormatCombination e) { logger.error("string to latin pinyin error", e); } return result.toString(); }
Example #9
Source File: PinyinUtil.java From jeecg with Apache License 2.0 | 6 votes |
/** * 汉字转换位汉语拼音首字母,英文字符不变 * * @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; }
Example #10
Source File: PinYinUtil.java From code with Apache License 2.0 | 6 votes |
/** * @param chars 汉字字符数组 * @param pinyinFormat 汉字转拼音格式化模式 */ public static String words2Pinyin(char[] chars, HanyuPinyinOutputFormat pinyinFormat) { StringBuilder pinyinBuilder = new StringBuilder(); try { for (char word : chars) { //是否为汉字字符 if (Character.toString(word).matches("[\\u4E00-\\u9FA5]+")) { // 多音字 String[] py = PinyinHelper.toHanyuPinyinStringArray(word, pinyinFormat); pinyinBuilder.append(py[0]); } else { pinyinBuilder.append(word); } } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return pinyinBuilder.toString(); }
Example #11
Source File: PinyinUtil.java From jeecg with Apache License 2.0 | 6 votes |
/** * 汉字转换位汉语拼音,英文字符不变 * * @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; }
Example #12
Source File: Pinyin4jUtil.java From dpCms with Apache License 2.0 | 6 votes |
/** * * @param inputString 赵宝东 * @return zhaobd */ public static String toHanyuPinyin(String inputString){ StringBuffer outputString = new StringBuffer(); try { char[] cs = inputString.toCharArray(); for(byte i=0;i<cs.length;i++){ String[] hanyuPinyin = PinyinHelper.toHanyuPinyinStringArray(cs[i], hanyuPinyinOutputFormat); String s = hanyuPinyin[0]; if(i==0){ outputString.append(s); }else if(i>0){ outputString.append(s.substring(0, 1)); } } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return outputString.toString(); }
Example #13
Source File: PinYinUtil.java From MicroCommunity with Apache License 2.0 | 6 votes |
/** * 获取汉字串拼音首字母,英文字符不变 * @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(); }
Example #14
Source File: PinYinUtil.java From MicroCommunity with Apache License 2.0 | 6 votes |
/** * 获取汉字串拼音,英文字符不变 * @param chinese 汉字串 * @return 汉语拼音 */ public static String getFullSpell(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(); }
Example #15
Source File: AppUtil.java From NewFastFrame with Apache License 2.0 | 6 votes |
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; } }
Example #16
Source File: PinYinUtil.java From MicroCommunity with Apache License 2.0 | 6 votes |
/** * 将字符串中的中文转化为拼音,其他字符不变 * * @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); 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; }
Example #17
Source File: CommonUtils.java From TestChat with Apache License 2.0 | 6 votes |
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; } }
Example #18
Source File: ToolPinYin.java From protools with Apache License 2.0 | 6 votes |
/** * 得到 全拼 * * @param src * * @return */ public static String getPinYin(String src) throws BadHanyuPinyinOutputFormatCombination { char[] t1; 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(); for (char aT1 : t1) { // 判断是否为汉字字符 if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]+")) { t2 = PinyinHelper.toHanyuPinyinStringArray(aT1, t3); t4.append(t2[0]); } else { t4.append(aT1); } } return t4.toString(); }
Example #19
Source File: I18nUtils.java From spring-backend-boilerplate with Apache License 2.0 | 6 votes |
public static String chineseToPinyin(String str) { if (str == null || StringUtils.isEmpty(str)) { return str; } StringBuilder sb = new StringBuilder(); for (char c : str.toCharArray()) { try { String[] result = PinyinHelper.toHanyuPinyinStringArray(c, format); if (result != null) { sb.append(result[0]); } } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) { badHanyuPinyinOutputFormatCombination.printStackTrace(); } } return sb.toString(); }
Example #20
Source File: PinyinUtil.java From jeewx with Apache License 2.0 | 6 votes |
/** * 汉字转换位汉语拼音,英文字符不变 * * @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; }
Example #21
Source File: ChinesUtil.java From utils with Apache License 2.0 | 6 votes |
/** * 将字符串中的中文转化为拼音,其他字符不变. * * @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); char[] input = inputString.trim().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 (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return output.toString(); }
Example #22
Source File: ChinesUtil.java From utils with Apache License 2.0 | 6 votes |
/** * 获取汉字串拼音首字母,英文字符不变. * * @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(); }
Example #23
Source File: ChinesUtil.java From utils with Apache License 2.0 | 6 votes |
/** * 获取汉字串拼音,英文字符不变. * * @param chinese 汉字串 * @return 汉语拼音 */ public static String getFullSpell(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(); }
Example #24
Source File: ChineseToEnglish.java From AndroidDemo with MIT License | 6 votes |
/** * 返回一个字的拼音 */ 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 "#"; } }
Example #25
Source File: PinyinUtils.java From springboot-admin with Apache License 2.0 | 6 votes |
/** * 将文字转为汉语拼音 * @param ChineseLanguage 要转成拼音的中文 */ private static String toHanyuPinyin(String ChineseLanguage, HanyuPinyinCaseType caseType){ char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(caseType); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调 defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ; try { for (int i=0; i<cl_chars.length; i++){ if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){// 如果字符是中文,则将中文转为汉语拼音 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0]; } else {// 如果字符不是中文,则不转换 hanyupinyin += cl_chars[i]; } } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return hanyupinyin; }
Example #26
Source File: PinyinUtils.java From springboot-admin with Apache License 2.0 | 6 votes |
/** * 取第一个汉字的第一个字符 * @param ChineseLanguage 要转成拼音的中文 * @param caseType 大写还是小写 * @return */ private static String getFirstLetter(String ChineseLanguage, HanyuPinyinCaseType caseType){ char[] cl_chars = ChineseLanguage.trim().toCharArray(); String hanyupinyin = ""; HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(caseType); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调 try { String str = String.valueOf(cl_chars[0]); if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母 hanyupinyin = PinyinHelper.toHanyuPinyinStringArray(cl_chars[0], defaultFormat)[0].substring(0, 1); } else {// 如果字符不是中文,则不转换 hanyupinyin += cl_chars[0]; } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return hanyupinyin; }
Example #27
Source File: PinyinUtil.java From jeewx with Apache License 2.0 | 6 votes |
/** * 汉字转换位汉语拼音首字母,英文字符不变 * * @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; }
Example #28
Source File: ChineseToEnglish.java From MeetMusic with Apache License 2.0 | 6 votes |
/** * 返回一个字的拼音 */ 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 "#"; } }
Example #29
Source File: PinyinTest.java From TinyPinyin with Apache License 2.0 | 6 votes |
@Test public void testIsChinese() throws BadHanyuPinyinOutputFormatCombination { char[] allChars = allChars(); final int allCharsLength = allChars.length; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setCaseType(HanyuPinyinCaseType.UPPERCASE); format.setVCharType(HanyuPinyinVCharType.WITH_V); for (int i = 0; i < allCharsLength; i++) { char targetChar = allChars[i]; String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(targetChar, format); if (pinyins != null && pinyins.length > 0) { // is chinese assertThat(Pinyin.isChinese(targetChar), is(true)); } else { // not chinese assertThat(Pinyin.isChinese(targetChar), is(false)); } } }
Example #30
Source File: StringHelper.java From wind-im with Apache License 2.0 | 6 votes |
public static String toLatinPinYin(String text) { // format for string HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写 format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); char[] input = StringUtils.trimToEmpty(text).toCharArray(); StringBuffer result = new StringBuffer(); try { for (char ch : input) { if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]+")) { String[] temp = PinyinHelper.toHanyuPinyinStringArray(ch, format); result.append(temp[0]); } else result.append(Character.toString(ch)); } } catch (BadHanyuPinyinOutputFormatCombination e) { logger.error("string to latin pinyin error", e); } return result.toString(); }