如何在用户输入中识别名称(从文本文件中)然后打印名称 [英] How to recognize name (from text file) in user input and then print name

查看:112
本文介绍了如何在用户输入中识别名称(从文本文件中)然后打印名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理想目标是让聊天机器人认识到您在谈论自己的同胞之一。因此,当您提到您的兄弟姐妹(通过名称或关键字:我的兄弟姐妹)时,聊天机器人将从其文本文件中提供的数据中知道他们是谁。我已经弄清楚了关键字部分,但是当我用名称提及它们时(例如:我受不了詹姆斯)。聊天机器人不会打印我想要的内容,并且当用户告诉聊天机器人其兄弟姐妹的名字时,它最终会同时打印(哦,所以你哥哥的名字是)和(我会确保记住那个,那么 + brother_status ['name'] +?怎么办。我该怎么做才能解决此问题?

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()


推荐答案

更大的问题是管理程序的状态。您正在测试所有 if ,并且它们为true的将一直执行,这不是您想要的。

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".

关于python 3的侧面注释:

Side notes about python 3 :

Python 2在2020年将变得过时,不应再使用。系统将不附带它们,并且人们应该使用python 3,并且对python 2的支持也将停止。
这并不是说您不应该继续使用python 2作为学习工具,而是应该考虑学习python 3,这样会更轻松地获得更多帮助。此外,还有一些python 2尚不具备的出色功能,您也不想错过它了^^

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^^

这篇关于如何在用户输入中识别名称(从文本文件中)然后打印名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆