Chatterbot: How to make chatbot do some tasks when asked a question(chatterbot)

Created on 24 Feb 2018  路  5Comments  路  Source: gunthercox/ChatterBot

I used chatterbot Api to create a chatbot. I was able to fetch data from a excel sheet and display it. But how to display an output (excel values and calculations done using excel values) when a question is asked? Normally we feed the question and answers. I need to get the results from excel file as an answer to a question to chatbot.

import xlrd  #import excel reader

from chatterbot import ChatBot
chatbot = ChatBot(
    'Charlie',
    storage_adapter='chatterbot.storage.SQLStorageAdapter',
    logic_adapters=[
        {
            "import_path": "chatterbot.logic.BestMatch"
        },
        {
            'import_path': 'chatterbot.logic.SpecificResponseAdapter',
            'input_text': 'Help me!',
            'output_text': 'Ok, here is a link: http://chatterbot.rtfd.org'
        },
        {
            'import_path': 'chatterbot.logic.LowConfidenceAdapter',
            'threshold': 0.65,
            'default_response': 'I am sorry, but I do not understand.'
        }
    ],
    trainer='chatterbot.trainers.ListTrainer'
)




#opening Excel
workbook = xlrd.open_workbook('D:\\chatbot\\Book1.xlsx')

#opening sheet
worksheet = workbook.sheet_by_name('Sheet2')


#for-loop for iteration
for row_idx in range(0, worksheet.nrows):
    #print ('-'*40)
    #print ('Row: %s' % row_idx)
    #for col_idx in range(0, worksheet.ncols):
    #    cell_obj = worksheet.cell(row_idx, col_idx).value
    #    print ('Column: [%s] cell_obj: [%s]' %(col_idx, cell_obj))
    ques=worksheet.cell(row_idx, 0).value
    ans=worksheet.cell(row_idx, 1).value
    chatbot.train([ques,ans])
response = chatbot.get_response('I would like to book a flight.')
print(response)
question

All 5 comments

Did you tried pandas if you are dealing MS excel files it's worth, May be this reference will provide some information https://plot.ly/pandas/intro-to-pandas-tutorial/

Thanks for the reply. I'm able to read and write data. But my question is, how to output values from an excel file as an answer when a question is asked to chatbot? For example, assume I have a column called "cost" which contains several values. Of I ask chatbot 'what is the total cost?' it should give answer as the total cost. It's not about reading and writing, but it's about displaying values from a chatbot.
Regards.

@jithinolickal i did a workaround on this by building a hub method witch detect the "serviceable" requests from chatbot response:

example:

  • train bot with: ['what is total cost?', 'call totalcost()']
  • receive client side request
  • before reply with bot response, analyse it for 'call' responses
  • if the bot response is a call, then call the method and send method response back

@vkosuri @gunthercox , would be nice if Chatterbot have some sort of "call function logic adapter" to handle this from inside, like chatterbot.logic.specific_response but with a function output, instead of text.

@jithinolickal I think you have to write you own logic adapter based on your question. More information about how to write a new logic adapter is documented here https://chatterbot.readthedocs.io/en/stable/logic/create-a-logic-adapter.html

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hemangsk picture hemangsk  路  4Comments

filipceglik picture filipceglik  路  3Comments

vkosuri picture vkosuri  路  4Comments

juanpialbano picture juanpialbano  路  4Comments

gunthercox picture gunthercox  路  3Comments