-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElm_test.py
64 lines (55 loc) · 1.96 KB
/
Elm_test.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
import speech_recognition as sr
import pyttsx3
from datetime import date, datetime
import json
import requests
import python_weather
import asyncio
import os
import pyaudio
reply_keyword = {}
with open("reply_keyword.json", "r") as f:
reply_keyword = json.load(f)
def checkMatch(sentence: str) -> str:
for key in reply_keyword:
if key in sentence:
match reply_keyword[key]:
case "GET_DATE":
return date.today().strftime("%B %d, %Y")
case "GET_TIME":
return datetime.now().strftime("%H:%M:%S")
case default:
return reply_keyword[key]
return "Sorry, I cannot understand."
def main() -> None:
voice_input = sr.Recognizer()
text_output = pyttsx3.init()
ready_to_talk = False
with sr.Microphone() as mic:
audio = voice_input.listen(mic)
while True:
try:
sentence = voice_input.recognize_google(audio)
print(f"You: {sentence}")
if "hello" in sentence and not ready_to_talk:
ready_to_talk = True
msg = "Hello user, how can I help you?"
print(f"Elm: {msg}")
text_output.say(msg)
text_output.runAndWait()
elif sentence == "goodbye" and ready_to_talk:
msg = "Goodbye user"
print(f"Elm: {msg}")
text_output.say(msg)
ready_to_talk = False
text_output.runAndWait()
elif ready_to_talk:
reply = checkMatch(sentence)
print("Elm: " + reply)
text_output.say(reply)
text_output.runAndWait()
except:
pass
audio = voice_input.listen(mic)
if __name__ == "__main__":
main()