Attributeerror: str Object has no attribute 'Confidence'

Asked

Viewed 716 times

-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.

  • 1

    Because resposta will be the contents of the archive arquivo/teste and that will always be a string. In Python the string does not have the confidence 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 library json, for example.

  • 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?

2 answers

2

The problem is that you store a Python object in the form of string in the file, later read it also as string and use it as if it were the same initial object. At the moment you save the object as string in the file you no longer have it as an object of the original type; it will become an instance object of string, with the fields of string. There is no field confidence in string, maybe only in the initial type of your object.

You save an object as string in the archive:

#Futaba original
def bot_convencional(message):
    ...
    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()

You read the file and access the field confidence of a string, generating the error cited in the question:

#Recebe qualquer outra mensagem
@bot.message_handler(func=lambda message:True)
def mensagem(message):
    ...
    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?")

If you need to persist an object in a file to use it later, you will need to use some serialization technique. In Python there is a native library pickle that does this.

Save a serialized object to file:

obj = {'foo': 'bar'}

with open('arquivo.pickle', 'wb') as stream:
    pickle.dump(obj, stream)

Read a serialized object from the file:

with open('arquivo.pickle', 'rb') as stream:
    obj = pickle.load(stream)

print(obj['foo'])  # 'bar'

See working on Repl.it

-1


To the confidence I needed to access the chatbot.get_response(message). For this, I created a variable called corpus to store this corpus = chatbot.get_response(message), I have made it global and if of confidence.

FUNCTION STORING THE CHATBOT

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)
    resposta = str(resposta)
    global corpus
    corpus = chatbot.get_response(message)

    mensagem = open("arquivos/teste", "w")
    mensagem.write(resposta)
    mensagem.close()

FUNCTION RESPONDING TO MESSAGES

#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(corpus.confidence) > 0.5:
        bot.reply_to(message, resposta)
    else:
        bot.reply_to(message, "Como você está se sentindo?")
bot.polling()

I don’t know if this is right, but it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.