SOLVED - Need Help with Name Filter Coding

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
User avatar
nanopicobanana
Newbie
Posts: 7
Joined: Fri Aug 19, 2016 1:15 am
Location: Bananaville
Contact:

SOLVED - Need Help with Name Filter Coding

#1 Post by nanopicobanana »

Problem Solved!
Thank you to everyone who helped out! This code (provided you have a list of words you want filtered) checks a user's inputted name/word for any rude words that are in a .txt list. I hope this is able to help others as well in the future. ^_^

BTW, the .txt file of rude words (called rude_words.txt in the code below) should have each word on a separate line and all be in lowercase. Also, Ren'Py will look for the file in game/docs which is not a standard directory in a new Ren'Py game directory. Makes sure to create the file folder or change the path if you decide to use the code.

Code: Select all

# Before start label
init python:
    import collections
    
    def ContainsBadWord(name):
        #Get list of bad words from text file, each word on a separate line
        rude_words_file = [line.rstrip('\n') for line in open(renpy.loader.transfn('docs/rude_words.txt'), 'r')]
        
        #Check if the name contains a bad word
        for badWord in rude_words_file:
            if (badWord in name):
                return True

# After start label
    label fname:
        $ PL_fname = renpy.input("What is your first name?", default="Frisk", allow="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", length=10)
        if not PL_fname:
            $ PL_fname = "Frisk"
        
        $ rude_words_file = [line.rstrip('\n') for line in open(renpy.loader.transfn('docs/rude_words.txt'), 'r')]
        #if ( PL_fname.lower().strip() in rude_words_file ):
        if (ContainsBadWord(PL_fname.lower().strip())):
            jump frude
        else:
            jump fcont
        
    label frude:
        ed "Don't be rude."
        jump fname
        
    label fcont:
    	ed "Continue!"
Again, thanks to everyone who helped out! ^_^

Original Post:
So I'm working on an educational game for a local teacher and she wants to make sure the kids can't put in rude names (like swear words and other offensive terms). My first couple of tries resulted in Ren'Py crashes before it could finish compiling the scripts for play testing, but I've figured out a "working" version... sort of.

What I'd really like to do is be able to check an inputted name versus a master list that also uses wildcards (*rude* would also catch words like prude, rudes, etc.). I've looked into regular Python code, but most of it goes right over my head. I've included a sample of the "working" code I'm using at the moment, but I'm more than open (and hoping) for improvements and help.
Removed the old code for a bit of clean up. ;)
Thanks in advance to anyone who reads this or can give me a hand! ^_^
Last edited by nanopicobanana on Sat Dec 16, 2017 7:37 pm, edited 2 times in total.

User avatar
Katy133
Miko-Class Veteran
Posts: 704
Joined: Sat Nov 16, 2013 1:21 pm
Completed: Eight Sweets, The Heart of Tales, [redacted] Life, Must Love Jaws, A Tune at the End of the World, Three Guys That Paint, The Journey of Ignorance, Portal 2.5.
Projects: The Butler Detective
Tumblr: katy-133
Deviantart: Katy133
Soundcloud: Katy133
itch: katy133
Location: Canada
Contact:

Re: Need Help with Name Filter Coding

#2 Post by Katy133 »

I've written a tutorial about this. :)

I've also pulled out some of the code I used for one of my games, The Heart of Tales:

Code: Select all

#Choose a name.
    #Character limit: 10/Elizabeths should be the longest name possible.
    $ name = renpy.input("Her name was...", length=10)

    $ name = name.strip()
    # The .strip() instruction removes any extra spaces the player may have typed by accident.

    #  If the player can't be bothered to choose a name, then we
    #  choose a suitable one for them:
    
    if name == "" or name == "Hiro" or name == "Hero":
        $ name="Hiro"
        "Her name was [name]. And that should have been the very first clue. [name]. "
        jump after_name
        
    elif name == "Poop" or name == "Shit" or name == "Showell":
        $ name="Showell"
        "Her name was [name]. It meant \"to shovel.\" "
        jump after_name
    
    #Anti-swearing feature:
    elif name == "Fuck" or name == "Fck" or name == "Cunt" or name == "Bitch" or name == "Damn" or name == "Liliha":
        $ name="Liliha"
        "Her name was [name]. It meant \"to angrily disregard.\" "
        jump after_name
             
    else:
        "Her name was [name]. "
        jump after_name

    label after_name:
        pass

    extend "And I am her."
ImageImage

My Website, which lists my visual novels.
Become a patron on my Patreon!

User avatar
Cindigo
Regular
Posts: 26
Joined: Wed Dec 06, 2017 8:05 am
Projects: Lalia Academy
Organization: LatgolAI
Skype: jonchiks
itch: latgolai
Location: Latvia
Contact:

Re: Need Help with Name Filter Coding

#3 Post by Cindigo »

I have a very safe and effective advice for solving this particular case - ask the teacher to actually do her job :) Solves the problem with 0 lines of code.
If that can't be done then I'm sorry - either get a full list of her students names (and update it, as necessary), or keep looking at that code that flies over Your head - this is an actual real world programming issue, and it can be solved only by that, actual programming.
Image

User avatar
nanopicobanana
Newbie
Posts: 7
Joined: Fri Aug 19, 2016 1:15 am
Location: Bananaville
Contact:

Re: Need Help with Name Filter Coding

#4 Post by nanopicobanana »

Katy133 wrote: Wed Dec 13, 2017 9:17 pm I've written a tutorial about this. :)

I've also pulled out some of the code I used for one of my games, The Heart of Tales:
Thank you for your swift reply! I'm actually using something very similar to this sort of name check right now (checking for specific names) but what I'd like to do is figure out how to use wildcards to catch all iterations of a word.

For example (and please pardon my French): "fuck" only currently catches that exact word in all capitalization instances (fuCK, Fuck, FUCK, etc. are all read as fuck with my current set up) so that's not really an issue. What I'd like to be able to try and stop are things like "fucker", "fucks", etc. which slip through since they aren't exact matches. I know there is a way to check for wild cards in Python (like "fuck*" would supposedly catch all of those above) but I don't know how to implement it. Such is my dilemma. :( But thank you for your suggestion!
Cindigo wrote: Thu Dec 14, 2017 8:18 am I have a very safe and effective advice for solving this particular case - ask the teacher to actually do her job :) Solves the problem with 0 lines of code..
While I do appreciate the attempt to suggest something helpful, this isn't really... erm... that? The teacher in question is quite dear to me and I'd like to do whatever I can to assist her since high school kids can be very uncooperative if they choose to be. She works extremely hard and often pushes herself to exhaustion trying to do what's best for the kids, so I am coding this game for her pro bono since I believe it will sincerely help her.

Thank you anyway for your response and taking the time to read my post. ^_^

User avatar
Kinjo
Veteran
Posts: 219
Joined: Mon Sep 19, 2011 6:48 pm
Completed: When the Seacats Cry
Projects: Detective Butler
Organization: Goldbar Games
Tumblr: kinjo-goldbar
Deviantart: Kinjo-Goldbar
Github: GoldbarGames
Skype: Kinjo Goldbar
itch: goldbargames
Location: /seacats/
Contact:

Re: Need Help with Name Filter Coding

#5 Post by Kinjo »

Here's some Python code to simplify the whole process down, assuming you only want to go to a single label no matter the word:

Code: Select all

# Get list of bad words from a text file, each word on a separate line
$ badWordsList = [line.rstrip('\n') for line in open("badwords.txt", "r")]

# Check if the name contains a bad word
for badWord in badWordsList:
  if (badWord in PL_fname.lower().strip()):
    jump frude
Just to be clear, this code should (mostly by coincidence) flag any variations of the words in your list. As long as the root word is in the name, it'll be flagged.

There are also other issues. For example, what if you check for the word "ass" but then accidentally rule out the name "Assassin?" You could also create a list of words that are okay to use and then check against those, too. But then what if they try typing something like "AssassinFatAss?" And this code also doesn't check tricks like using "SxHxIxT" to mask the name. Or if they intentionally misspell or type the word in a different language... I'll leave that for you to handle.

User avatar
nanopicobanana
Newbie
Posts: 7
Joined: Fri Aug 19, 2016 1:15 am
Location: Bananaville
Contact:

Re: Need Help with Name Filter Coding

#6 Post by nanopicobanana »

Kinjo wrote: Thu Dec 14, 2017 7:25 pm Here's some Python code to simplify the whole process down, assuming you only want to go to a single label no matter the word:

Code: Select all

# Get list of bad words from a text file, each word on a separate line
$ badWordsList = [line.rstrip('\n') for line in open("badwords.txt", "r")]

# Check if the name contains a bad word
for badWord in badWordsList:
  if (badWord in PL_fname.lower().strip()):
    jump frude
Just to be clear, this code should (mostly by coincidence) flag any variations of the words in your list. As long as the root word is in the name, it'll be flagged.

There are also other issues. For example, what if you check for the word "ass" but then accidentally rule out the name "Assassin?" You could also create a list of words that are okay to use and then check against those, too. But then what if they try typing something like "AssassinFatAss?" And this code also doesn't check tricks like using "SxHxIxT" to mask the name. Or if they intentionally misspell or type the word in a different language... I'll leave that for you to handle.
Oh! This feels so close and with a little fiddling I can get about half of this to work. Ren'Py is coming back with an error about badword however:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/scen_start.rpy", line 40, in script
    if (badword in PL_fname.lower().strip()):
  File "game/scen_start.rpy", line 40, in <module>
    if (badword in PL_fname.lower().strip()):
NameError: name 'badword' is not defined

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

Full traceback:
  File "game/scen_start.rpy", line 40, in script
    if (badword in PL_fname.lower().strip()):
  File "*\renpy-6.99.13-sdk\renpy\ast.py", line 1681, in execute
    if renpy.python.py_eval(condition):
  File "*\renpy-6.99.13-sdk\renpy\python.py", line 1794, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "*\renpy-6.99.13-sdk\renpy\python.py", line 1788, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/scen_start.rpy", line 40, in <module>
    if (badword in PL_fname.lower().strip()):
NameError: name 'badword' is not defined
I think 'badword' in this is supposed to be any of the individual lines in the txt file, but I'm not sure how to define it as such. Your help is greatly appreciated! I feel like I've gotten exponentially closer to what I'm trying to do!

User avatar
Kinjo
Veteran
Posts: 219
Joined: Mon Sep 19, 2011 6:48 pm
Completed: When the Seacats Cry
Projects: Detective Butler
Organization: Goldbar Games
Tumblr: kinjo-goldbar
Deviantart: Kinjo-Goldbar
Github: GoldbarGames
Skype: Kinjo Goldbar
itch: goldbargames
Location: /seacats/
Contact:

Re: Need Help with Name Filter Coding

#7 Post by Kinjo »

That's just a small syntax error. Given that you said you "fiddled" a bit with the code, I'm not sure whether it was something you changed or something in the original code I gave you. Either way, it was easy enough to take our two chunks of code and combine them. Perhaps a more Ren'Py savvy person could do it a cleaner way, but I've tested it and the following code should definitely work:

Code: Select all

init python:
    import collections
    
    def ContainsBadWord(name):
        # Get list of bad words from a text file, each word on a separate line
        badWordsList = [line.rstrip('\n') for line in open("badwords.txt", "r")]

        # Check if the name contains a bad word
        for badWord in badWordsList:
          if (badWord in name):
            return True

label start:

label fname:
    $ PL_fname = renpy.input("What is your first name?", default="Frisk", allow="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", length=10)
    
    if (ContainsBadWord(PL_fname.lower().strip())):
        jump frude
        
    jump fcont
        
label frude:
        "Don't be rude."
        jump fname
        
label fcont:
    "Continue!"
If you get a "file not found" error, make sure that the text file is in the same directory as the exe, or change the path to wherever it is. :)

User avatar
nanopicobanana
Newbie
Posts: 7
Joined: Fri Aug 19, 2016 1:15 am
Location: Bananaville
Contact:

Re: Need Help with Name Filter Coding

#8 Post by nanopicobanana »

Kinjo wrote: Sat Dec 16, 2017 8:19 am That's just a small syntax error. Given that you said you "fiddled" a bit with the code, I'm not sure whether it was something you changed or something in the original code I gave you. Either way, it was easy enough to take our two chunks of code and combine them. Perhaps a more Ren'Py savvy person could do it a cleaner way, but I've tested it and the following code should definitely work:

Code: Select all

init python:
    import collections
    
    def ContainsBadWord(name):
        # Get list of bad words from a text file, each word on a separate line
        badWordsList = [line.rstrip('\n') for line in open("badwords.txt", "r")]

        # Check if the name contains a bad word
        for badWord in badWordsList:
          if (badWord in name):
            return True

label start:

label fname:
    $ PL_fname = renpy.input("What is your first name?", default="Frisk", allow="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", length=10)
    
    if (ContainsBadWord(PL_fname.lower().strip())):
        jump frude
        
    jump fcont
        
label frude:
        "Don't be rude."
        jump fname
        
label fcont:
    "Continue!"
If you get a "file not found" error, make sure that the text file is in the same directory as the exe, or change the path to wherever it is. :)
OMG! Thank you so much! This worked beautifully! I'm going to update my first post with the combined and working code. Again, thank you so much! xD

Post Reply

Who is online

Users browsing this forum: Serolol