Skip to content

Commit

Permalink
Refactor the execute_commands for multiple commands
Browse files Browse the repository at this point in the history
  • Loading branch information
George Papa committed May 22, 2019
1 parent 758d7e1 commit 7e0b706
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
36 changes: 21 additions & 15 deletions src/jarvis/jarvis/action_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +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.
"""
for triggering_command in TRIGGERING_WORDS['open_browser']['triggering_words']:
reg_ex = re.search(triggering_command + ' ([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)
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)

@classmethod
def _create_url(cls, domain):
Expand All @@ -39,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 @@ -64,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
51 changes: 28 additions & 23 deletions src/jarvis/jarvis/command_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CommandManager:
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 Down Expand Up @@ -75,31 +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:
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)
"""
commands = set()
words = self.words.split()
for triggering_words in TRIGGERING_WORDS.values():
common_words = set(triggering_words['triggering_words']).intersection(set(words))
if bool(common_words):
commands.add(triggering_words['command'])
return commands

@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')
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

0 comments on commit 7e0b706

Please sign in to comment.