Python TypeError:必须是str,而不是语句

Python TypeError:必须是str,而不是语句,python,python-3.x,machine-learning,chatbot,chatterbot,Python,Python 3.x,Machine Learning,Chatbot,Chatterbot,我正在使用聊天机器人,代码是- 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 s

我正在使用聊天机器人,代码是-

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
机器人:我叫机器人。我会回答你关于聊天机器人的问题。如果要退出,请键入“再见”!
你好
机器人:你好
高瑞夫
机器人:func呼叫用户\u响应
高瑞夫
结束
用户响应
高瑞夫
类型
结束
假支票
你很忙
假端
用户响应
高瑞夫
回溯(最近一次呼叫最后一次):
文件“chatbot-new-1.py”,第118行,在
打印(响应(用户响应))
文件“chatbot-new-1.py”,第46行,作为响应
机器人响应=机器人响应+返回响应
TypeError:必须是str,而不是语句
它可以很好地用于问候和拜拜消息,但当它必须使用CHATTERBOT和CHATTERBOT_语料库时,它给出了一个错误

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
回溯(最近一次呼叫最后一次):
文件“chatbot-new-1.py”,第118行,在
打印(响应(用户响应))
文件“chatbot-new-1.py”,第46行,作为响应
机器人响应=机器人响应+返回响应
TypeError:必须是str,而不是语句
TypeError:必须是str,而不是语句


我甚至尝试过先生成user_响应字符串,然后再生成for循环,这样它可以对每个单一元素起作用,但会显示相同的消息。我无法解决错误,并且没有得到相同或类似问题的任何答案。

什么是
返回\u响应
?它看起来没有定义

也许可以替换这个:

robo_response = robo_response+return_response
据此:

robo_response = robo_response+user_response

什么是
返回\u响应
?它看起来没有定义

也许可以替换这个:

robo_response = robo_response+return_response
据此:

robo_response = robo_response+user_response

您可以使用此代码制作一个最佳的bot,首先安装English文件夹,该文件夹来自GitHub

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

您可以使用此代码制作一个最佳的bot,首先安装English文件夹,该文件夹来自GitHub

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

如果您查看聊天机器人程序,user_u响应是用户在CMD上输入的响应,return_u响应是聊天机器人应该在CMD上返回的响应。如果我按照您的建议更改robo_response的值,它将失去意义。如果您查看聊天机器人程序,user_response是用户在CMD上输入的响应,return_response是聊天机器人在CMD上应该返回的响应。如果我像你建议的那样改变机器人响应的值,它将失去它的意义。