chatterbot attributeError:'str'对象没有属性'text'

时间:2019-03-14 20:39:58

标签: chatterbot

 from chatterbot import ChatBot
 from chatterbot.trainers import ChatterBotCorpusTrainer
 from chatterbot.trainers import ListTrainer
 from chatterbot.comparisons import LevenshteinDistance
 from chatterbot import utils
 from chatterbot.preprocessors import clean_whitespace
 bot2 = ChatBot('Eagle',
           logic_adapters=['chatterbot.logic.BestMatch'
                           ],
           preprocessors=[
                   'chatterbot.preprocessors.clean_whitespace'
                         ],
           read_only=True
           )

while True:

message = input ('You: ')

val = utils.get_response_time(bot2,message)
print('Response time is ',val) # message response time

reply = bot2.get_response(message) 

dist = LevenshteinDistance().compare(message,reply)
print (dist,'   sdd')
print('Eagle: ',reply)

if message.strip()=='bye':
    print('Eagle: bye')
    break;

运行此代码时,出现此错误。我已经尝试过了,但是现在我需要一些帮助。

  [Traceback (most recent call last):
  File "two.py", line 51, in <module>
  dist = LevenshteinDistance().compare(message,reply)
  File "/home/anjarul/anaconda3/lib/python3.6/site-packages/chatterbot/comparisons.py", line 44, in compare
    if not statement.text or not other_statement.text:
AttributeError: 'str' object has no attribute 'text'][1]

1 个答案:

答案 0 :(得分:0)

您需要在致电Statement之前将消息转换为LevenshteinDistance.compare

语句是chatterbot程序包中的对象,代表对话的一部分。他们有属性“文本”

这对我有用:

from chatterbot import ChatBot
from chatterbot.conversation import Statement
from chatterbot.comparisons import LevenshteinDistance

bot2 = ChatBot('Eagle')

message = Statement(input ('You: '))
reply = bot2.get_response(message) 
dist = LevenshteinDistance().compare(message,reply)
print (dist, '   sdd')
print('Eagle: ', reply)