spring boot 實現敏感詞過濾

2021-09-18 01:20:56 字數 3473 閱讀 8457

首先新建乙個敏感詞過濾工具類,目的是傳入乙個字串,可以自動過濾並返回。**如下:

​​​​​​​

/** * 敏感詞處理工具 - dfa演算法實現

* * @author 2019 shiwq

*/public class sensitivewordutil

/*** 初始化敏感詞庫,構建dfa演算法模型

** @param sensitivewordset 敏感詞庫

*/private static void initsensitivewordmap(setsensitivewordset) else

if (i == key.length() - 1) }}

}/**

* 判斷文字是否包含敏感字元

** @param txt 文字

* @param matchtype 匹配規則 1:最小匹配規則,2:最大匹配規則

* @return 若包含返回true,否則返回false

*/public static boolean contains(string txt, int matchtype)

}return flag;

}/**

* 判斷文字是否包含敏感字元

** @param txt 文字

* @return 若包含返回true,否則返回false

*/public static boolean contains(string txt)

/*** 獲取文字中的敏感詞

** @param txt 文字

* @param matchtype 匹配規則 1:最小匹配規則,2:最大匹配規則

* @return

*/public static setgetsensitiveword(string txt, int matchtype)

}return sensitivewordlist;

}/**

* 獲取文字中的敏感詞

** @param txt 文字

* @return

*/public static setgetsensitiveword(string txt)

/*** 替換敏感字字元

** @param txt 文字

* @param replacechar 替換的字元,匹配的敏感詞以字元逐個替換,如 語句:我愛中國人 敏感詞:中國人,替換字元:*, 替換結果:我愛***

* @param matchtype 敏感詞匹配規則

* @return

*/public static string replacesensitiveword(string txt, char replacechar, int matchtype)

return resulttxt;

}/**

* 替換敏感字字元

** @param txt 文字

* @param replacechar 替換的字元,匹配的敏感詞以字元逐個替換,如 語句:我愛中國人 敏感詞:中國人,替換字元:*, 替換結果:我愛***

* @return

*/public static string replacesensitiveword(string txt, char replacechar)

/*** 替換敏感字字元

** @param txt 文字

* @param replacestr 替換的字串,匹配的敏感詞以字元逐個替換,如 語句:我愛中國人 敏感詞:中國人,替換字串:[遮蔽],替換結果:我愛[遮蔽]

* @param matchtype 敏感詞匹配規則

* @return

*/public static string replacesensitiveword(string txt, string replacestr, int matchtype)

return resulttxt;

}/**

* 替換敏感字字元

** @param txt 文字

* @param replacestr 替換的字串,匹配的敏感詞以字元逐個替換,如 語句:我愛中國人 敏感詞:中國人,替換字串:[遮蔽],替換結果:我愛[遮蔽]

* @return

*/public static string replacesensitiveword(string txt, string replacestr)

/*** 獲取替換字串

** @param replacechar

* @param length

* @return

*/private static string getreplacechars(char replacechar, int length)

return resultreplace;

}/**

* 檢查文字中是否包含敏感字元,檢查規則如下:

** @param txt

* @param beginindex

* @param matchtype

* @return 如果存在,則返回敏感詞字元的長度,不存在返回0

*/private static int checksensitiveword(string txt, int beginindex, int matchtype)

}} else

}//長度必須大於等於1,為詞

if (matchflag < 2 || !flag)

return matchflag;

}/**

* 敏感詞替換工具方法(對外方法)

** @param string

* @return

* @exception ioexception 讀寫檔案異常

*/public static string sensitivehelper(string string) throws ioexception

//不包含返回原本字元

return string;

}}

工具類寫好後,我們需要在過濾器中引用並對傳入的引數值做乙個過濾,新建乙個sensitivewordsfilter,通過使用request.getparameternames()方法獲取到前端引數名的列舉,然後遍歷處理資料,**如下:

@override

public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception

//放行

filterchain.dofilter(servletrequest,servletresponse);

}

如有錯誤,請及時指出,感謝大家。

mysql 敏感詞 PHP實現的敏感詞過濾方法示例

1 敏感詞過濾方法 todo 敏感詞過濾,返回結果 param array list 定義敏感詞一維陣列 param string string 要過濾的內容 return string log 處理結果 function sensitive list,string if count 0 else ...

敏感詞過濾演算法實現

說到敏感詞過濾,我也覺得這裡沒有必要寫這個文章,因為前人已經前前後後有過很多種演算法解決該問題。這裡我之所以寫這個文章,是因為我自己自創了一種演算法 真的是自創哦,因為我在寫這個演算法的時候,完全是自己想出來的方式,沒有借鑑任何 靈感來自於一篇文章中的一句話 如果能掃瞄一遍文字就能將所有的詞找出來,...

PHP實現敏感詞過濾

為了構建良好的網路環境以及為了大家健康的上網。編不下去了 反正你知道敏感詞過濾一定是要有的,如果沒有這東西有關部門不找你喝茶哦!壞笑 壞笑 廢話不多說指尖上乾貨 fake file get contents words.txt 讀取關鍵字文字資訊 這個文字就是敏感詞的文字網上都有 最好選擇最新最全的...