我正在使用Google API翻译句子。翻译完成后,我会使用文本到语音的Google API以及翻译结果。 一般而言,翻译和文字转语音的效果很好。但是,我对撇号有疑问。例如:
我需要在第一步中使用哪种编码才能正确输出字符串(即我很累) 该程序在python中。我在此处包含摘录:
def tts_translated_text (self, input_text, input_language):
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = input_text.encode ("utf-8")
# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text=input_text)
voice = texttospeech.types.VoiceSelectionParams( language_code=input_language, ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.LINEAR16)
response = client.synthesize_speech(synthesis_input, voice, audio_config)
# The response's audio_content is binary.
with open('output.wav', 'wb') as out:
# Write the response to the output file.
out.write(response.audio_content)
预先感谢, 酯
答案 0 :(得分:0)
我终于找到了问题所在。 Google Translate API返回带有HTML编码的字符串。而且Google Text-To-Speech希望使用UTF-8编码。 我被迫使用python2.7,所以我做了以下事情:
translated_text = HTMLParser.HTMLParser().unescape (translated_text_html)
其中translation_text_html是翻译API调用返回的字符串
在python3中,它应该是:
translated_text = html.unescape (translated_text_html)