java-汉字转换拼音工具

汉字转换拼音工具类:
public class CnConvertSpellUtil {
	private static final String WORD_BASE_PATH = "/F02-GB2312-to-PuTongHua-PinYin.txt";
	private static HashMap<Character,String> spellMap = null;

	static {
		if (spellMap == null) {
			spellMap = new HashMap<Character,String>(22587);
		}
		initialize();
	}

	private CnConvertSpellUtil() {
	}

	private static void initialize() {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(new File(CnConvertSpellUtil.class
					.getResource(WORD_BASE_PATH).getPath())));
			String lineContent = reader.readLine();
			String[] contentAry = new String[2];
		    while (StringUtils.isNotBlank(lineContent)) {
				if (StringUtils.isBlank(lineContent.trim())) {
					continue;
				}
				
				contentAry = lineContent.split(",");
				
				if(spellMap.containsKey(contentAry[0].charAt(0))){
					String value = spellMap.get(contentAry[0].charAt(0));
					spellMap.put(contentAry[0].charAt(0), value + "," +contentAry[1]);
				}else{
					spellMap.put(contentAry[0].charAt(0), contentAry[1]);
				}
				
				lineContent = reader.readLine();
			}
		    
		} catch (FileNotFoundException e) {
			throw new ServiceException("加载 "+WORD_BASE_PATH+" 文件失败!", e);
		} catch (IOException e) {
			throw new ServiceException("读取 "+WORD_BASE_PATH+" 文件失败!", e);
		}finally{
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					throw new ServiceException("关闭文件失败!", e);
				}				
			}
		}
	}
	
	public static String[] getFullSpell(String cnStr){
		if (StringUtils.isBlank(cnStr)) {
			return new String[0];
		}

		char[] chars = cnStr.toCharArray();
		String[] retStrs = new String[chars.length];
		for (int i = 0; i < chars.length; i++) {
			String spell = spellMap.get(chars[i]);
			if(StringUtils.isBlank(spell)){
				retStrs[i] = String.valueOf(chars[i]);
				continue;
			}
			
//			retStrs[i] = spell;
			retStrs[i] = StringUtils.substringBefore(spell, ",");
		} 

		return retStrs;
	}
	
	public static String getFirstSpell(String cnStr){
		if (StringUtils.isBlank(cnStr)) {
			return cnStr;
		}

		char[] chars = cnStr.toCharArray();
		StringBuffer retBuf = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			String spell = spellMap.get(chars[i]);
			if(StringUtils.isBlank(spell)){
				retBuf.append(chars[i]);
				continue;
			}
			
			if(spell.indexOf(",") != -1){
				String[] spells = spell.split(",");
				String spellsStr = "";			
				Set<String> firstSpellSet = new HashSet<String>();
				for (int j = 0; j < spells.length; j++) {
					String firstSpell = spells[j].substring(0,1);
					if(firstSpellSet.contains(firstSpell)){
						continue;
					}
					spellsStr = spellsStr + firstSpell + ",";
					firstSpellSet.add(firstSpell);
				}
				
				if(firstSpellSet.size() > 1){
//					retBuf.append("[" + spellsStr.substring(0,spellsStr.length()-1) + "]");
					retBuf.append(StringUtils.substringBefore(spellsStr, ","));
				}else{
					retBuf.append(spellsStr.substring(0,spellsStr.length()-1));
				}
			}else{
				retBuf.append(spell.substring(0,1));
			}
		} 

		return retBuf.toString();
	}
	
	public static void main(String[] args) {
//		System.out.println(((URL)CnToSpell.class.getResource("/F02-GB2312-to-PuTongHua-PinYin.txt"))
//				.getPath());
		
		String str = "安放山豆根反乐对的";
		String spellAry = CnConvertSpellUtil.getFirstSpell(str);
		String[] spellAry2 = CnConvertSpellUtil.getFullSpell(str);
		System.out.println(spellAry);
		
		System.out.println(StringUtils.join(spellAry2));
//		for (int i = 0; i < spellAry2.length; i++) {
//			System.out.print(spellAry2[i]);
//		}
	}
}

欢迎大家加入QQ群:258298887 共同学习 共同进步
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值