First we just created a main function out of the last section of code in her
chatbot.py script. Here’s what it looks like at the bottom of chatbot.py:
flag=True 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() ... else: flag=False print("ROBO: Bye! take care..")
And here’s what that looks like in our new
def main(): flag = True print("ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye!") while flag: user_response = input() ... else: flag = False print("ROBO: Bye! take care..") if __name__ == '__main__': main()
No substantive change. We just wanted to make sure we could import
parul_bots.py and use the
parul_bots.response(user_statement) function to answer questions about chatbots, like Parul did.
In fact, we won’t use this main function at all, except for regression testing of her nice video demo.
That reminds me, I need to put together a gif for the python user group talk tomorrow night at Qualcomm.
Next we used Parul’s
while True approach to create a “–persist” option for our bot, so it doesn’t have to load the corpus or the language model every time we tell it something from the command line app like this:
$ bot I'm telling you somthing.
class Bot: def reply(self, statement): """ Chatbot "main" function to respond to a user command or statement >>> respond('Hi') Hello! >>> len(respond('Hey Mycroft!')) 4 """ responses =  bot_statement = response(statement.lower()) responses.append((1.0, bot_statement)) return responses
But notice that we’re not just returning the statement we think the bot should reply with, we’re also giving it a score. That way the parul_bots reply can be combined with the pattern_bots.py replies and others. The
CLIBot object in
clibot.py can do whatever complicated logic it needs to do to select a reply and manage the conversation flow.
In the next post I’ll show you how we made it even smarter by pulling in multiple pages from Wikipedia and using some state of the art Question-Answering algorithms from papers like “WikiQA: A Challenge Dataset for Open-Domain Question Answering” by Yang et al. 2015
If you want to play around with Microsoft’s version of this, check out the WikiQACorpus search engine.