Repository: Uberi/speech_recognition

Issue:
unittest is failing due to change for recognize_google method to return tuples with confidence
Steps to reproduce
------------------

1. `git clone` this repository
2. `python3.9 -m venv venv --upgrade-deps`, then `source venv/bin/activate`
3. `pip install -e .`
4. `pip install pocketsphinx`
5. `python -m unittest discover --verbose`

Expected behaviour
------------------

All test passes.

Actual behaviour
----------------

Tests of `recognize_google` fails.

```
======================================================================
FAIL: test_google_chinese (tests.test_recognition.TestRecognition)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/.../speech_recognition/tests/test_recognition.py", line 34, in test_google_chinese
    self.assertEqual(r.recognize_google(audio, language="zh-CN"), u"砸自己的脚")
AssertionError: ('砸自己的脚', 0.96296197) != '砸自己的脚'

======================================================================
FAIL: test_google_english (tests.test_recognition.TestRecognition)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/.../speech_recognition/tests/test_recognition.py", line 24, in test_google_english
    self.assertIn(r.recognize_google(audio), ["1 2 3", "one two three"])
AssertionError: ('123', 0.77418035) not found in ['1 2 3', 'one two three']

======================================================================
FAIL: test_google_french (tests.test_recognition.TestRecognition)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/.../speech_recognition/tests/test_recognition.py", line 29, in test_google_french
    self.assertEqual(r.recognize_google(audio, language="fr-FR"), u"et c'est la dictée numéro 1")
AssertionError: ("et c'est la dictée numéro 1", 0.93855578) != "et c'est la dictée numéro 1"

----------------------------------------------------------------------
Ran 28 tests in 4.440s

FAILED (failures=3, skipped=8)
```

Output is here:

```
test_google_chinese (tests.test_recognition.TestRecognition) ... result2:
{   'alternative': [   {'confidence': 0.96296197, 'transcript': '砸自己的脚'},
                       {'transcript': '咱自己的脚'},
                       {'transcript': '打自己的脚'},
                       {'transcript': '咱自己的角'},
                       {'transcript': '砸自己的角'}],
    'final': True}
FAIL
test_google_english (tests.test_recognition.TestRecognition) ... result2:
{   'alternative': [   {'confidence': 0.77418035, 'transcript': '123'},
                       {'transcript': '1 2 3'},
                       {'transcript': 'one two three'},
                       {'transcript': '1-2-3'},
                       {'transcript': 'one-two-three'}],
    'final': True}
FAIL
test_google_french (tests.test_recognition.TestRecognition) ... result2:
{   'alternative': [   {   'confidence': 0.93855578,
                           'transcript': "et c'est la dictée numéro 1"},
                       {'transcript': "eh c'est la dictée numéro 1"},
                       {'transcript': "hé c'est la dictée numéro 1"},
                       {'transcript': "hey c'est la dictée numéro 1"},
                       {'transcript': "c'est la dictée numéro 1"}],
    'final': True}
FAIL
```

`recognize_google` was changed to return tuple (text, confidence) at https://github.com/Uberi/speech_recognition/commit/68627691f852b8af6da05aa647199079c346cba1 (pull request #434 )

`test_google_english` needs to be added `'123'` as expected value.

System information
------------------

(Delete all the statements that don't apply.)

My **system** is macOS Mojave.

My **Python version** is 3.9.4.

My **Pip version** is 22.2.

My **SpeechRecognition library version** is 3.8.1.

I **installed PocketSphinx from** https://pypi.org/project/pocketsphinx/ .

Comments:

Patch:
diff --git a/speech_recognition/__init__.py b/speech_recognition/__init__.py
index 39d042a..ed86b75 100644
--- a/speech_recognition/__init__.py
+++ b/speech_recognition/__init__.py
@@ -855,7 +855,7 @@ class Recognizer(AudioSource):
         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.
 
@@ -927,7 +927,9 @@ class Recognizer(AudioSource):
         # 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):
         """


Test patch:
diff --git a/.github/workflows/unittests.yml b/.github/workflows/unittests.yml
new file mode 100644
index 0000000..68ce9d5
--- /dev/null
+++ b/.github/workflows/unittests.yml
@@ -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
diff --git a/tests/test_recognition.py b/tests/test_recognition.py
index ef83ce4..34934c2 100644
--- a/tests/test_recognition.py
+++ b/tests/test_recognition.py
@@ -21,7 +21,7 @@ class TestRecognition(unittest.TestCase):
     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()