Search Unity

C# Bad words filter

Discussion in 'Scripting' started by VirusXProgramming, Mar 19, 2015.

  1. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Hi!
    How do I make a c# bad word filter?
    I have almost no idea where to start thanks,
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    string goodText = badText.Replace("Ass","***");
     
  3. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    If you need help coming up with bad words to look for, ask me... I know a lot of 'em.

    But I would do something like what @hpjohn suggested. Store all the bad words in a string array. Then when you're checking input, search for those bad word strings inside the input string, and either replace them with a censor or prohibit the action, whichever is appropriate for you.
     
    Malcolm240104 likes this.
  5. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    It's easy to implement, tough to get right. Search it as I said and read through some of it. As in the example above:

    string goodText = badText.Replace("Ass","***");


    Take this as an example:

    The Assassin ran up and kicked you in the ass!

    That version of replace does:

    The ***assin ran up and kicked you in the ass!

    For filtering you need more than just a replace, you'd need to check case, spelling, slang, and embedded words to make sure you aren't blocking legit words like the poor assassin.
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    It's pretty sloppy, but you could add variations for each possibility.
    Code (CSharp):
    1. public string[] awesomeWords = new string[] {" ass ", " ass.", " ass,", " ass;" ...}
    It would end up being a LOOOOOOONG array, and I'm sure you'd miss specific combinations.

    Maybe a better way would be to have an array of the naughty words, then loop through and search for those strings in the sentence. If found, check the characters preceding and following the string. If the string is at the start of the message OR the preceding character is a space or in a punctuation array AND the character after is a space or in a punctuation array OR the string is at the end of the message, block. I believe that would cover most real world cases.

    Are people going to find a way to game it and get a curse word in? Of course. But I think the idea is you do what you can to reasonably block offensive material and deal with offenders that slip through as necessary.


    Best example ever.