Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
Adiciona mais diálogos para melhorar conversas (#72)
Browse files Browse the repository at this point in the history
* Adiciona mais diálogos para melhorar conversas

* Acrescenta mais diálogos

* Separate functions

* Improve files
  • Loading branch information
alexandrebarbaruiva committed Oct 24, 2018
1 parent 486866e commit c9960ba
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
4 changes: 2 additions & 2 deletions bot/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def error(self, bot, update, error):

def run(self):
# Start the Bot
print("Bot configured. Receiving messages now.")
self.updater.start_polling()

# Run the bot until you press Ctrl-C or the process receives SIGINT,
Expand Down Expand Up @@ -172,7 +171,8 @@ def run(self):
try:
token = retrieve_default("TELEGRAM")["token"]
watson = eval(retrieve_default()["IBM Watson"])
x = Application(token=token, use_watson=watson).run()
x = Application(token=token, use_watson=watson)
x.run()
except FileNotFoundError:
print("Configuration file not found.")
sys.exit(1)
42 changes: 25 additions & 17 deletions bot/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.response_selection import get_random_response
from bot.config_reader import retrieve_default

sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from bot.watson import Watson
from bot.config_reader import retrieve_default


class Communication:
Expand Down Expand Up @@ -51,31 +51,39 @@ def __init__(self, use_watson=False, train=True):
exit()
else:
self.watson_usage = False
self.all_texts = ""
self.all_texts = " "

def respond(self, message):
"""
Receive message from user and returns corresponding answer.
"""
# if string isnt empty concatenate with space
if self.all_texts:
self.all_texts += " " + message
if len(message) > 50 and self.watson_usage:
top_answer = get_analysis(message)
return f"Hmm, você está falando sobre {top_answer}"
elif len(message) > 0:
return self.comm.get_response(self.clean(message))
else:
self.all_texts = message
return "Algo de errado não está certo, mande mensagem com o que " \
"você falou para os meus criadores!" \
" Digite /info para saber mais."

if len(message) > 50 and self.watson_usage:
analysis = self.watson_analyzer.get_analysis(message)
def get_analysis(message):
"""
Analyses message received by label and score and returns best answer
"""
# if string isnt empty concatenate with space
self.all_texts += f"{message} "

# Get top 1 categorie
top_score = analysis["categories"][0]["score"]
top_label = analysis["categories"][0]["label"]
analysis = self.watson_analyzer.get_analysis(message)

# Print the leaf from category tree
toplabel_index = top_label.rindex("/") + 1
leaf_category = top_label[toplabel_index:]
return f"Hmm, você está falando sobre {leaf_category}"
else:
return self.comm.get_response(self.clean(message))
# Get top 1 category
top_score = analysis["categories"][0]["score"]
top_label = analysis["categories"][0]["label"]

# Print the leaf from category tree
toplabel_index = top_label.rindex("/") + 1
top_answer = top_label[toplabel_index:]
return top_answer

def clean(self, message):
"""
Expand Down
34 changes: 34 additions & 0 deletions bot/dialogs/random/random.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
categories:
- Random
conversations:
- - legal
- Sim, legal mesmo :)

- - nada não, ignora
- Ok!

- - fazendo o que aí?
- Estou aumentando minha rede de contatos, e você?

- - você me ama?
- Um aglomerado de números virtuais é capaz de amar?

- - sim
- Ok

- - quais as novidades?
- Nada de mais, e você?
- Estou aumentando minha capacidade cognitiva neste exato instante :D

- - você gosta de que?
- Eu amo números e gatinhos. Um eu adoro ver vídeos e o outro é sucesso
na internet heheh

- - você odeia o que?
- Nada, eu sou uma entidade de puro amor e compreensão <3

- - sem problemas
- Tem certeza?

- - você está me ignorando?
- EU??? Foi só um inseto que engoli mesmo, tente de novo!
3 changes: 3 additions & 0 deletions bot/dialogs/removals/nouns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"rabot"
]
3 changes: 2 additions & 1 deletion bot/dialogs/removals/punctuation.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
".",
"!",
"?"
"?",
","
]
2 changes: 2 additions & 0 deletions tests/test_communication.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import unittest
import sys
import os
from unittest.mock import patch

sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from bot.communication import Communication
from bot.watson import Watson


class TestBotCommunication(unittest.TestCase):
Expand Down

0 comments on commit c9960ba

Please sign in to comment.