0

I have a list of comments that gets formed. The client asked us to use a profanity filter to check the comments before they get published. I have a ProfanityFilter.cs class. The only thing is I am not sure how to integrate the the two.

Simply put, how to I have the comments hit the filter before they get published.

Thanks in advance!

Here is the code where the list of comments get formed:

public void ProcessRequest (HttpContext context) 
{

    // ****************************************
    if (context.Request["postform"] == "1")
    {

        videomessage myVideoMessage = new videomessage();

        myVideoMessage.video_id = context.Request["video_id"];
        myVideoMessage.first_name_submitter = context.Request["first_name_submitter"];
        myVideoMessage.last_initial_submitter = context.Request["last_initial_submitter"];
        myVideoMessage.message = context.Request["message"];
        myVideoMessage.status = "0";

        myVideoMessage.Save();
    }
    // ****************************************

    // ****************************************
    StringBuilder myStringBuilder = new StringBuilder();


    // PULL VIDEOMESSAGES FOR VIDEO_ID
    videomessage[] myCommentsList = new videomessage().Listing("video_id", context.Request["video_id"], "entry_date" , "DESC");

    // FORM COMMENTS IF MORE THAN ONE COMMENT EXISTS
    foreach (videomessage tmpMessage in myCommentsList)
    {
        if (tmpMessage.status == "0" || tmpMessage.status == "1")
        {
            myStringBuilder.Append("<div class=\"comment_box\">");
            myStringBuilder.Append("<p class=\"comment_date\">");
            myStringBuilder.Append(Utility.FormatShortDate(tmpMessage.entry_date) + " " + tmpMessage.first_name_submitter + " " + tmpMessage.last_initial_submitter + "." + "</p>");

            if (!String.IsNullOrEmpty(tmpMessage.message))
            {
                myStringBuilder.Append("<p>" + tmpMessage.message + "</p>");
                myStringBuilder.Append("</div>");
            }
        }
    }
    string return_str = myStringBuilder.ToString();

    // IF NO COMMENTS RETURN THIS
    if( String.IsNullOrEmpty(return_str) )  return_str = "<p>No comments currently exist for this video.</p>";
    // ****************************************

    // RETURN STRING        
    context.Response.ContentType = "text/plain";
    context.Response.Write(return_str);
}

Here is the filter:

public class ProfanityFilter
{        
    // METHOD: containsProfanity
    public bool containsProfanity(string checkStr)
    {
        bool badwordpresent = false;

        string[] inStrArray = checkStr.Split(new char[] { ' ' });

        string[] words = this.profanityArray();

        // LOOP THROUGH WORDS IN MESSAGE
        for (int x = 0; x < inStrArray.Length; x++)
        {
            // LOOP THROUGH PROFANITY WORDS
            for (int i = 0; i < words.Length; i++)
            {
                // IF WORD IS PROFANITY, SET FLAG AND BREAK OUT OF LOOP
                //if (inStrArray[x].toString().toLowerCase().equals(words[i]))
                if( inStrArray[x].ToLower() == words[i].ToLower() )
                {
                    badwordpresent = true;
                    break;
                }
            }
            // IF FLAG IS SET, BREAK OUT OF OUTER LOOP
            if (badwordpresent == true) break;
        }

        return badwordpresent;
    }
    // ************************************************************************




    // ************************************************************************
    // METHOD: profanityArray()
    // METHOD OF PROFANITY WORDS
    private string[] profanityArray()
    {

        // THESE WERE UPDATED TO USE THE SAME BADWORDS FROM FACESOFMBCFBAPP
        string[] words = {//words in the array}
}
return words;
}
Peter
  • 427
  • 4
  • 9
  • 37
  • 1
    What are you asking? Can't you just add a call to containsProfanity() and throw an exception (or write an error message) if it returns true? Or are you looking for something more abstract? E.g. do you want to inject the filter using an IoC framework? – James Sep 21 '12 at 14:44
  • Nothing abstract, just not sure how when a user hits submit, the messgae has to pass through the filter first and if it is clean, then post, otherwise have it put an error message – Peter Sep 21 '12 at 14:47
  • 1
    Off Topic: That's not a very robust profanity checker. Let's imagine that "Partridge" is deemed to be an unacceptable word. This implementation would let the sentence "That's just -partridge- crazy!". Of course, a profanity filter is prone to false positives as well if you go too aggressive - I'm sure you can think of some 'bad' words that appear as substrings of other words. You may want to set expectations with your client on that score. – ZombieSheep Sep 21 '12 at 14:49
  • 1
    When will people (managers, designers, whoever) realise that dictionary lookups won't prevent profanity, I mean, how big is your dictionary going to be? And what about context? And then there's localisation issues as well. – Skizz Sep 21 '12 at 14:50
  • I agree with the above, but its what the client has asked for. Personally I would find a third party app and check against that, but its not my call. – Peter Sep 21 '12 at 15:06

2 Answers2

8

Check these questions:

I would suggest reading the accepted answer to the last question and the CodingHorror blog post. While the OP asked for a PHP solution, you might want to consider an alternate approach after reading those.

Community
  • 1
  • 1
Laoujin
  • 9,962
  • 7
  • 42
  • 69
3

After string return_str = myStringBuilder.ToString(); add:

ProfanityFilter filter = new ProfanityFilter();

if (filter.ContainsProfanity(return_str)
{
    // Do action when message contains profanity
}
else
{
    // Do action when message does not contains profanity     
}

Also have in mind that your code is not taking into account line breaks (I don't know if comments cannot have line breaks, just in case of).

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207