-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice.py
More file actions
57 lines (41 loc) · 1.26 KB
/
Copy pathvoice.py
File metadata and controls
57 lines (41 loc) · 1.26 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
import pyttsx3
import speech_recognition as sr
import regex as re
#targets a name from voice
def identify_name(text):
NameError=None
patterns=["me llamo([A-Za-z])", "mi nombre es([A-Za-z])", "^([A-Za-z]+)$"]
for pattern in patterns:
try:
name=re.findall(patterns, text)[0]
except IndexError:
print("No me ha dicho su nombre")
return name
#starts the engine
def initialize_engine():
engine=pyttsx3.init()
engine.setProperty("rate", 120)
engine.setProperty("voice", "spanish")
return engine
#hearing function and turns it into text
def recognize_voice(r):
with sr.Microphone() as source:
print("Puedes hablar...")
audio=r.listen(source)
text=r.recognize_google(audio, lenguage="es-ES")
return text
#main function
def main():
initialize_engine()
engine.say("Hola, como te llamas")
engine.runAndWait()
r=sr.Recognizer()
text=recognize_voice(r)
name=identify_name(text)
if name:
engine.say("Encantado de conocerte,{}".format(name))
else:
engine.say("no te entiendo mi loco")
engine.runAndWait()
if __name__=="__main__":
main()