Setting up a profanity filter for names?

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
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Setting up a profanity filter for names?

#1 Post by wyverngem »

I'm trying to set up a profanity filter for the user inputted character's name. I've found a profanity filter class: https://github.com/jared-mess/profanity-filter

It keeps giving me the error that 'bad_word.txt' doesn't exist.

Code: Select all

#Profanity Filter
init python:
    import os
    dir_path = os.path.dirname(os.path.realpath(__file__))
    my_var = dir_path 
    
    class Filter(object):
        ##Replaces a bad word in a string with something more PG friendly
        ##f = Filter(name, 'unicorn')
        def __init__(self, original_string, clean_word='****'):
            
            bad_words_file = open(renpy.loader.transfn("filter/bad_words.txt"),"r")
            
            self.bad_words = set(line.strip('\n') for line in open('bad_words.txt'))
            self.original_string = original_string
            self.clean_word = clean_word
            
        def clean(self):
            exp = '(%s)' %'|'.join(self.bad_words)
            r = re.compile(exp, re.IGNORECASE)
            return r.sub(self.clean_word, self.original_string)
Help please?

definitelynotmclovin
Newbie
Posts: 2
Joined: Tue Sep 13, 2016 11:01 pm
Contact:

Re: Setting up a profanity filter for names?

#2 Post by definitelynotmclovin »

Code: Select all

#Profanity Filter
import re
init python:
    class Filter(object):
        def __init__(self, original_string, clean_word='****'):            
            bad_words_file = open(renpy.loader.transfn("resources/bad_words.txt"),"r")      
            self.bad_words = set(line.strip('\n') for line in bad_words_file)
            self.original_string = original_string
            self.clean_word = clean_word
           
        def clean(self):
            exp = '(%s)' %'|'.join(self.bad_words)
            r = re.compile(exp, re.IGNORECASE)
            return r.sub(self.clean_word, self.original_string)
Put bad_words.txt in resources.
You can reference a full path, but when installed to a new computer it won't know where to put the file, since the full path may change.
So renpy handles this for you using renpy.loader.transfn()

edit: You should really use 'with open' to automatically close your file socket or close your file socket manually.

http://stackoverflow.com/questions/1832 ... ile-object

Code: Select all

with open(renpy.loader.transfn("resources/bad_words.txt"),"r") as bad_words_file:
                self.bad_words = set(line.strip('\n') for line in bad_words_file)
or

Code: Select all

#Profanity Filter
import re
init python:
    class Filter(object):
        def __init__(self, original_string, clean_word='****'):            
            bad_words_file = open(renpy.loader.transfn("resources/bad_words.txt"),"r")      
            self.bad_words = set(line.strip('\n') for line in bad_words_file)
            self.original_string = original_string
            self.clean_word = clean_word
            bad_words_file.close()
           
        def clean(self):
            exp = '(%s)' %'|'.join(self.bad_words)
            r = re.compile(exp, re.IGNORECASE)
            return r.sub(self.clean_word, self.original_string)

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Setting up a profanity filter for names?

#3 Post by PyTom »

You don't want to use functions in the renpy.loader module. That's an implementation detail that may change between Ren'Py versions.

The documented API for opening a data file is renpy.file.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Setting up a profanity filter for names?

#4 Post by wyverngem »

I'm sorry to bother everyone, but I'm having trouble understanding what's going wrong with this filter. It hasn't worked since the update and I could never get renpy.file to actually work. I am trying to figure out how to make a filter that takes a player's name and runs it against a list of bad_words. I have the txt file in the game directory. I changed the code to bad_words_file = renpy.file("bad_words.txt") from the renpy.loader, but I think I must have missed a step.

When it's like this it doesn't return an error and doesn't seem to check that the name is within the file. I'm not sure what I'm doing wrong.

Code: Select all

init python:
    import re
    class Filter(object):
        def __init__(self, original_string, clean_word='****'):
            bad_words_file = renpy.file("bad_words.txt")
            self.bad_words = set(line.strip('\---n') for line in bad_words_file)
            self.original_string = original_string
            self.clean_word = clean_word

        def clean(self):
            exp = '(%s)' %'|'.join(self.bad_words)
            r = re.compile(exp, re.IGNORECASE)
            return r.sub(self.clean_word, self.original_string)
label nameReina:
    $ name = "Amalia"
    $ name = renpy.input("Please give her a name. Max 10 characters", allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
    $ f = Filter(name, clean_word='badword')
    $ name = f.clean()
    if "badword" in name:
        mt "You can't use that name, please enter a different name."
        jump nameReina
    $ name.title()
    $ name.rstrip()
    "Name acceptable."
    jump setup

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Setting up a profanity filter for names?

#5 Post by Remix »

You don't really need to use a class, a simple function return should do... maybe:

Code: Select all

init python:

    import re

    def filter_bad_words(input, replace_with="unicorn"):

        with renpy.file('resources/bad_words.txt') as f: ## adjust the file name and folder to suit

            bad_words = [ ln.strip() for ln in f ]

            exp = '({})'.format( "|".join( bad_words ) )

            r = re.compile(exp, re.IGNORECASE)

            return r.sub(replace_with, input)


label nameReina:

    $ name = "Amalia"
    $ name = filter_bad_words( 
        renpy.input(
            "Please give her a name. Max 10 characters", 
            allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 
            length=10).strip().title(),
        replace_with = "Fluffy Wabbit" )

    if "Fluffy Wabbit" in name:
        "We've taken the liberty of tweaking that (to [name!q])... Want to try again?"
        jump nameReina

    "[name!q]"

    return
Frameworks & Scriptlets:

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Setting up a profanity filter for names?

#6 Post by wyverngem »

Remix wrote: Tue Feb 12, 2019 11:20 amYou don't really need to use a class, a simple function return should do... maybe:

Perfect, thanks so much!

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Setting up a profanity filter for names?

#7 Post by wyverngem »

One small issue, is there a way to define built in names for this? I can't name the main character "Hope" or "Michelle" "Rochelle" without it flagging a part of it as inappropriate.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Setting up a profanity filter for names?

#8 Post by Remix »

If you just want to replace versions of the badwords including a trailing space you could amend to:

exp = '({})'.format( " |".join( bad_words ) )
along with
replace_with = "Fluffy Wabbit " )

Preceding space (or spaces before and after would be much the same)
Frameworks & Scriptlets:

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Setting up a profanity filter for names?

#9 Post by wyverngem »

Okay, so not quite, since I tried it and some filtered words got through. I was thinking more along the lines of this, but I keep getting an error.

Code: Select all

init python:
    import re
    def filter_bad_words(input, replace_with="unicorn"):
        approved_name_list = ['Hope', 'Michelle', 'Rochelle']
        if input != None:
            if input in approved_name_list:
                message = "Approved Name"
                renpy.show_screen("popup_message", message=message)
            else:
                with renpy.file('bad_words.txt') as f: ## adjust the file name and folder to suit
                    bad_words = [ ln.strip() for ln in f ]
                    exp = '({})'.format( "|".join( bad_words ) )
                    r = re.compile(exp, re.IGNORECASE)
                    return r.sub(replace_with, input)
screen popup_message(message):
    text str(message)
    timer 3 action Hide("popup)message")
    
label naming:
    $ player = filter_bad_words(
        renpy.input(
            "What is her name? (Type in a name, 10 characters max.",
            allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
            length=10).strip().title(),
        replace_with = "Fluffy Wabbit" )
    if "Fluffy Wabbit" in player:
        "Not acceptable, please give her a proper name."
        jump naming
The error is this:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 74, in script
    if "Fluffy Wabbit" in player:
  File "game/script.rpy", line 74, in <module>
    if "Fluffy Wabbit" in player:
TypeError: argument of type 'NoneType' is not iterable

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 74, in script
    if "Fluffy Wabbit" in player:
  File "G:\Creative\VN\Renpy\renpy\ast.py", line 1762, in execute
    if renpy.python.py_eval(condition):
  File "G:\Creative\VN\Renpy\renpy\python.py", line 1944, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "G:\Creative\VN\Renpy\renpy\python.py", line 1937, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 74, in <module>
    if "Fluffy Wabbit" in player:
TypeError: argument of type 'NoneType' is not iterable

Windows-7-6.1.7601-SP1
Ren'Py 7.1.3.1092
LearningPython 1.0
Mon Feb 18 13:27:05 2019

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Setting up a profanity filter for names?

#10 Post by wyverngem »

I feel silly, I forgot to return the input variable, but I ended up changing it a little so I understood the variables. Thanks Remix!

Code: Select all

init python:
    import re
    def filter_bad_words(userinput, replace_with="unicorn"):
        approved_name_list = ['Hope', 'Michelle', 'Rochelle']
        if userinput in approved_name_list:
            #message = "Name: %s"%(userinput)
            #renpy.show_screen("popup_message", message=message)
            return userinput
        else:
            with renpy.file('bad_words.txt') as f: ## adjust the file name and folder to suit
                bad_words = [ ln.strip() for ln in f ]
                exp = '({})'.format( "|".join( bad_words ) )
                r = re.compile(exp, re.IGNORECASE)
                return r.sub(replace_with, userinput)

Post Reply

Who is online

Users browsing this forum: Google [Bot]