java实现姓名转拼音并处理多音字

最近工作中遇到姓名转拼音的需求,睿智的监工开始说只要实现姓名转拼音,做好之后又要求处理多音字,唉!
代码如下

首先是maven依赖

<dependency>
   <groupId>com.belerweb</groupId>
   <artifactId>pinyin4j</artifactId>
   <version>2.5.0</version>
</dependency>

创建姓名转拼音工具类

package com.xiaoa.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
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 PinYinUtil {
  private static final int ASCII_MIN_SIZE = 32;
  
  private static final int ASCII_MAX_SIZE = 125;
  
  public static String getPinyin(String word) {
    HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
    outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    char[] charArray = word.toCharArray();
    List<List<String>> lists = new ArrayList<>();
    for (int i = 0; i < charArray.length; i++) {
      List<String> asList = Arrays.asList(getPinyin(charArray[i]));
      lists.add(asList);
    } 
    StringBuilder sb = new StringBuilder();
    try {
      List<String> descartes = descartes(lists, 0, "", new ArrayList<>());
      for (int j = 0; j < descartes.size(); j++) {
        if (j != descartes.size() - 1) {
          sb.append(descartes.get(j)).append(" ");
        } else {
          sb.append(descartes.get(j));
        } 
      } 
    } catch (Exception e) {
      e.printStackTrace();
    } 
    return sb.toString();
  }
  
  private static List<String> descartes(List<List<String>> dimensionValues, int position, String originCode, List<String> result) {
    List<String> rowValue = dimensionValues.get(position);
    for (String code : rowValue) {
      String resultCode = (position == 0) ? code : (originCode + "" + code);
      if (position == dimensionValues.size() - 1) {
        result.add(resultCode);
        continue;
      } 
      descartes(dimensionValues, position + 1, resultCode, result);
    } 
    return result;
  }
  
  private PinYinUtil() {
    throw new IllegalStateException("Utility class");
  }
  
  public static String[] getPinyin(char ch) {
    try {
      HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
      outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
      outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
      outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
      if (ch >= ' ' && ch <= '}')
        return new String[] { String.valueOf(ch) }; 
      return distinct(PinyinHelper.toHanyuPinyinStringArray(ch, outputFormat));
    } catch (BadHanyuPinyinOutputFormatCombination e) {
      throw new IllegalStateException(e);
    } 
  }
  
  private static String[] distinct(String[] arr) {
    if (arr == null || arr.length == 0)
      return arr; 
    HashSet<String> set = new HashSet<>();
    for (String str : arr)
      set.add(str); 
    return (String[])set.toArray((Object[])new String[0]);
  }
}

测试方法如下

package com.xiaoa.test;

import com.xiaoa.utils.PinYinUtil;

/**
 * @BelongsProject: name2pinyin
 * @BelongsPackage: com.xiaoa.utils.com.xiaoa
 * @Author: crazy_data
 * @CreateTime: 2022-11-15  14:26
 * @Description: 测试姓名转拼音
 * @Version: 1.0
 */
public class PinyinTest {
    public static void main(String[] args) {
        String pinyin = PinYinUtil.getPinyin("中国红");
        System.out.println(pinyin);
    }
}

测试结果如下
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胖java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值