Skip to content

Instantly share code, notes, and snippets.

@NoraCodes
Last active February 24, 2019 03:18
Show Gist options
  • Save NoraCodes/e17468d747c7053c18e9964e3f91cece to your computer and use it in GitHub Desktop.
Save NoraCodes/e17468d747c7053c18e9964e3f91cece to your computer and use it in GitHub Desktop.
An example of proper handling of pronoun replacement for, e.g., text games.
################################################################
# pronouns.py - Leonora Tindall
#
# An example of proper handling of pronoun replacement in
# Python, including a selection menu. This handles only
# singular pronouns, providing a simple .format() based
# interface for replacing pronouns in prose.
# This script is limited to English, but a similar technique
# should work for most languages using a similar grammar.
################################################################
class UnknownBuiltinPronounException(Exception):
pass
class MissingRequiredPronounException(Exception):
pass
class Pronouns:
built_in_sets = {"he": ("him", "his", "his", "himself"),
"she": ("her", "her", "hers", "herself"),
"they": ("them", "their", "theirs", "themself")}
def __init__(self, subjective, objective=None, dep_posessive=None,
indep_posessive=None, reflexive=None):
if objective is None:
if subjective in self.built_in_sets.keys():
self.subjective = subjective,
self.objective, self.dep_posessive, self.indep_posessive, self.reflexive = self.built_in_sets[subjective]
return
raise UnknownBuiltinPronounException
elif dep_posessive is None:
raise MissingRequiredPronounException("A dependent posessive pronoun "
"is required.")
# The required pronouns (dependent posessive, objective, and subjective)
# are fulfilled, and the object hasn't been constructed based on a builtin set.
self.subjective = subjective
self.objective = objective
self.dep_posessive = dep_posessive
if reflexive is None:
self.reflexive = "{}self".format(self.objective)
else:
self.reflexive = reflexive
if indep_posessive is None:
self.indep_posessive = "{}s".format(dep_posessive)
else:
self.indep_posessive = indep_posessive
# This method asks the user to enter custom pronouns. If given three, it assumed they are
# subjective, objective, and dependent posessive, and the function will guess the
# independent posessive and reflexive forms
def get_custom_pronouns():
print("Enter pronouns, space separated.")
print("Examples: they them their; she her her hers herself: ")
pronouns = input("Enter: ").split()
if len(pronouns) == 3:
return Pronouns(pronouns[0], pronouns[1], pronouns[2])
# With five given pronouns, the function just fills them in
elif len(pronouns) == 5:
return Pronouns(pronouns[0], pronouns[1], pronouns[2], pronouns[3], pronouns[4])
# With four, more than five, or fewer than free, this function needs to try again.
else:
print("Please enter either sub/obj/ref prononous or "
"sub/obj/dep pos/indep pos/ref pronouns.")
return get_custom_pronouns()
def fill_pronouns(text: str, pro: Pronouns):
return text.format(sub=pro.subjective, obj=pro.objective,
dep_pos=pro.dep_posessive, ind_pos=pro.indep_posessive,ref=pro.reflexive)
# This function gives the user a menu of common pronoun sets as well as an option to invoke
# get_custom_pronouns() to enter a custom set.
def get_pronouns_menu():
print("Select your preferred pronouns.")
print("[1] They/them pronouns\n[2] He/him pronouns\n"
"[3] She/her pronouns\n[c] Custom pronouns...")
selection = input("Selection: ")
results = {
'1': Pronouns("they"),
'2': Pronouns("he"),
'3': Pronouns("she"),
}
if selection in results.keys():
return results[selection]
elif selection == 'c':
return get_custom_pronouns()
else:
print("Please enter a valid selection.")
return get_pronouns_menu()
pronouns = get_pronouns_menu()
print(fill_pronouns(
"The player picks {ref} up off the ground. After adjusting {dep_pos} "
"cap, {sub} looks about and grabs {dep_pos} dropped book, "
"resolving to take it with {obj}.", pronouns))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment