From 2a5db0cb4f0b3fa1a655b8ea3b0e661d35c08323 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 23 Aug 2018 11:10:18 +0000 Subject: [PATCH] Ignore stuff that's unlikely to be morse code --- morse.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/morse.py b/morse.py index d85489d..0fb4b0c 100644 --- a/morse.py +++ b/morse.py @@ -127,15 +127,22 @@ class Morse: await room.send("Can't demorse nothing.", message.mid) return - matches = re.findall(self.MORSE_RE, text) - matches = [match.strip() for match in matches if match.strip()] + whole = re.fullmatch(self.MORSE_RE, text.strip()) + if whole: + translated = self.from_morse(whole.group(0)) + await room.send(translated, message.mid) + return - if not matches: + matches = re.findall(self.MORSE_RE, text) + matches = [match for match in matches + if match.strip(" /") not in {"", ".", "-", "..."}] + + translated = [self.from_morse(match) for match in matches] + + if not translated: await room.send("No morse code found.", message.mid) return - translated = [self.from_morse(match) for match in matches] - translated = [word for word in translated if len(word) > 1] await room.send("\n".join(translated), message.mid) @yaboli.command("morse")