Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional utility functions #519

Open
wants to merge 5 commits into
base: jasper-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions jasper/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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):
Expand All @@ -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))
10 changes: 10 additions & 0 deletions jasper/local_mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
14 changes: 14 additions & 0 deletions jasper/mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions jasper/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,), {
Expand Down