---
title: "3行代码，实现高效的敏感词管理与内容过滤模块"
author: "myreligion"
date: "2010-10-26T08:35:21Z"
tags: []
categories: ["编程语言"]
slug: "3"
draft: false
lastmod: "2010-10-26T08:48:57Z"
id: 793856
url: "https://guzz.iteye.com/blog/793856"
---

## 一．功能介绍

提供对一段内容的敏感词检查与标红服务。对于涉及内容安全与审核的系统，对文字进行敏感词过滤是一项必要功能，敏感词服务即用于完成此项功能。

本服务提供敏感词的分组在线管理，词汇分级，内容过滤，涉及词汇提取，以及正文标红等功能。

在过滤时，附加支持：不区分大小写 + 可检测在词汇中插入特殊字符 + 不破坏HTML和UBB代码。

## 二．示例代码

```
//第1行，获取服务（JSP中写的）
WordFilterService wordFilterService = (WordFilterService) GuzzWebApplicationContextUtil.getGuzzContext(session.getServletContext()).getService("wordFilterService") ;
 
//第2行，进行内容过滤审查。
MatchResult result = (MatchResult) wordFilterService.filterHtml("你好，我是guzz，a*a，你是谁？", new String[]{"your groupId, something like 'b3vh5xmun0r2z4pkil2g5rpxnt2mu76n0r7qqoa'"}, true) ;
 
if(result != null){
	//含有敏感词
	out.println("<br>最高警告级别:" + result.getHighestLevel()) ;
	out.println("<br>匹配到的内容组成的字符串列表:" + result.getHittedContentList()) ;
	out.println("<br>标记以后的内容:" + result.getMarkedContent()) ;
	out.println("<br>匹配的过滤词列表:" + result.getMatchedContentList(",", 5)) ;
}else{
	//不包含敏感词
	out.println("<br>passed!") ;
}
```

## 三．配置服务

1\. 配置本服务依赖的“[通信信道服务](http://www.guzzservices.com/?p=6)”（RPC调用）。假设配置好的信道服务名称为”commandSocketChannelForServices”.

2\. 访问[http://cloud.guzzservices.com/services/console/filterWordGroupList.jsp](http://cloud.guzzservices.com/services/console/filterWordGroupList.jsp)创建过滤词组，增加过滤词。

3\. 在guzz.xml中增加此服务：

```
<service name="wordFilterService" dependsOn="commandSocketChannelForServices" class="com.guzzservices.secure.wordFilter.WordFilterServiceImpl"/>
```

 

4\. 配置服务参数（guzz的properties文件）：

**不需要配置**

## 四．服务API

在需要内容审核的地方，获取或注入wordFilterService，java接口为：com.guzzservices.secure.WordFilterService

**API定义：**

```
package com.guzzservices.secure;
public interface WordFilterService {
 
	/**
	 * 过滤一段文字，根据参数决定是否标红。如果不含有任何过滤词，返回null。
	 * 
	 * @param content 检测内容
	 * @param groupIds 配置的过滤词组编号
	 * @param markContent 是否同时标红过滤的内容。
	 * @return MatchResult
	 */
	public MatchResult filterText(String content, String[] groupIds, boolean markContent) throws Exception ;
 
	/**
	 * 过滤一段html代码段，根据参数决定是否标红。如果不含有任何过滤词，返回null。
	 * 
	 * @param content 检测内容
	 * @param groupIds 配置的过滤词组编号
	 * @param markContent 是否同时标红过滤的内容。
	 * @return MatchResult
	 */
	public MatchResult filterHtml(String content, String[] groupIds, boolean markContent) throws Exception ;
 
}
```

 

服务接口返回null则表示传入的内容不包含敏感词，否则返回MatchResult提供过滤细节。

“同时标红过滤的内容”将增加网络流量与延迟，如果仅仅是检查内容，不需要将标红的正文显示出来，传入false速度会更快。

**MatchResult定义：**

```
package com.guzzservices.secure.wordFilter;
public class MatchResult {
 
	/**
	 * 将发现的过滤词列表进行Distinct排重处理，同时统计每个词的出现次数
	 * 
	 * @return 返回Map<String, 出现次数> 包含 Distinct 处理后的过滤词列表以及相应的出现次数
	 */
	public Map<String, Integer> groupMatchedFilterWords() ;
 
	/**
	 * 返回得到的过滤词中最高警告级别
	 */
	public int getHighestLevel() ;
 
	/**是否可以通过给定的过滤词等级*/
	public boolean canPass(int level) ;
 
	/**标红以后的内容。如果调用服务接口时，参数markContent传入false，则此方法返回null。*/
	public String getMarkedContent() ;
 
	/**成功匹配到的过滤词*/
	public List<String> getMatchedFilterWords() ;
 
	/**匹配到的内容组成的字符串列表*/
	public String getHittedContentList() ;
 
	/**
	 * 返回匹配的过滤词列表。方法会自动删除重复的过滤词，并且将返回字符串长度限制在@param maxLength范围内。
	 * @param wordSep 过滤词之间用什么符号连接，如", "。
	 * @maxLength 返回的串最长允许多长。如果需要将返回结果存入到数据库中，则此参数一般传入数据库字段允许的最大长度。
	 */
	public String getMatchedContentList(String wordSep, int maxLength) ;
```