This repository was archived by the owner on Jun 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudy.py
More file actions
49 lines (40 loc) · 1.53 KB
/
Copy pathStudy.py
File metadata and controls
49 lines (40 loc) · 1.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
import speech_recognition as sr
import difflib
def get_text_input():
text = input("Enter the text for the user to repeat: ")
return text
def listen_and_recognize():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Please speak the text you just entered...")
audio = recognizer.listen(source)
try:
spoken_text = recognizer.recognize_google(audio)
print("You said:", spoken_text)
return spoken_text
except sr.UnknownValueError:
print("Could not understand the audio.")
except sr.RequestError:
print("Error with the speech recognition service.")
return ""
#--^^ best
def compare_texts(original_text, spoken_text):
original_words = original_text.split()
spoken_words = spoken_text.split()
diff = difflib.ndiff(original_words, spoken_words)
differences = [word for word in diff if word[0] != ' ']
if not differences:
print("Perfect match!")
else:
print("Differences found:")
for difference in differences:
if difference.startswith('-'):
print(f"Missing word: {difference[2:]}")
elif difference.startswith('+'):
print(f"Extra word: {difference[2:]}")
else:
print(f"Changed word: {difference[2:]}")
original_text = get_text_input()
spoken_text = listen_and_recognize()
if spoken_text:
compare_texts(original_text, spoken_text)