-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlistener.py
More file actions
132 lines (121 loc) · 4.53 KB
/
listener.py
File metadata and controls
132 lines (121 loc) · 4.53 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from commands import *
lst = [(u"kontrol", ifStatement), #0
(u"alta geç", newLine), #1
(u"küçüktür", lessThan), #2
(u"büyük eşittir", bigEqual), #3
(u"büyüktür", greaterThan), #4
(u"küçük eşittir", lowEqual), #5
(u"boşluk", getSpace), #6
(u"yazdır", getPrint), #7
(u"rastgele sayı", randomNum),#8
(u"paragraf", getTab), #9
(u"eşittir", equal), #10
(u"eşitse", doubleEqual), #11
(u"kapat", closeT), #12
]
listen=True
def stopKey(event):
global listen
listen = False
print "Microphone has stopped."
def startKey(event):
global listen
listen = True
print "Microphone has started."
def callFSetI(indx, i):
global lst
lst[indx][1]()
return (i+1)
def callback(recognizer, audio):
global listen, lst, sr
try:
if listen:
a = recognizer.recognize_google(audio, language="tr")
words = a.lower().split()
lstt = [s for s, f in lst]
i = 0
while i < len(words):
if words[i] not in lstt:
if (words[i] == lst[1][0].split()[0]):
if (len(words) > i + 1) and (words[i+1] == lst[1][0].split()[1]):
i = callFSetI(1, i)
else:
standard_write(words[i])
elif (words[i] == lst[3][0].split()[0]):
if (len(words) > i + 1) and (words[i+1] == lst[3][0].split()[1]):
i = callFSetI(3, i)
else:
standard_write(words[i])
elif (words[i] == lst[5][0].split()[0]):
if (len(words) > i + 1) and (words[i+1] == lst[5][0].split()[1]):
i = callFSetI(5, i)
else:
standard_write(words[i])
elif (words[i] == lst[8][0].split()[0]):
if (len(words) > i + 1) and (words[i+1] == lst[8][0].split()[1]):
i = callFSetI(8, i)
else:
standard_write(words[i])
else:
standard_write(words[i])
else:
if words[i] == lst[0][0]:
lst[0][1]()
elif words[i] == lst[2][0]:
lst[2][1]()
elif words[i] == lst[4][0]:
lst[4][1]()
elif words[i] == lst[6][0]:
lst[6][1]()
elif words[i] == lst[7][0]:
lst[7][1]()
elif words[i] == lst[9][0]:
lst[9][1]()
elif words[i] == lst[10][0]:
lst[10][1]()
elif words[i] == lst[11][0]:
lst[11][1]()
elif words[i] == lst[12][0]:
lst[12][1]()
i += 1
except sr.UnknownValueError:
print("waiting for a command...")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
r = sr.Recognizer()
r.energy_threshold = 500
r.pause_threshold = 0.5
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source)
stop_listening = r.listen_in_background(m, callback)
# Tkinter starts here
textPad.quit()
textPad.bind('<F12>', stopKey)
textPad.bind('<F11>', startKey)
# Modifications on editor
root.wm_attributes("-alpha", 0.9)
root.resizable(False, False)
# root.wm_protocol("WM_DELETE_WINDOW", Tk) # kapatmayi engeller
# Editor menus here
menu = Menu(root)
root.config(menu=menu)
file_menu = Menu(menu)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Open...", command=open_command)
file_menu.add_command(label="Save", command=save_command)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_command)
help_menu = Menu(menu)
menu.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About...", command=about_command)
textPad.pack()
root.mainloop()
import time
for _ in range(50):
time.sleep(0.1) # we're still listening even though the main thread is doing other things
# stop_listening() # calling this function requests that the background listener stop listening
while True:
time.sleep(0.1)