如何在用户输入中识别名称(从文本文件中识别,然后打印名称)

问题描述 投票:0回答:1

我的理想目标是让聊天机器人识别您正在谈论的是同胞之一。因此,当您提到您的兄弟姐妹(通过名称或关键字:我的兄弟姐妹)时,聊天机器人将从其文本文件中提供的数据中知道他们是谁。我已经弄清楚了关键字部分,但是当我用名称提及它们时(例如:我受不了詹姆斯)。聊天机器人不会打印我想要的内容,并且当用户告诉聊天机器人其兄弟姐妹的名字时,它最终会同时打印(“哦,所以你兄弟的名字是”)和(“我会确保记住,那么“ + 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()
python text-files python-2.x chatbot
1个回答
0
投票

我已经使用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()

0
投票

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

[我建议不要急于急忙,例如,我认为if not first:不能达到您的期望。

帮助组织和管理该状态的一种方法是使用功能。使用其中的大量!

然后,我建议您逐步进行:您需要弄清楚您希望每个问题/答案出现在代码中的条件。如果您要问一个您不认识的兄弟,那么谈论陌生兄弟的代码可能就不应该放在这里。或应具有防止代码执行的条件。

[您可能会到处都有条件,当这种情况发生时(而不是之前或出于好奇,您应该签出“状态机”。]]

关于python 3的附带说明:

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

© www.soinside.com 2019 - 2024. All rights reserved.