add markov bullshit

This commit is contained in:
Tomáš Mládek 2018-10-02 16:12:32 +02:00 committed by Tomáš Mládek
parent 225e81fded
commit 4d6647bd6b
3 changed files with 30 additions and 3 deletions

View file

@ -8,6 +8,7 @@ import sys
from glob import glob from glob import glob
import filetype import filetype
import markovify
import requests import requests
import youtube_dl import youtube_dl
from telegram import MessageEntity from telegram import MessageEntity
@ -21,6 +22,17 @@ logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger("kunsax") 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): def datestr(date):
return date.strftime("%Y-%m-%d@%H%M") 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) handle([url], update.message, download_raw, filename=filename)
def handle_text(bot, update):
add_to_corpus(update.message.text)
def start(bot, update): def start(bot, update):
update.message.reply_text('WOOP WOOP') update.message.reply_text(markov.make_sentence())
def error(bot, update, error): def error(bot, update, error):
logger.error(error) logger.error(error)
if "Timed out" in str(error): if "Timed out" in str(error):
if update is not None: 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) handle_rest(bot, update)
else: else:
if update is not None: if update is not None:
@ -194,6 +210,13 @@ def error(bot, update, error):
def main(): 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***") updater = Updater("***REMOVED***")
dp = updater.dispatcher dp = updater.dispatcher
@ -208,6 +231,7 @@ def main():
Filters.photo | Filters.video | Filters.video_note | Filters.audio | Filters.voice | Filters.document, Filters.photo | Filters.video | Filters.video_note | Filters.audio | Filters.voice | Filters.document,
handle_rest)) handle_rest))
dp.add_handler(MessageHandler(Filters.entity(MessageEntity.HASHTAG), handle_hashtag)) dp.add_handler(MessageHandler(Filters.entity(MessageEntity.HASHTAG), handle_hashtag))
dp.add_handler(MessageHandler(Filters.text, handle_text))
updater.start_polling() updater.start_polling()

View file

@ -2,3 +2,4 @@ python-telegram-bot
youtube-dl youtube-dl
requests requests
filetype filetype
markovify

View file

@ -12,9 +12,11 @@ cryptography==2.3.1 # via python-telegram-bot
filetype==1.0.1 filetype==1.0.1
future==0.16.0 # via python-telegram-bot future==0.16.0 # via python-telegram-bot
idna==2.7 # via cryptography, requests idna==2.7 # via cryptography, requests
markovify==0.7.1
pycparser==2.19 # via cffi pycparser==2.19 # via cffi
python-telegram-bot==11.1.0 python-telegram-bot==11.1.0
requests==2.19.1 requests==2.19.1
six==1.11.0 # via cryptography six==1.11.0 # via cryptography
unidecode==1.0.22 # via markovify
urllib3==1.23 # via requests urllib3==1.23 # via requests
youtube-dl==2018.9.26 youtube-dl==2018.9.26