Python,文字转语音,无效语法,使用IDLE编辑器

时间:2019-02-21 16:35:34

标签: python string syntax python-idle

我遵循了YouTube上有关如何使用python进行TextToSpeech的教程,但出现以下错误

import re
import wave
import pyaudio
import _thread
import time

class TextToSpeech:

CHUNK = 1024

def __init__(self, words_pron_dict:str = 'cmudict-0.7b.txt'):
    self._l = {}//Error right here ^
    self._load_words(words_pron_dict)

def _load_words(self, words_pron_dict:str):
    with open(words_pron_dict, 'r') as file:
        for line in file:
            if not line.startswith(';;;'):
                key, val = line.split('  ',2)
                self._l[key] = re.findall(r"[A-Z]+",val)

def get_pronunciation(self, str_input):
    list_pron = []
    for word in re.findall(r"[\w']+",str_input.upper()):
        if word in self._l:
            list_pron += self._l[word]
    print(list_pron)
    delay=0
    for pron in list_pron:
        _thread.start_new_thread( TextToSpeech._play_audio, (pron,delay,))
        delay += 0.145

def _play_audio(sound, delay):
    try:
        time.sleep(delay)
        wf = wave.open("sounds/"+sound+".wav", 'rb')
        p = pyaudio.PyAudio()
        stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                        channels=wf.getnchannels(),
                        rate=wf.getframerate(),
                        output=True)

        data = wf.readframes(TextToSpeech.CHUNK)

        while data:
            stream.write(data)
            data = wf.readframes(TextToSpeech.CHUNK)

        stream.stop_stream()
        stream.close()

        p.terminate()
        return
    except:
        pass

if __name__ == '__main__':
tts = TextToSpeech()
while True:
    tts.get_pronunciation(input('Enter a word or phrase: '))

错误是“无效Sytanx”,其中冒号在顶部的“ str”之前。我不确定自己在做什么错。我正在使用IDLE作为编辑器,此脚本需要安装的pyaudio,也需要我也有的文档“ cmudict-0.7b.text”。

我尝试将文件名直接复制到代码中,并添加括号,将'更改为txt文件名的位置,以免占上风。如果有人可以帮助我并给出帮助,我将不胜感激我对自己做错了一些了解。

我正在使用Python 2.7。

谢谢。

0 个答案:

没有答案