如何在用户输入中识别名称(来自文本文件)然后打印名称
How to recognize name (from text file) in user input and then print name
我的理想目标是让聊天机器人识别出您正在谈论您的一个兄弟姐妹。因此,当您提到您的兄弟或姐妹时(通过名字或关键字:my brother/sister),聊天机器人已经根据文本文件中提供的数据知道他们是谁。我已经弄清楚了关键字部分,但是当我提到他们的名字时(例如:I can't stand James)。聊天机器人不打印我想要的内容,当用户告诉聊天机器人他们兄弟姐妹的名字时,它最终打印了 ("Oh, so your brother's name is") 和 ("I'll make sure to remember that, so what about " + brother_status['name'] + "?")。我该怎么做才能解决这个问题?
我已经试过了,但这似乎也不起作用:
import string
user_input = raw_input("Please enter your sisters name: ").translate(string.maketrans("",""), string.punctuation)
with open('file.txt') as sibling_database:
if len(user_input.split()) >= 2:
for line in sibling_database:
for word in line.split(':'):
for words in user_input.split():
if words in word:
print("Oh, so your brother's name is " + line.split(':')[1])
这也是我的原始代码(如果您想进行任何其他更改):
import string
brother_status = dict([
('name', ''),
('nickname', ''),
('current age', ''),
('relationship', '')])
brother_keywords = ["Brother", "brother"]
sister_keywords = ["Sister", "sister"]
def main():
while True:
user_input = raw_input("What type of sibling do you have: ").translate(string.maketrans("",""), string.punctuation)
for keyword in brother_keywords:
if keyword in user_input:
with open('file.txt', 'r') as sibling_database:
for brother_data in sibling_database:
if brother_data.startswith("Brother's Name"):
print (brother_data.split(':')[1])
return
break
if user_input.split():
with open('file.txt') as sibling_database:
for line in sibling_database:
for word in user_input.split():
if word in line:
print("Oh, so your brother's name is " + line.split(':')[1] * 1)
return
break
if user_input.split():
for keyword in brother_keywords:
if keyword in user_input:
if user_input not in brother_status:
with open ('file.txt') as sibling_database:
first = sibling_database.read(1)
if not first:
print ("You never mentioned a brother. What's his name?")
user_input = raw_input("What's his name: ")
brother_status['name'] = (user_input)
with open('file.txt', 'w') as sibling_database:
sibling_database.write("Brother's Name:" + brother_status['name'] * 1 + '\n')
print ("I'll make sure to remember that, so what about " + brother_status['name'] + "?")
continue
if __name__ == "__main__":
main()
我已经使用 nltk pos 标签更新了您的代码,以从文本文件中提取名称。试一试:
import string
import nltk
nltk.download('maxent_treebank_pos_tagger')
brother_status = dict([
('name', ''),
('nickname', ''),
('current age', ''),
('relationship', '')])
brother_keywords = ["Brother", "brother"]
sister_keywords = ["Sister", "sister"]
def main():
while True:
user_input = raw_input("What type of sibling do you have: ").translate(string.maketrans("",""), string.punctuation)
for keyword in brother_keywords:
if keyword in user_input:
with open('file.txt', 'r') as sibling_database:
data = sibling_database.readline()
data = nltk.word_tokenize(data)
tagged_data = nltk.pos_tag(data)
for name, pos in tagged_data:
if pos == "NNP":
print(name)
return
break
if user_input.split():
with open('file.txt') as sibling_database:
for line in sibling_database:
for word in user_input.split():
if word in line:
print("Oh, so your brother's name is " + line.split(':')[1] * 1)
return
break
if user_input.split():
for keyword in brother_keywords:
if keyword in user_input:
if user_input not in brother_status:
with open ('file.txt') as sibling_database:
first = sibling_database.read(1)
if not first:
print ("You never mentioned a brother. What's his name?")
user_input = raw_input("What's his name: ")
brother_status['name'] = (user_input)
with open('file.txt', 'w') as sibling_database:
sibling_database.write("Brother's Name:" + brother_status['name'] * 1 + '\n')
print ("I'll make sure to remember that, so what about " + brother_status['name'] + "?")
continue
if __name__ == "__main__":
main()
您更大的问题是管理程序的状态。
每个循环;您正在测试所有 if
,如果它们为真,将一直执行,这不是您想要的。
我建议不要操之过急,例如我认为 if not first:
没有达到您的预期。
帮助组织和管理该状态的一种方法是使用函数。大量使用它们!
然后我建议逐条进行:您需要弄清楚您希望每个 question/answer 在什么条件下出现在您的代码中。如果你问的是一个你不认识的兄弟,那么谈论一个不知名的兄弟的代码可能不应该出现在这个地方。或者应该有条件来保护代码不被执行。
你可能会遇到到处都有条件的情况,当这种情况发生时(而不是之前,或者出于好奇)你应该查看 "state machines"。
关于 python 3 的附注:
Python 2 将在 2020 年过时,不应再使用。系统不会附带它们,人们预计会使用 python 3,对 python 2 的支持将停止。
这并不是说您不应该继续使用 python 2 作为学习工具,而是您应该考虑学习 python 3,您会更容易获得更多帮助。还有一些 python 2 没有的很酷的功能,你不会想错过的^^
我的理想目标是让聊天机器人识别出您正在谈论您的一个兄弟姐妹。因此,当您提到您的兄弟或姐妹时(通过名字或关键字:my brother/sister),聊天机器人已经根据文本文件中提供的数据知道他们是谁。我已经弄清楚了关键字部分,但是当我提到他们的名字时(例如:I can't stand James)。聊天机器人不打印我想要的内容,当用户告诉聊天机器人他们兄弟姐妹的名字时,它最终打印了 ("Oh, so your brother's name is") 和 ("I'll make sure to remember that, so what about " + brother_status['name'] + "?")。我该怎么做才能解决这个问题?
我已经试过了,但这似乎也不起作用:
import string
user_input = raw_input("Please enter your sisters name: ").translate(string.maketrans("",""), string.punctuation)
with open('file.txt') as sibling_database:
if len(user_input.split()) >= 2:
for line in sibling_database:
for word in line.split(':'):
for words in user_input.split():
if words in word:
print("Oh, so your brother's name is " + line.split(':')[1])
这也是我的原始代码(如果您想进行任何其他更改):
import string
brother_status = dict([
('name', ''),
('nickname', ''),
('current age', ''),
('relationship', '')])
brother_keywords = ["Brother", "brother"]
sister_keywords = ["Sister", "sister"]
def main():
while True:
user_input = raw_input("What type of sibling do you have: ").translate(string.maketrans("",""), string.punctuation)
for keyword in brother_keywords:
if keyword in user_input:
with open('file.txt', 'r') as sibling_database:
for brother_data in sibling_database:
if brother_data.startswith("Brother's Name"):
print (brother_data.split(':')[1])
return
break
if user_input.split():
with open('file.txt') as sibling_database:
for line in sibling_database:
for word in user_input.split():
if word in line:
print("Oh, so your brother's name is " + line.split(':')[1] * 1)
return
break
if user_input.split():
for keyword in brother_keywords:
if keyword in user_input:
if user_input not in brother_status:
with open ('file.txt') as sibling_database:
first = sibling_database.read(1)
if not first:
print ("You never mentioned a brother. What's his name?")
user_input = raw_input("What's his name: ")
brother_status['name'] = (user_input)
with open('file.txt', 'w') as sibling_database:
sibling_database.write("Brother's Name:" + brother_status['name'] * 1 + '\n')
print ("I'll make sure to remember that, so what about " + brother_status['name'] + "?")
continue
if __name__ == "__main__":
main()
我已经使用 nltk pos 标签更新了您的代码,以从文本文件中提取名称。试一试:
import string
import nltk
nltk.download('maxent_treebank_pos_tagger')
brother_status = dict([
('name', ''),
('nickname', ''),
('current age', ''),
('relationship', '')])
brother_keywords = ["Brother", "brother"]
sister_keywords = ["Sister", "sister"]
def main():
while True:
user_input = raw_input("What type of sibling do you have: ").translate(string.maketrans("",""), string.punctuation)
for keyword in brother_keywords:
if keyword in user_input:
with open('file.txt', 'r') as sibling_database:
data = sibling_database.readline()
data = nltk.word_tokenize(data)
tagged_data = nltk.pos_tag(data)
for name, pos in tagged_data:
if pos == "NNP":
print(name)
return
break
if user_input.split():
with open('file.txt') as sibling_database:
for line in sibling_database:
for word in user_input.split():
if word in line:
print("Oh, so your brother's name is " + line.split(':')[1] * 1)
return
break
if user_input.split():
for keyword in brother_keywords:
if keyword in user_input:
if user_input not in brother_status:
with open ('file.txt') as sibling_database:
first = sibling_database.read(1)
if not first:
print ("You never mentioned a brother. What's his name?")
user_input = raw_input("What's his name: ")
brother_status['name'] = (user_input)
with open('file.txt', 'w') as sibling_database:
sibling_database.write("Brother's Name:" + brother_status['name'] * 1 + '\n')
print ("I'll make sure to remember that, so what about " + brother_status['name'] + "?")
continue
if __name__ == "__main__":
main()
您更大的问题是管理程序的状态。
每个循环;您正在测试所有 if
,如果它们为真,将一直执行,这不是您想要的。
我建议不要操之过急,例如我认为 if not first:
没有达到您的预期。
帮助组织和管理该状态的一种方法是使用函数。大量使用它们!
然后我建议逐条进行:您需要弄清楚您希望每个 question/answer 在什么条件下出现在您的代码中。如果你问的是一个你不认识的兄弟,那么谈论一个不知名的兄弟的代码可能不应该出现在这个地方。或者应该有条件来保护代码不被执行。
你可能会遇到到处都有条件的情况,当这种情况发生时(而不是之前,或者出于好奇)你应该查看 "state machines"。
关于 python 3 的附注:
Python 2 将在 2020 年过时,不应再使用。系统不会附带它们,人们预计会使用 python 3,对 python 2 的支持将停止。 这并不是说您不应该继续使用 python 2 作为学习工具,而是您应该考虑学习 python 3,您会更容易获得更多帮助。还有一些 python 2 没有的很酷的功能,你不会想错过的^^