TypeError: must be str, not Statement

TypeError: must be str, not Statement

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

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,而不是语句

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

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

也许替换这个:

robo_response = robo_response+return_response

通过这个 :

robo_response = robo_response+user_response

您可以使用此代码制作最好的机器人,首先安装来自 GitHub[= 的 chatterbot 尸体的 English 文件夹11=]

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