-
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
Merged
ftnext
merged 6 commits into
Uberi:master
from
ftnext:codex/add-verbose-argument-to-modify-output-format
May 18, 2025
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46a82ec
docs: update Vosk signature
ftnext d489e45
docs: Fix Codex's reST bug
ftnext e2e9565
refactor: Extract into test fixture
ftnext c223be4
docs: Refine recognize_vosk's type hint
ftnext 54cbd11
chore: Ignore E704 multiple statements on one line
ftnext 3c72772
style: thanks Copilot's nitpick reviews
ftnext File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( # noqa: E704 | ||||||||||||||
| recognizer, audio_data: AudioData, *, verbose: Literal[False] | ||||||||||||||
| ) -> str: ... | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| @overload | ||||||||||||||
| def recognize( # noqa: E704 | ||||||||||||||
| recognizer, audio_data: AudioData, *, verbose: Literal[True] | ||||||||||||||
| ) -> VoskResponse: ... | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def recognize( | ||||||||||||||
| recognizer, audio_data: AudioData, *, verbose: bool = False | ||||||||||||||
| ) -> Union[str, VoskResponse]: | ||||||||||||||
| """ | ||||||||||||||
| Perform speech recognition on ``audio_data`` using Vosk. | ||||||||||||||
|
|
||||||||||||||
| Requires the Vosk model to be downloaded and unpacked in a folder named 'model' (``$PWD/model``). | ||||||||||||||
|
|
||||||||||||||
| If ``verbose`` is ``False`` (default), only the recognized text is returned. | ||||||||||||||
| If ``verbose`` is ``True``, the parsed result dictionary from Vosk is returned. | ||||||||||||||
|
Comment on lines
+35
to
+36
|
||||||||||||||
| 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``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.