问题
My ideal goal is for the chat bot to recognize that you are talking about one of your siblings. So, when you mention your brother or sister (either by name or the keywords: my brother/sister) the chat bot will already know who they are from the data given in their text file. I have already figured out the keyword part, but when I mention them by name (For example: I can't stand James). The chat bot doesn't print what I want it to and when the user tells the chat bot their sibling's name it ends up printing both ("Oh, so your brother's name is") and ("I'll make sure to remember that, so what about " + brother_status['name'] + "?"). What can I do to fix this problem?
I have already tried this, but this doesn't seem to work either:
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])
Also here is my original code (In case you want to make any other changes):
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()
回答1:
Your bigger problem is managing the state of your program.
Everyloop; you are testing all of your if
, and them being true will get executed all the time, which is not what you want.
I would suggest to not rush steps too quickly, for instance I don't think if not first:
does what you expect.
One way to help organise and manage that state is to use functions. Use plenty of them!
Then I would suggest going piece by piece : you need to figure out under what conditions you want each question/answer to appear in your code. If you're asking about a brother you don't know, then probably the code that talks about an unknown brother shouldn't be in the place. Or should have condition to guard the code from executing.
You'll probably get to a point where you'll have conditions all over the place, when that happens (and not before, or for curiosity) you should check out "state machines".
Side notes about python 3 :
Python 2 is becoming obsolete in 2020 and should not be used anymore. Systems will not ship with them, and people are expected to use python 3, and support for python 2 will stop. This is not to say you shouldn't continue using python 2 as a learning tool, but you should think about learning python 3, you'll get more help more easily. Also there's a few cool features that python 2 doesn't have and you wouldn't want to miss out on that^^
回答2:
I have updated your code with nltk pos tags to extract out the names from text file. Try it:
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()
来源:https://stackoverflow.com/questions/59537751/how-to-recognize-name-from-text-file-in-user-input-and-then-print-name