lin
2024-04-16 2b1a74f4faa5a00a294bdc6a6d956c2e009cf467
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.springblade.modules.words;
 
import org.springblade.modules.words.sensitiveword.service.ISensitivewordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 敏感词过滤服务
 * @author zhongrj
 * @date 2024-01-22
 */
@Component
public class WorksService {
 
    @Autowired
    private ISensitivewordService sensitivewordService;
 
 
    /**
     * 敏感词过滤
     * @param content
     * @return
     */
    public Map<String,Object> interceptWords(String content) {
        // 查询所有的敏感词数据集合
        List<String> list = sensitivewordService.getBadSensitivewordList();
        // 创建查询对象
        StringSearch iwords = new StringSearch();
        // 设置敏感词集合
        iwords.SetKeywords(list);
        // 判断是否存在敏感词
        boolean b = iwords.ContainsAny(content);
        // 创建map 对象返回
        Map<String,Object> res = new HashMap(3);
        // 设置是否有敏感词
        res.put("iswords",String.valueOf(b));
        if (b) {
            // 在文本中替换所有的关键字
            String str = iwords.Replace(content, '*');
            // 设置替换后的内容
            res.put("content", str);
            String text = "";
            // 在文本中查找所有的关键字
            List<String> all = iwords.FindAll(content);
            for (int i = 0; i < all.size(); i++) {
                text += all.get(i) + ",";
            }
            String words = "";
            if (!text.equals("")) {
                words = text.substring(0, text.length() - 1);
            }
            // 设置找到的敏感关键字
            res.put("words", words);
        }
        // 返回
        return res;
    }
}