【问题标题】:TypeError: must be str, not StatementTypeError:必须是 str,而不是 Statement
【发布时间】:2020-11-11 17:42:22
【问题描述】:

我正在开发一个聊天机器人,代码是 -

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.trainers import ChatterBotCorpusTrainer
import nltk
import numpy as np
import random
import string # to process standard python strings

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

import os  
language = 'en'

remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)

GREETING_INPUTS = ("hello", "hi", "greetings", "sup", "what's up","hey","hii")
GREETING_RESPONSES = ["hi", "hey", "*nods*", "hi there", "hello", "I am glad! You are talking to me"]

def greeting(sentence): 
    for word in sentence.split():
        if word.lower() in GREETING_INPUTS:
            greetinput = random.choice(GREETING_RESPONSES)
            return greetinput


def response(user_response):
    robo_response=''
    user_response = str(user_response)
    robo_response = robo_response+return_response
    return robo_response

flag=True

my_bot = ChatBot(name='PyBot', read_only=True,logic_adapters=['chatterbot.logic.MathematicalEvaluation','chatterbot.logic.BestMatch'])
small_talk = ['hi there!',
              'hi!',
              'how do you do?',
              'how are you?',
              'i\'m cool.',
              'fine, you?',
              'always cool.',
              'i\'m ok',
              'glad to hear that.',
              'i\'m fine',
              'glad to hear that.',
              'i feel awesome',
              'excellent, glad to hear that.',
              'not so good',
              'sorry to hear that.',
              'what\'s your name?',
              'i\'m pybot. ask me a math question, please.']

math_talk_1 = ['pythagorean theorem',
           'a squared plus b squared equals c squared.']

math_talk_2 = ['law of cosines',
           'c**2 = a**2 + b**2 - 2 * a * b * cos(gamma)']


list_trainer = ListTrainer(my_bot)

for item in (small_talk, math_talk_1, math_talk_2):
    list_trainer.train(item)

corpus_trainer = ChatterBotCorpusTrainer(my_bot)
corpus_trainer.train('chatterbot.corpus.english')


openremark = "ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye!"

print("ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye!")

while(flag==True):
    user_response = input()
    user_response=user_response.lower()
    if(user_response!='bye'):
        if(user_response=='thanks' or user_response=='thank you' ):
            flag=False
            print("ROBO: You are welcome..")
        else:
            if(greeting(user_response)!=None):
                print("ROBO: "+greeting(user_response))
            else:
                print("ROBO: ",end="")
                print("func call user_response")
                print(user_response)
                print("end")
                user_response = str(user_response)
                print(response(user_response))
    else:
        flag=False

        offremark2 = "Bye! take care"
        print("ROBO: Bye! take care..")

在运行命令时它不能正常工作 -

ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye!
hi
ROBO: hi there
gaurav
ROBO: func call user_response
gaurav
end
user_response
gaurav
type
<class 'str'>
end
dummy check
you are busy
dummy end
user_response
gaurav
Traceback (most recent call last):
  File "chatbot-new-1.py", line 118, in <module>
    print(response(user_response))
  File "chatbot-new-1.py", line 46, in response
    robo_response = robo_response+return_response
TypeError: must be str, not Statement

问候语和 BYE 消息工作正常,但是当它必须与 CHATTERBOT 和 CHATTERBOT_CORPUS 一起使用时,它会出错。

Traceback (most recent call last):
  File "chatbot-new-1.py", line 118, in <module>
    print(response(user_response))
  File "chatbot-new-1.py", line 46, in response
    robo_response = robo_response+return_response
TypeError: must be str, not Statement

TypeError: 必须是 str,而不是 Statement

我什至尝试先创建 user_response 字符串并使用 for 循环,以便它适用于每个单数元素,但它会显示相同的消息。我无法解决错误,也没有得到相同或类似问题的任何答案。

【问题讨论】:

    标签: python python-3.x machine-learning chatbot chatterbot


    【解决方案1】:

    return_response 是什么?它看起来不像是定义的。

    也许替换这个:

    robo_response = robo_response+return_response
    

    通过这个:

    robo_response = robo_response+user_response
    

    【讨论】:

    • 如果你看聊天机器人程序 user_response 是用户在 CMD 上输入的响应,return_response 是 CHATBOT 应该在 CMD 上返回的响应。如果我按照您的建议更改 robo_response 的值,它将失去意义。
    【解决方案2】:

    您可以使用此代码制作最佳机器人,首先从 GitHub

    的 chatterbot 尸体中安装 English 文件夹
    from chatterbot.trainers import ListTrainer
    from chatterbot import ChatBot
    bot = ChatBot('Text')
    conv = open('english/ai.yml','r').readlines()
    
    bot.set_trainer(ListTrainer)
    bot.train(conv)
    
    while True:
        request = input('you:')
        response = bot.get_response(request)
        print('Bot: ', response())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多