Help Needed: Making a profanity filter

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Angelo Seraphim
Regular
Posts: 32
Joined: Tue May 21, 2019 8:00 am
Completed: Enamored Risks (NaNoReNo 2020)
Projects: 616 Charagma
Organization: GLSUoA
Deviantart: glsuoa
itch: glsuoa
Location: London, UK
Discord: Just A Concept#9599
Contact:

Help Needed: Making a profanity filter

#1 Post by Angelo Seraphim » Wed Jul 22, 2020 2:24 pm

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.
Image

User avatar
Andredron
Miko-Class Veteran
Posts: 535
Joined: Thu Dec 28, 2017 2:37 pm
Completed: Kimi ga nozomu renpy-port(demo), Albatross Koukairoku(demo)
Projects: Sisters ~Natsu no Saigo no Hi~(renpy-port)
Location: Russia
Contact:

Re: Help Needed: Making a profanity filter

#2 Post by Andredron » Thu Jul 23, 2020 1:06 am

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."
I'm writing a Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18

Help me to register in QQ International

Honest Critique

Post Reply

Who is online

Users browsing this forum: Bing [Bot]