0

I followed a tutorial on Youtube on how to do TextToSpeech with python, and I am getting the following error

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: '))

The error is "Invalid Sytanx" right where the colon is right before "str" at the top. I'm not sure what I am doing wrong. I am using IDLE for the editor, this script requires pyaudio, which I have installed, and it also requires the document "cmudict-0.7b.text" which I also have.

I've tried copying the name of the file directly to the code, adding parenthesis changing the ' to a " where the txt file name is, to no prevail. I would appreciate it if someone could help me on this and give me some insight on what I'm doing wrong.

I'm using Python 2.7.

Thanks.

Ejay
  • 27
  • 1
  • 7
  • Tutorial right here: https://www.youtube.com/watch?v=KSSsVhoR7FQ I have a feeling that the error has something to do with this video being 3 years old – Ejay Feb 21 '19 at 16:36
  • Related: [What are Type hints in Python 3.5](https://stackoverflow.com/q/32557920/1639625) – tobias_k Feb 21 '19 at 16:38
  • So is the issue that that part of the code is outdated? If it is, what's the new alternative for it? @tobias_k – Ejay Feb 21 '19 at 16:45
  • Just get rid of the type hint, change `words_pron_dict:str = 'cmudict-0.7b.txt'` to `words_pron_dict='cmudict-0.7b.txt'` (There might be other Python 3 vs. 2 problems, though) – tobias_k Feb 21 '19 at 16:46
  • I don't know that module, but I could import it with no problem on Python 3.6, but not on 2.7. You might not need that (use some other means of thread handling, or just play the audio in the main thread), or change to Python 3. – tobias_k Feb 21 '19 at 17:09
  • When I run it, and put something in, I get this error:\text-to-speech-sample-master\load.py", line 65, in tts.get_pronunciation(input('Enter a word or phrase: ')) File "", line 1, in NameError: name 'at' is not defined – Ejay Feb 21 '19 at 17:09
  • Basically I should be able to put in any word and it outputs audio, but that's the error (above) I get when I put in any word – Ejay Feb 21 '19 at 17:10
  • I solved the _thread issue, I just had to rename it to "thread" – Ejay Feb 21 '19 at 17:11
  • `input` works differently in Python 2. Use `raw_input` instead. Or, as I said, take this opportunity to switch to Python 3 ;-) – tobias_k Feb 21 '19 at 17:16
  • @tobias_k So if I switch to python 3 than I won't have these issues? – Ejay Feb 21 '19 at 17:20
  • @tobias_k Can you upvote this question please? – Ejay Mar 05 '19 at 05:22

0 Answers0