basic splitting

master
Tomáš Mládek 2021-09-14 16:06:10 +02:00
parent 6b7949e1a1
commit 0d6914d14d
3 changed files with 47 additions and 37 deletions

View File

@ -2,6 +2,7 @@
import errno import errno
import logging import logging
from markov import MarkovBlabberer
import os import os
import pprint import pprint
import re import re
@ -19,6 +20,7 @@ from random import random
from sqlite3.dbapi2 import Connection from sqlite3.dbapi2 import Connection
from time import sleep from time import sleep
from typing import Any, List, Optional, Tuple, cast from typing import Any, List, Optional, Tuple, cast
from util import datestr, mkdir_p
import acoustid import acoustid
import filetype import filetype
@ -32,26 +34,11 @@ from _typeshed import StrPath
from markovify.text import Text from markovify.text import Text
from mutagen import File, FileType from mutagen import File, FileType
from mutagen.easyid3 import EasyID3 from mutagen.easyid3 import EasyID3
from requests.api import options
from telegram.ext import CommandHandler, MessageHandler, Updater from telegram.ext import CommandHandler, MessageHandler, Updater
from youtube_dl import DownloadError from youtube_dl import DownloadError
from youtube_dl.version import __version__ as YTDL_VERSION from youtube_dl.version import __version__ as YTDL_VERSION
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def datestr(date: datetime):
return date.strftime("%Y-%m-%d@%H%M")
class DelojzaDB: class DelojzaDB:
def __init__(self, db_path): def __init__(self, db_path):
self.db_path = db_path self.db_path = db_path
@ -115,28 +102,6 @@ class DelojzaDB:
) )
self.db.commit() self.db.commit()
class MarkovBlabberer:
def __init__(self, filepath: StrPath):
self.logger = logging.getLogger("markov")
self.filepath = filepath
with open(filepath) as f:
text = f.read()
self.markov: Text = markovify.NewlineText(text.lower())
self.logger.info("Sentence of the day: " + self.make_sentence())
def make_sentence(self, tries: int = 100):
return self.markov.make_sentence(tries=tries) or "???"
def add_to_corpus(self, text: str):
text = text.lower()
new_sentence = markovify.NewlineText(text)
self.markov = cast(Text, markovify.combine([self.markov, new_sentence]))
with open(self.filepath, "a") as f:
f.write(text + "\n")
class DelojzaBot: class DelojzaBot:
def __init__( def __init__(
self, self,

27
markov.py Normal file
View File

@ -0,0 +1,27 @@
import logging
from typing import cast
import markovify
from _typeshed import StrPath
from markovify import Text
class MarkovBlabberer:
def __init__(self, filepath: StrPath):
self.logger = logging.getLogger("markov")
self.filepath = filepath
with open(filepath) as f:
text = f.read()
self.markov: Text = markovify.NewlineText(text.lower())
self.logger.info("Sentence of the day: " + self.make_sentence())
def make_sentence(self, tries: int = 100):
return self.markov.make_sentence(tries=tries) or "???"
def add_to_corpus(self, text: str):
text = text.lower()
new_sentence = markovify.NewlineText(text)
self.markov = cast(Text, markovify.combine([self.markov, new_sentence]))
with open(self.filepath, "a") as f:
f.write(text + "\n")

18
util.py Normal file
View File

@ -0,0 +1,18 @@
from _typeshed import StrPath
from datetime import datetime
import os
import errno
def mkdir_p(path: StrPath) -> None:
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def datestr(date: datetime) -> str:
return date.strftime("%Y-%m-%d@%H%M")