TypeError: 必须是字符串类型,不能是Statement

我正在开发一个聊机器人,代码如下 –

from chatterbot import ChatBotfrom chatterbot.trainers import ListTrainerfrom chatterbot.trainers import ChatterBotCorpusTrainerimport nltkimport numpy as npimport randomimport string # to process standard python stringsfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.metrics.pairwise import cosine_similarityimport 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 greetinputdef response(user_response):    robo_response=''    user_response = str(user_response)    robo_response = robo_response+return_response    return robo_responseflag=Truemy_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!hiROBO: hi theregauravROBO: func call user_responsegauravenduser_responsegauravtype<class 'str'>enddummy checkyou are busydummy enduser_responsegauravTraceback (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_responseTypeError: 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_responseTypeError: must be str, not Statement

TypeError: 必须是字符串类型,不能是Statement

我甚至尝试先将user_response转换为字符串类型,并使用for循环以便它能对每个单一元素工作,但结果还是同样的错误。我无法解决这个问题,也没有找到相同或相似问题的答案。


回答:

return_response是什么?看起来它没有被定义。

也许可以将这行代码:

robo_response = robo_response+return_response

替换为这行:

robo_response = robo_response+user_response

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注