多线程过滤敏感词

原文:https://git.oschina.net/tianzhenjiu/codes/y9z6t471s3euinoj5vcdf79

package io.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Testoss {

    public static void main(String[] args) throws Exception {    

        String[] keywords={"毛","周","二逼","xx","王五","刘能","你好吗","共 产 党","哈哈","中国人"};

        String content="你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。你好哈222哈2哈啊大a我们是中国人在主实例的读请求较多、读压力比较大的时候,可以通过 DRDS 读写分离功能对读流量进行分流,减轻 RDS 主实例的读压力。";
        int splitthread=2;

        int count=keywords.length/splitthread;

        ExecutorService executorService=Executors.newFixedThreadPool(4);

        boolean filterit=false;

        List<Future<String>> futures=new ArrayList<>();
        for(int i=0;i<keywords.length;i+=count){

            final List<String> strings=new ArrayList<>();

            for(int j=0;i+j<keywords.length&&
                        j<count;j++){

                strings.add(keywords[i+j]);
            }

            futures.add(executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {

                    return filter(content, strings);
                }

            }));

        }

        for(Future<String> future:futures){

            if(future.get()!=null){
                filterit=true;
                System.out.println(future.get());
            }
        }

        executorService.shutdown();

        System.out.println(filterit);

    }

    /**
     *
     * @param content  内容
     * @param keywords 过滤词组
     * @return
     */
    public static String filter(String content,List<String> keywords){
//
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
        for(String keyword:keywords){

            if(content.indexOf(keyword)>0)
                return keyword;
         }

        return null;
    }

}
时间: 2025-01-06 15:49:44

多线程过滤敏感词的相关文章

过滤敏感词方式

一.利用正则表达式 关键正则表达式 .*(关键词1|关键词2|关键词3).* 模拟业务代码 @WebServlet(name = "PatternControl", urlPatterns = {"/p"}) public class PatternControl extends HttpServlet { private static final Pattern pattern = initPattern(); private static Pattern ini

DFA算法过滤敏感词整理

这里有部分是从网上找的,但看起来太乱了,分的太散了.研究了几天,整理出来,有问题的话还请大虾们提出来.... package org.rui.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; i

[转]Filter实现处理中文乱码,转义html标签,过滤敏感词

原文地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目标资源,从而实现一些特殊需求. 一.Decorator设计模式 1.1.Decorator设计模式介绍 当某个对象的方法不适应业务需求时,通常有2种方式可以对方法进行增强: 编写子类,覆盖需增强的

javaEE之---------过滤敏感词(filter)

我们在聊天的时候的或者留言的时候,有部分词是不允许发表出来.我们可以采用过滤器实现这个功能. 我们只是简单利用过滤器实现这个过滤的功能,有些地方没写的很全 前台代码: <span style="font-size:18px;"> <body> <form action="<c:url value='/WordServlet'/>" method="post"> 姓名:<input type=&

js 过滤敏感词 ,可将带有标点符号的敏感词过滤掉

function transSensitive(content) { // var Sensitive = H.getStorage("Sensitive");//敏感词数组 var Sensitive=["张三","李四"];//敏感词数组 var v = new RegExp(Sensitive.join('|'), "gi");//将数组转成正则 var array = new Array(); //将特殊符号的过滤并且

php过滤敏感词

<?php /**  * 敏感词过滤工具类  * 使用方法  * echo FilterTools::filterContent("你妈的我操一色狼杂种二山食物","*",DIR."config/word.txt",$GLOBALS["p_memcache"]["bad_words"]);  */ class FilterTools {     public static $keyword = arr

text1 正则过滤敏感词。

直播平台,内容,聊天网站对用户输入的敏感词进行屏蔽. 例如,写两个文本域和一个btn按钮.当按钮按下时.将用户输入的敏感词转化为"**",再把文本变成转化后的文本. 例子如下: btn.onclick=function(){ var re=/忠臣|反贼|内奸/g: oText2.value=oTxt1.value.replace(re,"**"); }

20170513 Python练习册0011过滤敏感词

#!/usr/bin/env python# -*-coding:utf-8-*- # 第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,# 当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights.import re def filted_word(filename): word_list=[]#定义一个空列表 with open(filename,'r') as f:#以读打开文件 for line in f:#以行为

Python爬虫框架Scrapy 学习笔记 5 ------- 使用pipelines过滤敏感词

还是上一篇博客的那个网站,我们增加了pipeline.py items.py from scrapy.item import Item, Field class Website(Item):     name = Field()     description = Field()     url = Field() dmoz.py from scrapy.spider import Spider from scrapy.selector import Selector from dirbot.i