generated from cis3296f22/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechRecognition.py
54 lines (49 loc) · 1.79 KB
/
speechRecognition.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
import speech_recognition as sr
num_dict = {
'one': '1',
'two': '2',
'to': '2',
'three': '3',
'four': '4',
'for': '4',
'five': '5',
'six': '6',
'seven': '7',
}
COLUMN_COUNT = 7
class SpeechRecognizer:
def __init__(self):
self.recognizer = sr.Recognizer()
self.microphone = sr.Microphone()
def get_speech_input(self):
with self.microphone as source:
print("Say the column number to drop the chip:")
self.recognizer.adjust_for_ambient_noise(source, duration=1)
audio = self.recognizer.listen(source)
try:
spoken_text = self.recognizer.recognize_google(audio).lower()
print(spoken_text)
return spoken_text
except sr.UnknownValueError:
print("Sorry, I did not understand. Please try again.")
return None
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
return None
def speech_recognition_move(self, gl, board):
spoken_text = self.get_speech_input()
if spoken_text is not None:
try:
numbers = [word for word in spoken_text.split() if word.lower() in num_dict]
print(numbers)
column = int(''.join(num_dict[word.lower()] for word in numbers))
print(column)
if 1 <= column <= COLUMN_COUNT and gl.is_valid_location(board, column - 1):
return column - 1
else:
print("Invalid column number. Please try again.")
return None
except ValueError:
print("Invalid input. Please say a valid column number.")
return None
return None