Chatbot in Python with NLTK

3

I'm a beginner in python, some time I've been interested in Text Mining and would like to ask for help with a question in a project.

Some time I've been studying how to use the Python NLTK Library to create a chatbot.

I even got to work out some codes, but there was a doubt.

My code:

#coding: utf-8 

from nltk.chat.util import Chat, reflections 

pairs = [ 
    [ 
        r'oi', 
        ['olá', 'Como vai?', 'Tudo Bem?',] 
    ], 
] 

def bob_bot(): 
    print("Como posso te ajudar hoje?") 
    chat = Chat(pairs, reflections) 
    chat.converse() 

if __name__ == "__main__": 
    bob_bot() 

I noticed that in this tuple pairs the nltk.chat.util module uses a function of the python 're' module to create the bot dialog.

In the nltk.chat.util module it takes the contents of the tuple and uses this function:

[(re.compile(x, re.IGNORECASE),y) for (x,y) in pairs]

to transform the content of 'pairs' into:

[(re.compile('oi', re.IGNORECASE), ['olá', 'Como vai?', 'Tudo Bem?'])]

My question is whether you can get the dialogs from a text file, put them inside the pairs' tuple as if they were a sentence, eg 'How are you?', 'All right?'. So when I run the code python reads the dialogs from within the text file.

Someone who has experience, can you help me?

    
asked by anonymous 24.01.2018 / 11:46

2 answers

1

(assuming linux / mac / unix is worth)

Suggestion to create a "chat.py" file, an "input.txt" file and:

$ cat chat.py
... o ficheiro da pergunta

$ cat in.txt
oi
oi
oi
adeus

$ python chat.py < in.txt > out.txt

$ paste -d'\n' out.txt in.txt
Como posso te ajudar hoje?
oi
>Tudo Bem?
oi
>Como vai?
oi
>olá
até depois
>None
adeus
>None

>quit

None
    
24.01.2018 / 16:49
1

You can create a CSV file, use the semicolon as the delimiter to separate the fields, the first field will be the regular expression, and the second field the answer. Note that I removed the single quotes and commas, and added vertical bar , it became a delimiter to separate each answer.

Archive chat.csv

oi;olá|Como vai?|Tudo Bem?

Now just open the archi through Python:

with open('chat.csv', newline='') as File:  
  reader = csv.reader(File, delimiter=';')
  for linha in reader:
    add = [
      r"{}".format(linha[0]), # Expressão regular
      [] # Resposta
    ]
    # Adiciona cada resposta na tupla "add[1]"
    for x in linha[1].split('|'): add[1].append(x)
    # Adiciona "add" em "pairs"
    pairs.append(add)

Now, just below a print will have the output:

[
  [ 'oi', [ 'olá', 'Como vai?', 'Tudo Bem?' ] ]
]

If you modify the chat.csv

oi;olá|Como vai?|Tudo Bem?
qual seu nome?;Bob

You will have the output:

[
  [ 'oi', [ 'olá', 'Como vai?', 'Tudo Bem?' ] ],
  [ 'qual seu nome?', [ 'Bob' ] ]
]

And when to boot:

Como posso te ajudar hoje?
> qual seu nome
Bob

See working at repl.it

    
19.04.2018 / 07:59