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 unit tests of recognize_google method #619

Merged
merged 3 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Unit tests

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
python-version: ["3.9"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install build dependencies
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -y libpulse-dev libasound2-dev
- name: Install Python dependencies
run: |
python -m pip install pocketsphinx
python -m pip install .
- name: Test with unittest
run: |
python -m unittest discover --verbose
6 changes: 4 additions & 2 deletions speech_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ def recognize_sphinx(self, audio_data, language="en-US", keyword_entries=None, g
if hypothesis is not None: return hypothesis.hypstr
raise UnknownValueError() # no transcriptions available

def recognize_google(self, audio_data, key=None, language="en-US", pfilter=0, show_all=False):
def recognize_google(self, audio_data, key=None, language="en-US", pfilter=0, show_all=False, with_confidence=False):
"""
Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Google Speech Recognition API.

Expand Down Expand Up @@ -927,7 +927,9 @@ def recognize_google(self, audio_data, key=None, language="en-US", pfilter=0, sh
# https://cloud.google.com/speech-to-text/docs/basics#confidence-values
# "Your code should not require the confidence field as it is not guaranteed to be accurate, or even set, in any of the results."
confidence = best_hypothesis.get("confidence", 0.5)
return best_hypothesis["transcript"], confidence
if with_confidence:
return best_hypothesis["transcript"], confidence
return best_hypothesis["transcript"]

def recognize_google_cloud(self, audio_data, credentials_json=None, language="en-US", preferred_phrases=None, show_all=False):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_sphinx_english(self):
def test_google_english(self):
r = sr.Recognizer()
with sr.AudioFile(self.AUDIO_FILE_EN) as source: audio = r.record(source)
self.assertIn(r.recognize_google(audio), ["1 2 3", "one two three"])
self.assertIn(r.recognize_google(audio), ["123", "1 2 3", "one two three"])

def test_google_french(self):
r = sr.Recognizer()
Expand Down