Search code examples
pythontext-to-speech

Python gtts language detection


Hello guys I am working on a multi language social network and I want to add an option to listen to the post as audio I tried gtts with python and I was just asking if there is a way to detect the language automatically. I was about to use a module called googletrans in python to detect the language of the post and then read it without translating but I feel it's not the best solution. I'll be thankful if you passed any piece of help:)


Solution

  • You could use textblob to detect the language: pip install textblob

    from textblob import TextBlob
    b = TextBlob("bonjour")
    text = b.detect_language()
    

    and then use pyttsx3 to read it out loud: pip install pyttsx3

    import pyttsx3
    engine = pyttsx3.init() # object creation
    
    
    rate = engine.getProperty('rate')   # getting details of current speaking rate
    print (rate)                        #printing current voice rate
    engine.setProperty('rate', 125)     # setting up new voice rate
    
    
    
    voices = engine.getProperty('voices')       #getting details of current voice
    #engine.setProperty('voice', voices[0].id)  #changing index, changes voices. o for male
    engine.setProperty('voice', voices[1].id)   #changing index, changes voices. 1 for female
    
    engine.say(text)
    
    engine.save_to_file('Hello World', 'test.mp3')
    engine.runAndWait()