sensitive-word v0.13 特性版本发布 支持英文单词全词匹配

news/2024/7/5 3:43:13

拓展阅读

sensitive-word-admin v1.3.0 发布 如何支持分布式部署?

sensitive-word-admin 敏感词控台 v1.2.0 版本开源

sensitive-word 基于 DFA 算法实现的高性能敏感词工具介绍

更多技术交流

view

业务背景

对于英文单词 Disburse 之类的,其中的 sb 字母会被替换,要怎么处理,能不能只有整个单词匹配的时候才替换。

针对匹配词进一步判断

说明

支持版本:v0.13.0

有时候我们可能希望对匹配的敏感词进一步限制,比如虽然我们定义了【av】作为敏感词,但是不希望【have】被匹配。

就可以自定义实现 wordResultCondition 接口,实现自己的策略。

系统内置的策略在 WordResultConditions#alwaysTrue() 恒为真,WordResultConditions#englishWordMatch() 则要求英文必须全词匹配。

入门例子

原始的默认情况:

final String text = "I have a nice day。";

List<String> wordList = SensitiveWordBs.newInstance()
        .wordDeny(new IWordDeny() {
            @Override
            public List<String> deny() {
                return Collections.singletonList("av");
            }
        })
        .wordResultCondition(WordResultConditions.alwaysTrue())
        .init()
        .findAll(text);
Assert.assertEquals("[av]", wordList.toString());

我们可以指定为英文必须全词匹配。

final String text = "I have a nice day。";

List<String> wordList = SensitiveWordBs.newInstance()
        .wordDeny(new IWordDeny() {
            @Override
            public List<String> deny() {
                return Collections.singletonList("av");
            }
        })
        .wordResultCondition(WordResultConditions.englishWordMatch())
        .init()
        .findAll(text);
Assert.assertEquals("[]", wordList.toString());

当然可以根据需要实现更加复杂的策略。

如何自定义自己的策略

可以参考 WordResultConditions#englishWordMatch() 实现类,只需要继承 AbstractWordResultCondition 实现对应的方法即可。

策略的定义

以 englishWordMatch 实现类为例:

package com.github.houbb.sensitive.word.support.resultcondition;

import com.github.houbb.heaven.util.lang.CharUtil;
import com.github.houbb.heaven.util.util.CharsetUtil;
import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.api.IWordResult;
import com.github.houbb.sensitive.word.constant.enums.WordValidModeEnum;

/**
 * 英文单词必须要全词匹配
 *
 * https://github.com/houbb/sensitive-word/issues/45
 *
 * @since 0.13.0
 */
public class WordResultConditionEnglishWordMatch extends AbstractWordResultCondition {

    @Override
    protected boolean doMatch(IWordResult wordResult, String text, WordValidModeEnum modeEnum, IWordContext context) {
        final int startIndex = wordResult.startIndex();
        final int endIndex = wordResult.endIndex();
        // 判断当前是否为英文单词
        for(int i = startIndex; i < endIndex; i++) {
            char c = text.charAt(i);
            if(!CharUtil.isEnglish(c)) {
                return true;
            }
        }

        // 判断处理,判断前一个字符是否为英文。如果是,则不满足
        if(startIndex > 0) {
            char preC = text.charAt(startIndex-1);
            if(CharUtil.isEnglish(preC)) {
                return false;
            }
        }

        // 判断后一个字符是否为英文
        if(endIndex < text.length() - 1) {
            char afterC = text.charAt(endIndex+1);
            if(CharUtil.isEnglish(afterC)) {
                return false;
            }
        }

        return true;
    }

}

策略的指定

然后用引导类指定我们的策略即可:

List<String> wordList = SensitiveWordBs.newInstance()
        .wordResultCondition(new WordResultConditionEnglishWordMatch())
        .init()
        .findAll(text);

小结

实际应用的场景会被预想的复杂,所以此处设计为接口,内置一些常见的实现策略。

同时支持用户自定义拓展。

开源代码

https://github.com/houbb/sensitive-word 本文由博客一文多发平台 OpenWrite 发布!


http://lihuaxi.xjx100.cn/news/2036057.html

相关文章

ElasticSearch之Index Template 和Dynamic Template

写在前面 在ElasticSearch之Mapping 一文中我们一起看了es的dynamic mapping机制&#xff0c;通过该机制允许我们不需要显式的定义mapping信息&#xff0c;而是es根据插入的文档值来自动生成 &#xff0c;比如插入如下的文档&#xff1a; {"firstName": "Chan…

Vue报错,xxx is defined #变量未定义

vue.js:5129 [Vue warn]: Error in v-on handler: "ReferenceError: count is not defined" 浏览器将这个变量 当做全局变量了&#xff0c;事实上它只是实例中的变量 加上this指定&#xff0c;是vue实例中的变量

使用php实现pc端和移动端分离

使用php实现pc端和移动端分离 自适应技术可以实现根据浏览器的宽度来实现移动端和pc的自适应&#xff0c;但会影响用户的体验&#xff0c;以下代码实现在同一个链接下&#xff0c;移动端和pc分别有各自的html&#xff0c; $browser get_browser(null, true);// 获取设备宽度$d…

数据库应用:kylin 部署 达梦数据库DM8

目录 一、实验 1.环境 2.部署前规划 3.部署达梦数据库DM8 4.创建数据库及数据库事例管理 5.达梦数据库的基本操作 二、问题 1.xhost命令报错 2.执行安装程序DMInstall.bin 报错 3.解压安装程序报错 4.安装程序找不到文件 5.图像化界面打不开 6.安装内存太小 7.打开…

C++常用算法

c常用算法介绍 类似python里面一些调用 提示&#xff1a;写完文章后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 c常用算法介绍前言一、排序算法1.C sort()排序函数2.stable_sort() 函数3.partial_sort()函数4.nth_element()函数 二、使用…

网站管理新利器:免费在线生成 robots.txt 文件!

&#x1f916; 探索网站管理新利器&#xff1a;免费在线生成 robots.txt 文件&#xff01; 你是否曾为搜索引擎爬虫而烦恼&#xff1f;现在&#xff0c;我们推出全新的在线 robots.txt 文件生成工具&#xff0c;让你轻松管理网站爬虫访问权限&#xff0c;提升网站的可搜索性和…

有关数字各位数之和的题

CFround928 div4的C题 题目解析 这道题首先强调的是一个测试样例给了t之后的限时为0.5s而t的范围在1e4以内&#xff0c;n的范围在2e5以内&#xff0c;因此对于每一个n应该以O(1)的时间复杂度给出答案&#xff0c;故&#xff0c;必须预处理2e5以内的任何一个数&#xff0c;从而…

SICTF Round#3 Web方向 题解WP

100&#xff05;_upload 题目描述&#xff1a;小茂夫说&#xff1a;一直上传恶意文件尊嘟要生气了&#xff0c;世事莫固守&#xff0c;转变思路求突破 开题&#xff0c;注意有个文件包含 题目把后缀过滤死了&#xff0c;无法上传php后缀文件。文件内容些许过滤&#xff0c;短…