-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
69 lines (45 loc) · 1.74 KB
/
main.py
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
import os
import random
import speech_recognition
sr = speech_recognition.Recognizer()
sr.pause_threshold = 0.5
commands_dict = {
'commands': {
'greeting': ['привет', 'приветствую'],
'create_task': ['добавить задачу', 'создать задачу', 'заметка'],
'play_music': ['включить музыку', 'дискотека']
}
}
def listen_command():
"""The function will return the recognized command"""
try:
with speech_recognition.Microphone() as mic:
sr.adjust_for_ambient_noise(source=mic, duration=0.5)
audio = sr.listen(source=mic)
query = sr.recognize_google(audio_data=audio, language='ru-RU').lower()
return query
except speech_recognition.UnknownValueError:
return 'Damn... Не понял что ты сказал :/'
def greeting():
"""Greeting function"""
return 'Привет нищеброд!'
def create_task():
"""Create a todo task"""
print('Что добавим в список дел?')
query = listen_command()
with open('todo-list.txt', 'a') as file:
file.write(f'❗️ {query}\n')
return f'Задача {query} добавлена в todo-list!'
def play_music():
"""Play a random mp3 file"""
files = os.listdir('music')
random_file = f'music/{random.choice(files)}'
os.system(f'xdg-open {random_file}')
return f'Танцуем под {random_file.split("/")[-1]} 🔊🔊🔊'
def main():
query = listen_command()
for k, v in commands_dict['commands'].items():
if query in v:
print(globals()[k]())
if __name__ == '__main__':
main()