1

I am working on a project to make the computer speak back when I give it commands. I just started, but when I run the code, it doesn't respond in the way I expect it to respond. Here is my code:

from pocketsphinx import LiveSpeech
import os
for phrase in LiveSpeech(): print(phrase)
if phrase == 'oh' :
    os.system('espeak' ' "hi"')

I got an error when I run this code. Here is the error:

Traceback (most recent call last):
  File "xxt", line 4, in <module>
    print(phrase)
  File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 93, in __str__
    return self.hypothesis()
  File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 126, in hypothesis
    hyp = self.hyp()
  File "/Library/Python/2.7/site-packages/pocketsphinx/pocketsphinx.py", line 359, in hyp
    return _pocketsphinx.Decoder_hyp(self)
  File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 225, in stop
    raise StopIteration
StopIteration

Thanks, Aditya

  • 1
    This perhaps is too simple, but are you sure that your `if` statement is being executed in your `for` loop? It looks like your `for` loop simply prints each phrase without checking if the value of the phrase is `'oh'`. Try adding a newline before `print` and indenting that line, as well as the two lines beneath it to be nested under the `for` loop. – cole Aug 15 '17 at 06:12
  • @cole Thanks, but it is not working –  Aug 15 '17 at 19:57
  • Hello? Anybody? –  Aug 17 '17 at 23:33

1 Answers1

1

This is because the if statement is not in the loop, the loop's body is the print statement

for phrase in LiveSpeech():
    print(phrase)
    if phrase == 'oh' :
        os.system('espeak' ' "hi"')

this is what it should look like

Sedy Vlk
  • 565
  • 4
  • 12