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

com.taotao.boot.pinyin.api.impl.Pinyin Maven / Gradle / Ivy

There is a newer version: 2025.01
Show 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.api.impl;

import com.google.common.collect.Lists;
import com.taotao.boot.common.support.handler.IHandler;
import com.taotao.boot.common.utils.collection.CollectionUtils;
import com.taotao.boot.common.utils.lang.StringUtils;
import com.taotao.boot.pinyin.api.IPinyin;
import com.taotao.boot.pinyin.api.IPinyinContext;
import com.taotao.boot.pinyin.spi.IPinyinChinese;
import com.taotao.boot.pinyin.spi.IPinyinData;
import com.taotao.boot.pinyin.spi.IPinyinSegment;
import com.taotao.boot.pinyin.spi.IPinyinTone;
import com.taotao.boot.pinyin.spi.IPinyinToneReverse;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Pinyin implements IPinyin {

    @Override
    public String toPinyin(String string, IPinyinContext context) {
        List stringList = toPinyinList(string, context);
        final String connector = context.connector();
        return StringUtils.join(stringList, connector);
    }

    @Override
    public List toPinyinList(char chinese, IPinyinContext context) {
        String original = String.valueOf(chinese);

        // 基本属性
        final IPinyinChinese pinyinChinese = context.chinese();
        final IPinyinTone pinyinTone = context.tone();

        if (pinyinChinese.isChinese(original)) {
            String simple = pinyinChinese.toSimple(original);
            return pinyinTone.toneList(simple, context);
        }

        // 直接返回
        return Collections.singletonList(original);
    }

    @Override
    public boolean hasSamePinyin(char chineseOne, char chineseTwo, IPinyinContext context) {
        // 基本属性
        final IPinyinChinese pinyinChinese = context.chinese();
        final IPinyinTone pinyinTone = context.tone();

        if (pinyinChinese.isChinese(chineseOne) && pinyinChinese.isChinese(chineseTwo)) {
            // fast-return
            if (chineseOne == chineseTwo) {
                return true;
            }

            String simpleOne = pinyinChinese.toSimple(chineseOne);
            String simpleTwo = pinyinChinese.toSimple(chineseTwo);

            List tonesOne = pinyinTone.toneList(simpleOne, context);
            List tonesTwo = pinyinTone.toneList(simpleTwo, context);

            // 交集大于0
            return CollectionUtils.containAny(tonesOne, tonesTwo);
        }

        return false;
    }

    @Deprecated
    @Override
    public List toneNumList(String chinese, IPinyinContext context) {
        // 获取拼音结果
        List pinyinList = this.toPinyinList(chinese, context);

        return this.buildToneNumList(pinyinList, context.tone());
    }

    @Deprecated
    @Override
    public List toneNumList(char chinese, IPinyinContext context) {
        List pinyinList = toPinyinList(chinese, context);

        return this.buildToneNumList(pinyinList, context.tone());
    }

    @Deprecated
    @Override
    public List shengMuList(String chinese, IPinyinContext context) {
        final IPinyinData pinyinData = context.data();
        return normalPinyinHandler(chinese, context, pinyinData::shengMu);
    }

    @Deprecated
    @Override
    public List yunMuList(String chinese, IPinyinContext context) {
        final IPinyinData pinyinData = context.data();
        return normalPinyinHandler(chinese, context, pinyinData::yunMu);
    }

    @Override
    public List samePinyinList(String pinyin, boolean sameToneNum, IPinyinContext context) {
        IPinyinToneReverse pinyinToneReverse = context.pinyinToneReverse();

        // 是否相同的拼音
        if (sameToneNum) {
            return pinyinToneReverse.getHanziList(pinyin);
        }

        // 构建所有的拼音
        List resultList = new ArrayList<>();
        String pinyinRaw = pinyin.substring(0, pinyin.length() - 1);
        for (int i = 1; i <= 5; i++) {
            String pinyinLast = pinyinRaw + i;
            List characterList = pinyinToneReverse.getHanziList(pinyinLast);
            if (CollectionUtils.isNotEmpty(characterList)) {
                resultList.addAll(characterList);
            }
        }

        return resultList;
    }

    /**
     * 转换为拼音列表
     *
     * @param string 字符串
     * @return 结果
     */
    private List toPinyinList(final String string, final IPinyinContext context) {
        if (StringUtils.isEmptyTrim(string)) {
            return Collections.emptyList();
        }

        // 基本属性
        final IPinyinSegment pinyinSegment = context.segment();
        final IPinyinChinese pinyinChinese = context.chinese();
        final IPinyinTone pinyinTone = context.tone();

        List entryList = pinyinSegment.segment(string);
        List resultList = Lists.newArrayList();

        // 映射处理与连接
        for (String entry : entryList) {
            // 直接加入原始信息
            if (StringUtils.isEmptyTrim(entry)) {
                // 跳过空白信息
                continue;
            }

            // 这里理论上是不用判断是否为中文
            if (pinyinChinese.isChinese(entry)) {
                // 是否简体转换也应该加一个开关
                String simple = pinyinChinese.toSimple(entry);
                String tone = pinyinTone.tone(simple, context);

                resultList.add(tone);
            } else {
                resultList.add(entry);
            }
        }

        return resultList;
    }

    /**
     * 构建拼音编号列表
     *
     * @param pinyinList 拼音结果
     * @param pinyinTone 拼音标注实现
     * @return 结果
     */
    private List buildToneNumList(List pinyinList, final IPinyinTone pinyinTone) {
        List resultList = Lists.newArrayList();

        for (String pinyin : pinyinList) {
            Integer toneNum = pinyinTone.toneNum(pinyin);
            resultList.add(toneNum);
        }
        return resultList;
    }

    /**
     * 默认拼音处理类
     *
     * @param chinese 中文拼音
     * @param context 上下文
     * @param handler 拼音处理类
     * @return 结果
     */
    private List normalPinyinHandler(
            final String chinese, final IPinyinContext context, final IHandler handler) {
        // 获取拼音结果
        List pinyinList = this.toPinyinList(chinese, context);
        List resultList = Lists.newArrayList();

        for (String pinyin : pinyinList) {
            String result = handler.handle(pinyin);
            resultList.add(result);
        }

        return resultList;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy