gunthercox / ChatterBot

ChatterBot is a machine learning, conversational dialog engine for creating chat bots
https://chatterbot.readthedocs.io
BSD 3-Clause "New" or "Revised" License
14.05k stars 4.44k forks source link

how can i import data from .txt file. and train that data. #1048

Closed PoojaPatel05 closed 6 years ago

PoojaPatel05 commented 6 years ago
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

vkosuri commented 6 years ago

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

vkosuri commented 6 years ago

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

vkosuri commented 6 years ago

Could you please let me know fallowing details?

  1. which version of chatterbot are you using?
  2. Are you using python or anaconda?
PoojaPatel05 commented 6 years ago

I am using anaconda. and chatterbot version is 0.7.6

vkosuri commented 6 years ago

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
PoojaPatel05 commented 6 years ago

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

PoojaPatel05 commented 6 years ago

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 < notifications@github.com> 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

vkosuri commented 6 years ago

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
PoojaPatel05 commented 6 years ago

Thank you so much. It works. Thanks alot

On Fri, Nov 3, 2017 at 12:34 PM, Mallikarjunarao Kosuri < notifications@github.com> 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 commented 6 years ago

It is giving answer which is below the line of question. But what if answer is in paragraph?

On Fri, Nov 3, 2017 at 4:00 PM, Mallikarjunarao Kosuri < notifications@github.com> wrote:

Closed #1048 https://github.com/gunthercox/ChatterBot/issues/1048.

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

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

crynomore commented 6 years ago

@poojapatel05 can i have your trainer txt file

crynomore commented 6 years ago

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

vkosuri commented 6 years ago

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

sanjaytagove commented 6 years ago

@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"

crynomore commented 6 years ago

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

sanjaytagove commented 6 years ago

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 commented 6 years ago

Yes, https://github.com/gunthercox/ChatterBot/blob/4b5ae262bab0bc0c83555d39400049f20aaca9cd/chatterbot/constants.py

sanjaytagove commented 6 years ago

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

vkosuri commented 6 years ago

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

sanjaytagove commented 6 years ago

after putting:

from chatterbot import constants
constants.STATEMENT_TEXT_MAX_LENGTH = 2000;

it still not train large text statement.

Alvarden15 commented 5 years ago

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
Alvarden15 commented 5 years ago

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

shahali647 commented 5 years ago

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

lock[bot] commented 4 years ago

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.