Java 敏感詞檢測工具的實(shí)現(xiàn)
使用步驟
引入 Maven 依賴
引入最新的版本即可,見附錄開源地址。
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>sensitive-word</artifactId>
<version>0.18.0</version>
</dependency>
核心方法使用實(shí)例
包含了主要的一些功能和方法,如下所示:
- 常規(guī)用法 查找替換;
- 指定替換字符串;
- 檢測忽略大小寫,特殊字符,重復(fù)字符,簡繁體,中英文等;
- 自定義替換檢測策略示例;
package com.example.demo;
import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.api.IWordReplace;
import com.github.houbb.sensitive.word.api.IWordResult;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.github.houbb.sensitive.word.support.result.WordResultHandlers;
import com.github.houbb.sensitive.word.utils.InnerWordCharUtils;
import java.util.List;
public class SensitiveWordTestDemo {
public static void main(String[] args) {
//testNormal();
//testDefineReplace();
//testSensitiveWordResultHandler();
//testOtherFeatures();
testMoreFeatures();
}
// 常規(guī)使用案例:替換敏感詞
public static void testNormal() {
final String text = "五星紅旗迎風(fēng)飄揚(yáng),毛主席的畫像屹立在天安門前。";
System.out.println("是否包含銘感詞:" + SensitiveWordHelper.contains(text));
System.out.println("查找第一個(gè)銘感詞:" + SensitiveWordHelper.findFirst(text));
System.out.println("查找所有銘感詞:" + SensitiveWordHelper.findAll(text));
System.out.println("替換所有銘感詞:" + SensitiveWordHelper.replace(text));
System.out.println("替換所有銘感詞(指定替換符號):" + SensitiveWordHelper.replace(text, '?'));
}
/**
* 此案例講解:IWordResultHandler 可以對敏感詞的結(jié)果進(jìn)行處理,允許用戶自定義。
*/
public static void testSensitiveWordResultHandler() {
final String text = "五星紅旗迎風(fēng)飄揚(yáng),毛主席的畫像屹立在天安門前。";
List<String> wordList = SensitiveWordHelper.findAll(text);
//Assert.assertEquals("[五星紅旗, 毛主席, 天安門]", wordList.toString());
System.out.println("1.查找到所有銘感詞:" + wordList);
List<String> wordList2 = SensitiveWordHelper.findAll(text, WordResultHandlers.word());
//Assert.assertEquals("[五星紅旗, 毛主席, 天安門]", wordList2.toString());
System.out.println("2.默認(rèn)內(nèi)置處理(同直接查找到所有敏感詞):" + wordList2);
List<IWordResult> wordList3 = SensitiveWordHelper.findAll(text, WordResultHandlers.raw());
//Assert.assertEquals("[WordResult{startIndex=0, endIndex=4}, WordResult{startIndex=9, endIndex=12}, WordResult{startIndex=18, endIndex=21}]", wordList3.toString());
System.out.println("3.查找敏感詞單詞本身的起始位置到終止位置:" + wordList3);
}
// 實(shí)例:常規(guī)忽略檢測特性
public static void testOtherFeatures() {
System.out.println("\n其他屬性\n");
String text = "fuCK the bad words.";
String word = SensitiveWordHelper.findFirst(text);
//Assert.assertEquals("fuCK", word);
System.out.println("忽略大小寫:" + word);
System.out.println("替換大小寫字符:" + SensitiveWordHelper.replace(text));
text = "fuck the bad words.";
word = SensitiveWordHelper.findFirst(text);
//Assert.assertEquals("fuck", word);
System.out.println("忽略半圓角:" + word);
System.out.println("替換半圓角字符:" + SensitiveWordHelper.replace(text));
text = "這個(gè)是我的微信:9?二肆??③⑸⒋?㈤五";
List<String> wordList = SensitiveWordBs.newInstance().enableNumCheck(true).init().findAll(text);
//Assert.assertEquals("[9?二肆??③⑸⒋?㈤五]", wordList.toString());
System.out.println("忽略數(shù)字的寫法:" + wordList.toString());
System.out.println("替換數(shù)字字符:" + SensitiveWordBs.newInstance().enableNumCheck(true).init().replace(text));
text = "我愛我的祖國和五星紅旗。";
List<String> wordList1 = SensitiveWordHelper.findAll(text);
//Assert.assertEquals("[五星紅旗]", wordList1.toString());
System.out.println("檢測敏感詞簡繁體格式是否存在:" + wordList1.toString());
text = "??c? the bad words";
List<String> wordList2 = SensitiveWordHelper.findAll(text);
//Assert.assertEquals("[??c?]", wordList2.toString());
System.out.println("檢測敏感詞是否存在英文的書寫格式:" + wordList2.toString());
text = "???f?u??c?? the bad words";
List<String> wordList3 = SensitiveWordBs.newInstance()
.ignoreRepeat(true)
.init()
.findAll(text);
//Assert.assertEquals("[???f?u??c??]", wordList3.toString());
System.out.println("檢測重復(fù)詞每個(gè)字符是否重復(fù):" + wordList3.toString());
}
// 實(shí)例:更多檢測特性
public static void testMoreFeatures() {
// 1.郵箱檢測(郵箱等個(gè)人信息,默認(rèn)未啟用。)
String text = "樓主好人,郵箱 sensitiveword@xx.com";
List<String> wordList = SensitiveWordBs.newInstance().enableEmailCheck(true).init().findAll(text);
//Assert.assertEquals("[sensitiveword@xx.com]", wordList.toString());
System.out.println("是否存在郵箱:" + wordList.toString());
// 2.連續(xù)數(shù)字檢測(一般用于過濾手機(jī)號/QQ等廣告信息,默認(rèn)未啟用。)
text = "你懂得:12345678";
// 默認(rèn)檢測 8 位
List<String> wordList1 = SensitiveWordBs.newInstance()
.enableNumCheck(true)
.init().findAll(text);
//Assert.assertEquals("[12345678]", wordList.toString());
System.out.println("是否存在連續(xù)數(shù)字字符串:" + wordList1);
// 指定數(shù)字的長度,避免誤殺
List<String> wordList2 = SensitiveWordBs.newInstance()
.enableNumCheck(true)
.numCheckLen(9)
.init().findAll(text);
//Assert.assertEquals("[]", wordList2.toString());
System.out.println("是否存在連續(xù)數(shù)字字符串2:" + wordList2.toString());
// 3.網(wǎng)址檢測(用于過濾常見的網(wǎng)址信息,默認(rèn)未啟用, v0.18.0 優(yōu)化 URL 檢測,更加嚴(yán)格,降低誤判率)
text = "點(diǎn)擊鏈接 https://www.baidu.com 查看答案";
SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance().enableUrlCheck(true).init();
List<String> wordList3 = sensitiveWordBs.findAll(text);
//Assert.assertEquals("[https://www.baidu.com]", wordList3.toString());
//Assert.assertEquals("點(diǎn)擊鏈接 ********************* 查看答案", sensitiveWordBs.replace(text));
System.out.println("是否存在網(wǎng)址信息:" + wordList3.toString());
System.out.println("是否存在網(wǎng)址信息2并替換:" + sensitiveWordBs.replace(text));
// 4.IPv4 檢測: 避免用戶通過 ip 繞過網(wǎng)址檢測等,默認(rèn)未啟用。
text = "個(gè)人網(wǎng)站,如果網(wǎng)址打不開可以訪問 127.0.0.1。";
SensitiveWordBs sensitiveWordBs2 = SensitiveWordBs.newInstance().enableIpv4Check(true).init();
List<String> wordList4 = sensitiveWordBs2.findAll(text);
//Assert.assertEquals("[127.0.0.1]", wordList4.toString());
System.out.println("是否存在 IPv4:" + wordList4.toString());
}
// 實(shí)例:自定義檢測替換策略
public static void testDefineReplace() {
System.out.println("自定義敏感詞替換策略:(策略:指定敏感詞替換)");
final String text = "五星紅旗迎風(fēng)飄揚(yáng),毛主席的畫像屹立在天安門前。";
MySensitiveWordReplace replace = new MySensitiveWordReplace();
String result = SensitiveWordHelper.replace(text, replace);
System.out.println("自定義替換策略結(jié)果:" + result);
}
}
class MySensitiveWordReplace implements IWordReplace {
@Override
public void replace(StringBuilder stringBuilder, char[] chars, IWordResult wordResult, IWordContext iWordContext) {
String sensitiveWord = InnerWordCharUtils.getString(chars, wordResult);
// 自定義不同的敏感詞替換策略,可以從數(shù)據(jù)庫等地方讀取
if ("五星紅旗".equals(sensitiveWord)) {
stringBuilder.append("國家旗幟");
} else if ("毛主席".equals(sensitiveWord)) {
stringBuilder.append("教員");
} else {
// 其他默認(rèn)使用 * 代替
int wordLength = wordResult.endIndex() - wordResult.startIndex();
for (int i = 0; i < wordLength; i++) {
stringBuilder.append('*');
}
}
}
}
輸出結(jié)果展示:
是否包含銘感詞:true
查找第一個(gè)銘感詞:五星紅旗
查找所有銘感詞:[五星紅旗, 毛主席, 天安門]
替換所有銘感詞:****迎風(fēng)飄揚(yáng),***的畫像屹立在***前。
替換所有銘感詞(指定替換符號):????迎風(fēng)飄揚(yáng),???的畫像屹立在???前。
自定義敏感詞替換策略:(策略:指定敏感詞替換)
自定義替換策略結(jié)果:國家旗幟迎風(fēng)飄揚(yáng),教員的畫像屹立在***前。
1.查找到所有銘感詞:[五星紅旗, 毛主席, 天安門]
2.默認(rèn)內(nèi)置處理(同直接查找到所有敏感詞):[五星紅旗, 毛主席, 天安門]
3.查找敏感詞單詞本身的起始位置到終止位置:[WordResult{startIndex=0, endIndex=4, type='WORD'}, WordResult{startIndex=9, endIndex=12, type='WORD'}, WordResult{startIndex=18, endIndex=21, type='WORD'}]其他屬性
忽略大小寫:fuCK
替換大小寫字符:**** the bad words.
忽略半圓角:fuck
替換半圓角字符:**** the bad words.
忽略數(shù)字的寫法:[9?二肆??③⑸⒋?㈤五]
替換數(shù)字字符:這個(gè)是我的微信:************
檢測敏感詞簡繁體格式是否存在:[五星紅旗]
檢測敏感詞是否存在英文的書寫格式:[??c?]
檢測重復(fù)詞每個(gè)字符是否重復(fù):[???f?u??c??]
是否存在郵箱:[sensitiveword@xx.com]
是否存在連續(xù)數(shù)字字符串:[12345678]
是否存在連續(xù)數(shù)字字符串2:[]
是否存在網(wǎng)址信息:[https://www.baidu.com]
是否存在網(wǎng)址信息2并替換:點(diǎn)擊鏈接 ********************* 查看答案
是否存在 IPv4:[127.0.0.1]
核心方法:查找 / 替換

更多的檢測策略(自定義)
郵箱-網(wǎng)址-IPV4-連續(xù)字符檢測
// 實(shí)例:更多檢測特性
public static void testMoreFeatures() {
// 1.郵箱檢測(郵箱等個(gè)人信息,默認(rèn)未啟用。)
String text = "樓主好人,郵箱 sensitiveword@xx.com";
List<String> wordList = SensitiveWordBs.newInstance().enableEmailCheck(true).init().findAll(text);
//Assert.assertEquals("[sensitiveword@xx.com]", wordList.toString());
System.out.println("是否存在郵箱:" + wordList.toString());
// 2.連續(xù)數(shù)字檢測(一般用于過濾手機(jī)號/QQ等廣告信息,默認(rèn)未啟用。)
text = "你懂得:12345678";
// 默認(rèn)檢測 8 位
List<String> wordList1 = SensitiveWordBs.newInstance()
.enableNumCheck(true)
.init().findAll(text);
//Assert.assertEquals("[12345678]", wordList.toString());
System.out.println("是否存在連續(xù)數(shù)字字符串:" + wordList1);
// 指定數(shù)字的長度,避免誤殺
List<String> wordList2 = SensitiveWordBs.newInstance()
.enableNumCheck(true)
.numCheckLen(9)
.init().findAll(text);
//Assert.assertEquals("[]", wordList2.toString());
System.out.println("是否存在連續(xù)數(shù)字字符串2:" + wordList2.toString());
// 3.網(wǎng)址檢測(用于過濾常見的網(wǎng)址信息,默認(rèn)未啟用, v0.18.0 優(yōu)化 URL 檢測,更加嚴(yán)格,降低誤判率)
text = "點(diǎn)擊鏈接 https://www.baidu.com 查看答案";
SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance().enableUrlCheck(true).init();
List<String> wordList3 = sensitiveWordBs.findAll(text);
//Assert.assertEquals("[https://www.baidu.com]", wordList3.toString());
//Assert.assertEquals("點(diǎn)擊鏈接 ********************* 查看答案", sensitiveWordBs.replace(text));
System.out.println("是否存在網(wǎng)址信息:" + wordList3.toString());
System.out.println("是否存在網(wǎng)址信息2并替換:" + sensitiveWordBs.replace(text));
// 4.IPv4 檢測: 避免用戶通過 ip 繞過網(wǎng)址檢測等,默認(rèn)未啟用。
text = "個(gè)人網(wǎng)站,如果網(wǎng)址打不開可以訪問 127.0.0.1。";
SensitiveWordBs sensitiveWordBs2 = SensitiveWordBs.newInstance().enableIpv4Check(true).init();
List<String> wordList4 = sensitiveWordBs2.findAll(text);
//Assert.assertEquals("[127.0.0.1]", wordList4.toString());
System.out.println("是否存在 IPv4:" + wordList4.toString());
}
常規(guī)檢測:大小寫-特殊字符-重復(fù)字符-簡繁體等
// 實(shí)例:常規(guī)忽略檢測特性
public static void testOtherFeatures() {
System.out.println("\n其他屬性\n");
String text = "fuCK the bad words.";
String word = SensitiveWordHelper.findFirst(text);
//Assert.assertEquals("fuCK", word);
System.out.println("忽略大小寫:" + word);
System.out.println("替換大小寫字符:" + SensitiveWordHelper.replace(text));
text = "fuck the bad words.";
word = SensitiveWordHelper.findFirst(text);
//Assert.assertEquals("fuck", word);
System.out.println("忽略半圓角:" + word);
System.out.println("替換半圓角字符:" + SensitiveWordHelper.replace(text));
text = "這個(gè)是我的微信:9?二肆??③⑸⒋?㈤五";
List<String> wordList = SensitiveWordBs.newInstance().enableNumCheck(true).init().findAll(text);
//Assert.assertEquals("[9?二肆??③⑸⒋?㈤五]", wordList.toString());
System.out.println("忽略數(shù)字的寫法:" + wordList.toString());
System.out.println("替換數(shù)字字符:" + SensitiveWordBs.newInstance().enableNumCheck(true).init().replace(text));
text = "我愛我的祖國和五星紅旗。";
List<String> wordList1 = SensitiveWordHelper.findAll(text);
//Assert.assertEquals("[五星紅旗]", wordList1.toString());
System.out.println("檢測敏感詞簡繁體格式是否存在:" + wordList1.toString());
text = "??c? the bad words";
List<String> wordList2 = SensitiveWordHelper.findAll(text);
//Assert.assertEquals("[??c?]", wordList2.toString());
System.out.println("檢測敏感詞是否存在英文的書寫格式:" + wordList2.toString());
text = "???f?u??c?? the bad words";
List<String> wordList3 = SensitiveWordBs.newInstance()
.ignoreRepeat(true)
.init()
.findAll(text);
//Assert.assertEquals("[???f?u??c??]", wordList3.toString());
System.out.println("檢測重復(fù)詞每個(gè)字符是否重復(fù):" + wordList3.toString());
}
自定義檢測替換策略
自定義檢測替換
class MySensitiveWordReplace implements IWordReplace {
@Override
public void replace(StringBuilder stringBuilder, char[] chars, IWordResult wordResult, IWordContext iWordContext) {
String sensitiveWord = InnerWordCharUtils.getString(chars, wordResult);
// 自定義不同的敏感詞替換策略,可以從數(shù)據(jù)庫等地方讀取
if ("五星紅旗".equals(sensitiveWord)) {
stringBuilder.append("國家旗幟");
} else if ("毛主席".equals(sensitiveWord)) {
stringBuilder.append("教員");
} else {
// 其他默認(rèn)使用 * 代替
int wordLength = wordResult.endIndex() - wordResult.startIndex();
for (int i = 0; i < wordLength; i++) {
stringBuilder.append('*');
}
}
}
}
使用實(shí)例:
// 實(shí)例:自定義檢測替換策略
public static void testDefineReplace() {
System.out.println("自定義敏感詞替換策略:(策略:指定敏感詞替換)");
final String text = "五星紅旗迎風(fēng)飄揚(yáng),毛主席的畫像屹立在天安門前。";
MySensitiveWordReplace replace = new MySensitiveWordReplace();
String result = SensitiveWordHelper.replace(text, replace);
System.out.println("自定義替換策略結(jié)果:" + result);
}
開源地址
到此這篇關(guān)于Java 敏感詞檢測工具的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java 敏感詞檢測工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot+redis 實(shí)現(xiàn)分布式限流令牌桶的示例代碼
這篇文章主要介紹了springboot+redis 實(shí)現(xiàn)分布式限流令牌桶 ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
java 如何實(shí)現(xiàn)正確的刪除集合中的元素
這篇文章主要介紹了java 如何實(shí)現(xiàn)正確的刪除集合中的元素,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
idea中創(chuàng)建jsp項(xiàng)目的詳細(xì)實(shí)戰(zhàn)步驟
才學(xué)javaWeb,以防自己忘記創(chuàng)建項(xiàng)目的過程,所以淺淺的記錄一下吧,下面這篇文章主要給大家介紹了關(guān)于idea中創(chuàng)建jsp項(xiàng)目的詳細(xì)步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Spring中@Value設(shè)置默認(rèn)值問題解決
本文主要介紹了Spring中@Value設(shè)置默認(rèn)值問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Springboot與vue實(shí)例講解實(shí)現(xiàn)前后端分離的人事管理系統(tǒng)
這篇文章主要介紹了如何用Java實(shí)現(xiàn)企業(yè)人事管理系統(tǒng),文中采用springboot+vue實(shí)現(xiàn)前后端分離,感興趣的小伙伴可以學(xué)習(xí)一下2022-06-06
spring security在分布式項(xiàng)目下的配置方法(案例詳解)
這篇文章主要介紹了spring security在分布式項(xiàng)目下的配置方法,本文通過一個(gè)項(xiàng)目案例給大家詳細(xì)介紹,通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

