敏感词过滤小程序,文中敏感词中间有空格,换行符的也可以处理
来源:互联网 发布:dvd刻录软件免费版 编辑:程序博客网 时间:2024/04/28 10:06
今天写了一个敏感词过滤的程序,可以对中英文敏感词 进行过滤,对与文中出现敏感词但是敏感词中间有的空格还有换行的可以进行处理
#include<iostream>
#include<stdlib.h>
#include<string>
#include<bitset>
#include<map>
#include<fstream>
#define NUM 60
using namespace std;
int cmp(const void *a,const void *b)
{
int *x=(int *)a;
int *y=(int *)b;
return (*y)-(*x);
}
// 匹配的字符串 敏感词统计 敏感词大小数组
void find(string &p,map<string,int> &word, int *b ,int len )
{
int i=0,j;
string str;
map<string,int>::iterator l_it;
while(p[i]!='\0')
{
for(j=0;j<len&&b[j]!=0;)
{
str=p.substr(i,b[j]);
if(str.empty())
{
j++;
continue;
}
l_it=word.find(str);
if(l_it==word.end())
{ j++;
continue;
}
else
{
word[str]++;
for(int loop=i;loop<i+b[j];loop++)
p[loop]=' ';//已经去掉了空格。再次加上表明是敏感词。
i+=b[j];// 匹配成功则i从匹配之后的位置开始进行继续匹配。
i--;
break;
}
}
i++;
}
}
int main()
{
map<string ,int> mapWord;
map<string,int>::iterator l_it;
int lenFile=-1,lenstr=0;
char w;;
string p="",arr=" ",str;
char ch[300]="";
int i,j,max,wordLen[20],len;//假设敏感词的长度最多有10种
memset(wordLen,0,sizeof(wordLen));
//提取敏感词
ifstream in("word.txt");
max=0;
len=0;
while(in>>str)
{
if(!str.empty())
{
l_it= mapWord.find(str);
if(l_it==mapWord.end())
{
mapWord[str]=-1;
len++;
}
max=str.length();
if(wordLen[max]==0)
{
wordLen[max]=max;
}
}
}
qsort(wordLen,len,sizeof(wordLen[0]),cmp);
in.close();
cout<<"(1) 获取原文:\n";
ifstream fin("test.txt");
memset(ch,'%',sizeof(ch));
while(fin.get(w))
{
cout<<w;
lenFile++;
if(w=='\n')
ch[lenFile]='#';
else if(w==' ')
ch[lenFile]='*';
else
{
arr[0]=w;
arr[1]='\0';
p+=arr;
}
}
fin.close();
cout<<" \n\n(2) 去除换行符,空格之后"<<endl;
cout<<p<<endl;
find(p,mapWord,wordLen,len);
cout<<"\n\n(3) 敏感词过滤之后:"<<endl;
for(i=0,j=0;j<=lenFile;j++)
{
if(ch[j]=='%')
{
if(p[i]==' ')
cout<<"*";
else
cout<<p[i];
i++;
}
else if(ch[j]=='#')
cout<<endl;
else if(ch[j]=='*')
cout<<" ";
}
cout<<"\n_________________________________________________________\n";
cout<<"(4) 统计敏感词出现的个数\n";
for(l_it=mapWord.begin();l_it!=mapWord.end();l_it++)
if(l_it->second!=-1)
cout<<" \n"<<l_it->first<<"\t "<<l_it->second+1;
cout<<"\nthanks!\n";
}
- 敏感词过滤小程序,文中敏感词中间有空格,换行符的也可以处理
- 以下是一个敏感词过滤的的小例题
- java过滤文章中的敏感词的小例题
- 配置过滤去处理 敏感词
- 过滤敏感词
- JAVA过滤敏感词
- 敏感词过滤算法
- web过滤敏感词
- 敏感词过滤
- php敏感词过滤
- 过滤敏感词
- 过滤敏感词
- 敏感词过滤
- 过滤敏感词,脏词
- c++敏感词过滤
- 敏感词过滤
- php过滤敏感词
- 敏感词过滤
- oracle启动之后连接的时候报ora-01033:oracle initializationg or shutdown in progress
- linux下C++SocketTCP编程---客户端
- 关掉firefox(火狐)和palemoon地址栏自动加www.前缀功能
- Doing Homework again之贪心解题报告
- css实现右侧固定宽度,左侧宽度自适应
- 敏感词过滤小程序,文中敏感词中间有空格,换行符的也可以处理
- Yupoo(又拍网)的系统架构
- CSDN数据库泄露!那些网站够安全吗?
- CSDN爆库内幕:密码为什么明文存放?
- 定义好word模板之后,使用java和jacob代码替换里面的指定的内容生成word
- Android学习网站
- java语言基础——4常量与变量
- 在Hi3531上运行QT
- 从CSDN用户密码泄露谈一些非技术安全对策