Chatterbot: how can i import data from .txt file. and train that data.

Created on 2 Nov 2017  Â·  24Comments  Â·  Source: gunthercox/ChatterBot

from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot

f = open('dataq2.txt','r').read()

chatbot = ChatBot(
    "Terminal",
    storage_adapter="chatterbot.storage.SQLStorageAdapter", #allows the chat bot to connect to SQL databases
    input_adapter="chatterbot.input.TerminalAdapter", #allows a user to type into their terminal to communicate with the chat bot.
    output_adapter="chatterbot.output.TerminalAdapter", # print chatbot responce
    database="../database.db"   # specify the path to the database that the chat bot will use
)

chatbot.set_trainer(ListTrainer)
chatbot.train(f)


print("Type your question here...")
while True:
    try:
        chatbot_input = chatbot.get_response(None)
    # Press ctrl-c or ctrl-d to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

this is my code. it is running but not giving proper answer (which is stored in dataq2.txt ) it do not train data which is in dataq2.txt file. or may be it trains that but can not store that.
dataq2.txt have QA data. what i want is if I write 1st line than it should reply line 2.

dataq2 is in the form of this

Q:hello
A:hi
Q:who
A:girl 
or
Q:
hello
A:
hi
Q:
who
A:
girl

here if i input hello than it should reply hi.
please help me

answered question

Most helpful comment

Here is your solution

from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot

f = open('dataq2.txt','r')

train_data = []

for line in f:
    m = re.search('(Q:|A:)?(.+)', line)
    if m:
        train_data.append(m.groups()[1])

chatbot = ChatBot(
    "Terminal",
    storage_adapter="chatterbot.storage.SQLStorageAdapter", #allows the chat bot to connect to SQL databases
    input_adapter="chatterbot.input.TerminalAdapter", #allows a user to type into their terminal to communicate with the chat bot.
    output_adapter="chatterbot.output.TerminalAdapter", # print chatbot responce
    database="../database.db"   # specify the path to the database that the chat bot will use
)

chatbot.set_trainer(ListTrainer)
chatbot.train(train_data)


print("Type your question here...")
while True:
    try:
        chatbot_input = chatbot.get_response(None)
    # Press ctrl-c or ctrl-d to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

All 24 comments

@PoojaPatel05 Yes you could train your bot with some small modifications.

from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot

f = open('dataq2.txt','r').read()

train_data = []

for line in f:
    train_data.append(re.match('(Q:|A:)?(.+)', line).group()[1])

chatbot = ChatBot(
    "Terminal",
    storage_adapter="chatterbot.storage.SQLStorageAdapter", #allows the chat bot to connect to SQL databases
    input_adapter="chatterbot.input.TerminalAdapter", #allows a user to type into their terminal to communicate with the chat bot.
    output_adapter="chatterbot.output.TerminalAdapter", # print chatbot responce
    database="../database.db"   # specify the path to the database that the chat bot will use
)

chatbot.set_trainer(ListTrainer)
chatbot.train(train_data)


print("Type your question here...")
while True:
    try:
        chatbot_input = chatbot.get_response(None)
    # Press ctrl-c or ctrl-d to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

FYI... I haven't verified above code, let me know your inputs on this.

@PoojaPatel05 I made some changes on your script if you want you could use them.

Could you please let me know fallowing details?

  1. which version of chatterbot are you using?
  2. Are you using python or anaconda?

I am using anaconda. and chatterbot version is 0.7.6

Python regular expression module, More information available here https://docs.python.org/2/library/re.html,

To avoid your issue import re module at the top of the script

import re
import Chatterbot

# paste your code here

in this line:
train_data.append(re.match('(Q:|A:)?(.+)', line).group()[1])
its showing error
IndexError: string index out of range

train_data = []

for line in f:
train_data.append(re.match('(Q:|A:)?(.+)', line).group()[1])

CAN YOU EXPLAIN THIS LINES?

On Fri, Nov 3, 2017 at 10:17 AM, Pooja Patel pooja.tagove@gmail.com wrote:

hey, Please help me.
May be it have a silly solution. I dont know. but I am new in this. so
please help me

On Thu, Nov 2, 2017 at 6:41 PM, Mallikarjunarao Kosuri <
[email protected]> wrote:

Python regular expression module, More information available here
https://docs.python.org/2/library/re.html,

To avoid your import re module at the top

import reimport Chatterbot

paste your code here

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/gunthercox/ChatterBot/issues/1048#issuecomment-341416225,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AfFEgsbULng_ktoWZW5EWTjIqw2JOAmsks5syb-agaJpZM4QPlgl
.

--
Pooja Patel
Python Developer
w: www.tagove.com
https://u3476773.ct.sendgrid.net/wf/click?upn=AvVTWHpii7nfl55rPBWDlFzoSSG-2BuGg5dAeZQNDakXg-3D_ls62w2VqncErQRLLYqULEUYLnD2M4VQenauY8ZodonLGqFgeX4dvd6ZyrtANiPzAcOslZAPxp-2F98z04-2BFw1-2BtZloOEzXXgh7hLucUKp4pnbAGbkOIpKJ3gPrbhlBLqcVU3MIE3lGgOYZNFqbW6JnRK-2FNNfefFZGcowRXklQ-2B7Say56IAV18mPZWzgvAHlCkqkWaLCLU0Dy4pveYMkZ1NBAWQEvEompfUlqJvuV3vdUY-3D

--
Pooja Patel
Python Developer
w: www.tagove.com
https://u3476773.ct.sendgrid.net/wf/click?upn=AvVTWHpii7nfl55rPBWDlFzoSSG-2BuGg5dAeZQNDakXg-3D_ls62w2VqncErQRLLYqULEUYLnD2M4VQenauY8ZodonLGqFgeX4dvd6ZyrtANiPzAcOslZAPxp-2F98z04-2BFw1-2BtZloOEzXXgh7hLucUKp4pnbAGbkOIpKJ3gPrbhlBLqcVU3MIE3lGgOYZNFqbW6JnRK-2FNNfefFZGcowRXklQ-2B7Say56IAV18mPZWzgvAHlCkqkWaLCLU0Dy4pveYMkZ1NBAWQEvEompfUlqJvuV3vdUY-3D

Here is your solution

from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot

f = open('dataq2.txt','r')

train_data = []

for line in f:
    m = re.search('(Q:|A:)?(.+)', line)
    if m:
        train_data.append(m.groups()[1])

chatbot = ChatBot(
    "Terminal",
    storage_adapter="chatterbot.storage.SQLStorageAdapter", #allows the chat bot to connect to SQL databases
    input_adapter="chatterbot.input.TerminalAdapter", #allows a user to type into their terminal to communicate with the chat bot.
    output_adapter="chatterbot.output.TerminalAdapter", # print chatbot responce
    database="../database.db"   # specify the path to the database that the chat bot will use
)

chatbot.set_trainer(ListTrainer)
chatbot.train(train_data)


print("Type your question here...")
while True:
    try:
        chatbot_input = chatbot.get_response(None)
    # Press ctrl-c or ctrl-d to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

Thank you so much.
It works. Thanks alot

On Fri, Nov 3, 2017 at 12:34 PM, Mallikarjunarao Kosuri <
[email protected]> wrote:

Here is your solution

from chatterbot.trainers import ListTrainerfrom chatterbot import ChatBot

f = open('test_data.txt','r')

train_data = []
for line in f:
# print line
m = re.search('(Q:|A:)?(.+)', line)
if m:
train_data.append(m.groups()[1])

chatbot = ChatBot(
"Terminal",
storage_adapter="chatterbot.storage.SQLStorageAdapter", #allows the chat bot to connect to SQL databases
input_adapter="chatterbot.input.TerminalAdapter", #allows a user to type into their terminal to communicate with the chat bot.
output_adapter="chatterbot.output.TerminalAdapter", # print chatbot responce
database="../database.db" # specify the path to the database that the chat bot will use
)

chatbot.set_trainer(ListTrainer)
chatbot.train(train_data)

print("Type your question here...")while True:
try:
chatbot_input = chatbot.get_response(None)
# Press ctrl-c or ctrl-d to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/gunthercox/ChatterBot/issues/1048#issuecomment-341633602,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AfFEgrEzf2XVkC9TumsOhiVzT6BTLJCdks5syrrjgaJpZM4QPlgl
.

--
Pooja Patel
Python Developer
w: www.tagove.com
https://u3476773.ct.sendgrid.net/wf/click?upn=AvVTWHpii7nfl55rPBWDlFzoSSG-2BuGg5dAeZQNDakXg-3D_ls62w2VqncErQRLLYqULEUYLnD2M4VQenauY8ZodonLGqFgeX4dvd6ZyrtANiPzAcOslZAPxp-2F98z04-2BFw1-2BtZloOEzXXgh7hLucUKp4pnbAGbkOIpKJ3gPrbhlBLqcVU3MIE3lGgOYZNFqbW6JnRK-2FNNfefFZGcowRXklQ-2B7Say56IAV18mPZWzgvAHlCkqkWaLCLU0Dy4pveYMkZ1NBAWQEvEompfUlqJvuV3vdUY-3D

@poojapatel05 can i have your trainer txt file

i am using this python code

from chatterbot.trainers import ListTrainer

from chatterbot import ChatBot

bot = ChatBot('Test')

conv = open('chats.txt','r').readlines()

bot.set_trainer(ListTrainer)

bot.train(conv)

while True:
    request = input('You: ')
    response = bot.get_response(request)

    print('Bot: ',response)

i want a trainer file
can anyone help me

I think are you looking this type example https://github.com/gunthercox/ChatterBot/issues/1048#issuecomment-341633602

@PoojaPatel05 can i have your trainer txt file

@beingnvn pooja want to say that, if we have question and answer with single line, then it working fine.
but what if we have answer of multiple lines(paragraphed answer), that time it gives error like:
No handlers could be found for logger "chatterbot.storage.storage_adapter"

Just a guess, try using three inverted commas
Ex: """Hi"""

Just a guess, try using three inverted commas
Ex: """Hi"""

@beingnvn, is there any character limits in dialog statement for question and answer?
if we pass shorter character length then its okay, but it gives error while we putting long character stream.

@vkosuri got your point.
but I have a question, by default it set to 400 characters, but it can train above 400 characters and up to 1011 characters. how this is possible?

import constants.py file and override the required property in your machine.

after putting:

from chatterbot import constants
constants.STATEMENT_TEXT_MAX_LENGTH = 2000;

it still not train large text statement.

Can someone update that code to the lastest version of ChatterBot?. I tried it, but it doesn't seem to work properly.
Here's the code so far (i changed 1 to 0 in the append because it kept giving me an index error)

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.trainers import ChatterBotCorpusTrainer
import re



f=open('FIA.txt','r+')

train_data=[]

for line in f:
    m=re.search('(Q:|A:)?(.+)',line)
    if m:
        train_data.append(m.group()[0])



chatb=ChatBot("EDI",
    storage_adapter="chatterbot.storage.SQLStorageAdapter",
    input_adapter="chatterbot.input.TerminalAdapter",
    output_adapter="chatterbot.output.TerminalAdapter",
    database="../database.db"
    )

trainerCorpus=ChatterBotCorpusTrainer(chatb)
trainerCorpus.train(
    "chatterbot.corpus.spanish"
)

trainer=ListTrainer(chatb)

trainer.train(
    train_data

)

print("Buenas")
while True:
    try:
        chatbot_input=chatb.get_response(input('Q:'))
        print('A:',chatbot_input)
    except(KeyboardInterrupt,EOFError,SystemExit):
        break

Nevermind, i found a solution. Although i'm using a .txt file; instead it's with a .yaml file.

Here is your solution

from chatterbot.trainers import ListTrainer
from chatterbot import ChatBot

f = open('dataq2.txt','r')

train_data = []

for line in f:
    m = re.search('(Q:|A:)?(.+)', line)
    if m:
        train_data.append(m.groups()[1])

chatbot = ChatBot(
    "Terminal",
    storage_adapter="chatterbot.storage.SQLStorageAdapter", #allows the chat bot to connect to SQL databases
    input_adapter="chatterbot.input.TerminalAdapter", #allows a user to type into their terminal to communicate with the chat bot.
    output_adapter="chatterbot.output.TerminalAdapter", # print chatbot responce
    database="../database.db"   # specify the path to the database that the chat bot will use
)

chatbot.set_trainer(ListTrainer)
chatbot.train(train_data)


print("Type your question here...")
while True:
    try:
        chatbot_input = chatbot.get_response(None)
    # Press ctrl-c or ctrl-d to exit
    except (KeyboardInterrupt, EOFError, SystemExit):
        break

I got this error:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 7159: character maps to

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

vkosuri picture vkosuri  Â·  4Comments

gunthercox picture gunthercox  Â·  3Comments

coolrb picture coolrb  Â·  3Comments

decode007 picture decode007  Â·  3Comments

AmusingThrone picture AmusingThrone  Â·  3Comments