用java过滤文章中的敏感词
来源:互联网 发布:excel数据列转行 编辑:程序博客网 时间:2024/04/28 03:36
我们做博客或bbs时;文章中经常会有些敏感词要去掉;
以下是一个java实现这个功能的小例题:
两个个文件words.properties和KeyWordFilter.java;
1、words.properties文件是个文本文件;内容如下:
敏感词一
敏感词二
敏感词三
2、KeyWordFilter.java是个java文件内容如下:
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class KeyWordFilter
{
private static Pattern pattern = null;
// 从words.properties初始化正则表达式字符串
private static void initPattern()
{
StringBuffer patternBuf = new StringBuffer("");
try
{
InputStream in = KeyWordFilter.class.getClassLoader().getResourceAsStream("words.properties");
Properties pro = new Properties();
pro.load(in);
Enumeration enu = pro.propertyNames();
patternBuf.append("(");
while(enu.hasMoreElements())
{
patternBuf.append((String)enu.nextElement()+"|");
}
patternBuf.deleteCharAt(patternBuf.length()-1);
patternBuf.append(")");
{
private static Pattern pattern = null;
// 从words.properties初始化正则表达式字符串
private static void initPattern()
{
StringBuffer patternBuf = new StringBuffer("");
try
{
InputStream in = KeyWordFilter.class.getClassLoader().getResourceAsStream("words.properties");
Properties pro = new Properties();
pro.load(in);
Enumeration enu = pro.propertyNames();
patternBuf.append("(");
while(enu.hasMoreElements())
{
patternBuf.append((String)enu.nextElement()+"|");
}
patternBuf.deleteCharAt(patternBuf.length()-1);
patternBuf.append(")");
//unix换成UTF-8
//pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "UTF-8"));
//pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "UTF-8"));
//win下换成gb2312
pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "gb2312"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
}
private static String doFilter(String str)
{
Matcher m = pattern.matcher(str);
str = m.replaceAll("");
return str;
}
public static void main(String[] args)
{
String str = "国敏感词一院学位办就敏感词三的报道表示敏感词二";
System.out.println("str:"+str);
initPattern();
Date d1 = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss:SSS Z");
System.out.println("start:"+formatter.format(d1));
System.out.println("共"+str.length()+"个字符,查到" + KeyWordFilter.doFilter(str));
Date d2 = new Date();
System.out.println("end:"+formatter.format(d2));
}
}
pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "gb2312"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
}
private static String doFilter(String str)
{
Matcher m = pattern.matcher(str);
str = m.replaceAll("");
return str;
}
public static void main(String[] args)
{
String str = "国敏感词一院学位办就敏感词三的报道表示敏感词二";
System.out.println("str:"+str);
initPattern();
Date d1 = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss:SSS Z");
System.out.println("start:"+formatter.format(d1));
System.out.println("共"+str.length()+"个字符,查到" + KeyWordFilter.doFilter(str));
Date d2 = new Date();
System.out.println("end:"+formatter.format(d2));
}
}
输出为:
__________________________________
str:国敏感词一院学位办就敏感词三的报道表示敏感词二
start:星期二, 24 三月 2009 14:50:17:171 +0800
共23个字符,查到国院学位办就的报道表示
end:星期二, 24 三月 2009 14:50:17:531 +0800
- 用java过滤文章中的敏感词
- java过滤文章中的敏感词的小例题
- JAVA过滤敏感词
- JAVA 敏感词过滤
- JAVA过滤敏感词
- java敏感词过滤
- JAVA 敏感词过滤
- Java 敏感词过滤
- JAVA敏感词过滤
- Java敏感词过滤
- flex + java 过滤敏感词
- Java实现敏感词过滤
- Java敏感词过滤算法
- java过滤敏感词替换**
- Java实现敏感词过滤
- Java实现敏感词过滤
- Java实现敏感词过滤
- Java实现敏感词过滤
- 百度搜索结果中的网站描述
- IE6、IE7、IE8之IE多版本共存的几种方法
- excel的VBA中两种调用工作表函数的方法
- c3p0 com.mysql.jdbc.CommunicationsException异常
- 如何处理遗留代码
- 用java过滤文章中的敏感词
- js模式对话框实现用户登录
- Tomcat崩溃事件
- 禁忌搜索(Tabu Search或Taboo Search,简称TS)
- Mysql5的auto Reconnect错误
- Eclispe RCP 异常处理 显示异常对话框
- 根据平均值修改单元格颜色
- 怎样从web.config中读取连接字符串的设置
- 2009.03.24