我试图制作一个Mad Libs游戏,用户在句子中输入多次出现的单词。目前,我发现用户需要多次输入这些相同的单词。
sentence_a = """GENRE_OF_MUSIC was created by PERSON in Middle Earth.
We only know because NUMBER years ago,
MUSICIAN went on an epic quest with only a OBJECT for
company. MUSICIAN had to steal GENRE_OF_MUSIC from PERSON
and did this by playing a game of SPORT as a distraction."""
#Words to be replaced
parts_of_speech = ["MUSICIAN", "GENRE_OF_MUSIC", "NUMBER",
"OBJECT", "PAST_TENSE_VERB", "PERSON", "SPORT"]
# Checks if a word in parts_of_speech is a substring of the word passed in.
def word_in_pos(word, parts_of_speech):
for pos in parts_of_speech:
if pos in word:
return pos
return None
def play_game(ml_string, parts_of_speech):
replaced = []
ml_string = ml_string.split()
for word in ml_string:
replacement = word_in_pos(word, parts_of_speech)
if replacement != None:
user_input = raw_input("Type in a: " + replacement + " ")
word = word.replace(replacement, user_input)
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
return replaced
print play_game(sentence_a, parts_of_speech)
当您运行代码时,我希望用户只输入一次GENRE_OF_MUSIC,并且Mad Libs句子仅在每次出现时使用该条目。
答案 0 :(得分:1)
你走在正确的轨道上。你不做的是跟踪你已经改变为什么价值的单词。
我建议在Python中查看Dictionaries并使用它们。它们以键值对方式工作,非常适合您的需求。
对于您想要的工作版本,请检查以下编辑内容:
sentence_a = """GENRE_OF_MUSIC was created by PERSON in Middle Earth.
We only know because NUMBER years ago,
MUSICIAN went on an epic quest with only a OBJECT for
company. MUSICIAN had to steal GENRE_OF_MUSIC from PERSON
and did this by playing a game of SPORT as a distraction."""
#Words to be replaced
parts_of_speech = ["MUSICIAN", "GENRE_OF_MUSIC", "NUMBER",
"OBJECT", "PAST_TENSE_VERB", "PERSON", "SPORT"]
# DICTIONARY to hold replaced key-value pairs
replace_dictionary = {}
# Checks if a word in parts_of_speech is a substring of the word passed in.
def word_in_pos(word, parts_of_speech):
for pos in parts_of_speech:
if pos in word:
return pos
return None
def play_game(ml_string, parts_of_speech):
replaced = []
ml_string = ml_string.split()
for word in ml_string:
replacement = word_in_pos(word, parts_of_speech)
# See if the word is in our dictionary. If so, use that value
if replacement in replace_dictionary.keys():
word = word.replace(replacement, replace_dictionary[replacement])
replaced.append(word)
elif replacement != None and replacement not in replace_dictionary.keys():
user_input = input("Type in a: " + replacement + " ")
# This replacement is new, so we add it to Dictionary
replace_dictionary[replacement] = user_input
word = word.replace(replacement, user_input)
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
return replaced
print (play_game(sentence_a, parts_of_speech))
答案 1 :(得分:1)
我建议使用字符串格式代替搜索和替换。
以下是它的工作原理:
import string
from textwrap import dedent
FMT = string.Formatter()
def get_field_names(format_string):
"""
Return a list of unique field names from the format string
"""
return sorted(set(p[1] for p in FMT.parse(format_string) if p[1] is not None))
sentence_a = dedent("""
{GENRE_OF_MUSIC} was created by {PERSON} in Middle Earth.
We only know because {NUMBER} years ago, {MUSICIAN} went
on an epic quest with only a {OBJECT} for company.
{MUSICIAN} had to steal {GENRE_OF_MUSIC} from {PERSON}
and did this by playing a game of {SPORT} as a distraction.
""")
parts_of_speech = get_field_names(sentence_a)
replace_dict = {pos:pos for pos in parts_of_speech}
# after getting input from player
replace_dict["MUSICIAN"] = "Chuck Berry"
# show result
print(sentence_a.format(**replace_dict))
答案 2 :(得分:0)
您没有跟踪替换以处理重复的关键字。
您可以更改代码以循环显示关键字:
sentence_a = """GENRE_OF_MUSIC was created by PERSON in Middle Earth.
We only know because NUMBER years ago,
MUSICIAN went on an epic quest with only a OBJECT for
company. MUSICIAN had to steal GENRE_OF_MUSIC from PERSON
and did this by playing a game of SPORT as a distraction."""
#Words to be replaced
parts_of_speech = ["MUSICIAN", "GENRE_OF_MUSIC", "NUMBER",
"OBJECT", "PAST_TENSE_VERB", "PERSON", "SPORT"]
def play_game(ml_string, parts_of_speech):
for word in parts_of_speech:
if word in ml_string:
user_input = raw_input("Type in a: " + word + " ")
ml_string = ml_string.replace(word, user_input)
return ml_string
print play_game(sentence_a, parts_of_speech)
答案 3 :(得分:0)
您的代码太复杂了。
首先,您可以询问用户要替换的词性。您可以通过 parts_of_speech 上的循环执行此操作,并将每个用户输入存储到映射中:
parts = {}
for replacement in parts_of_speech:
user_input = raw_input("Type in a " + replacement + ": ")
parts[replacement] = user_input
然后您可以拆分句子并用用户替换替换每个单词(如果存在)。如果没有,请保留一句话:
words = [parts.get(word, word)
for word in ml_string.split()]
return " ".join(words)
你得到:
Type in a MUSICIAN: Bach
Type in a GENRE_OF_MUSIC: piano
Type in a NUMBER: 23
Type in a OBJECT: roller
Type in a PAST_TENSE_VERB: dig
Type in a PERSON: Mr president
Type in a SPORT: football
piano was created by Mr president in Middle Earth. We only know because 23 years ago, Bach went on an epic quest with only a roller for company. Bach had to steal piano from Mr president and did this by playing a game of football as a distraction.
注意: word_in_pos 功能无用。