Skip to content

Commit 3dcc3c3

Browse files
authored
Merge pull request ggeop#15 from ggeop/issue#12
Give multiple values to triggering words
2 parents bea3756 + da23677 commit 3dcc3c3

File tree

3 files changed

+63
-46
lines changed

3 files changed

+63
-46
lines changed

src/jarvis/jarvis/action_manager.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
class ActionManager:
1313

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

2932
@classmethod
3033
def _create_url(cls, domain):
@@ -40,12 +43,13 @@ def _create_url(cls, domain):
4043
return url
4144

4245
@classmethod
43-
def tell_the_weather(cls, words):
46+
def tell_the_weather(cls,triggered_word, words):
4447
"""
4548
Tells the weather of a place
46-
:param words: string (e.g weather in London)
49+
:param triggered_word: string (e.g 'weather')
50+
:param words: string (e.g 'weather in London')
4751
"""
48-
reg_ex = re.search('weather in (.*)', words)
52+
reg_ex = re.search(triggered_word + ' in ([a-zA-Z]+)', words)
4953
if reg_ex:
5054
city = reg_ex.group(1)
5155
owm = OWM(API_key=WEATHER_API['key'])
@@ -65,12 +69,13 @@ def tell_the_time(cls, *args):
6569
assistant_response('Current time is: {0}:{1}'.format(now.hour, now.minute))
6670

6771
@classmethod
68-
def tell_me_about(cls, words):
72+
def tell_me_about(cls, triggered_word, words):
6973
"""
7074
Tells about something by searching in wikipedia
71-
:param words: string (e.g about google)
75+
:param triggered_word: string (e.g 'about')
76+
:param words: string (e.g 'about google')
7277
"""
73-
reg_ex = re.search('about (.*)', words)
78+
reg_ex = re.search(triggered_word + ' ([a-zA-Z]+)', words)
7479
try:
7580
if reg_ex:
7681
topic = reg_ex.group(1)

src/jarvis/jarvis/command_manager.py

+41-27
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
class CommandManager:
1212
commands_dict = {
13-
TRIGGERING_WORDS['open_browser']: ActionManager.open_website_in_browser,
14-
TRIGGERING_WORDS['tell_time']: ActionManager.tell_the_time,
15-
TRIGGERING_WORDS['tell_about']: ActionManager.tell_me_about,
16-
TRIGGERING_WORDS['current_weather']: ActionManager.tell_the_weather,
13+
TRIGGERING_WORDS['open_browser']['command']: ActionManager.open_website_in_browser,
14+
TRIGGERING_WORDS['tell_time']['command']: ActionManager.tell_the_time,
15+
TRIGGERING_WORDS['tell_about']['command']: ActionManager.tell_me_about,
16+
TRIGGERING_WORDS['current_weather']['command']: ActionManager.tell_the_weather,
17+
TRIGGERING_WORDS['disable_jarvis']['command']: ActionManager.disable_jarvis,
1718
}
1819

1920
def __init__(self):
@@ -24,8 +25,6 @@ def __init__(self):
2425
@log
2526
def run(self):
2627
self.words = self._get_words()
27-
commands = self._get_commands()
28-
logging.debug('The {0} commands will be execute'.format(commands))
2928
self._execute_commands(commands)
3029

3130
def wake_up_check(self):
@@ -38,7 +37,10 @@ def wake_up_check(self):
3837
self.words = self.r.recognize_google(audio).lower()
3938
except sr.UnknownValueError:
4039
self.words = self._get_words()
41-
if TRIGGERING_WORDS['enable_jarvis'] in self.words:
40+
# Check if a word from the triggering list exist in user words
41+
triggering_words = [triggering_word for triggering_word in
42+
TRIGGERING_WORDS['enable_jarvis']['triggering_words']if triggering_word in self.words]
43+
if triggering_words:
4244
self._wake_up_response()
4345
return True
4446
else:
@@ -49,7 +51,10 @@ def shutdown_check(self):
4951
"""
5052
Checks if there is the shutdown word, and if exists the assistant service stops.
5153
"""
52-
if TRIGGERING_WORDS['disable_jarvis'] in self.words:
54+
# Check if a word from the triggering list exist in user words
55+
triggering_words = [triggering_word for triggering_word in
56+
TRIGGERING_WORDS['disable_jarvis']['triggering_words'] if triggering_word in self.words]
57+
if triggering_words:
5358
assistant_response('Bye bye Sir. Have a nice day')
5459
logging.debug('Application terminated gracefully.')
5560
sys.exit()
@@ -69,28 +74,37 @@ def _wake_up_response():
6974
assistant_response('Hello Sir. Good evening')
7075
assistant_response('What do you want to do for you sir?')
7176

72-
def _get_commands(self):
77+
def _execute_commands(self):
7378
"""
74-
Retrieve all the commands from the user text (free text --> commands).
75-
:return:
76-
"""
77-
words = self.words.split()
78-
commands_set = set(self.commands_dict.keys())
79-
words_set = set(words)
80-
return commands_set.intersection(words_set)
79+
Execute user commands. Checks one-by-one all the triggering _get_words
80+
and if a triggering word exist in user words then it executes the
81+
corresponding command.
82+
e.x.
83+
self.words ='open youtube and tell me the time'
84+
words =['open', 'youtube', 'and', 'tell', 'me', 'the', 'time']
85+
The application runs the following:
86+
execute--> open_website_in_browser('open', self.words)
87+
execute--> tell_the_time('time', self.words)
88+
89+
NOTE: If the same triggering command exists more than once the Application
90+
execute the command once.
91+
e.x.
92+
words =['open', 'youtube', 'and', 'open', 'netflix']
93+
The application will run only once the open_website_in_browser.
94+
execute--> open_website_in_browser('open', self.words)
8195
82-
@log
83-
def _execute_commands(self, commands):
84-
"""
85-
Execute iteratively all the commands in the input dict.
86-
:param commands: set (e.g {'open', 'search'})
8796
"""
88-
if bool(commands):
89-
for command in commands:
90-
logging.debug('Execute the command {0}'.format(command))
91-
self.commands_dict[command](self.words)
92-
else:
93-
assistant_response('Sorry, no commands to execute')
97+
words = self.words.split()
98+
for triggering_words in TRIGGERING_WORDS.values():
99+
for triggering_word in triggering_words['triggering_words']:
100+
if triggering_word in words:
101+
command = triggering_words['command']
102+
exist_command = self.commands_dict.get(command)
103+
if exist_command:
104+
logging.debug('Execute the command {0}'.format(command))
105+
self.commands_dict[command](triggering_word, self.words)
106+
else:
107+
logging.debug('Not command {0} to execute'.format(command))
94108

95109
def _get_words(self):
96110
"""

src/jarvis/jarvis/settings.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@
3838

3939
# Trigger words
4040
TRIGGERING_WORDS = {
41-
'enable_jarvis': 'start',
42-
'disable_jarvis': 'stop',
43-
'open_browser': 'open',
44-
'sent_email': 'email',
45-
'launch_application': 'launch',
46-
'tell_time': 'time',
47-
'tell_about': 'about',
48-
'current_weather': 'weather'
41+
'enable_jarvis': {'command': 'enable_jarvis', 'triggering_words': ['start', 'hi', 'jarvis']},
42+
'disable_jarvis': {'command': 'disable_jarvis', 'triggering_words': ['stop', 'shut down']},
43+
'open_browser': {'command': 'open_browser', 'triggering_words': ['open', 'do']},
44+
'tell_time': {'command': 'tell_time', 'triggering_words': ['time']},
45+
'tell_about': {'command': 'tell_about', 'triggering_words': ['about']},
46+
'current_weather': {'command': 'current_weather', 'triggering_words': ['weather']},
4947
}
5048

5149
# Google API Speech recognition settings

0 commit comments

Comments
 (0)