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

com.taotao.boot.pinyin.util.InnerToneHelper 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.util;

import com.taotao.boot.common.utils.io.FileStreamUtils;
import com.taotao.boot.common.utils.lang.ObjectUtils;
import com.taotao.boot.common.utils.lang.StringUtils;
import com.taotao.boot.pinyin.constant.PinyinConst;
import com.taotao.boot.pinyin.model.CharToneInfo;
import com.taotao.boot.pinyin.model.ToneItem;

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

/**
 * 拼音声调工具类
 *
 * @author shuigedeng
 * @version 2023.04
 * @since 2023-04-12 09:32:26
 */
public final class InnerToneHelper {

    private InnerToneHelper() {}

    /** 存放对应的拼音声调信息 */
    private static final Map TONE_ITEM_MAP;

    static {
        TONE_ITEM_MAP = new HashMap<>(34);

        List allLines = FileStreamUtils.readAllLines(PinyinConst.PINYIN_DICT_TONE_SYSTEM);
        for (String line : allLines) {
            String[] strings = line.split(StringUtils.BLANK);
            ToneItem item = ToneItem.of(strings[0].charAt(0), Integer.parseInt(strings[1]));

            TONE_ITEM_MAP.put(strings[2].charAt(0), item);
        }
    }

    /**
     * 获取对应的拼音信息
     *
     * @param c 拼音
     * @return 结果
     */
    public static ToneItem getToneItem(final char c) {
        return TONE_ITEM_MAP.get(c);
    }

    /**
     * 获取对应的声调信息
     *
     * @param tone 拼音信息
     * @return 声调信息
     */
    public static CharToneInfo getCharToneInfo(final String tone) {
        CharToneInfo charToneInfo = new CharToneInfo();
        charToneInfo.setIndex(-1);

        int length = tone.length();
        for (int i = 0; i < length; i++) {
            char currentChar = tone.charAt(i);
            ToneItem toneItem = InnerToneHelper.getToneItem(currentChar);

            if (ObjectUtils.isNotNull(toneItem)) {
                charToneInfo.setToneItem(toneItem);
                charToneInfo.setIndex(i);
                break;
            }
        }

        return charToneInfo;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy