java实现中文转拼音包含声调

功能: 读取excel中的中文,转为拼音,包含声调

引入依赖包:  

implementation 'cn.hutool:hutool-all:5.7.16'
  implementation 'com.belerweb:pinyin4j:2.5.1'
  implementation 'org.apache.poi:poi-ooxml:5.1.0'

package com.soft.util;

import java.util.List;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
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;

public class ToPinYin {

	public static void main(String[] args) {
		String file = "I:/myData/qq_data/3442175060/FileRecv/一3学生名单.xlsx";
		ExcelUtil.readBySax(file, 0, createRowHandler());
	}

	private static RowHandler createRowHandler() {
		return new RowHandler() {
			@Override
			public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {
				if (rowIndex > 1) {
					String name1 = (String) rowlist.get(1);
					String name2 = (String) rowlist.get(9);
					Console.log("[{}] [{}] {}\t\t{}\t\t{}\t\t{}", sheetIndex, rowIndex, name1, getPinYin(name1), name2,
							getPinYin(name2));
				}

			}
		};
	}


	public static String getPinYin(String src) {
		if (StrUtil.isBlank(src) || "null".equals(src)) {
			return "null";
		}
		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		// t3是全部的拼音,不带声调
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);

		// format是全部的拼音并且带声调
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
		format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

		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);
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], format);
					t4 += t2[0] + " ";
				} else {
					t4 += java.lang.Character.toString(t1[i]);
				}
			}
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}
}

参考: (153条消息) java实现将汉字转为拼音并包含音调_Micky的博客-CSDN博客_java中文转拼音 https://blog.csdn.net/SoumnsJ/article/details/89146371

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值