-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathears.py
98 lines (72 loc) · 3.05 KB
/
ears.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
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
# This file is part of ClIDE
# ClIDE - A live-modifiable command-line IDE that can accept commands as pseudocode
# @author Andrew Phillips
# @copyright 2017 Andrew Phillips <[email protected]>
# ClIDE is free software; you can redistribute it and/or
# modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
# License as published by the Free Software Foundation; either
# version 3 of the License, or any later version.
# ClIDE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU AFFERO GENERAL PUBLIC LICENSE for more details.
# You should have received a copy of the GNU Affero General Public
# License along with ClIDE. If not, see <http://www.gnu.org/licenses/>.
'''
'''
import speech_recognition as sr
from utils import noalsaerr
import constants
class Ear():
def __init__(self, responder=None, lquit=''):
self._recog = sr.Recognizer()
self._audio = None
self._text = ''
self._last_text = ''
self._lual_quit = lquit if lquit else constants.LUAL_QUIT
self._responder = responder # function to use for answer
def lual(self, accepted=constants.ACCEPTED, out=True):
while True:
self.listen(False)
self.understand()
if self._text == self._lual_quit: break
self._post_process_text(accepted)
self.answer(self._text)
if out: print(self._text)
def listen(self, result=True, adjust=True, report=True):
with noalsaerr() as n, sr.Microphone() as source:
if adjust: self._recog.adjust_for_ambient_noise(source)
if report: print(constants.EARS_LISTENING)
self._audio = self._recog.listen(source)
if result: return self.understand() # for listen-specific call
def understand(self, audio=None, report=True):
if report: print(constants.EARS_UNDERSTANDING)
if audio: self._audio = audio
self._last_text = self._text
try:
self._text = self._recog.recognize_sphinx(self._audio).lower()
except sr.UnknownValueError:
print("Sphinx didn't understand anything you said...")
return None
except sr.RequestError as e:
print("Sphinx error: {0}".format(e))
return None
for char in self.text:
# zap non-accepted
if not char in constants.ACCEPTED: self._text = self._text.replace(char, '')
return self._text
def answer(self, *args, report=True):
result = None
try:
if self._responder: result = self._responder(*args)
except Exception as e:
print('Error: unable to answer; %s' % str(e))
return None
self._text = result
return result
def _post_process_text(self, accepted=constants.ACCEPTED):
text = self._text
for char in text:
if not char in accepted:
text.replace(char, '')
self._text = text