10
10
11
11
class CommandManager :
12
12
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 ,
17
18
}
18
19
19
20
def __init__ (self ):
@@ -24,8 +25,6 @@ def __init__(self):
24
25
@log
25
26
def run (self ):
26
27
self .words = self ._get_words ()
27
- commands = self ._get_commands ()
28
- logging .debug ('The {0} commands will be execute' .format (commands ))
29
28
self ._execute_commands (commands )
30
29
31
30
def wake_up_check (self ):
@@ -38,7 +37,10 @@ def wake_up_check(self):
38
37
self .words = self .r .recognize_google (audio ).lower ()
39
38
except sr .UnknownValueError :
40
39
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 :
42
44
self ._wake_up_response ()
43
45
return True
44
46
else :
@@ -49,7 +51,10 @@ def shutdown_check(self):
49
51
"""
50
52
Checks if there is the shutdown word, and if exists the assistant service stops.
51
53
"""
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 :
53
58
assistant_response ('Bye bye Sir. Have a nice day' )
54
59
logging .debug ('Application terminated gracefully.' )
55
60
sys .exit ()
@@ -69,28 +74,37 @@ def _wake_up_response():
69
74
assistant_response ('Hello Sir. Good evening' )
70
75
assistant_response ('What do you want to do for you sir?' )
71
76
72
- def _get_commands (self ):
77
+ def _execute_commands (self ):
73
78
"""
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)
81
95
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'})
87
96
"""
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 ))
94
108
95
109
def _get_words (self ):
96
110
"""
0 commit comments