java脏字过滤_脏字过滤

1.[文件]

SensitiveWordFilter.java ~ 7KB

下载(141)

package com.forgov.sharpc.infrastruture.util;

import static java.util.Collections.sort;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Comparator;

import java.util.HashSet;

import java.util.LinkedList;

import java.util.List;

/**

*

* @author loudyn

*

*/

public class SensitiveWordFilter {

private WordNode rootWordNode = new WordNode(null, 'R', WordNode.MIDSIDE_TYPE);

/**

*

* @param word

* @return

*/

public final SensitiveWordFilter addFilterWord(String word) {

char[] chars = word.toCharArray();

if (chars.length > 0) {

insertWordNode(rootWordNode, chars, 0);

}

return this;

}

/**

*

* @param word

* @return

*/

public final SensitiveWordFilter removeFilterWord(String word) {

char[] chars = word.toCharArray();

if (chars.length > 0) {

removeWordNode(rootWordNode, chars, 0);

}

return this;

}

/**

*

* @param wordNode

* @param chars

* @param index

*/

private void removeWordNode(WordNode wordNode, char[] chars, int index) {

convertEnglishAlphabetToLowerCase(chars, index);

WordNode node = searchWordNode(wordNode, chars[index]);

if (null == node) {

return;

}

if (index == chars.length - 1) {

node.type = WordNode.MIDSIDE_TYPE;

}

if (++index < chars.length) {

removeWordNode(node, chars, index);

}

}

/**

*

* @param words

* @return

*/

public final SensitiveWordFilter addFilterWords(Collection words) {

for (String word : words) {

addFilterWord(word);

}

return this;

}

/**

*

* @param words

* @return

*/

public final SensitiveWordFilter removeFilterWords(Collection words) {

for (String word : words) {

removeFilterWord(word);

}

return this;

}

/**

*

* @return

*/

public final SensitiveWordFilter reset() {

destroyFilterWordTree(rootWordNode);

rootWordNode = new WordNode(null, 'R', WordNode.MIDSIDE_TYPE);

return this;

}

/**

*

* @param wordnode

* @return

*/

private SensitiveWordFilter destroyFilterWordTree(WordNode wordnode) {

if (null != wordnode.children && !wordnode.children.isEmpty()) {

for (WordNode node : wordnode.children) {

destroyFilterWordTree(node);

}

}

wordnode.children = null;

wordnode = null;

return this;

}

/**

*

* @param text

* @param replacement

* @return

*/

public final String doFilter(String text, String replacement) {

List hitWords = new LinkedList();

try {

if (beforeFilter(text, replacement)) {

hitWords = hit(text);

return doInternalFilter(text, hitWords, replacement);

}

} catch (Exception e) {

ExceptionUtils.toUnchecked(e);

} finally {

afterFilter(hitWords, text, replacement);

}

return text;

}

/**

*

* @param wordNode

* @param chars

* @param index

*/

private void insertWordNode(WordNode wordNode, char[] chars, int index) {

convertEnglishAlphabetToLowerCase(chars, index);

WordNode node = searchWordNode(wordNode, chars[index]);

if (null == node) {

node = new WordNode(wordNode, chars[index], WordNode.MIDSIDE_TYPE);

wordNode.children.add(node);

}

if (index == chars.length - 1) {

node.type = node.children.isEmpty() ? WordNode.END_TYPE : WordNode.BOTH_TYPE;

}

if (node.parent.type == WordNode.END_TYPE) {

node.parent.type = WordNode.BOTH_TYPE;

}

if (++index < chars.length) {

insertWordNode(node, chars, index);

}

}

/**

*

* @param chars

* @param index

*/

private void convertEnglishAlphabetToLowerCase(char[] chars, int index) {

if (chars[index] >= 65 && chars[index] <= 90) {

chars[index] = (char) (chars[index] + 32);

}

}

/**

*

* @param wordNode

* @param c

* @return

*/

private WordNode searchWordNode(WordNode wordNode, char c) {

List children = wordNode.children;

for (WordNode node : children) {

if (node.value == c) {

return node;

}

}

return null;

}

/**

*

* @param text

* @param replacement

* @return

*/

protected boolean beforeFilter(String text, String replacement) {

// add hook by overwrite this method;

return true;

}

/**

*

* @param text

* @return

*/

private List hit(String text) {

List hitWords = new LinkedList();

List foundChars = new LinkedList();

WordNode node = rootWordNode;

int index = 0;

char[] chars = text.toCharArray();

while (index < chars.length) {

convertEnglishAlphabetToLowerCase(chars, index);

node = searchWordNode(node, chars[index]);

if (null == node) {

node = rootWordNode;

index -= foundChars.size();

foundChars.clear();

}

else if (node.type == WordNode.END_TYPE) {

node = rootWordNode;

foundChars.add(chars[index]);

hitWords.add(charListToString(foundChars));

index -= (foundChars.size() - 1);

foundChars.clear();

}

else if (node.type == WordNode.BOTH_TYPE) {

foundChars.add(chars[index]);

hitWords.add(charListToString(foundChars));

}

else {

foundChars.add(chars[index]);

}

index++;

}

return hitWords;

}

/**

*

* @param hitWords

* @param text

* @param replacement

*/

protected void afterFilter(List hitWords, String text, String replacement) {

// add hook by overwrite this method;

}

/**

*

* @param chars

* @return

*/

private String charListToString(List chars) {

StringBuilder buf = new StringBuilder();

for (char c : chars) {

buf.append(c);

}

return buf.toString();

}

/**

*

* @param text

* @param hitWords

* @param replacement

* @return

*/

private String doInternalFilter(String text, List hitWords, String replacement) {

List copy = new ArrayList(new HashSet(hitWords));

hitWords.clear();

hitWords.addAll(copy);

sort(hitWords, WORD_COMPARATOR);

for (String foundWord : hitWords) {

text = text.replaceAll("(?iu)" + foundWord, replacement);

}

return text;

}

private final static Comparator WORD_COMPARATOR = new Comparator() {

public int compare(String one, String other) {

return other.length() - one.length();

}

};

/**

*

* @author loudyn

*

*/

protected static final class WordNode {

static final int END_TYPE = 1;

static final int MIDSIDE_TYPE = 1 << 1;

static final int BOTH_TYPE = END_TYPE | MIDSIDE_TYPE;

final char value;

int type;

WordNode parent;

List children = new ArrayList(0);

WordNode(WordNode parent, char value, int type) {

this.parent = parent;

this.value = value;

this.type = type;

}

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393320.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

react中使用构建缓存_完整的React课程:如何使用React构建聊天室应用

react中使用构建缓存In this video course, youll learn React by building a chat room app.在本视频课程中&#xff0c;您将通过构建聊天室应用程序来学习React。 By the end of the video, youll have a solid understanding of React.js and have your very own chat room…

leetcode1509. 三次操作后最大值与最小值的最小差

给你一个数组 nums &#xff0c;每次操作你可以选择 nums 中的任意一个元素并将它改成任意值。 请你返回三次操作后&#xff0c; nums 中最大值与最小值的差的最小值。 示例 1&#xff1a; 输入&#xff1a;nums [5,3,2,4] 输出&#xff1a;0 解释&#xff1a;将数组 [5,3,…

MySQL异步复制

准备&#xff1a;主备库版本一致&#xff0c;正常安装软件。 1、主库上设置一个复制使用的账户&#xff1a; mysql> grant replication slave on *.* to rep1192.168.100.136 identified by dbking; Query OK, 0 rows affected (0.18 sec) mysql> select user,host,passw…

开源一个爬取redmine数据的测试报告系统

背景 软件测试的最后有一道比较繁琐的工作&#xff0c;就是编写测试报告。手写测试报告在数据统计和分析上面要耗费比较大的事件和精力。之前工作室使用mantis管理bug缺陷。公司有内部有个系统&#xff0c;可以直接从mantis上面获取数据并进行统计&#xff0c;生成一份测试报告…

java cxf 双向通讯_CXF 在spring boot 2 发布多个服务

0. 问题来源之前配置cxf服务端都是在spring 3以下&#xff0c;后来使用spring mvc 还都是基于xml的配置文件模式&#xff0c;在springboot模式下&#xff0c;实现起来更为简单了。此次记录下spring boot 2下的实现方式。1. 准备工作项目中&#xff0c;直接拉入spring boot cxf相…

小程序 坚屏_如何构建坚如磐石的应用程序

小程序 坚屏不同的应用程序设计选项概述 (An overview of different app design options) When we design software, we constantly think about error cases. Errors have a huge impact on the way we design and architecture a solution. So much so, in fact, that there …

C# 分层

三层架构分为&#xff1a;表现层&#xff08;UI&#xff09;、业务逻辑层&#xff08;BLL&#xff09;、数据访问层&#xff08;DAL&#xff09;再加上实体类库&#xff08;Model&#xff09; 转载请注明出自朱朱家园http://blog.csdn.net/zhgl7688 1、实体类库&#xff08;Mod…

leetcode1177. 构建回文串检测(前缀和)

给你一个字符串 s&#xff0c;请你对 s 的子串进行检测。 每次检测&#xff0c;待检子串都可以表示为 queries[i] [left, right, k]。我们可以 重新排列 子串 s[left], …, s[right]&#xff0c;并从中选择 最多 k 项替换成任何小写英文字母。 如果在上述检测过程中&#xf…

java界面化二叉排序树_层次序创建二叉树(图形界面和控制台输入实现)

1 2018.11.72 XT34 /**5 * 功能&#xff1a;构造二叉树6 * 说明&#xff1a;7 * 1.主函数输入模式有两种&#xff0c;BT参数 true 图形界面&#xff0c;false 控制台输入8 * 2.构造树是按层次遍历结果输入的 如&#xff1a;ABCDE*F**GH9 */1011 import javax.swing.*;12 import…

web开发环境_Web开发人员的开发环境

web开发环境With all the tools and programs available, it can be challenging to figure out the best way to set up your development environment on your computer.使用所有可用的工具和程序&#xff0c;寻找在计算机上设置开发环境的最佳方法可能是一项挑战。 In this…

使用.net Stopwatch class 来分析你的代码

当我们在调试&#xff0c;优化我们的代码的时候&#xff0c;想知道某段代码的真正的执行时间&#xff0c;或者我们怀疑某段代码&#xff0c;或是某几段代码执行比较慢&#xff0c; 需要得到具体的某段代码的具体执行时间的时候。有一个很好用的类Stopwatch。 Stopwatch 类在 Sy…

Docker 部署 postgresql 与 pgadmin4

Docker快速部署PostgreSQL服务 快速开始 请新建一个目录postgresql&#xff0c;进入目录postgresql&#xff0c;将以下文件保存为docker-compose.yml&#xff0c;然后执行docker-compose up version: 3 services:mydb:image: postgres:11volumes:- db-data:/var/lib/postgresql…

leetcode151. 翻转字符串里的单词

给定一个字符串&#xff0c;逐个翻转字符串中的每个单词。 示例 1&#xff1a; 输入: “the sky is blue” 输出: “blue is sky the” 代码 class Solution {public String reverseWords(String s) {int ns.length(),i0;ArrayList<String> arrayListnew ArrayList<…

java衍生作用_java-如何从AffineTransform衍生的形状对象中“...

您可以使用AffineTransform.transform(Point2D, Point2D)变换多边形上的单个点.如果您不使用旋转变换来移动船,而是将船的位置保持在一个(x,y)位置,那么事情就简单得多.您可以在move()中移动飞船的位置,而不是尝试平移多边形.然后,当您想给船上油漆时,例如做&#xff1a;// Opt…

初学者设计数据库_面向初学者的完整数据库设计课程

初学者设计数据库This database design course will give you a deeper grasp of database design. Caleb Curry teaches the equivalent of an entire college course during this eight hour video.本数据库设计课程将使您更深入地了解数据库设计。 在这8个小时的视频中&…

Qt QTcpSocket使用总结

socket的连接是异步的&#xff0c;所以必须等连接建立完成才能使用&#xff0c;所以分别加入waitForConnected()和waitForBytesWritten()后调试通过1&#xff09;只有使用waitForConnected()后,QTcpSocket才真正尝试连接服务器&#xff0c;并返回是否连接的结果2&#xff09;避…

[bzoj1303][CQOI2009]中位数图

来自FallDream的博客&#xff0c;未经允许&#xff0c;请勿转载&#xff0c;谢谢。 给定一个n个数排列&#xff0c;求有多少段长度为奇数的区间&#xff0c;中位数是b. n<100000 时间限制0.1s 我一开始没看到排列&#xff0c;想着怎么还能O(n)做的啊&#xff1f;&#xff1f…

falsk 请求没有返回值报错

线上报警 5xx 错误&#xff0c;查看日志发现报这个错&#xff0c; TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. 这个方法没有有效的返回结果 页面报这个错误 Internal Server Err…

java的iterator接口_java Iterator接口和LIstIterator接口分析_java_脚本之家

java Iterator接口和LIstIterator接口分析目录1.Iterator接口2.ListIterator3.Iterator和ListIterator的区别正文在继续看ArrayList源码之前&#xff0c;先了解Iterator接口和ListIterator接口&#xff0c;下篇文章详细讲解ArrayList是如何实现它们的。我们知道&#xff0c;接…

leetcode468. 验证IP地址

编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址。 IPv4 地址由十进制数和点来表示&#xff0c;每个地址包含4个十进制数&#xff0c;其范围为 0 - 255&#xff0c; 用(".")分割。比如&#xff0c;172.16.254.1&#xff1b; 同时&#xff0c;IPv4 地…