Page 1 of 1

Help Needed: Making a profanity filter

Posted: Wed Jul 22, 2020 2:24 pm
by Angelo Seraphim
Hi, all!

I hope everyone is doing well.

So I've been working on creating a code that can filter/censor bad words on the fly.

Below is my pseudo code. :mrgreen:

So I was kind of messing around with this code that is made to censor bad words and it seems to work as intended (outside of RenPy).
But now I'm struggling to figure out how to implement it correctly to work properly in RenPy.

Here's the code as a function;

Code: Select all

init python:

    def profane_filter(sentence, text_censor=False):

        p_filter = { <list of bad words here> }  ## Removed bad words for the sake of keeping it friendly.

        if sentence is not None:
            if text_censor:
                for f in p_filter:
                    sentence = sentence.replace(f, ('*' * len(f)))
                    sentence = sentence.replace(f.upper(), ('*' * len(f)))
                    sentence = sentence.replace(f.capitalize(), ('*' * len(f)))

            return sentence
    
    ## Not sure if I'm using this correctly or if it's not the one I want, but it doesn't yield any results.
    config.say_menu_text_filter = profane_filter(renpy.get_say_attributes(), text_censor=True)
So I've tried;

Code: Select all

screen say(who, what):
    
    style_prefix "say"

    python:
        ## Hm. Maybe this list should be outside the python block?
        p_filter = { <list of bad words here> }

        for f in p_filter:
            what = what.replace(f, ('*' * len(f)))
            what = what.replace(f.upper(), ('*' * len(f)))
            what = what.replace(f.capitalize(), ('*' * len(f)))

    window:
        id "window"

        if who is not None:

            window:
                id "namebox"
                style "namebox"
                text who.upper() id "who"

        text what id "what"

    ...
But this results in an exception occurring;

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 31, in script
    "What the fuck?"
Exception: The displayable with id 'what' was not given the exact contents of the what variable given to the say screen.
... I kinda understand what it's trying to tell me, but... I have no idea how to solve this.

Any and all suggestions are welcome. Thank you! :D

EDIT: Upon further testing I realised that the function above is actually really agressive.
Example;

Code: Select all

def profane_filter(sentence, text_censor=False):

    p_filter = { 'tit' }  ## Removed bad words for the sake of keeping it friendly.

    if sentence is not None:
        if text_censor:
	    for f in p_filter:
                sentence = sentence.replace(f, ('*' * len(f)))
		sentence = sentence.replace(f.upper(), ('*' * len(f)))
                sentence = sentence.replace(f.capitalize(), ('*' * len(f)))

        return sentence
            
print(profane_filter("Title", text_censor=True))

Output <<< "***le".
Perhaps I may be asking for too much, but of course, this code isn't ideal to censor part of a word that isn't meant to be censored.

Re: Help Needed: Making a profanity filter

Posted: Thu Jul 23, 2020 1:06 am
by Andredron
init python:
# Your name (empty yet)
temp = ""
# The first list of names with the names of the gods of Olympus
greek_gods = ['Zeus', 'Hera', 'Apollo', 'Artemis', 'Dionysus', 'Hephaestus', 'Ares', 'Aphrodite', 'Hermes', 'Athena', 'Poseidon', 'Demeter' ]
# The second list of names with Slavic names
slavic = ["Borislav", "Borislava", "Belian", "Belianka", "Bogumil", "Bogumila", "Bronislav", "Bronislava", "Vishnyak", "Cherry", "Volia", "Vogneda" , "Vognedar", "Gremislav", "Gremislava", "Share", "Zhdan", "Zhdana", "Lyubomil", "Lyubomila", "Miroslav", "Miroslava", "Mlad", "Mlada", " Radomir "," Radomira "," Orimir "," Orimira "," Yaroslava "]
# the function remembers the entered text in the temp variable (All these functions work only for entering a name through the screen)
def inp (newstr):
# make global variables of lists and empty name
global temp
global greek_gods
global slavic
# We do so that instead of an empty line, the name you entered will be
temp = newstr
# make it so that if you enter a name from the list with gods, then the character will react to it
for i in greek_gods:
if i == temp:
greek_gods = i
# make it so that if you enter a name from the list with Slavic names, then the character will react to it
for i in slavic:
if i == temp:
slavic = i
# confirmation of the name input and hiding the screen with the name input
def clickOK ():
global temp
global greek_gods
global slavic
Hide ("choise3") ()
ClickOK = renpy.curry (clickOK)
# in the label after the screen with the name input, you can put the following:
# reaction to the name "Jesus"
$ jesus = "Jesus"
# if no name is entered
if temp == "":
# default name for the girl (if there is a gender choice)
if girl:
$ temp = "Alexandra"
# default name for boyfriend (if there is gender choice)
if boy:
$ temp = "Alexander"
jump global_generete2
----- Reactions --—
# reaction if there is a default female name
if temp == "Alexandra":
"With awakening, [temp]. I see your creativity is bad - it is being treated."
# reaction if there is a male name by default
elif temp == "Alexander":
"With awakening, [temp]. I see your creativity is bad - it is being treated."
# reaction if one of the Slavic names is entered
elif temp == slavic:
"With awakening, [temp]. I hear the echoes of the glorious Mother Russia in your name."
# reaction if one of the names of the gods of Olympus is entered
elif temp == greek_gods:
"Wake up, [temp]. How's it going on Olympus?"
# reaction if the name "Jesus" is entered
elif temp == jesus:
"Wake up, [temp]. Stop ... Wait ... [temp] ?! Seriously? Are you sick? \"
# reaction if a name is entered that is not in the lists
else:
"With awakening, [temp]. You are just superintelligent once you have written your own name instead of choosing the default."