国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

sae wordpress 圖片北京網(wǎng)站優(yōu)化站優(yōu)化

sae wordpress 圖片,北京網(wǎng)站優(yōu)化站優(yōu)化,免費咨詢律師在線,微信小程序如何做文章目錄 敏感詞過濾方案一:正則表達式方案二:基于DFA算法的敏感詞過濾工具框架-sensitive-wordspringboot集成sensitive-word步驟一:引入pom步驟二:自定義配置步驟三:自定義敏感詞白名單步驟四:核心方法測…

文章目錄

  • 敏感詞過濾
  • 方案一:正則表達式
  • 方案二:基于DFA算法的敏感詞過濾工具框架-sensitive-word
    • springboot集成sensitive-word
      • 步驟一:引入pom
      • 步驟二:自定義配置
      • 步驟三:自定義敏感詞+白名單
      • 步驟四:核心方法測試

敏感詞過濾

敏感詞過濾通常是指從文本中檢測并移除或替換掉被認為是不適當、冒犯性或違反特定社區(qū)準則的詞匯。這個過程常用于在線平臺、論壇、社交媒體和聊天系統(tǒng)等,以確保交流環(huán)境的健康和積極.

方案一:正則表達式

實現(xiàn)敏感詞過濾.只適合于敏感詞較少、文本量較少的場合,并且無法處理同音字、錯別字等,案例:

public static void main(String[] args) {String text = "這是一個包含敏感詞匯的文本,例如色情、賭博等。";String[] sensitiveWords = {"色情", "賭博"};for (String word : sensitiveWords) {text = filterSensitiveWords(text, word);}System.out.println("過濾后的文本: " + text);testSensitiveWordFrame();}/*** 方案一:正則表達式實現(xiàn)敏感詞過濾.只適合于敏感詞較少、文本量較少的場合,并且無法處理同音字、錯別字等.** @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+ 詞庫,且不斷優(yōu)化更新* 基于 DFA 算法,性能較好* 基于 fluent-api 實現(xiàn),使用優(yōu)雅簡潔* 支持敏感詞的判斷、返回、脫敏等常見操作* 支持全角半角互換* 支持英文大小寫互換* 支持數(shù)字常見形式的互換* 支持中文繁簡體互換* 支持英文常見形式的互換* 支持用戶自定義敏感詞和白名單* 支持數(shù)據(jù)的數(shù)據(jù)動態(tài)更新,實時生效

springboot集成sensitive-word

步驟一:引入pom

<dependency><groupId>com.github.houbb</groupId><artifactId>sensitive-word</artifactId><version>0.2.0</version>
</dependency>

步驟二:自定義配置

@Configuration
public class MySensitiveWordBs {@Autowiredprivate MyWordAllow myWordAllow;@Autowiredprivate MyWordDeny myWordDeny;@Autowiredprivate MyWordReplace myWordReplace;/*** 初始化引導類** @return 初始化引導類* @since 1.0.0*/@Beanpublic SensitiveWordBs sensitiveWordBs() {SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance()
//                .wordAllow(WordAllows.chains(WordAllows.defaults(), myWordAllow)) // 設置多個敏感詞,系統(tǒng)默認和自定義
//                .wordDeny(WordDenys.chains(WordDenys.defaults(), myWordDeny))     // 設置多個敏感詞,系統(tǒng)默認和自定義.wordAllow(WordAllows.chains(myWordAllow))  // 自定義.wordDeny(WordDenys.chains(myWordDeny))     // 自定義.wordReplace(myWordReplace)                                        // 自定義替換規(guī)則.ignoreCase(true)           // 忽略大小寫.ignoreWidth(true)          // 忽略半角圓角.ignoreNumStyle(true)       // 忽略數(shù)字的寫法.ignoreChineseStyle(true)   // 忽略中文的書寫格式.ignoreEnglishStyle(true)   // 忽略英文的書寫格式.ignoreRepeat(true)         // 忽略重復詞.enableNumCheck(true)       // 是否啟用數(shù)字檢測。默認連續(xù) 8 位數(shù)字認為是敏感詞.enableEmailCheck(true)     // 是有啟用郵箱檢測.enableUrlCheck(true)       // 是否啟用鏈接檢測.init();return sensitiveWordBs;}
}

步驟三:自定義敏感詞+白名單

/*** 自定義非敏感詞* 注意每一行為一個非敏感詞,單行不能只包括空格,否則,也會把空格識別為非敏感詞*/
@Component
@Slf4j
public class MyWordAllow implements IWordAllow {@Overridepublic 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 {@Overridepublic 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;}
}
/*** 自定義敏感詞對應的替換值.* 場景說明:有時候我們希望不同的敏感詞有不同的替換結果。比如【游戲】替換為【電子競技】,【失業(yè)】替換為【靈活就業(yè)】。*/
@Configuration
public class MyWordReplace implements IWordReplace {@Overridepublic 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 {@Autowiredprivate MyWordReplace myWordReplace;@Autowiredprivate 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+ 詞庫,且不斷優(yōu)化更新* 基于 DFA 算法,性能較好* 基于 fluent-api 實現(xiàn),使用優(yōu)雅簡潔* 支持敏感詞的判斷、返回、脫敏等常見操作* 支持全角半角互換* 支持英文大小寫互換* 支持數(shù)字常見形式的互換* 支持中文繁簡體互換* 支持英文常見形式的互換* 支持用戶自定義敏感詞和白名單* 支持數(shù)據(jù)的數(shù)據(jù)動態(tài)更新,實時生效*/@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的方法,要使用SensitiveWordBsSystem.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));}
}
http://m.aloenet.com.cn/news/36429.html

相關文章:

  • 學做蛋糕網(wǎng)站如何讓自己網(wǎng)站排名提高
  • 針對網(wǎng)站做搜索引擎做優(yōu)化網(wǎng)盤資源共享網(wǎng)站
  • 微交易網(wǎng)站建設實時積分榜
  • 溫州龍灣做網(wǎng)站2022年最好用的搜索引擎
  • wordpress搜索產(chǎn)品偽靜態(tài)博客seo優(yōu)化技術
  • 政府門戶網(wǎng)站建設的意義電腦培訓學校
  • 無錫知名網(wǎng)站制作百度網(wǎng)站打不開
  • 網(wǎng)站設計過程中需要注意的問題網(wǎng)站網(wǎng)頁的優(yōu)化方法
  • 網(wǎng)站開發(fā)的作用建站之星
  • 做論壇網(wǎng)站需要什么備案網(wǎng)站友鏈交換平臺
  • 做網(wǎng)站網(wǎng)絡營銷策略有哪幾種
  • 福永網(wǎng)站設計多少錢seo入門免費教程
  • 北京自助模板建站黑馬程序員培訓機構在哪
  • 學生做的網(wǎng)站成品網(wǎng)絡客服
  • 西安微網(wǎng)站制作網(wǎng)上廣告宣傳怎么做
  • 畢業(yè)設計網(wǎng)站成品百度接單平臺
  • 做B2C獨立網(wǎng)站的話需要做海外倉嗎人際網(wǎng)絡營銷2900
  • 高端電子網(wǎng)站建設谷歌官方seo入門指南
  • 子目錄 獨立的網(wǎng)站網(wǎng)上怎么發(fā)布廣告
  • 網(wǎng)站源碼破解版網(wǎng)站關鍵詞優(yōu)化排名外包
  • 金融投資公司網(wǎng)站模板網(wǎng)站工具查詢
  • 江蘇省城鄉(xiāng)建設官網(wǎng)站免費私人網(wǎng)站建設
  • wordpress文章全部刪除優(yōu)化seo深圳
  • 不建網(wǎng)站如何做淘寶客社交網(wǎng)絡推廣方法
  • 江陰外貿網(wǎng)站制作福州網(wǎng)站建設
  • 修改網(wǎng)站圖標卡一卡二卡三入口2021
  • 浙里建官方網(wǎng)站百度指數(shù)的主要功能有
  • 手機端網(wǎng)站開發(fā)長沙網(wǎng)站seo方法
  • 美食網(wǎng)站的設計與制作友情鏈接方面pr的選擇應該優(yōu)先選擇的鏈接為
  • 怎樣做原創(chuàng)短視頻網(wǎng)站百度關鍵詞工具