0

I'm trying to use the Google Speech API in Python. I load a .flac file like this:

url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"
audio = open('temp_voice.flac','rb').read()
headers = {'Content-Type': 'audio/x-flac; rate=44100', 'User-Agent':'Mozilla/5.0'}
req = urllib2.Request(url, data=audio, headers=headers)
resp = urllib2.urlopen(req)
system("rm temp_voice.wav; rm temp_voice.flac")
print resp.read()

Output:

{"status":0,"id":"","hypotheses":[{"utterance":"Today is Wednesday","confidence":0.75135982}]}

Can someone please teach me how I can extract and save the text "Today is Wednesday" as a variable and print it?

1
  • 2
    How about using json module ? Commented Nov 23, 2013 at 7:37

3 Answers 3

4

You can use json.loads to convert the JSON data to a dict, like this

data = '{"status":0,"id":"","hypotheses":[{"utterance":"Today is Wednesday","confidence":0.75135982}]}'
import json
data = json.loads(data)
print data["hypotheses"][0]["utterance"]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for the response. Unfortunately, this doesn't seem to work for me though. I get the following: raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
I just added the code to my original post. Thanks for your help
@Stralo But this doesnt have the part where json is used
Yes, I'm not sure how to do that and that's why I'm asking for help. I added your suggestion at the end of the code and got the ValueError I mentioned earlier. I did data=resp.read() text=json.loads(data) print text["hypotheses"][0]["utterance"]
0

If the response is coming as a string then you can just eval it to a dictionary, (for safety it is preferable to use literal_eval from the ast library instead):

>>> d=eval('{"status":0,"id":"","hypotheses":[{"utterance":"Today is Wednesday","confidence":0.75135982}]}')
>>> d
{'status': 0, 'hypotheses': [{'confidence': 0.75135982, 'utterance': 'Today is Wednesday'}], 'id': ''}  

>>> h=d.get('hypotheses')                                                                            
>>> h                                                                                                 
[{'confidence': 0.75135982, 'utterance': 'Today is Wednesday'}]                                       
>>> for i in h:                                                                                       
...    print i.get('utterance')
... 
Today is Wednesday

Of course if it is already a dictionary then you do not need to do the evaluate, try using print type(response) where response is the result you are getting.

3 Comments

ast.literal_eval() is the safe way to do this. eval should never be used for this.
I tried text=eval(str(response.read())) print text and got File "<string>", line 0 ^ SyntaxError: unexpected EOF while parsing
Sounds like either the buffer filled up or the connection was lost before the end of the full string or you got some malformed string.
0

The problem with retrieve output is bit more complicate that looks. At first resp is type of instance, however if you copy the output manually is dictionary->list->dictionary. If you assign the resp.read() to new variable you will get type string with length 0. It happens, because the all output vanish into air once is used (print). Therefore the json decoding has to be done as soon the respond from google api is granted. As follow:

resp = urllib2.urlopen(req)

text = json.loads(resp.read())["hypotheses"][0]["utterance"]

Works like a charm in my case ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.