Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix_add_special_feature_tests #778 #783

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added tests/english.mp3
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/test_special_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

class TestSpecialFeatures(unittest.TestCase):
def setUp(self):
# Define paths to audio files
self.AUDIO_FILE_EN = os.path.join(os.path.dirname(os.path.realpath(__file__)), "english.wav")
self.AUDIO_FILE_EN_MP3 = os.path.join(os.path.dirname(os.path.realpath(__file__)), "english.mp3")
self.addTypeEqualityFunc(str, self.assertSameWords)

@unittest.skipIf(sys.platform.startswith("win"), "skip on Windows")
Expand All @@ -21,6 +23,24 @@ def test_sphinx_keywords(self):
self.assertEqual(r.recognize_sphinx(audio, keyword_entries=[("wan", 0.95), ("too", 1.0), ("tree", 1.0)]), "tree too wan")
self.assertEqual(r.recognize_sphinx(audio, keyword_entries=[("un", 0.95), ("to", 1.0), ("tee", 1.0)]), "tee to un")

# Added test for MP3
# Test that attempting to recognize speech from an incompatible audio file raises a ValueError.
def test_incompatible_audio_file_error(self):

# Create a Recognizer instance
r = sr.Recognizer()
# Verify that a ValueError is raised when an incompatible audio file is used
with self.assertRaises(ValueError) as context:
# Load the audio file for recognition
# Attempt to record audio from the MP3 file
with sr.AudioFile(self.AUDIO_FILE_EN_MP3) as source:r.record(source)

# Check that the raised ValueError contains the expected message
self.assertEqual(
str(context.exception),
"Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format"
)

def assertSameWords(self, tested, reference, msg=None):
set_tested = set(tested.split())
set_reference = set(reference.split())
Expand Down