All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.pinyin.util.PinyinHelper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.pinyin.util;

import com.taotao.boot.common.utils.collection.CollectionUtils;
import com.taotao.boot.common.utils.common.ArgUtils;
import com.taotao.boot.common.utils.lang.StringUtils;
import com.taotao.boot.pinyin.bs.PinyinBs;
import com.taotao.boot.pinyin.constant.enums.PinyinStyleEnum;
import com.taotao.boot.pinyin.spi.IPinyinToneStyle;
import com.taotao.boot.pinyin.support.style.PinyinToneStyles;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 拼音工具类
 *
 * @author shuigedeng
 * @version 2023.04
 * @since 2023-04-12 09:33:00
 */
public final class PinyinHelper {

    private PinyinHelper() {}

    /**
     * 默认的实现类
     *
     * 

避免最基本的方法调用,多次创建对象的问题 */ private static final PinyinBs PINYIN_BS_DEFAULT = PinyinBs.newInstance().init(); /** * 转换为拼音 * * @param string 原始信息 * @return 结果 */ public static String toPinyin(final String string) { return PINYIN_BS_DEFAULT.toPinyin(string); } /** * 转换为拼音 * * @param string 原始信息 * @param styleEnum 样式枚举 * @return 结果 */ public static String toPinyin(final String string, final PinyinStyleEnum styleEnum) { return toPinyin(string, styleEnum, StringUtils.BLANK); } /** * 转换为拼音 * * @param string 原始信息 * @param styleEnum 样式枚举 * @param connector 连接符号 * @return 结果 */ public static String toPinyin(final String string, final PinyinStyleEnum styleEnum, final String connector) { ArgUtils.notNull(connector, "connector"); if (StringUtils.isEmptyTrim(string)) { return string; } final IPinyinToneStyle style = PinyinToneStyles.getTone(styleEnum); return PinyinBs.newInstance().style(style).connector(connector).init().toPinyin(string); } /** * 返回拼音列表 * * @param chinese 中文字符 * @return 结果 */ public static List toPinyinList(final char chinese) { return PINYIN_BS_DEFAULT.toPinyinList(chinese); } /** * 返回拼音列表 * * @param chinese 中文字符 * @param styleEnum 指定样式 * @return 结果 */ public static List toPinyinList(final char chinese, final PinyinStyleEnum styleEnum) { final IPinyinToneStyle pinyinTone = PinyinToneStyles.getTone(styleEnum); return PinyinBs.newInstance().style(pinyinTone).init().toPinyinList(chinese); } /** * 是否拥有相同的拼音 * * @param chineseOne 汉字一 * @param chineseTwo 汉字二 * @return 是否相同 */ public static boolean hasSamePinyin(final char chineseOne, final char chineseTwo) { return PINYIN_BS_DEFAULT.hasSamePinyin(chineseOne, chineseTwo); } /** * 同音字 map * *

因为汉字存在多音字,所以这里返回的是一个 map key: 汉字的拼音 value: 对应的同音字列表 * * @param hanzi 汉字 * @return 字符列表 */ public static Map> samePinyinMap(final char hanzi) { // 1. 获取汉字的多音字列表 List pinyinList = toPinyinList(hanzi, PinyinStyleEnum.NUM_LAST); if (CollectionUtils.isEmpty(pinyinList)) { return Collections.emptyMap(); } // 2. 获取每一个汉字对应的同音字列表 Map> map = new HashMap<>(pinyinList.size()); for (String pinyin : pinyinList) { List characterList = samePinyinList(pinyin); map.put(pinyin, characterList); } return map; } /** * 获取拼音对应的同音字列表 * * @param pinyinNumLast 拼音 * @return 结果 */ public static List samePinyinList(String pinyinNumLast) { final boolean sameTone = true; return PINYIN_BS_DEFAULT.samePinyinList(pinyinNumLast, sameTone); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy