-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_parser.py
More file actions
179 lines (149 loc) · 6.6 KB
/
Copy pathcommand_parser.py
File metadata and controls
179 lines (149 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import spacy
# Load the spaCy model
nlp = spacy.load("en_core_web_sm")
applications = {
'settings': 'ms-settings:',
'whatsapp': 'C:\\Program Files\\WindowsApps\\Microsoft.WhatsApp_...',
'notepad': 'notepad.exe',
'calculator': 'calc.exe',
'word': 'winword.exe'
}
def parse_command(command):
"""
Parses the user command to determine the action to perform.
"""
try:
# Lowercase and process the command
doc = nlp(command.lower())
# Check for brightness command
if 'brightness' in command:
for ent in doc.ents:
if ent.label_ == 'PERCENT':
try:
brightness = int(ent.text.replace('%', ''))
return 'set_brightness', brightness
except ValueError:
return 'unknown_command', "Invalid brightness value."
command = command.lower()
# Check for opening settings
if 'open settings' in command:
return 'open_settings', None
if 'windows security' in command:
return 'open_windows_security',None
if 'app' in command:
for app_name in applications:
if app_name in command:
return 'open_application', app_name
# Check for closing settings
if 'close settings' in command:
return 'close', 'settings'
# Check for creating a folder
if 'create folder' in command:
try:
folder_name = command.split('create folder', 1)[1].strip()
return 'create_folder', folder_name
except IndexError:
return 'unknown_command', "Folder name not provided."
# Check for renaming a folder
if 'rename folder' in command:
try:
parts = command.split('rename folder', 1)[1].split('to', 1)
if len(parts) == 2:
old_name = parts[0].strip()
new_name = parts[1].strip()
return 'rename_folder', (old_name, new_name)
else:
return 'unknown_command', "Invalid rename format."
except IndexError:
return 'unknown_command', "Folder names not provided."
# Check for deleting a file or folder
if 'delete' in command:
try:
item_name = command.split('delete', 1)[1].strip()
return 'delete_file_or_folder', item_name
except IndexError:
return 'unknown_command', "Item name not provided."
# Check for opening a file
if 'open' in command and ('my pc' in command or 'this pc' in command):
return 'open', 'my pc'
# Check for opening a specific drive
if 'open' in command and any(drive in command for drive in ['c:', 'd:', 'e:', 'f:', 'a:', 'b:']):
for drive in ['c:', 'd:', 'e:', 'f:', 'a:', 'b:']:
if drive in command:
return 'open', drive.upper()
# Check for opening a folder or file by name
if 'open' in command:
path = command.split('open', 1)[1].strip()
return 'open', path
# Check for searching a file
if 'search file' in command:
try:
file_name = command.split('search file', 1)[1].strip()
return 'search_file', file_name
except IndexError:
return 'unknown_command', "File name not provided."
# Check for closing an application
if 'close application' in command:
try:
app_name = command.split('close application', 1)[1].strip()
return 'close_application', app_name
except IndexError:
return 'unknown_command', "Application name not provided."
# Check for 'close' command
if 'close' in command:
if 'file' in command:
try:
# Extract file path to close
file_path = command.split('close file', 1)[1].strip()
return 'close_file', file_path
except IndexError:
return 'unknown_command', "File path not provided."
elif 'settings' in command:
return 'close', 'settings'
elif 'my pc' in command:
return 'close', 'my pc'
else:
# Extract the target of the 'close' command
try:
target = command.split('close', 1)[1].strip()
# Check if the target is a disk drive
if target in ['c:', 'd:', 'e:', 'f:']:
return 'close', target.upper()
# If not a disk drive, return the target as is
return 'close', target
except IndexError:
# In case no specific target is provided
return 'close', None
# Check for opening the task manager
if 'task manager' in command:
return 'open_task_manager', None
# Check for taking a screenshot
if 'take screenshot' in command:
try:
file_name = command.split('take screenshot', 1)[1].strip()
return 'take_screenshot', file_name
except IndexError:
return 'unknown_command', "File name for screenshot not provided."
# Check for search bar, refresh, maximize, and minimize commands
# Check for search command
if 'search' in command:
try:
# Extract the search query after 'search'
search_query = command.split('search', 1)[1].strip()
return 'search', search_query
except IndexError:
return 'unknown_command', "Search query not provided."
if 'refresh' in command:
return 'refresh', None
if 'maximize' in command:
return 'maximize', None
if 'minimize' in command:
return 'minimize', None
if 'wordcontent' in command:
return 'word_content','Ferrari'
if 'create google account' in command:
return 'create_google_account',None
except Exception as e:
return 'unknown_command', f"Error parsing command: {e}"
# Return unknown command if no matches are found
return 'unknown_command', "Command not recognized."