From c7642722e2f7d5b83f971d9d94be9bfb5ca160d6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 20 Mar 2018 17:33:04 +0100 Subject: [PATCH] edited the chatbook to prevent loop --- Makefile | 2 +- src/chatbook.py | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 22f8da3..50c9fa0 100644 --- a/Makefile +++ b/Makefile @@ -114,5 +114,5 @@ tts: output/chatbot.txt ocr/output.txt ## text to speech. Dependencies: espea ttssr-human-only: ocr/output.txt ## Loop: text to speech-speech recognition. Dependencies: espeak, pocketsphinx bash src/ttssr-loop-human-only.sh ocr/output.txt -chatbook: ocr/output.txt +chatbook: ocr/output.txt #chatbot based on the knowledge of the scans Dependencies: nltk_rake, irc, nltk python3 src/chatbook.py diff --git a/src/chatbook.py b/src/chatbook.py index e94dbaa..758657c 100644 --- a/src/chatbook.py +++ b/src/chatbook.py @@ -4,6 +4,7 @@ import random from nltk.tokenize import sent_tokenize, word_tokenize import json #from thread import start_new_thread +import os r= Rake() @@ -14,36 +15,44 @@ def chunks(l, n): class HelloBot(irc.bot.SingleServerIRCBot): def __init__(self, channel, nickname, server, port=6667, index=None): + print("connecting to chatroom...") irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) self.channel = channel self.index = index def on_welcome(self, c, e): c.join(self.channel) + print("joined chatroom") def on_privmsg(self, c, e): pass def on_pubmsg(self, c, e): print(e.arguments, e.source) - msg = e.arguments[0] + msg=e.arguments[0] + incoming_msg = e.arguments[0] + print(incoming_msg) r.extract_keywords_from_text(msg) #r.get_ranked_phrases_with_scores() listOfKeys = r.get_ranked_phrases() - for keyWord in listOfKeys: - if keyWord in self.index: - msg = (index.get(keyWord)[0].get('sentence')) - msg_where = "I found this in {}".format(index.get(keyWord)[0].get('filename')) - else: - msg = "I don't know anything about that" - msg_where = "" + msg_where = "" - for chunk in chunks(msg, 400): - c.privmsg(self.channel, chunk) + if incoming_msg != "I don't know anything about that": + for keyWord in listOfKeys: + if keyWord in self.index: + msg = (index.get(keyWord)[0].get('sentence')) + msg_where = "I found this in {}".format(index.get(keyWord)[0].get('filename')) + else: + msg = "I don't know anything about that" + msg_where = "" + for chunk in chunks(msg, 400): + c.privmsg(self.channel, chunk) + print(chunk) - c.privmsg(self.channel, msg_where) + + c.privmsg(self.channel, msg_where) @@ -68,6 +77,8 @@ if __name__ == "__main__": index={} #print(index) + myhost = os.uname()[1] + - bot = HelloBot(args.channel, args.nickname, args.server, args.port, index) + bot = HelloBot(args.channel, "{0}-{1}-bot".format(myhost, len(index)), args.server, args.port, index) bot.start()