关于#敏感词#的问题,如何解决?

有人能帮帮忙吗,完善一下代码。过滤敏感词的程序

 

#include 
#include 
#define PRODUCTION 0 /** 0=debug-mode | 1=production mode **/
#define MAXSTRSIZE 1000
#define MAXCURSESIZE 20
/** function prototype **/
int censor(char *, char *);
int main(void)
{
    int foul;
    int validCurseWord;
    char str[MAXSTRSIZE];
    char curse[MAXCURSESIZE];
    if(PRODUCTION)
    {
        /** call your function to fetch a phrase from the user to test for bad 
language **/
    }
    else
    {
        strcpy(str, "darn this is my darnest test string to test for those darn 
words in this crap assignment.");
    }
    printf("\nTesting the text '%s' now....", str);
    // Loop until the user wants to stop checking the string for another curse word
    validCurseWord = 1;
    while(validCurseWord == 1)
    {
        if(PRODUCTION)
        {
            /** call a function to get a single curse word and store
                the return value in the variable validCurseWord
            **/
        }
        else
        {
            strcpy(curse, "darn");
            validCurseWord = 1;    // indicate that we have a valid curse word
        }
        // stop this while loop if the user didn't enter a curse word
        if(!validCurseWord)
        {
            printf("\nTerminating this program because no curse word to test was 
entered.");
            break;
        }
        else
        {
            foul = censor(str, curse);
            if (foul == 1)
                printf("\nThere was potty language '%s' in your phrase. It was 
censored to:\n'%s'\n", curse, str);
            else
                printf("\nYour sentence was clean. We didn't find '%s' in your 
string.", curse);
        }
        // in Debug mode, run this while loop only once
        if(!PRODUCTION)
            break;
    }
    /***END OF THIS PROGRAM***/
    printf("\n\nDone!\n");
    return 0;
}
/******************************************************************
    Function to do what???
    Input parameters:
        phrase -    ??
        curseWord - ??
    Return value
        ???
*****************************************************************/
int censor(char phrase[], char curseWord[])
{
    int i, j, n, k, match;
    i = 0;
    // loop over all characters in the char array "phrase"
    while(phrase[i] != '\0')
    {
        // If the first letter matches
        if(phrase[i] == curseWord[0])
        {
            j = 1;
            match = 1; // let's assume we find a matching letter => match is true
            while(curseWord[j] != '\0' && match == 1)
            {
                if(curseWord[j] != str[i+j])
                {
                    match = 1; // we didn't find a matching letter => match is 
false
                }
            }
            if(curseWord[j] == '\0')
            {
                if(phrase[i+j] == ' ' || phrase[i+j] == '\0')
                {
                    foul = 1;
                    k = 0;
                    while(k <= j);
                    {
                        phrase[i+k] = '*';
                        k = k + 1;
                    }
                }
            }
        }
        // Skip to the next word
        while(phrase[i] != ' ' && phrase[i] != '\0')
        {
            i = i + 1;
        }
        if (phrase[i] = ' ')
            i + i + 1;
    }
    return 0;
}

字符过滤器
函数filter(char str[],char invalid[])功能是过滤掉str字符串中所有在invalid字符串中出现过的字符

img

#include <stdio.h>
#define MAX 255
void filter(char str[],char invalid[])
{
    int i,j,k;
    for(j=0; str[j]!='\0'; j++)
    {
        for(i=0; invalid[i]!='\0'; i++)
        {
            if(str[j]==invalid[i])
            {
                for(k=j; str[k]!='\0'; k++)
                {
                    str[k]=str[k+1];
                }
                j--;
            }
        }
    }
}
int main()
{
    char str[MAX];
    char invalid[MAX];
    gets(str);
    gets(invalid);
    filter(str,invalid);
    puts(str);
    return 0;
}

单纯用字符过滤还是性能差了些吧,是不是可以考虑加上搜索引擎,性能会有所提高。


#pragma once

#ifndef __KAIHUA_SENSITIVE__
#define __KAIHUA_SENSITIVE__
#include <map>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


class Sensitive
{
map<string,char> ctr;
public:




//==========================================================
//  功能:导入敏感词库  //
//  参数:需要导入敏感词库的,文件txt的名字  //
//==========================================================


void load( const char* filePath );


//==========================================================
//  功能:敏感词过滤  //
//  需要过滤的字符串  //
//==========================================================
string replace(string str);


Sensitive(void);
~Sensitive(void);
};


#endif


.cpp

#include "Sensitive.h"




Sensitive::Sensitive(void)
{
}




Sensitive::~Sensitive(void)
{
}


void Sensitive::load( const char* filePath )
{
char str[128] = {};
//map<string,char>ctr;
ifstream f;
f.open(filePath,ios::in);
int i=0;
while(EOF != (str[i] = f.get()))
{
string str1;
if (str[i] == '\n')
{
str1.assign(str,i);
ctr.insert(pair<string,char>(str1,'a'));
i = 0;
}
else 
i++;
}
}


string Sensitive::replace( string str)
{
int len = str.size();
string sContent;
for (int i=0;i<len;i++)
{
sContent = str.substr(0,len-i);
int a = ctr.count(sContent);
if( 0 != a)
{
for (int j=0;j<len-i;j++) str[j] = '*';
break;
}else
{
for (int j=2;j<10;j++)
{
sContent = str.substr(i,j);
int a = ctr.count(sContent);
if( 0 != a)
{
for (int z=0;z<j;z++) str[i+z] = '*';
break;
}
}
}


} 
return str;
}

加上搜索引擎

#include <stdio.h>
#include <string.h>
#define PRODUCTION 0 /** 0=debug-mode | 1=production mode /
#define MAXSTRSIZE 1000
#define MAXCURSESIZE 20
/
function prototype /
int censor(char
, char
);
int foul;
int main(void)
{

int validCurseWord;
char str[MAXSTRSIZE];
char curse[MAXCURSESIZE];
if (PRODUCTION)
{
    /** call your function to fetch a phrase from the user to test for bad

language /
}
else
{
strcpy(str, "darn this is my darnest test string to test for those darn words in this crap assignment.");
}
printf("\nTesting the text '%s' now....", str);
// Loop until the user wants to stop checking the string for another curse word
validCurseWord = 1;
while (validCurseWord == 1)
{
if (PRODUCTION)
{
/
call a function to get a single curse word and store
the return value in the variable validCurseWord
/
}
else
{
strcpy(curse, "darn");
validCurseWord = 1; // indicate that we have a valid curse word
}
// stop this while loop if the user didn't enter a curse word
if (!validCurseWord)
{
printf("\nTerminating this program because no curse word to test was entered.");
break;
}
else
{
foul = censor(str, curse);
if (foul == 1)
printf("\nThere was potty language '%s' in your phrase. It was censored to : \n'%s'\n", curse, str);
else
printf("\nYour sentence was clean. We didn't find '%s' in your string.", curse);
}
// in Debug mode, run this while loop only once
if (!PRODUCTION)
break;
}
/END OF THIS PROGRAM/
printf("\n\nDone!\n");
return 0;
}
/
****************************************************************
Function to do what???
Input parameters:
phrase - ??
curseWord - ??
Return value
???
*****************************************************************/
int censor(char phrase[], char curseWord[])
{
int i, j, n, k, match;
i = 0;
// loop over all characters in the char array "phrase"
while (phrase[i] != '\0')
{
// If the first letter matches
if (phrase[i] == curseWord[0])
{
j = 1;
match = 1; // let's assume we find a matching letter => match is true
while (curseWord[j] != '\0' && match == 1)
{
if (curseWord[j] != phrase[i + j])
{
match = 1; // we didn't find a matching letter => match is
false;
}
}
if (curseWord[j] == '\0')
{
if (phrase[i + j] == ' ' || phrase[i + j] == '\0')
{
foul = 1;
k = 0;
while (k <= j);
{
phrase[i + k] = '*';
k = k + 1;
}
}
}
}
// Skip to the next word
while (phrase[i] != ' ' && phrase[i] != '\0')
{
i = i + 1;
}
if (phrase[i] = ' ')
i + i + 1;
}
return 0;
}