Skip to content

Commit

Permalink
Give multiple values to triggering words
Browse files Browse the repository at this point in the history
  • Loading branch information
George Papa committed May 22, 2019
1 parent bea3756 commit da23677
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 46 deletions.
27 changes: 16 additions & 11 deletions src/jarvis/jarvis/action_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
class ActionManager:

@classmethod
def open_website_in_browser(cls, words):
def open_website_in_browser(cls, triggered_word, words):
"""
Opens a web page in the browser.
:param words: string (e.g Open the site youtube)
:param triggered_word: string (e.g 'open')
:param words: string (e.g 'open youtube')
NOTE: If in the words there are more than one commands_dict
e.g words='open youtube and open netflix' the application will find
and execute only the first one, in our case will open the youtube.
"""
reg_ex = re.search(TRIGGERING_WORDS['open_browser'] + ' ([a-zA-Z]+)', words)
reg_ex = re.search(triggered_word + ' ([a-zA-Z]+)', words)
if reg_ex:
domain = reg_ex.group(1)
assistant_response('Yes sir, I will open the {0}'.format(domain))
url = cls._create_url(domain)
subprocess.Popen(["python", "-m", "webbrowser", "-t", url], stdout=subprocess.PIPE)
else:
pass

@classmethod
def _create_url(cls, domain):
Expand All @@ -40,12 +43,13 @@ def _create_url(cls, domain):
return url

@classmethod
def tell_the_weather(cls, words):
def tell_the_weather(cls,triggered_word, words):
"""
Tells the weather of a place
:param words: string (e.g weather in London)
:param triggered_word: string (e.g 'weather')
:param words: string (e.g 'weather in London')
"""
reg_ex = re.search('weather in (.*)', words)
reg_ex = re.search(triggered_word + ' in ([a-zA-Z]+)', words)
if reg_ex:
city = reg_ex.group(1)
owm = OWM(API_key=WEATHER_API['key'])
Expand All @@ -65,12 +69,13 @@ def tell_the_time(cls, *args):
assistant_response('Current time is: {0}:{1}'.format(now.hour, now.minute))

@classmethod
def tell_me_about(cls, words):
def tell_me_about(cls, triggered_word, words):
"""
Tells about something by searching in wikipedia
:param words: string (e.g about google)
:param triggered_word: string (e.g 'about')
:param words: string (e.g 'about google')
"""
reg_ex = re.search('about (.*)', words)
reg_ex = re.search(triggered_word + ' ([a-zA-Z]+)', words)
try:
if reg_ex:
topic = reg_ex.group(1)
Expand Down
68 changes: 41 additions & 27 deletions src/jarvis/jarvis/command_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

class CommandManager:
commands_dict = {
TRIGGERING_WORDS['open_browser']: ActionManager.open_website_in_browser,
TRIGGERING_WORDS['tell_time']: ActionManager.tell_the_time,
TRIGGERING_WORDS['tell_about']: ActionManager.tell_me_about,
TRIGGERING_WORDS['current_weather']: ActionManager.tell_the_weather,
TRIGGERING_WORDS['open_browser']['command']: ActionManager.open_website_in_browser,
TRIGGERING_WORDS['tell_time']['command']: ActionManager.tell_the_time,
TRIGGERING_WORDS['tell_about']['command']: ActionManager.tell_me_about,
TRIGGERING_WORDS['current_weather']['command']: ActionManager.tell_the_weather,
TRIGGERING_WORDS['disable_jarvis']['command']: ActionManager.disable_jarvis,
}

def __init__(self):
Expand All @@ -24,8 +25,6 @@ def __init__(self):
@log
def run(self):
self.words = self._get_words()
commands = self._get_commands()
logging.debug('The {0} commands will be execute'.format(commands))
self._execute_commands(commands)

def wake_up_check(self):
Expand All @@ -38,7 +37,10 @@ def wake_up_check(self):
self.words = self.r.recognize_google(audio).lower()
except sr.UnknownValueError:
self.words = self._get_words()
if TRIGGERING_WORDS['enable_jarvis'] in self.words:
# Check if a word from the triggering list exist in user words
triggering_words = [triggering_word for triggering_word in
TRIGGERING_WORDS['enable_jarvis']['triggering_words']if triggering_word in self.words]
if triggering_words:
self._wake_up_response()
return True
else:
Expand All @@ -49,7 +51,10 @@ def shutdown_check(self):
"""
Checks if there is the shutdown word, and if exists the assistant service stops.
"""
if TRIGGERING_WORDS['disable_jarvis'] in self.words:
# Check if a word from the triggering list exist in user words
triggering_words = [triggering_word for triggering_word in
TRIGGERING_WORDS['disable_jarvis']['triggering_words'] if triggering_word in self.words]
if triggering_words:
assistant_response('Bye bye Sir. Have a nice day')
logging.debug('Application terminated gracefully.')
sys.exit()
Expand All @@ -69,28 +74,37 @@ def _wake_up_response():
assistant_response('Hello Sir. Good evening')
assistant_response('What do you want to do for you sir?')

def _get_commands(self):
def _execute_commands(self):
"""
Retrieve all the commands from the user text (free text --> commands).
:return:
"""
words = self.words.split()
commands_set = set(self.commands_dict.keys())
words_set = set(words)
return commands_set.intersection(words_set)
Execute user commands. Checks one-by-one all the triggering _get_words
and if a triggering word exist in user words then it executes the
corresponding command.
e.x.
self.words ='open youtube and tell me the time'
words =['open', 'youtube', 'and', 'tell', 'me', 'the', 'time']
The application runs the following:
execute--> open_website_in_browser('open', self.words)
execute--> tell_the_time('time', self.words)
NOTE: If the same triggering command exists more than once the Application
execute the command once.
e.x.
words =['open', 'youtube', 'and', 'open', 'netflix']
The application will run only once the open_website_in_browser.
execute--> open_website_in_browser('open', self.words)
@log
def _execute_commands(self, commands):
"""
Execute iteratively all the commands in the input dict.
:param commands: set (e.g {'open', 'search'})
"""
if bool(commands):
for command in commands:
logging.debug('Execute the command {0}'.format(command))
self.commands_dict[command](self.words)
else:
assistant_response('Sorry, no commands to execute')
words = self.words.split()
for triggering_words in TRIGGERING_WORDS.values():
for triggering_word in triggering_words['triggering_words']:
if triggering_word in words:
command = triggering_words['command']
exist_command = self.commands_dict.get(command)
if exist_command:
logging.debug('Execute the command {0}'.format(command))
self.commands_dict[command](triggering_word, self.words)
else:
logging.debug('Not command {0} to execute'.format(command))

def _get_words(self):
"""
Expand Down
14 changes: 6 additions & 8 deletions src/jarvis/jarvis/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@

# Trigger words
TRIGGERING_WORDS = {
'enable_jarvis': 'start',
'disable_jarvis': 'stop',
'open_browser': 'open',
'sent_email': 'email',
'launch_application': 'launch',
'tell_time': 'time',
'tell_about': 'about',
'current_weather': 'weather'
'enable_jarvis': {'command': 'enable_jarvis', 'triggering_words': ['start', 'hi', 'jarvis']},
'disable_jarvis': {'command': 'disable_jarvis', 'triggering_words': ['stop', 'shut down']},
'open_browser': {'command': 'open_browser', 'triggering_words': ['open', 'do']},
'tell_time': {'command': 'tell_time', 'triggering_words': ['time']},
'tell_about': {'command': 'tell_about', 'triggering_words': ['about']},
'current_weather': {'command': 'current_weather', 'triggering_words': ['weather']},
}

# Google API Speech recognition settings
Expand Down

0 comments on commit da23677

Please sign in to comment.