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 interceptWords(String content) { // 查询所有的敏感词数据集合 List list = sensitivewordService.getBadSensitivewordList(); // 创建查询对象 StringSearch iwords = new StringSearch(); // 设置敏感词集合 iwords.SetKeywords(list); // 判断是否存在敏感词 boolean b = iwords.ContainsAny(content); // 创建map 对象返回 Map res = new HashMap(3); // 设置是否有敏感词 res.put("iswords",String.valueOf(b)); if (b) { // 在文本中替换所有的关键字 String str = iwords.Replace(content, '*'); // 设置替换后的内容 res.put("content", str); String text = ""; // 在文本中查找所有的关键字 List 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; } }