diff --git a/jasper/app_utils.py b/jasper/app_utils.py index e6d4dfc35..355bbdc11 100644 --- a/jasper/app_utils.py +++ b/jasper/app_utils.py @@ -5,6 +5,18 @@ import re from pytz import timezone +NEGATIVE = ["no", "nope", "not now", "deny", "don\'t", "stop", "end", "n"] +POSITIVE = ["yes", "yeah", "yup", "ok(ay)?", "al(l\s)?right(y)?", + "(sounds\s)?good", "check", "cool", "confirm", + "affirm", "sure", "go", "y"] +CANCEL = ["never(\s)?mind", "cancel"] +REPEAT = ["repeat", "again", "what was that"] + +NEGATIVE = re.compile(r"\b(%s)\b" % "|".join(NEGATIVE), re.IGNORECASE) +POSITIVE = re.compile(r"\b(%s)\b" % "|".join(POSITIVE), re.IGNORECASE) +CANCEL = re.compile(r"\b(%s)\b" % "|".join(CANCEL), re.IGNORECASE) +REPEAT = re.compile(r"\b(%s)\b" % "|".join(REPEAT), re.IGNORECASE) + def send_email(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER): """Sends an HTML email.""" @@ -115,8 +127,7 @@ def is_negative(phrase): Arguments: phrase -- the input phrase to-be evaluated """ - return bool(re.search(r'\b(no(t)?|don\'t|stop|end|n)\b', phrase, - re.IGNORECASE)) + return check_regex(NEGATIVE, phrase) def is_positive(phrase): @@ -126,6 +137,39 @@ def is_positive(phrase): Arguments: phrase -- the input phrase to-be evaluated """ - return bool(re.search(r'\b(sure|yes|yeah|go|yup|y)\b', - phrase, - re.IGNORECASE)) + return check_regex(POSITIVE, phrase) + + +def is_cancel(phrase): + """ + Returns True if the input phrase has similar meaning to cancel. + + Arguments: + phrase -- the input phrase to-be evaluated + """ + return check_regex(CANCEL, phrase) + + +def is_repeat(phrase): + """ + Returns True if the input phrase has similar meaning to repeat. + + Arguments: + phrase -- the input phrase to-be evaluated + """ + return check_regex(REPEAT, phrase) + + +def check_regex(pattern, phrase): + """ + Returns True if the input phrase has matches the supplied regex + + Arguments: + phrase -- the input phrase to-be evaluated + pattern -- the regex pattern to search with + """ + if not phrase: + return False + if type(phrase) is list or type(phrase) is tuple: + phrase = " ".join(phrase) + return bool(pattern.search(phrase)) diff --git a/jasper/local_mic.py b/jasper/local_mic.py index 34fd13c0f..6e997651f 100644 --- a/jasper/local_mic.py +++ b/jasper/local_mic.py @@ -25,3 +25,13 @@ def listen(self): def say(self, phrase, OPTIONS=None): print("JASPER: %s" % phrase) + + def ask(self, question): + """ + Asks a questions and then returns the response + + Arguments: + question -- the question to ask + """ + self.say(question) + return self.active_listen() diff --git a/jasper/mic.py b/jasper/mic.py index fd9653cd1..fb9baedeb 100644 --- a/jasper/mic.py +++ b/jasper/mic.py @@ -214,12 +214,26 @@ def play_file(self, filename): add_padding=self._output_padding) def say(self, phrase): + if type(phrase) is list or type(phrase) is tuple: + for words in phrase: + self.say(words) + return altered_phrase = alteration.clean(phrase) with tempfile.SpooledTemporaryFile() as f: f.write(self.tts_engine.say(altered_phrase)) f.seek(0) self._output_device.play_fp(f) + def ask(self, question): + """ + Asks a questions and then returns the response + + Arguments: + question -- the question to ask + """ + self.say(question) + return self.active_listen() + if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) diff --git a/jasper/testutils.py b/jasper/testutils.py index 2bf022f84..3cb944f33 100644 --- a/jasper/testutils.py +++ b/jasper/testutils.py @@ -30,6 +30,16 @@ def active_listen(self, timeout=3): def say(self, phrase): self.outputs.append(phrase) + def ask(self, question): + """ + Asks a questions and then returns the response + + Arguments: + question -- the question to ask + """ + self.say(question) + return self.active_listen() + def get_plugin_instance(plugin_class, *extra_args): info = type('', (object,), {