From a1d3db61140b397e30bd1e1d2142ef1467019eab Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sat, 6 Sep 2025 19:09:55 -0700 Subject: a fancy new matching method defines commands with arbitrary suffixes Force-Push: yeah Change-Id: Ibe3a058173cded6355fadd413349cf9cc8823fdd --- src/pollyanna/arbitrary.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/pollyanna/arbitrary.py (limited to 'src/pollyanna/arbitrary.py') diff --git a/src/pollyanna/arbitrary.py b/src/pollyanna/arbitrary.py new file mode 100644 index 0000000..fbaba4c --- /dev/null +++ b/src/pollyanna/arbitrary.py @@ -0,0 +1,48 @@ +from more_itertools import peekable +import re + + +class ArbitraryParser: + """Matches command strings with %s in them denoting arbitrary phrases""" + + def __init__(self): + self.all_words = [] + self.all_words.append("amazing") + self.all_words.append("this") + self.all_words.append("is") + self.all_words.append("a") + self.all_words.append("test") + self.all_words.append("of") + self.all_words.append("pollyana") + self.all_words.append("voice") + self.all_words.append("typing") + self.all_words.append("fuck") + self.all_words.append("yeah") + + def match(self, command, text_line): + """ + Determine whether a line of input text matches a command that has %s + in it. Commands without %s may be passed, and will never match. + + Returns None if no match, or a list of the words that matched the %s + if one was found. + + Only a single %s is supported and it must be at the end of the + command. + """ + + command_words = re.split(r'[,\s-]+', command.strip()) + text_words = re.split(r'[,\s-]+', text_line.strip()) + + text_iter = peekable(text_words) + for word in command_words: + is_text_end = text_iter.peek(default=None) == None + if word == '%s': + if not is_text_end: + return list(text_iter) + else: + return None + elif is_text_end or word != next(text_iter): + return None + + return None -- cgit 1.4.1