From 3c640fc4f1d19ac34cf871eb32369b90d9381a8c Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Fri, 5 Feb 2016 14:20:56 +0100 Subject: [PATCH 1/1] Minor improvements to markov generator. --- plomlombot.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/plomlombot.py b/plomlombot.py index ed02792..7bf2208 100755 --- a/plomlombot.py +++ b/plomlombot.py @@ -190,16 +190,23 @@ def handle_command(command, argument, notice, target, session): if not os.access(markovfeed_name, os.F_OK): notice("NOT ENOUGH TEXT TO MARKOV.") return + + # Lowercase incoming lines, ensure they end in a sentence end mark. file = open(markovfeed_name, "r") lines = file.readlines() file.close() tokens = [] for line in lines: - line = line.replace("\n", "").lower() + line = line.lower().replace("\n", "") + if line[-1] not in ".!?": + line += "." tokens += line.split() if len(tokens) <= select_length: notice("NOT ENOUGH TEXT TO MARKOV.") return + + # Replace URLs with escape string for now, so that the Markov selector + # won't see them as different strings. Stash replaced URLs in urls. urls = [] url_escape = "\nURL" url_starts = ["http://", "https://", " 200: break @@ -236,12 +247,16 @@ def handle_command(command, argument, notice, target, session): for i in range(select_length - 1): snippet[i] = snippet[i + 1] snippet[select_length - 1] = new_end + + # Replace occurences of url escape string with random choice from urls. while True: index = msg.find(url_escape) if index < 0: break msg = msg.replace(url_escape, choice(urls), 1) - notice(msg + "malkovich.") + + # More meaningful ways to randomly end sentences. + notice(msg + malkovich + ".") def twt(): def try_open(mode): -- 2.30.2