0

Created a chatbot using python and the flow in which it is working is that I am messeging and according to that Chatbot is replying. But it should be done in reverse means chat bot should start first by welcoming/asking questions, then user will reply to that. Please suggest some transformation to be made in the code so that it can work accordingly. Thanking you in advance.

The code for the above mentioned goes like this:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os

bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)

for files in os.listdir('C:/Users/Username\Desktop\chatterbot\chatterbot_corpus\data/english/'):
    data = open('C:/Users/Username\Desktop\chatterbot\chatterbot_corpus\data/english/' + files, 'r').readlines()
    bot.train(data)

while True:
    message = input('You: ')
    if message.strip() != 'Bye'.lower():
        reply = bot.get_response(message)
        print('ChatBot:',reply)
    if message.strip() == 'Bye'.lower():
        print('ChatBot: Bye')
        break
  • if you want chat bot to ask predefined question then you can just print the question. – Mufeed Jun 29 '18 at 19:00
  • @Mufeed, Thanks for reply. But i want series of interative conversation with my chatbot in which the bot asks you question and for that i will be replying. So please tell me the part of code to be changed to get that feature enabled in above code. – Sarah Sagittarius Jun 30 '18 at 06:39

1 Answers1

0

In my opinion you should train your bot accordingly.I mean start with an answer,then add subsequent conversation to training data.
For example training data should be something like this.

data = [
    "Tony",        #I started with an answer
    "that's a good name",
    "thank you",
     "you are welcome"]

Then start your conversation with a static statement like print("chatBot: hai, what is your name?") I added sample code snippet below.

data = [
     "Tony",
     "that's a good name",
    "thank you",
     "you are welcome"]
bot.train(data)

print("chatbot: what is your name?")
message = input("you: ")

while True:
    if message.strip() != 'Bye'.lower():
        reply = bot.get_response(message)
        print('ChatBot:',reply)
    if message.strip() == 'Bye'.lower():
        print('ChatBot: Bye')
        break
    message = input("you: ")

From docs:
For the training process, you will need to pass in a list of statements where the order of each statement is based on its placement in a given conversation.
For example, if you were to run bot of the following training calls, then the resulting chatterbot would respond to both statements of “Hi there!” and “Greetings!” by saying “Hello”.

from chatterbot.trainers import ListTrainer

chatterbot = ChatBot("Training Example")
chatterbot.set_trainer(ListTrainer)

chatterbot.train([
    "Hi there!",
    "Hello",
])

chatterbot.train([
    "Greetings!",
    "Hello",
])

That means the order of your question and answer matters. When you train two sentences the bot takes first one as question and second one as its answer.

Mufeed
  • 3,018
  • 4
  • 20
  • 29
  • I am new to this. Can you please tell me how this training of **'data = ["Tony", "that's a good name", "thank you", "you are welcome"] bot.train(data)'** will help in any way? – Sarah Sagittarius Jun 30 '18 at 19:43
  • now the scenario works like this. It asks the question to you. "what is your name"(static print statement). You give answer as "Tony". For you it is an answer. But for bot it is the question("Tony"). because it is the first sentence in training data. Obviously bot says you very next sentence "That's a good name" and so on. Now read my updated answer. You will get some ideas. – Mufeed Jul 02 '18 at 07:58
  • can you just guide me if I want to add **sentiment analyser to every user's reply**. How should I proceed? Your quick response will be appreciated as I want to proceed further and I am very curious to know? – Sarah Sagittarius Jul 04 '18 at 12:27
  • Can you explain the problem in detail? – Mufeed Jul 04 '18 at 12:39
  • Please go through this [link] (https://stackoverflow.com/questions/51167284/how-to-integrate-the-sentiment-analysis-script-with-the-chatbot-for-analysing-th) for detail problem. You have been so helpful. Thank you – Sarah Sagittarius Jul 04 '18 at 13:15
  • I mean tick this answer as an accepted answer it it helped you. Otherwise people may think your question is never git solved. – Mufeed Jul 04 '18 at 15:27
  • Thank you so much for being so helpful. How I forgot to mark that. Now its Done! – Sarah Sagittarius Jul 04 '18 at 16:02