package com.ruoyi.project.utils;

import java.util.regex.Pattern;

public class AdvancedSensitiveFilter {

    private static final Pattern CHINESE_PATTERN = Pattern.compile("[\\u4e00-\\u9fa5]");  // 移除了^和$，只匹配单个中文字符
    private static final int CONTEXT_SIZE = 6; // 检查前后6个字符

    public static void main(String[] args) {
        System.out.println(AdvancedSensitiveFilter.needsFiltering("11sb1", "sb")); // 现在应该输出true
    }

    public static boolean needsFiltering(String fullText, String sensitiveWord) {
        int index = fullText.indexOf(sensitiveWord);
        if (index == -1) return false;

        // 获取检查范围（前后CONTEXT_SIZE个字符）
        int start = Math.max(0, index - CONTEXT_SIZE);
        int end = Math.min(fullText.length(), index + sensitiveWord.length() + CONTEXT_SIZE);
        String context = fullText.substring(start, end);

        // 只要上下文范围内存在中文就需要校验
        return containsChinese(context);
    }

    private static boolean containsChinese(String str) {
        return CHINESE_PATTERN.matcher(str).find();  // 使用find()而不是matches()
    }
}