From 93d4a8745c60daec1d8c4d8f42fb5383a9b5b586 Mon Sep 17 00:00:00 2001 From: Dan Buchta Date: Wed, 18 May 2016 20:09:13 -0500 Subject: [PATCH 1/5] Added additional utility functions --- jasper/app_utils.py | 34 +++++++++++++++++++++++++++++----- jasper/local_mic.py | 4 ++++ jasper/mic.py | 8 ++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/jasper/app_utils.py b/jasper/app_utils.py index e6d4dfc35..08ab806d4 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,19 @@ 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): + return check_regex(CANCEL, phrase) + +def is_repeat(phrase): + return check_regex(REPEAT, phrase) + + +def check_regex(pattern, phrase): + if not phrase: + return False + if isinstance(phrase, list): + phrase = " ".join(phrase) + return bool(pattern.search(phrase)) diff --git a/jasper/local_mic.py b/jasper/local_mic.py index 34fd13c0f..03dc482e9 100644 --- a/jasper/local_mic.py +++ b/jasper/local_mic.py @@ -25,3 +25,7 @@ def listen(self): def say(self, phrase, OPTIONS=None): print("JASPER: %s" % phrase) + + def ask(self, question): + self.say(question) + return self.active_listen() diff --git a/jasper/mic.py b/jasper/mic.py index fd9653cd1..7139e1f14 100644 --- a/jasper/mic.py +++ b/jasper/mic.py @@ -214,12 +214,20 @@ def play_file(self, filename): add_padding=self._output_padding) def say(self, phrase): + if isinstance(phrase, list): + 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): + self.say(question) + return self.active_listen() + if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) From 7ed5a1903cb7351ca1d6a35d73322d3142235cdc Mon Sep 17 00:00:00 2001 From: Dan Buchta Date: Wed, 18 May 2016 20:23:40 -0500 Subject: [PATCH 2/5] Fixed indentation formatting --- jasper/app_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jasper/app_utils.py b/jasper/app_utils.py index 08ab806d4..33dd7d86d 100644 --- a/jasper/app_utils.py +++ b/jasper/app_utils.py @@ -7,8 +7,8 @@ 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"] + "(sounds\s)?good", "check", "cool", "confirm", + "affirm", "sure", "go", "y"] CANCEL = ["never(\s)?mind", "cancel"] REPEAT = ["repeat", "again", "what was that"] @@ -143,6 +143,7 @@ def is_positive(phrase): def is_cancel(phrase): return check_regex(CANCEL, phrase) + def is_repeat(phrase): return check_regex(REPEAT, phrase) From 9ef0d2299c986ca4968a7b197fe7176646f142d1 Mon Sep 17 00:00:00 2001 From: Dan Buchta Date: Thu, 19 May 2016 18:17:46 -0500 Subject: [PATCH 3/5] Added additional documentation. --- jasper/app_utils.py | 19 +++++++++++++++++++ jasper/local_mic.py | 6 ++++++ jasper/mic.py | 6 ++++++ 3 files changed, 31 insertions(+) diff --git a/jasper/app_utils.py b/jasper/app_utils.py index 33dd7d86d..224bdb507 100644 --- a/jasper/app_utils.py +++ b/jasper/app_utils.py @@ -141,14 +141,33 @@ def is_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 isinstance(phrase, list): diff --git a/jasper/local_mic.py b/jasper/local_mic.py index 03dc482e9..6e997651f 100644 --- a/jasper/local_mic.py +++ b/jasper/local_mic.py @@ -27,5 +27,11 @@ 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 7139e1f14..86ef274fd 100644 --- a/jasper/mic.py +++ b/jasper/mic.py @@ -225,6 +225,12 @@ def say(self, phrase): 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() From 00b5c502cb5049895d90fac4dd4ad930e48a8466 Mon Sep 17 00:00:00 2001 From: Dan Buchta Date: Thu, 19 May 2016 20:42:07 -0500 Subject: [PATCH 4/5] Added checking for a tuple along with list --- jasper/app_utils.py | 2 +- jasper/mic.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jasper/app_utils.py b/jasper/app_utils.py index 224bdb507..355bbdc11 100644 --- a/jasper/app_utils.py +++ b/jasper/app_utils.py @@ -170,6 +170,6 @@ def check_regex(pattern, phrase): """ if not phrase: return False - if isinstance(phrase, list): + if type(phrase) is list or type(phrase) is tuple: phrase = " ".join(phrase) return bool(pattern.search(phrase)) diff --git a/jasper/mic.py b/jasper/mic.py index 86ef274fd..fb9baedeb 100644 --- a/jasper/mic.py +++ b/jasper/mic.py @@ -214,7 +214,7 @@ def play_file(self, filename): add_padding=self._output_padding) def say(self, phrase): - if isinstance(phrase, list): + if type(phrase) is list or type(phrase) is tuple: for words in phrase: self.say(words) return From 2d70a3bd9147c8a116083f25fff4c6d1621573b8 Mon Sep 17 00:00:00 2001 From: Dan Buchta Date: Fri, 20 May 2016 19:58:26 -0500 Subject: [PATCH 5/5] Added method to TestMic --- jasper/testutils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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,), {