TL; DR:我试图在Windows 10上的python 3.7.2中将XML对象(使用ET)传递给Comtypes(SAPI)对象。由于无效的字符,失败(请参阅下面的错误)。可以从文件中正确读取Unicode字符,可以将其打印出来(但不能在控制台上正确显示)。好像XML是作为ASCII传递的,或者我缺少标志? (https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee431843(v%3Dvs.85))。如果它是一个丢失的标志,如何传递它? (我还没有弄清楚那部分。)
我在Windows 10上使用Python 3.7.2,并尝试发送创建XML(SSML:https://www.w3.org/TR/speech-synthesis/)文件以与Microsoft的语音API一起使用。语音会与某些单词发生冲突,当我查看SSML格式时,它支持音素标签,该标签可让您指定如何发音给定的单词。 Microsoft实现了部分标准(https://docs.microsoft.com/en-us/cortana/skills/speech-synthesis-markup-language#phoneme-element),因此我找到了包含IPA发音的UTF-8编码库。当我尝试调用SAPI时,替换了部分代码,我收到以下错误:
Traceback (most recent call last):
File "pdf_to_speech.py", line 132, in <module>
audioConverter(text = "Hello world extended test",outputFile = output_file)
File "pdf_to_speech.py", line 88, in __call__
self.engine.speak(text)
_ctypes.COMError: (-2147200902, None, ("'ph' attribute in 'phoneme' element is not valid.", None, None, 0, None))
我一直在尝试调试,但是当我打印单词的发音时,字符是方框。但是,如果我从控制台复制并粘贴它们,它们看起来就很好(请参见下文)。
həˈloʊ,
ˈwɝːld
ɪkˈstɛndəd,
ˈtɛst
我不确定问题是否是由 1)我更改了python版本以能够打印unicode 2)我解决了读取文件的问题 3)我对字符串的操作不正确
我很确定问题是我没有将它作为unicode传递给comtype对象。我正在研究的想法是 1)是否缺少标志? 2)当它传递给comtypes(C类型错误)时,它是否被转换为ascii? 3)XML传递不正确/我错过了一步吗?
这是读取IPA词典然后生成XML文件的类。查看_load_phonemes和_pronounce。
class SSML_Generator:
def __init__(self,pause,phonemeFile):
self.pause = pause
if isinstance(phonemeFile,str):
print("Loading dictionary")
self.phonemeDict = self._load_phonemes(phonemeFile)
print(len(self.phonemeDict))
else:
self.phonemeDict = {}
def _load_phonemes(self, phonemeFile):
phonemeDict = {}
with io.open(phonemeFile, 'r',encoding='utf-8') as f:
for line in f:
tok = line.split()
#print(len(tok))
phonemeDict[tok[0].lower()] = tok[1].lower()
return phonemeDict
def __call__(self,text):
SSML_document = self._header()
for utterance in text:
parent_tag = self._pronounce(utterance,SSML_document)
#parent_tag.tail = self._pause(parent_tag)
SSML_document.append(parent_tag)
ET.dump(SSML_document)
return SSML_document
def _pause(self,parent_tag):
return ET.fromstring("<break time=\"150ms\" />") # ET.SubElement(parent_tag,"break",{"time":str(self.pause)+"ms"})
def _header(self):
return ET.Element("speak",{"version":"1.0", "xmlns":"http://www.w3.org/2001/10/synthesis", "xml:lang":"en-US"})
# TODO: Add rate https://docs.microsoft.com/en-us/cortana/skills/speech-synthesis-markup-language#prosody-element
def _rate(self):
pass
# TODO: Add pitch
def _pitch(self):
pass
def _pronounce(self,word,parent_tag):
if word in self.phonemeDict:
sys.stdout.buffer.write(self.phonemeDict[word].encode("utf-8"))
return ET.fromstring("<phoneme alphabet=\"ipa\" ph=\"" + self.phonemeDict[word] + "\"> </phoneme>")#ET.SubElement(parent_tag,"phoneme",{"alphabet":"ipa","ph":self.phonemeDict[word]})#<phoneme alphabet="string" ph="string"></phoneme>
else:
return parent_tag
# Nice to have: Transform acronyms into their pronunciation (See say as tag)
如果出现错误,我还添加了代码如何写入comtype对象(SAPI)。
def __call__(self,text,outputFile):
# https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms723606(v%3Dvs.85)
self.stream.Open(outputFile + ".wav", self.SpeechLib.SSFMCreateForWrite)
self.engine.AudioOutputStream = self.stream
text = self._text_processing(text)
text = self.SSML_generator(text)
text = ET.tostring(text,encoding='utf8', method='xml').decode('utf-8')
self.engine.speak(text)
self.stream.Close()
在此先感谢您的帮助!
答案 0 :(得分:1)
请尝试在ph attrubute中使用单引号。 像这样
function doSomehting(newURL) {
console.log('yep');
}
window.addEventListener('load', function (event) {
var lastURL = localStorage.getItem('lastURL');
var newURL = location.href;
(lastURL && decodeURI(lastURL) != newURL) && doSomehting(newURL);
localStorage.setItem('lastURL', encodeURI(newURL));
});
还请记住使用\来转义单引号
UPD 同样,此错误可能意味着您的ph无法解析。您可以在此处检查文档:https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/speech-synthesis-markup
此示例将起作用
my_text = '<speak><phoneme alphabet="x-sampa" ph=\'v"e.de.ni.e\'>ведение</phoneme></speak>'
但这不是
<speak version="1.0" xmlns="https://www.w3.org/2001/10/synthesis" xml:lang="en-US">
<voice name="en-US-Jessa24kRUS">
<s>His name is Mike <phoneme alphabet="ups" ph="JH AU"> Zhou </phoneme></s>
</voice>
</speak>