-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add verbose parameter to recognize_vosk() #845
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
Changes from 4 commits
46a82ec
d489e45
e2e9565
c223be4
54cbd11
3c72772
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,17 +1,39 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import TYPE_CHECKING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import TYPE_CHECKING, Literal, TypedDict, Union, cast, overload | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if TYPE_CHECKING: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from speech_recognition.audio import AudioData | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def recognize(recognizer, audio_data: AudioData) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| class VoskResponse(TypedDict): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| text: str | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @overload | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def recognize( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| recognizer, audio_data: AudioData, *, verbose: Literal[False] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> str: ... | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @overload | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def recognize( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| recognizer, audio_data: AudioData, *, verbose: Literal[True] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> VoskResponse: ... | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def recognize( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| recognizer, audio_data: AudioData, *, verbose: bool = False | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| recognizer, audio_data: AudioData, *, verbose: Literal[False] | |
| ) -> str: ... | |
| @overload | |
| def recognize( | |
| recognizer, audio_data: AudioData, *, verbose: Literal[True] | |
| ) -> VoskResponse: ... | |
| def recognize( | |
| recognizer, audio_data: AudioData, *, verbose: bool = False | |
| _recognizer, audio_data: AudioData, *, verbose: Literal[False] | |
| ) -> str: ... | |
| @overload | |
| def recognize( | |
| _recognizer, audio_data: AudioData, *, verbose: Literal[True] | |
| ) -> VoskResponse: ... | |
| def recognize( | |
| _recognizer, audio_data: AudioData, *, verbose: bool = False |
Copilot
AI
May 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The docstring describes the verbose flag but doesn't use a structured :param verbose: block. Adding a proper parameter section could make the docs more consistent and easier to parse.
| If ``verbose`` is ``False`` (default), only the recognized text is returned. | |
| If ``verbose`` is ``True``, the parsed result dictionary from Vosk is returned. | |
| :param recognizer: The recognizer instance performing the recognition. | |
| :param audio_data: The audio data to recognize, provided as an instance of ``AudioData``. | |
| :param verbose: If ``False`` (default), only the recognized text is returned. If ``True``, the parsed result dictionary from Vosk is returned. | |
| :return: The recognized text as a string if ``verbose`` is ``False``, or the parsed result dictionary if ``verbose`` is ``True``. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,25 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from speech_recognition import AudioData, Recognizer | ||
|
|
||
|
|
||
| def test_recognize_vosk(): | ||
| @pytest.fixture | ||
| def audio_data() -> AudioData: | ||
| audio_file = str(Path(__file__).parent.parent / "english.wav") | ||
| audio_data = AudioData.from_file(audio_file) | ||
| sut = Recognizer() | ||
| return AudioData.from_file(audio_file) | ||
|
|
||
|
|
||
| def test_recognize_vosk(audio_data): | ||
| recognizer = Recognizer() | ||
| actual = recognizer.recognize_vosk(audio_data) | ||
|
|
||
| assert actual == "one two three" | ||
|
|
||
|
|
||
| actual = sut.recognize_vosk(audio_data) | ||
| def test_recognize_vosk_verbose(audio_data): | ||
| recognizer = Recognizer() | ||
| actual = recognizer.recognize_vosk(audio_data, verbose=True) | ||
|
|
||
| expected = """\ | ||
| { | ||
| "text" : "one two three" | ||
| }\ | ||
| """ | ||
| assert actual == expected | ||
| assert actual == {"text": "one two three"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The docs specify a generic
Dict[str, Any]return type, but the code returns a specificVoskResponseTypedDict. Updating this to the precise type (or at leastDict[str, str]) would improve clarity.