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

Refactor recognize_google #719

Closed
ftnext opened this issue Dec 1, 2023 · 0 comments · Fixed by #721
Closed

Refactor recognize_google #719

ftnext opened this issue Dec 1, 2023 · 0 comments · Fixed by #721
Assignees
Milestone

Comments

@ftnext
Copy link
Collaborator

ftnext commented Dec 1, 2023

@patch("speech_recognition.urlopen")
@patch("speech_recognition.Request")
class RecognizeGoogleTestCase(unittest.TestCase):
def setUp(self) -> None:
self.response = MagicMock(spec=http.client.HTTPResponse)
self.response.read.return_value = b"""\
{"result":[]}
{"result":[{"alternative":[{"transcript":"one two three","confidence":0.49585345},{"transcript":"1 2","confidence":0.42899391}],"final":true}],"result_index":0}
"""
# mock has AudioData's attributes (e.g. sample_rate)
self.audio = MagicMock(spec=sr.audio.AudioData(None, 1, 1))
self.r = sr.Recognizer()
def test_return_best_hypothesis_transcript_with_default_parameters(self, Request, urlopen):
urlopen.return_value = self.response
self.audio.sample_rate = 16_000
actual = self.r.recognize_google(self.audio)
self.assertEqual(actual, "one two three")
self.audio.get_flac_data.assert_called_once_with(convert_rate=None, convert_width=2)
Request.assert_called_once_with(
"http://www.google.com/speech-api/v2/recognize?client=chromium&lang=en-US&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&pFilter=0",
data=self.audio.get_flac_data.return_value,
headers={"Content-Type": "audio/x-flac; rate=16000"},
)
urlopen.assert_called_once_with(Request.return_value, timeout=None)
self.response.read.assert_called_once_with()
def test_minimum_sample_rate(self, Request, urlopen):
urlopen.return_value = self.response
self.audio.sample_rate = 7_999
_ = self.r.recognize_google(self.audio)
self.audio.get_flac_data.assert_called_once_with(convert_rate=8000, convert_width=2)
def test_specified_language_request(self, Request, urlopen):
urlopen.return_value = self.response
self.audio.sample_rate = 16_000
_ = self.r.recognize_google(self.audio, language="zh-CN")
Request.assert_called_once_with(
"http://www.google.com/speech-api/v2/recognize?client=chromium&lang=zh-CN&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&pFilter=0",
data=self.audio.get_flac_data.return_value,
headers={"Content-Type": "audio/x-flac; rate=16000"},
)
def test_specified_key_request(self, Request, urlopen):
urlopen.return_value = self.response
self.audio.sample_rate = 16_000
_ = self.r.recognize_google(self.audio, key="awesome-key")
Request.assert_called_once_with(
"http://www.google.com/speech-api/v2/recognize?client=chromium&lang=en-US&key=awesome-key&pFilter=0",
data=self.audio.get_flac_data.return_value,
headers={"Content-Type": "audio/x-flac; rate=16000"},
)
def test_show_all(self, Request, urlopen):
urlopen.return_value = self.response
self.audio.sample_rate = 16_000
actual = self.r.recognize_google(self.audio, show_all=True)
expected = {
"alternative": [
{"transcript": "one two three", "confidence": 0.49585345},
{"transcript": "1 2", "confidence": 0.42899391}
],
"final": True
}
self.assertEqual(actual, expected)
def test_with_confidence(self, Request, urlopen):
urlopen.return_value = self.response
self.audio.sample_rate = 16_000
actual = self.r.recognize_google(self.audio, with_confidence=True)
self.assertEqual(actual, ("one two three", 0.49585345))

@ftnext ftnext self-assigned this Dec 1, 2023
@ftnext ftnext added this to the 3.10 milestone Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant