Java关键词替换

最近公司需要对名称进行关键词替换,于是简单写了个关键词替换工具类
还能添加许多功能和进行优化,欢迎大家使用

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;

import java.util.Set;

/**
 *  @Author Roushan
 *  @Date 2022/6/6 16:49
 *
 *  采用回溯算法实现
 *  用了Fastjson和lang3的包
 */
public class KeywordUtil {

    private static final JSONObject KEYWORD_JSON = new JSONObject();

    private static final String END = "end";

    private static final String SUBSTITUTE = "*";

    public static void readDatasource(String keyword) {
        loadKeywordJson(keyword, 0, KEYWORD_JSON);
    }

    public static void readDatasource(Set<String> keywordSet) {
        for (String keyword: keywordSet) {
            loadKeywordJson(keyword, 0, KEYWORD_JSON);
        }
    }

    public static void deleteDatasource(String keyword) {
        remove(keyword, 0, KEYWORD_JSON);
    }

    public static void deleteDatasource(Set<String> keywordSet) {
        for (String keyword: keywordSet) {
            remove(keyword, 0, KEYWORD_JSON);
        }
    }

    public static String replace(String text) {
        for (int i = 0; i < text.length(); i++) {
            StringBuilder sb = new StringBuilder();
            search(text, i, KEYWORD_JSON, sb);
            if (sb.length() == 0) {
                continue;
            }
            text = text.replaceAll(sb.toString(), generateSubstitute(sb.length()));
        }

        return text;
    }

    private static boolean remove(String keyword, int index, JSONObject sourceJson) {
        if (keyword.length() == index) {
            return sourceJson.remove(END) != null;
        }
        String str = String.valueOf(keyword.charAt(index));
        JSONObject tmpJson = sourceJson.getJSONObject(str);
        if (tmpJson == null) {
            return false;
        }
        boolean lastResult = remove(keyword, ++index, sourceJson.getJSONObject(str));
        if (lastResult) {
            if (sourceJson.getJSONObject(str).values().size() > 0) {
                return false;
            } else if (sourceJson.keySet().size() > 1) {
                sourceJson.remove(str);
                return false;
            } else {
                return sourceJson.remove(str) != null;
            }
        }
        return false;
    }

    private static void loadKeywordJson(String keyword, int index, JSONObject sourceJson) {
        if (keyword.length() == index) {
            sourceJson.put(END, true);
            return;
        }
        String str = String.valueOf(keyword.charAt(index));
        JSONObject tmpJson = sourceJson.getJSONObject(str);
        if (tmpJson == null) {
            sourceJson.put(str, new JSONObject());
        }
        loadKeywordJson(keyword, ++index, sourceJson.getJSONObject(str));
    }

    private static boolean search(String text, int index, JSONObject sourceJson, StringBuilder targetBuilder) {
        if (text.length() == index) {
            return false;
        }
        String str = String.valueOf(text.charAt(index));
        if (str.equals(SUBSTITUTE)) {
            return false;
        }
        JSONObject tmpJson = sourceJson.getJSONObject(str);
        if (CollectionUtils.isEmpty(tmpJson)) {
            return false;
        }

        if (tmpJson.containsKey(END) && tmpJson.getBoolean(END)) {
            targetBuilder.append(str);
            return true;
        }

        boolean result = search(text, ++index, sourceJson.getJSONObject(str), targetBuilder);
        if (result) {
            targetBuilder.insert(0, str);
        }

        return result;
    }

    private static String generateSubstitute(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(SUBSTITUTE);
        }
        return sb.toString();
    }
}

依赖

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值