From 4d6647bd6b1e7b6c38fd1e0e929fc0981922d0e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Tue, 2 Oct 2018 16:12:32 +0200 Subject: [PATCH] add markov bullshit --- delojza.py | 28 ++++++++++++++++++++++++++-- requirements.in | 3 ++- requirements.txt | 2 ++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/delojza.py b/delojza.py index d117d34..fae1955 100755 --- a/delojza.py +++ b/delojza.py @@ -8,6 +8,7 @@ import sys from glob import glob import filetype +import markovify import requests import youtube_dl from telegram import MessageEntity @@ -21,6 +22,17 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logger = logging.getLogger("kunsax") +markov: markovify.NewlineText + + +def add_to_corpus(text): + global markov + text = text.lower() + new_sentence = markovify.NewlineText(text) + markov = markovify.combine([markov, new_sentence]) + with open("initial.txt", 'a') as f: + f.write(text + '\n') + def datestr(date): return date.strftime("%Y-%m-%d@%H%M") @@ -178,15 +190,19 @@ def handle_rest(bot, update): handle([url], update.message, download_raw, filename=filename) +def handle_text(bot, update): + add_to_corpus(update.message.text) + + def start(bot, update): - update.message.reply_text('WOOP WOOP') + update.message.reply_text(markov.make_sentence()) def error(bot, update, error): logger.error(error) if "Timed out" in str(error): if update is not None: - update.message.reply_text("Mmmm, I like it.") + update.message.reply_text(markov.make_sentence(tries=100) or "Mmmm, I like it...") handle_rest(bot, update) else: if update is not None: @@ -194,6 +210,13 @@ def error(bot, update, error): def main(): + global markov + + with open("initial.txt") as f: + text = f.read() + markov = markovify.NewlineText(text.lower()) + print(markov.make_sentence()) + updater = Updater("***REMOVED***") dp = updater.dispatcher @@ -208,6 +231,7 @@ def main(): Filters.photo | Filters.video | Filters.video_note | Filters.audio | Filters.voice | Filters.document, handle_rest)) dp.add_handler(MessageHandler(Filters.entity(MessageEntity.HASHTAG), handle_hashtag)) + dp.add_handler(MessageHandler(Filters.text, handle_text)) updater.start_polling() diff --git a/requirements.in b/requirements.in index dc174ff..bb7f778 100644 --- a/requirements.in +++ b/requirements.in @@ -1,4 +1,5 @@ python-telegram-bot youtube-dl requests -filetype \ No newline at end of file +filetype +markovify \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 9a7a900..79f6366 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,9 +12,11 @@ cryptography==2.3.1 # via python-telegram-bot filetype==1.0.1 future==0.16.0 # via python-telegram-bot idna==2.7 # via cryptography, requests +markovify==0.7.1 pycparser==2.19 # via cffi python-telegram-bot==11.1.0 requests==2.19.1 six==1.11.0 # via cryptography +unidecode==1.0.22 # via markovify urllib3==1.23 # via requests youtube-dl==2018.9.26