Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to filter sensitive words

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use Java to achieve sensitive word filtering", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Java to achieve sensitive word filtering" article.

First, guide package

The sensitive word filter in this article is used in the SpringBoot project, so you first need to import the following dependencies into the pom.xml file

Org.springframework.boot spring-boot-starter-aop org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime org.springframework.boot Spring-boot-starter-test test org.apache.commons commons-lang3 3.9 II. Sensitive word file

Under the resources directory, create a sensitive-word.txt and fill it with the sensitive word information that needs to be filtered.

Third, the realization of prefix tree

The prefix tree TrieNode starts with an empty node, each node contains several child nodes, and different nodes represent different characters. TrieNode consists of two parts, the first is a boolean variable indicating whether the node is the endpoint of a keyword. The second is the collection of child nodes of the node. In this paper, HashMap is used to store the child node, and key stores the characters represented by the node. The type of Character,value is TrieNode, which represents the child node. The code for the implementation is as follows.

/ / prefix tree private class TrieNode {/ / keyword end tag private boolean isKeywordEnd = false; / / Child node private Map subNodes = new HashMap (); / / get and set methods of isKeywordEnd public boolean isKeywordEnd () {return isKeywordEnd;} public void setKeywordEnd (boolean keywordEnd) {isKeywordEnd = keywordEnd } / / add a child node public void addSubNode (Character cMagneTrieNode node) {subNodes.put (cForce node);} / / get a child node public TrieNode getSubNode (Character c) {return subNodes.get (c) 4. Implementation of sensitive word filter @ Componentpublic class SensitiveFilter {/ / substitution character private static final String REPLACEMENT = "*"; / / Root node private TrieNode rootNode = new TrieNode () / / initialization method of bean. Once the service starts, the container automatically executes this method to bean to complete initialization / / the purpose of this method is to read sensitive word files and build sensitive word prefix tree @ PostConstruct public void init () {try (InputStream is = this.getClass (). GetClassLoader (). GetResourceAsStream ("sensitive-words.txt"); BufferedReader reader = new BufferedReader (new InputStreamReader (is) ) {String keyword; while ((keyword=reader.readLine ())! = null) {this.addKeyword (keyword);}} catch (IOException e) {logger.error ("failed to load sensitive word file:" + e.getMessage ()) Add a sensitive word to the prefix tree private void addKeyword (String keyword) {TrieNode tempNode = rootNode; for (int I = 0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report