当前位置: 代码网 > it编程>编程语言>Java > springboot集成sensitive-word实现敏感词过滤的两种方案

springboot集成sensitive-word实现敏感词过滤的两种方案

2024年08月18日 Java 我要评论
敏感词过滤敏感词过滤通常是指从文本中检测并移除或替换掉被认为是不适当、冒犯性或违反特定社区准则的词汇。这个过程常用于在线平台、论坛、社交媒体和聊天系统等,以确保交流环境的健康和积极.方案一:正则表达式

敏感词过滤

敏感词过滤通常是指从文本中检测并移除或替换掉被认为是不适当、冒犯性或违反特定社区准则的词汇。这个过程常用于在线平台、论坛、社交媒体和聊天系统等,以确保交流环境的健康和积极.

方案一:正则表达式

实现敏感词过滤.只适合于敏感词较少、文本量较少的场合,并且无法处理同音字、错别字等,案例:

public static void main(string[] args) {
        string text = "这是一个包含敏感词汇的文本,例如色情、赌博等。";
        string[] sensitivewords = {"色情", "赌博"};
        for (string word : sensitivewords) {
            text = filtersensitivewords(text, word);
        }
        system.out.println("过滤后的文本: " + text);
        testsensitivewordframe();
    }
    /**
     * 方案一:正则表达式实现敏感词过滤.只适合于敏感词较少、文本量较少的场合,并且无法处理同音字、错别字等.
     *
     * @param text
     * @param sensitiveword
     * @return
     */
    public static string filtersensitivewords(string text, string sensitiveword) {
        pattern pattern = pattern.compile(sensitiveword);
        matcher matcher = pattern.matcher(text);
        return matcher.replaceall("***");
    }

方案二:基于dfa算法的敏感词过滤工具框架-sensitive-word

 * 6w+ 词库,且不断优化更新
 * 基于 dfa 算法,性能较好
 * 基于 fluent-api 实现,使用优雅简洁
 * 支持敏感词的判断、返回、脱敏等常见操作
 * 支持全角半角互换
 * 支持英文大小写互换
 * 支持数字常见形式的互换
 * 支持中文繁简体互换
 * 支持英文常见形式的互换
 * 支持用户自定义敏感词和白名单
 * 支持数据的数据动态更新,实时生效

springboot集成sensitive-word

步骤一:引入pom

<dependency>
	<groupid>com.github.houbb</groupid>
	<artifactid>sensitive-word</artifactid>
	<version>0.2.0</version>
</dependency>

步骤二:自定义配置

@configuration
public class mysensitivewordbs {
    @autowired
    private mywordallow mywordallow;
    @autowired
    private myworddeny myworddeny;
    @autowired
    private mywordreplace mywordreplace;
    /**
     * 初始化引导类
     *
     * @return 初始化引导类
     * @since 1.0.0
     */
    @bean
    public sensitivewordbs sensitivewordbs() {
        sensitivewordbs sensitivewordbs = sensitivewordbs.newinstance()
//                .wordallow(wordallows.chains(wordallows.defaults(), mywordallow)) // 设置多个敏感词,系统默认和自定义
//                .worddeny(worddenys.chains(worddenys.defaults(), myworddeny))     // 设置多个敏感词,系统默认和自定义
                .wordallow(wordallows.chains(mywordallow))  // 自定义
                .worddeny(worddenys.chains(myworddeny))     // 自定义
                .wordreplace(mywordreplace)                                        // 自定义替换规则
                .ignorecase(true)           // 忽略大小写
                .ignorewidth(true)          // 忽略半角圆角
                .ignorenumstyle(true)       // 忽略数字的写法
                .ignorechinesestyle(true)   // 忽略中文的书写格式
                .ignoreenglishstyle(true)   // 忽略英文的书写格式
                .ignorerepeat(true)         // 忽略重复词
                .enablenumcheck(true)       // 是否启用数字检测。默认连续 8 位数字认为是敏感词
                .enableemailcheck(true)     // 是有启用邮箱检测
                .enableurlcheck(true)       // 是否启用链接检测
                .init();
        return sensitivewordbs;
    }
}

步骤三:自定义敏感词+白名单

/**
 * 自定义非敏感词
 * 注意每一行为一个非敏感词,单行不能只包括空格,否则,也会把空格识别为非敏感词
 */
@component
@slf4j
public class mywordallow implements iwordallow {
    @override
    public list<string> allow() {
        list<string> allowwords = new arraylist<>();
        try {
            classpathresource resource = new classpathresource("myallowwords.txt");
            path myallowwordspath = paths.get(resource.geturl().touri());
            allowwords = files.readalllines(myallowwordspath, standardcharsets.utf_8);
        } catch (ioexception ioexception) {
            log.error("读取非敏感词文件错误:{}", ioexception);
        } catch (urisyntaxexception e) {
            throw new runtimeexception(e);
        }
        return allowwords;
    }
}
@component
@slf4j
public class myworddeny implements iworddeny {
    @override
    public list<string> deny() {
        list<string> denywords = new arraylist<>();
        try {
            classpathresource resource = new classpathresource("mydenywords.txt");
            path myallowwordspath = paths.get(resource.geturl().touri());
            denywords = files.readalllines(myallowwordspath, standardcharsets.utf_8);
        } catch (
                ioexception ioexception) {
            log.error("读取敏感词文件错误:{}", ioexception);
        } catch (urisyntaxexception e) {
            throw new runtimeexception(e);
        }
        return denywords;
    }
}
/**
 * 自定义敏感词对应的替换值.
 * 场景说明:有时候我们希望不同的敏感词有不同的替换结果。比如【游戏】替换为【电子竞技】,【失业】替换为【灵活就业】。
 */
@configuration
public class mywordreplace implements iwordreplace {
    @override
    public void replace(stringbuilder stringbuilder, final char[] rawchars, iwordresult wordresult, iwordcontext wordcontext) {
        string sensitiveword = innerwordcharutils.getstring(rawchars, wordresult);
        if ("zhupeng".equals(sensitiveword)) {
            stringbuilder.append("朱鹏");
        } else {
            // 其他默认使用 * 代替
            int wordlength = wordresult.endindex() - wordresult.startindex();
            for (int i = 0; i < wordlength; i++) {
                stringbuilder.append('-');
            }
        }
    }
}

步骤四:核心方法测试

public class sensitivewordcontroller {
    @autowired
    private mywordreplace mywordreplace;
    @autowired
    private sensitivewordbs sensitivewordbs;
    private static final string text = "五星红旗迎风飘扬,毛主席的画像屹立在天安门前,zhuzhuhzu";
    @getmapping("/pattern")
    public void testsensitiveword2() {
        string text = "这是一个包含敏感词汇的文本,例如色情、赌博等。";
        string[] sensitivewords = {"色情", "赌博"};
        for (string word : sensitivewords) {
            text = filtersensitivewords(text, word);
        }
        system.out.println("过滤后的文本: " + text);
    }
    /**
     * 方案二:基于dfa算法的敏感词过滤工具框架-sensitive-word:https://github.com/houbb/sensitive-word
     * 6w+ 词库,且不断优化更新
     * 基于 dfa 算法,性能较好
     * 基于 fluent-api 实现,使用优雅简洁
     * 支持敏感词的判断、返回、脱敏等常见操作
     * 支持全角半角互换
     * 支持英文大小写互换
     * 支持数字常见形式的互换
     * 支持中文繁简体互换
     * 支持英文常见形式的互换
     * 支持用户自定义敏感词和白名单
     * 支持数据的数据动态更新,实时生效
     */
    @getmapping("/filter")
    public void testsensitiveword() {
        system.out.println("sensitivewordhelper.contains(text) = " + sensitivewordhelper.contains(text));
        system.out.println("sensitivewordhelper.findall(text) = " + sensitivewordhelper.findall(text));
        system.out.println("sensitivewordhelper.replace(text,mywordreplace) = " + sensitivewordhelper.replace(text, mywordreplace));
        // 如果自定义敏感词,不要使用sensitivewordhelper的方法,要使用sensitivewordbs
        system.out.println("sensitivewordbs.contains(text) = " + sensitivewordbs.contains(text));
        system.out.println("sensitivewordbs.findall(text) = " + sensitivewordbs.findall(text));
        system.out.println("sensitivewordbs.replace(text) = " + sensitivewordbs.replace(text));
    }
}

到此这篇关于springboot集成sensitive-word实现敏感词过滤的文章就介绍到这了,更多相关springboot敏感词过滤内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com