-2
I’m developing a chatbot and I wish it would only respond if it had determined level of confidence in the answer.
# -*- codding: utf-8 -*-
import os
import telebot
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
bot = telebot.TeleBot("TEM UM CÓDIGO DO TELEGRAM AQUI")
#Futaba original
def bot_convencional(message):
chatbot = ChatBot("Futaba")
trainer = ListTrainer(chatbot)
for arquivos in os.listdir('arquivos'):
chats = open('arquivos/' + arquivos, 'r').readlines()
trainer.train(chats)
resposta = chatbot.get_response(message) #ESSE GET DENTRO DO IF DA ÚLTIMA FUNÇÃO
resposta = str(resposta)
mensagem = open("arquivos/teste", "w")
mensagem.write(resposta)
mensagem.close()
#Recebe e envia uma resposta inicial
@bot.message_handler(commands = ["help", "start"])
def enviar_mensagem(message):
bot.reply_to(message, "Hey")
#Recebe qualquer outra mensagem
@bot.message_handler(func=lambda message:True)
def mensagem(message):
bot_convencional(message.text)
resposta = open("arquivos/teste", "r")
resposta = resposta.read()
if float(resposta.confidence) > 0.5:
bot.reply_to(message, resposta)
else:
bot.reply_to(message, "Como você está se sentindo?")
bot.polling()
The problem is that when I try to use the Confidence gives the error
if float(response.Confidence) > 0.5: Attributeerror: str Object has no attribute 'Confidence'
I’ve used Confidence on another chatbot and I don’t understand why you’re giving this problem this time. Can someone help me?
I wanted to put the bot_conventional function get.Response inside the message if.
Because
resposta
will be the contents of the archivearquivo/teste
and that will always be a string. In Python the string does not have theconfidence
how you’re trying to access it. If the content of the file is a JSON or any other structured format you first need to evaluate it before using it, with the libraryjson
, for example.– Woss
Ah, my Facebook is trying to get the last user statement and not the corpus inside the files folder. I’ll put the whole code in the question: Is there any way to tell me how I would put the get.Answer in the if of the Confidence?
– Mariana Bayonetta