我正在开发一个聊机器人,代码如下 –
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