Java 拼音工具类 解决姓氏多音字问题

avatar 2022年04月11日18:00:51 6 2176 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

直接上代码

1、pom.xml

<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
<dependency>
	<groupId>com.belerweb</groupId>
	<artifactId>pinyin4j</artifactId>
	<version>2.5.0</version>
</dependency>

 

2、工具类

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.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class PinyinUtil
{

	/**
	 * 多音字拼音
	 */
	public static Map<String, String> FIRST_NAME_PINYIN = new HashMap<>();
	static {
		FIRST_NAME_PINYIN.put("繁", "po");
		FIRST_NAME_PINYIN.put("区", "ou");
		FIRST_NAME_PINYIN.put("仇", "qiu");
		FIRST_NAME_PINYIN.put("种", "chong");
		FIRST_NAME_PINYIN.put("单", "shan");
		FIRST_NAME_PINYIN.put("解", "xie");
		FIRST_NAME_PINYIN.put("查", "zha");
		FIRST_NAME_PINYIN.put("曾", "zeng");
		FIRST_NAME_PINYIN.put("秘", "bi");
		FIRST_NAME_PINYIN.put("乐", "yue");
		FIRST_NAME_PINYIN.put("重", "chong");
		FIRST_NAME_PINYIN.put("朴", "piao");
		FIRST_NAME_PINYIN.put("缪", " miao");
		FIRST_NAME_PINYIN.put("冼", " xian");
		FIRST_NAME_PINYIN.put("翟", "zhai");
		FIRST_NAME_PINYIN.put("折", "she");
		FIRST_NAME_PINYIN.put("黑", "he");
		FIRST_NAME_PINYIN.put("盖", "ge");
		FIRST_NAME_PINYIN.put("沈", "shen");
		FIRST_NAME_PINYIN.put("尉迟", "yuchi");
		FIRST_NAME_PINYIN.put("万俟", "moqi");
	}
    
	/**
	 * @Description: 过滤掉中文特殊字符
	 * @throws PatternSyntaxException
	 */
	public static String filterSpecialChars(String target) throws PatternSyntaxException
	{
		String regEx = "[\\uac00-\\ud7af\\u3130–\\u318F]*[\\u0800-\\u4e00`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?_]";
		Pattern pat = Pattern.compile(regEx);
		Matcher mat = pat.matcher(target);
		return mat.replaceAll("").trim();
	}


	/**
	 * 获取汉字串全拼音,英文字符不变
	 * @param chinese 汉字串
	 * @return 汉语拼音
	 */
	private static String chinese2Spell(String chinese)
	{
		try {
			chinese = PinyinUtil.filterSpecialChars(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[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
						if (pinyinStringArray != null && pinyinStringArray.length > 0) {
							pybf.append(pinyinStringArray[0]);
						}
					} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.getMessage();
					}
				} else {
					pybf.append(arr[i]);
				}
			}
			return pybf.toString();
		} catch (Exception e) {
			e.getMessage();
			return null;
		}
	}




	/**
	 * 获得完整拼音
	 * 解决姓多音字问题
	 *
	 * @param keywords
	 * @return
	 */
	public static String getFullPinyin(String keywords) {
		if(keywords == null || "".equals(keywords)) {
			return keywords;
		}
		for (Map.Entry<String, String> map : FIRST_NAME_PINYIN.entrySet()) {
			if (keywords.startsWith(map.getKey())) {
				String suffix = keywords.substring(map.getKey().length());
				return map.getValue() + chinese2Spell(suffix);
			}
		}
		return chinese2Spell(keywords);
	}

	public static void main(String[] args) {
		System.out.println(getFullPinyin("尉迟恭啊"));
		System.out.println(getFullPinyin("乐毅啊"));
		System.out.println(getFullPinyin("刘言曌"));
	}

}

 

  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0