|
-
Oct 28th, 2004, 10:37 PM
#1
Thread Starter
Frenzied Member
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:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim xmlDocPath As String = Server.MapPath("bad_words.xml")
Dim xmlReader As XmlTextReader = New XmlTextReader(xmlDocPath)
While (xmlReader.Read())
If xmlReader.NodeType = XmlNodeType.Text Then
alWordList.Add(xmlReader.Value)
Trace.Write("Added: " & xmlReader.Value)
End If
End While
xmlReader.Close()
End Sub
Public Function CheckString(ByVal InputString As String) As String
Dim r As Regex
Dim element As String
Dim output As String
Trace.Write("Checking " & InputString)
For Each element In alWordList
r = New Regex("\b" & element)
Trace.Write("Checking: " & element)
InputString = r.Replace(InputString, "****")
Next
Trace.Write("Returning " & InputString)
Return InputString
End Function
XML
<words>
<word>badword</word>
<word>reallybadword</word>
<word>omgyouusedthisword</word>
</words>
-
Oct 29th, 2004, 12:26 PM
#2
Fanatic Member
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.
-
Oct 31st, 2004, 06:12 PM
#3
Thread Starter
Frenzied Member
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.
-
Oct 31st, 2004, 06:46 PM
#4
Fanatic Member
If wishes were fishes we'd all cast nets.
-
Nov 1st, 2004, 11:13 PM
#5
PowerPoster
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....
-
Nov 3rd, 2004, 10:20 AM
#6
Thread Starter
Frenzied Member
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|