Search code examples
pythonjsontext-to-speechcloudsight

How do I enclose with quotes a specific text from a json response? I'm using Python 3.6.0 in Windows 8


How do I enclose with quotes ("s) a specific text from a JSON response? I'm using Python 3.6.0.

My script uses Cloudsight image recognition API. This allows me to upload an image and get a description of that image from Cloudsight.

Now, what I'm trying to do is use a TTS command line tool to speak out the response from Cloudsight. The TTS that I use is https://github.com/brookhong/tts

My problem is that this TTS can only speak out strings if it's enclosed by quotes ("s). Otherwise, it will only speak out the last word in the string. Here is what I tried so far:

image_results = get_results_for_token(image_token, api_key)  # This gets the JSON response from Cloudsight.
phrase = '"I think this shows "'
description =(image_results['name'])  # This is for getting the string that I want from the JSON response.
combination = phrase + description
import subprocess
subprocess.call('tts.exe -f 10 -v 4 '+combination, shell=False)  # Initialize TTS to speak out combination of phrase and description, which does not work as TTS only speaks out last word in description.
subprocess.call('tts.exe -f 10 -v 4 '+phrase, shell=False)  # Try to speak out the phrase first, which works because it's enclosed by quotes.
subprocess.call('tts.exe -f 10 -v 4 '+description, shell=False)  # Try to speak out description, which does not work. TTS only speaks out last word probably because the name string from the JSON response is not enclosed by quotes.
print(combination)  # This works. The script is parsing the text correctly.

Even if the script is parsing the text correctly, the TTS only speaks out the last word in the description. This is even if I use a combination of the two strings, and the separate strings, as I did in my code above.

What could be wrong?


Solution

  • Quote-wrapping is not a specific problem to tts, it's just about argument parsing/delimiting when there are spaces in the argument.

    Which probably happens when you're not enclosing your text into quotes is that all words from the sentence are passed as arguments to tts, and the program just considers the last one.

    To fix this, you shouldn't use subprocess.call like that, but use the argument list syntax instead which protects arguments with quotes/quotes quote chars when needed:

    subprocess.call(['tts.exe','-f','10','-v','4',phrase])