Results 1 to 6 of 6

Thread: Bad word filter

  1. #1

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    Bad word filter

    Does anyone know how to make a bad word filter that is case insinsitive? I have been google-ing all night and found some different examples of word filters (some better than others), but none that are case insensitive without changing the case of the text. I basically want to do what this forum does without having to add diferent cases to the words. Like lets say crap is a bad word, I want it to find Crap, crAP, CRAP, etc, but not crape. I know it can be done with regular expressions (which I think this board uses) but I don't know regular expressions worth crap. Here is the code I have now, but it is case sensative:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim xmlDocPath As String = Server.MapPath("bad_words.xml")
    3.         Dim xmlReader As XmlTextReader = New XmlTextReader(xmlDocPath)
    4.         While (xmlReader.Read())
    5.             If xmlReader.NodeType = XmlNodeType.Text Then
    6.                 alWordList.Add(xmlReader.Value)
    7.                 Trace.Write("Added: " & xmlReader.Value)
    8.             End If
    9.         End While
    10.         xmlReader.Close()
    11.     End Sub
    12.  
    13.     Public Function CheckString(ByVal InputString As String) As String
    14.         Dim r As Regex
    15.         Dim element As String
    16.         Dim output As String
    17.         Trace.Write("Checking " & InputString)
    18.         For Each element In alWordList
    19.             r = New Regex("\b" & element)
    20.             Trace.Write("Checking: " & element)
    21.             InputString = r.Replace(InputString, "****")
    22.         Next
    23.         Trace.Write("Returning " & InputString)
    24.         Return InputString
    25.     End Function
    26.  
    27.  
    28. XML
    29. <words>
    30.     <word>badword</word>
    31.     <word>reallybadword</word>
    32.     <word>omgyouusedthisword</word>
    33. </words>
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  2. #2
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    Easiest way I can think of is to take the input string put it in a temp string, lower/upper case it all and then compare it against your filter that is also lower/upper cased. Then apply the filter to your input string.

    This doesn't take care of your partial matches but it does your case sensativity.
    If wishes were fishes we'd all cast nets.

  3. #3

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    Yeah, that is really all I am finding. I was just hoping someone had a regular expreassion filter like this message board. I am building a website for my 7 year old to review video games, and I want pople to be able to leave comments, but what they write is important to audit. I may just have to review everything until I can find/build a robust filter.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  4. #4
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    http://www.sitepoint.com/forums/show...oto=nextoldest

    Maybe this will point you in the right direction
    If wishes were fishes we'd all cast nets.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Code:
    // Load up an array of bad words from some ficticious method that returns them.
    string[] badWords = GetBadWordList();
    
    string[] myWords = TheText.Split(" ");
    bool badWordInString = false;
    
    foreach(string word in myWords)
    {
      foreach(string badword in badWords)
      {
         if(word.ToUpper() == badWord.ToUpper())
         {
            badWordInString = true;
            break;
         }
      }
    }
    
    if(badWordInString)
       //Do Something....

  6. #6

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    That looks pretty good hellswraith, and if I find a bad word, then I can update that element of the array, and then make a new string
    Code:
    foreach(string word in myWords)
    {
      foreach(string badword in badWords)
      {
         if(word.ToUpper() == badWord.ToUpper())
         {
            word = '***';
         }
      }
    }
    
    string myNewString = '';
    foreach(string word in myWords)
    {
       myNewString = myNewString + word;
    }
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width