[Snippet] Simple censor function!

Started by ., Mar 12, 2015, 04:38 PM

Previous topic - Next topic

.

Someone asked me today about something like this because he had trouble implementing one so I decided to make a simple implementation for anyone who needs it. The code is very simple and should be fairly straight forward.

Snippet:
local _CENSOR_WORDS = [
    "poop", "cunt",
    "jerk", "crap",
    "shit", "faggot"
    // More words follow ...
];

local _CENSOR_MASK = "****************************************************************";

function Censor(text)
{
    if (typeof text != "string") return "";
   
    local str = text, pos = null;

    foreach (word in _CENSOR_WORDS)
    {
        while (pos = str.find(word)) {
            str = format(@"%s%s%s",
                str.slice(0, pos),
                _CENSOR_MASK.slice(0, word.len()),
                str.slice(pos + word.len()));
        }
    }
    return str;
}

Example:
print( Censor("Test for jerk, cunt, shit and then for some poop, faggot, crap!\n") );
print( Censor("Clean string!") );
print( Censor("") ); // Empty
print( Censor(32) ); // Invalid

Output:
Test for ****, ****, **** and then for some ****, ******, ****!
Clean string!

NOTE: Sorry for the words in the code but I'd expect you to understand that they are just for example.
.

Thijn

If this is going to be used for onPlayerChat I suggest you return what's given if it isn't a string.

DizzasTeR

Quote from: S.L.C on Mar 12, 2015, 04:38 PMSomeone asked me today about something like this because he had trouble implementing on.

meh... anyway i learned something new from this.

.

#3
Quote from: Thijn on Mar 12, 2015, 05:05 PMIf this is going to be used for onPlayerChat I suggest you return what's given if it isn't a string.

I did that for the sake of consistency. Because the function should be always (strong guarantee) return a string (empty or not). But yeah, I made the update. It should be easy to change it back in case anyone needs it.
.

Sebastian

Heh, really useful. Good job !

@Doom_Killer, see ? It is possible. haha. 8)

Kratos_


Nice . Converting text to lowercase will make it case-insensitive .
In the middle of chaos , lies opportunity.

DizzasTeR

Quote from: Kratos_ on Mar 12, 2015, 05:44 PMNice . Converting text to lowercase will make it case-insensitive .

Just take it tolower....

.

Updated to support word duplicates like:
print( Censor("Just shit as shit in shit!") );Output:
Just **** as **** in ****!
I forgot about those. Next update will be about case insensitivity.
.

FinchDon

Sorry for Bump I add them Above ScriptLoad

and add this onPlayerChat

Censor( text ); but when i type faggot etc it show faggot not ****** why
For any help and support Join #s-s at IRC for Help in Scripting
( For Newbies )

KAKAN

READ what Thijn has posted^^
Quote from: Thijn on Mar 12, 2015, 05:05 PMIf this is going to be used for onPlayerChat I suggest you return what's given if it isn't a string.
oh no

.

Have you added the word in the _CENSOR_WORDS array? If you haven't , then how is the script supposed to know about it. Can you post the whole onPlayerChat fuction? Im sure that you forgot to block the original message and send the censored one instead.
.

KAKAN

Ahh wait. A bug
Try using print(Censor("jerk"));
And see it won't work, it doesn't work for the 1st word, but it works in other part of the sentence
Example:-
print(Censor("jerk"));Output:-[SCRIPT]jerk
Try this:-
print(Censor("Test for jerk"));Output:-[SCRIPT]Test for ****
oh no

KAKAN

And yes how can we do it in .tolower() form? I use it in onplayerchat, and I don't want the messages in .tolower() form, so any way to do it?
oh no

FinchDon

function onPlayerChat( player, text )
{
Censor(" " + text + " " );
Censor(text);
print( " " + player.Name + " : " + text + "." );
local spammer = (GetTickCount()-MSNCount[player.ID])/MSNxSEC;
    MSNPlayer[player.ID]+=spammer-1;
    if(MSNPlayer[player.ID]>LIMIT_REP_SPAM)MSNPlayer[player.ID]=LIMIT_REP_SPAM-1;
    if(MSNPlayer[player.ID]<0)MSNPlayer[player.ID]= -1;
        MSNCount[player.ID]=GetTickCount();
        if(MSNPlayer[player.ID]<0) {
        Message(">> Auto-Kick " + player.Name + " "+ "Reason: [ Spam ].");
      player.Kick();
  }
EchoMessage( "** [" + player.ID + "] " + player.Name + "  " + text + "." );
if(text[0].tochar().tostring()=="!") { local texto=split(text,"! "); local momo=""; for(local i=1;i<texto.len();i++){momo=momo+texto[i]+" ";} onPlayerCommand2(player,texto[0],momo); }
if ( pstats[ player.ID ].Muted == true )
{
PrivMessage( player, "You are currently muted so don't spam wait until 30 seconds");
return 0;
}
return 1;
}

Here you go...
For any help and support Join #s-s at IRC for Help in Scripting
( For Newbies )

DizzasTeR

#14
Here is the fixed one.
function Censor(text)
{
    if (typeof text != "string") return "";
   
    local str = text, pos = null;

    foreach (word in _CENSOR_WORDS)
    {
        while (str.find(word, 0) != null) {
        pos = str.find(word);
        str = format(@"%s%s%s",
        str.slice(0, pos),
        _CENSOR_MASK.slice(0, word.len()),
        str.slice(pos + word.len()));
        }
    }
    return str;
}

Sample:
print( Censor( "shit this shit works" ) );

// Output:

[SCRIPT] **** this **** works

Edit: @FinchDon, You are just passing the chat, the function does its job but you are storing the string which it returns, you need to do it like this:
function onPlayerChat( player, text )
{
    local r_string = Censor( text );
    Message( format( "%s: %s", player.Name, r_string ) );
    return 0;
}