Hi,
Just wondering the best / easiest way to filter string for offensive language is. I have some text that I want to filter for offensive language based on a list of key words.
Thanks
Simon
Hi,
Just wondering the best / easiest way to filter string for offensive language is. I have some text that I want to filter for offensive language based on a list of key words.
Thanks
Simon
Whats the easiest way to filter string for offensive language
Patrick.I
hi,
i have tried this i don't think its the best practice but it works
class Program{
static void Main(string[] args){
string[] badwords = { "badword1", "badword2", "badword3", "bad4" }; string mytext = Console.ReadLine(); string Cleantext = ""; foreach (string badword in badwords){
if(mytext.Contains(badword)){
string newValue = ""; for (int i = 0; i < badword.Length;i++ ){
newValue +=
"*";}
Cleantext = mytext.Replace(badword, newValue);
}
}
if (Cleantext.Length == 0){
Cleantext = mytext;
}
Console.WriteLine(Cleantext); Console.ReadLine();}
}
hope this helps
Christophe Kung
Hello again,
I had a look, but string does not have a method called RemoveAll. However, I did find Regex.Replace (System.Text.RegularExpressions), which does I similar thing to what you mentioned. So the syntax would be:
Str = Regex.Replace(Str,WORD,ALTERNATIVE);
Looping if you have a list.
Cheers
Chellam
You can simply use String.RemoveAll(word). (not 100% on that syntax, it might just be Remove(word)).
If you start with your string STR and your list of words WORDS[], then repeatedly calling
STR = STR.RemoveAll(WORDS[index])
as you step through the words will remove the offending terms. You could also have a secondary list of words that you use to replace offending terms, such as f*ck, fyck, or firetruck. Then it would be a case of
STR = STR.ReplaceAll(WORDS[index], ALTERNATIVES[index]).
Hope this helps.
Alex Lerner
I checked again, and string also has a Replace method, not sure what the difference is between the Regex.Replace and String.Replace, I guess you could use either one.
Cheers
rhenders
Lisa Slater Nicholls
EvelynR
PS: If you have any comments and suggestions, please let me know.
Sheva
Naga Satish Rupenaguntla
And another factor is that regex can be compiled into IL, and you can get some extra performance benefit from it.
Sheva