-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_parser.py
More file actions
688 lines (587 loc) · 23.5 KB
/
command_parser.py
File metadata and controls
688 lines (587 loc) · 23.5 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import re
import subprocess
import os
import platform
import psutil
import webbrowser
from rapidfuzz import fuzz
from datetime import datetime
class CommandParser:
def __init__(self):
self.system = platform.system()
# Application paths for Windows
self.app_paths = {
'vscode': r'C:\Users\{}\AppData\Local\Programs\Microsoft VS Code\Code.exe',
'notepad': 'notepad.exe',
'calculator': 'calc.exe',
'paint': 'mspaint.exe',
'chrome': r'C:\Program Files\Google\Chrome\Application\chrome.exe',
'firefox': r'C:\Program Files\Mozilla Firefox\firefox.exe',
'edge': r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe',
'explorer': 'explorer.exe',
'cmd': 'cmd.exe',
'powershell': 'powershell.exe',
'word': r'C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE',
'excel': r'C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE',
}
# Command templates
self.commands = {
# File Operations
'list_files': {
'patterns': ['list files', 'show files', 'display files', 'ls', 'dir', 'what files'],
'execute': self.list_files
},
'create_file': {
'patterns': ['create file', 'make file', 'new file', 'touch'],
'execute': self.create_file
},
'delete_file': {
'patterns': ['delete file', 'remove file', 'delete', 'rm'],
'execute': self.delete_file
},
'read_file': {
'patterns': ['read file', 'show file', 'cat', 'open file', 'display file'],
'execute': self.read_file
},
'copy_file': {
'patterns': ['copy file', 'duplicate file', 'cp'],
'execute': self.copy_file
},
'move_file': {
'patterns': ['move file', 'mv', 'relocate file'],
'execute': self.move_file
},
'rename_file': {
'patterns': ['rename file', 'change name'],
'execute': self.rename_file
},
# Directory Operations
'change_directory': {
'patterns': ['go to', 'change directory', 'cd', 'open folder', 'navigate to'],
'execute': self.change_directory
},
'current_directory': {
'patterns': ['where am i', 'current directory', 'pwd', 'current location', 'current folder'],
'execute': self.current_directory
},
'create_directory': {
'patterns': ['create folder', 'make directory', 'new folder', 'mkdir', 'create directory'],
'execute': self.create_directory
},
'list_directory_tree': {
'patterns': ['show tree', 'directory tree', 'folder structure', 'tree'],
'execute': self.list_directory_tree
},
# Application Launch
'open_application': {
'patterns': ['open', 'launch', 'start', 'run application', 'open app'],
'execute': self.open_application
},
'close_application': {
'patterns': ['close', 'kill', 'stop', 'terminate'],
'execute': self.close_application
},
# System Operations
'system_info': {
'patterns': ['system info', 'system information', 'pc info', 'computer info'],
'execute': self.system_info
},
'disk_usage': {
'patterns': ['disk space', 'storage', 'disk usage', 'free space'],
'execute': self.disk_usage
},
'memory_usage': {
'patterns': ['memory', 'ram usage', 'memory usage'],
'execute': self.memory_usage
},
'cpu_usage': {
'patterns': ['cpu', 'processor', 'cpu usage'],
'execute': self.cpu_usage
},
'running_processes': {
'patterns': ['processes', 'running programs', 'task list', 'show processes'],
'execute': self.running_processes
},
# File Execution
'execute_file': {
'patterns': ['execute', 'run', 'run file', 'execute file', 'start file'],
'execute': self.execute_file
},
# Web Operations
'open_website': {
'patterns': ['open website', 'browse', 'go to website', 'open url'],
'execute': self.open_website
},
'search_web': {
'patterns': ['search', 'google', 'search for', 'look up'],
'execute': self.search_web
},
# Time & Date
'get_time': {
'patterns': ['time', 'what time', 'current time'],
'execute': self.get_time
},
'get_date': {
'patterns': ['date', 'what date', 'today'],
'execute': self.get_date
},
# Power Operations
'shutdown': {
'patterns': ['shutdown', 'power off', 'turn off'],
'execute': self.shutdown
},
'restart': {
'patterns': ['restart', 'reboot'],
'execute': self.restart
},
'sleep': {
'patterns': ['sleep', 'hibernate'],
'execute': self.sleep
},
# Volume Control
'volume_up': {
'patterns': ['volume up', 'increase volume', 'louder'],
'execute': self.volume_up
},
'volume_down': {
'patterns': ['volume down', 'decrease volume', 'quieter'],
'execute': self.volume_down
},
'mute': {
'patterns': ['mute', 'silence'],
'execute': self.mute
}
}
def parse(self, text):
"""Parse voice command and find best match"""
text = text.lower().strip()
best_match = None
best_score = 0
for cmd_name, cmd_data in self.commands.items():
for pattern in cmd_data['patterns']:
score = fuzz.partial_ratio(pattern, text)
if score > best_score:
best_score = score
best_match = cmd_name
if best_score > 60:
return best_match, text
else:
return None, text
def execute(self, command_name, full_text):
"""Execute the matched command"""
if command_name in self.commands:
return self.commands[command_name]['execute'](full_text)
else:
return False, "Command not recognized"
# ============== FILE OPERATIONS ==============
def list_files(self, text):
try:
files = os.listdir('.')
if not files:
return True, "Directory is empty"
output = "Files and folders:\n"
for item in files:
if os.path.isdir(item):
output += f"📁 {item}\n"
else:
size = os.path.getsize(item)
output += f"📄 {item} ({size} bytes)\n"
return True, output
except Exception as e:
return False, f"Error: {str(e)}"
def create_file(self, text):
match = re.search(r'(?:named?|called)\s+(\S+)', text)
if match:
filename = match.group(1)
else:
words = text.split()
filename = words[-1] if words else "newfile.txt"
try:
with open(filename, 'w') as f:
f.write("")
return True, f"File '{filename}' created successfully"
except Exception as e:
return False, f"Error: {str(e)}"
def delete_file(self, text):
match = re.search(r'(?:file|named?)\s+(\S+)', text)
if match:
filename = match.group(1)
else:
words = text.split()
filename = words[-1] if words else None
if not filename:
return False, "Please specify a filename"
try:
if os.path.exists(filename):
os.remove(filename)
return True, f"File '{filename}' deleted"
else:
return False, f"File '{filename}' not found"
except Exception as e:
return False, f"Error: {str(e)}"
def read_file(self, text):
match = re.search(r'(?:file|named?)\s+(\S+)', text)
if match:
filename = match.group(1)
else:
words = text.split()
filename = words[-1] if words else None
if not filename:
return False, "Please specify a filename"
try:
if os.path.exists(filename):
with open(filename, 'r') as f:
content = f.read()
if len(content) > 500:
content = content[:500] + "\n... (truncated)"
return True, f"Content of '{filename}':\n{content}"
else:
return False, f"File '{filename}' not found"
except Exception as e:
return False, f"Error: {str(e)}"
def copy_file(self, text):
match = re.search(r'(?:file\s+)?(\S+)\s+(?:to|as)\s+(\S+)', text)
if match:
source = match.group(1)
dest = match.group(2)
else:
return False, "Please specify source and destination"
try:
import shutil
shutil.copy2(source, dest)
return True, f"Copied '{source}' to '{dest}'"
except Exception as e:
return False, f"Error: {str(e)}"
def move_file(self, text):
match = re.search(r'(?:file\s+)?(\S+)\s+(?:to)\s+(\S+)', text)
if match:
source = match.group(1)
dest = match.group(2)
else:
return False, "Please specify source and destination"
try:
import shutil
shutil.move(source, dest)
return True, f"Moved '{source}' to '{dest}'"
except Exception as e:
return False, f"Error: {str(e)}"
def rename_file(self, text):
match = re.search(r'(?:file\s+)?(\S+)\s+(?:to|as)\s+(\S+)', text)
if match:
old_name = match.group(1)
new_name = match.group(2)
else:
return False, "Please specify old and new names"
try:
os.rename(old_name, new_name)
return True, f"Renamed '{old_name}' to '{new_name}'"
except Exception as e:
return False, f"Error: {str(e)}"
# ============== DIRECTORY OPERATIONS ==============
def change_directory(self, text):
match = re.search(r'(?:to|directory)\s+(\S+)', text)
if match:
dirname = match.group(1)
else:
words = text.split()
dirname = words[-1] if words else None
if not dirname:
return False, "Please specify a directory"
try:
os.chdir(dirname)
return True, f"Changed to: {os.getcwd()}"
except Exception as e:
return False, f"Error: {str(e)}"
def current_directory(self, text):
try:
cwd = os.getcwd()
return True, f"Current directory: {cwd}"
except Exception as e:
return False, f"Error: {str(e)}"
def create_directory(self, text):
match = re.search(r'(?:named?|called)\s+(\S+)', text)
if match:
dirname = match.group(1)
else:
words = text.split()
dirname = words[-1] if words else "newfolder"
try:
os.mkdir(dirname)
return True, f"Directory '{dirname}' created"
except Exception as e:
return False, f"Error: {str(e)}"
def list_directory_tree(self, text):
try:
result = subprocess.run(['tree', '/F'], capture_output=True, text=True, shell=True)
output = result.stdout[:1000] # Limit output
return True, f"Directory tree:\n{output}"
except:
return False, "Tree command not available"
# ============== APPLICATION OPERATIONS ==============
def open_application(self, text):
# Extract app name
app_name = None
for app in self.app_paths.keys():
if app in text.lower():
app_name = app
break
if not app_name:
return False, "Application not recognized. Try: vscode, notepad, calculator, chrome, etc."
try:
app_path = self.app_paths[app_name]
# Expand user path if needed
if '{}' in app_path:
username = os.getenv('USERNAME')
app_path = app_path.format(username)
# Check if path exists or it's a system command
if os.path.exists(app_path) or app_name in ['notepad', 'calc', 'calculator', 'mspaint', 'paint', 'explorer', 'cmd', 'powershell']:
subprocess.Popen(app_path, shell=True)
return True, f"Opening {app_name}"
else:
# Try as system command
subprocess.Popen(app_name, shell=True)
return True, f"Opening {app_name}"
except Exception as e:
return False, f"Could not open {app_name}: {str(e)}"
def close_application(self, text):
# Extract app name
match = re.search(r'(?:close|kill|stop)\s+(\w+)', text)
if match:
app_name = match.group(1).lower()
else:
return False, "Please specify application to close"
try:
# Map common names to process names
process_map = {
'chrome': 'chrome.exe',
'firefox': 'firefox.exe',
'edge': 'msedge.exe',
'vscode': 'Code.exe',
'notepad': 'notepad.exe',
'calculator': 'Calculator.exe',
'paint': 'mspaint.exe'
}
process_name = process_map.get(app_name, f"{app_name}.exe")
# Kill process
subprocess.run(['taskkill', '/F', '/IM', process_name], capture_output=True)
return True, f"Closed {app_name}"
except Exception as e:
return False, f"Error: {str(e)}"
# ============== SYSTEM OPERATIONS ==============
def system_info(self, text):
try:
info = f"""System Information:
OS: {platform.system()} {platform.release()}
Version: {platform.version()}
Machine: {platform.machine()}
Processor: {platform.processor()}
Hostname: {platform.node()}"""
return True, info
except Exception as e:
return False, f"Error: {str(e)}"
def disk_usage(self, text):
try:
partitions = psutil.disk_partitions()
output = "Disk Usage:\n"
for partition in partitions:
try:
usage = psutil.disk_usage(partition.mountpoint)
output += f"{partition.device}: {usage.percent}% used ({usage.used // (1024**3)}GB / {usage.total // (1024**3)}GB)\n"
except:
pass
return True, output
except Exception as e:
return False, f"Error: {str(e)}"
def memory_usage(self, text):
try:
mem = psutil.virtual_memory()
output = f"""Memory Usage:
Total: {mem.total // (1024**3)} GB
Available: {mem.available // (1024**3)} GB
Used: {mem.used // (1024**3)} GB
Percentage: {mem.percent}%"""
return True, output
except Exception as e:
return False, f"Error: {str(e)}"
def cpu_usage(self, text):
try:
cpu_percent = psutil.cpu_percent(interval=1)
cpu_count = psutil.cpu_count()
output = f"""CPU Usage:
Cores: {cpu_count}
Usage: {cpu_percent}%"""
return True, output
except Exception as e:
return False, f"Error: {str(e)}"
def running_processes(self, text):
try:
processes = []
for proc in psutil.process_iter(['pid', 'name', 'cpu_percent']):
try:
processes.append(proc.info)
except:
pass
# Sort by CPU usage
processes = sorted(processes, key=lambda x: x['cpu_percent'] or 0, reverse=True)[:10]
output = "Top 10 Processes:\n"
for proc in processes:
output += f"{proc['name']} (PID: {proc['pid']}) - CPU: {proc['cpu_percent']}%\n"
return True, output
except Exception as e:
return False, f"Error: {str(e)}"
# ============== FILE EXECUTION ==============
def execute_file(self, text):
# Extract filename
match = re.search(r'(?:run|execute)\s+(\S+)', text)
if match:
filename = match.group(1)
else:
words = text.split()
filename = words[-1] if words else None
if not filename:
return False, "Please specify a file to execute"
try:
if not os.path.exists(filename):
return False, f"File '{filename}' not found"
# Determine file type and execute accordingly
ext = os.path.splitext(filename)[1].lower()
if ext == '.py':
result = subprocess.run(['python', filename], capture_output=True, text=True, timeout=10)
return True, f"Output:\n{result.stdout}\n{result.stderr}"
elif ext in ['.exe', '.bat', '.cmd']:
subprocess.Popen(filename, shell=True)
return True, f"Executing {filename}"
elif ext == '.c':
return False, "C files need to be compiled first"
else:
# Try to open with default program
os.startfile(filename)
return True, f"Opening {filename}"
except subprocess.TimeoutExpired:
return False, "Execution timeout"
except Exception as e:
return False, f"Error: {str(e)}"
# ============== WEB OPERATIONS ==============
def open_website(self, text):
match = re.search(r'(?:open|go to|browse)\s+(?:website\s+)?(\S+)', text)
if match:
url = match.group(1)
if not url.startswith('http'):
url = 'https://' + url
else:
return False, "Please specify a website"
try:
webbrowser.open(url)
return True, f"Opening {url}"
except Exception as e:
return False, f"Error: {str(e)}"
def search_web(self, text):
match = re.search(r'(?:search|google|look up)\s+(?:for\s+)?(.+)', text)
if match:
query = match.group(1)
else:
return False, "Please specify search query"
try:
search_url = f"https://www.google.com/search?q={query.replace(' ', '+')}"
webbrowser.open(search_url)
return True, f"Searching for: {query}"
except Exception as e:
return False, f"Error: {str(e)}"
# ============== TIME & DATE ==============
def get_time(self, text):
try:
current_time = datetime.now().strftime("%I:%M %p")
return True, f"Current time: {current_time}"
except Exception as e:
return False, f"Error: {str(e)}"
def get_date(self, text):
try:
current_date = datetime.now().strftime("%B %d, %Y")
return True, f"Current date: {current_date}"
except Exception as e:
return False, f"Error: {str(e)}"
# ============== POWER OPERATIONS ==============
def shutdown(self, text):
return True, "Shutdown command disabled for safety. Use Windows menu to shutdown."
def restart(self, text):
return True, "Restart command disabled for safety. Use Windows menu to restart."
def sleep(self, text):
try:
subprocess.run(['rundll32.exe', 'powrprof.dll,SetSuspendState', '0,1,0'])
return True, "Putting system to sleep"
except Exception as e:
return False, f"Error: {str(e)}"
# ============== VOLUME CONTROL ==============
def volume_up(self, text):
try:
# Use nircmd or keyboard simulation
return True, "Volume control requires additional setup (nircmd)"
except Exception as e:
return False, f"Error: {str(e)}"
def volume_down(self, text):
return True, "Volume control requires additional setup (nircmd)"
def mute(self, text):
return True, "Mute control requires additional setup (nircmd)"
# ============== TEST/DEMO FUNCTION ==============
if __name__ == "__main__":
print("=" * 60)
print("COMMAND PARSER TEST - Voice OS")
print("=" * 60)
parser = CommandParser()
# Test commands
test_commands = [
"list files",
"create file named test.txt",
"where am i",
"open notepad",
"system info",
"memory usage",
"what time is it",
"search for python tutorials",
"run demo.py"
]
print("\nTesting command parsing and execution:\n")
for i, cmd in enumerate(test_commands, 1):
print(f"\n[Test {i}] Command: \"{cmd}\"")
print("-" * 60)
# Parse
command_name, full_text = parser.parse(cmd)
if command_name:
print(f"✓ Matched: {command_name}")
# Execute
success, output = parser.execute(command_name, full_text)
if success:
print(f"✓ Result: {output}")
else:
print(f"✗ Error: {output}")
else:
print(f"✗ No command matched for: {cmd}")
print("\n" + "=" * 60)
print("Test completed!")
print("=" * 60)
# Interactive mode
print("\n[Interactive Mode] - Type commands to test (type 'exit' to quit)")
print("Example: list files, open notepad, system info, etc.\n")
while True:
try:
user_input = input("🎤 Command: ").strip()
if not user_input:
continue
if user_input.lower() in ['exit', 'quit', 'q']:
print("Goodbye!")
break
command_name, full_text = parser.parse(user_input)
if command_name:
print(f" Executing: {command_name}")
success, output = parser.execute(command_name, full_text)
if success:
print(f" {output}\n")
else:
print(f" {output}\n")
else:
print(f" Command not recognized\n")
except KeyboardInterrupt:
print("\n\nGoodbye!")
break
except Exception as e:
print(f" Error: {str(e)}\n")