From 1da79e6fc04833aef97b981f1d3f7e72d3e0ea2b Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 13 Mar 2017 19:13:42 +0100 Subject: [PATCH] Added a markov command --- basicbot.py | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/basicbot.py b/basicbot.py index 3889ee4b..a540de69 100644 --- a/basicbot.py +++ b/basicbot.py @@ -49,34 +49,50 @@ async def leggi(bot, update, arguments): """Leggi una frase dal diario Royal Games. Puoi visualizzare il diario [qui](https://royal.steffo.me/diario.htm), leggere una frase casuale scrivendo `/leggi random` o leggere una frase specifica scrivendo `/leggi `. -Puoi anche generare una frase usando catene di markov scrivendo `/leggi markov`. -Sintassi: `/leggi `""" +Sintassi: `/leggi `""" if len(arguments) == 0 or len(arguments) > 1: await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/leggi `", parse_mode="Markdown") return - file = open("diario.txt", "r", encoding="utf8") - string = file.read() + file = open("diario.txt", "r") + entries = file.read().split("\n") file.close() - if arguments[0] == "markov": - generator = markovify.NewlineText(string) - line = None - while line is None: - line = generator.make_sentence() - entry_number = "???" + if arguments[0] == "random": + entry_number = random.randrange(len(entries)) else: - entries = string.split("\n") - if arguments[0] == "random": - entry_number = random.randrange(len(entries)) - else: - entry_number = arguments[0] - line = entries[entry_number] - entry = line.split("|", 1) - date = datetime.datetime.fromtimestamp(int(entry[0])).isoformat() + entry_number = arguments[0] + entry = entries[entry_number].split("|", 1) + date = datetime.datetime.fromtimestamp(entry[0]).isoformat() text = entry[1] await update.message.reply(bot, f"Frase #{entry_number} | {date}\n{text}", parse_mode="Markdown") +async def markov(bot, update, arguments): + """Genera una frase del diario utilizzando le catene di Markov. + +Puoi specificare con che parola deve iniziare la frase generata. + +Sintassi: `/markov [inizio]`""" + file = open("diario.txt", "r", encoding="utf8") + # Clean the diario + clean_diario = str() + # Remove the timestamps in each row + for row in file: + clean_diario += row.split("|", 1)[1] + # The text is split by newlines + generator = markovify.NewlineText(clean_diario) + file.close() + if len(arguments) == 0: + # Generate a sentence with a random start + # TODO: the generator sometimes returns None? + text = generator.make_sentence() + else: + # Generate a sentence with a specific start + start = " ".join(arguments) + text = generator.make_sentence_with_start(start) + await update.message.reply(bot, f"*Frase generata:*\n{text}", parse_mode="Markdown", tries=50) + + async def help(bot, update, arguments): """Visualizza la descrizione di un comando. @@ -179,5 +195,6 @@ if __name__ == "__main__": b.commands["sync"] = sync b.commands["changepassword"] = changepassword b.commands["help"] = help + b.commands["markov"] = markov print("Bot started!") b.run() \ No newline at end of file