Newbie make a bot on python. I try to make the bot respond to what I say, according to a template and silently it responds, but I cannot figure out how to make it read (pronounce) each letter from the list. And so that the code is not huge (I have just that). If that bot I write is not for Twitter, VK, etc. But just for myself

import pygame.mixer, time a=1 while a == 1: say = input('') l=list(''.join(say)) 

Closed due to the fact that the essence of the issue is incomprehensible by the participants andreymal , mymedia , Xander , cheops , Lex Hobbit 30 Oct '17 at 13:34 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    If the code is not huge (15-20 lines) give it directly in your question (you can add a button to correct it ), if the code is more, create a minimal reproducible example . - mymedia
  • one
    ΠΊΠ°ΠΊ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΠ½ Ρ‡ΠΈΡ‚Π°Π» (произносил) ΠΊΠ°ΠΆΠ΄ΡƒΡŽ Π±ΡƒΠΊΠ²Ρƒ ΠΈΠ· списка - do we need each letter separately, or all the same to read the words? - diraria
  • And google? pythono.ru/tts-python - Igor Igoryanych
  • @diraria to make words from sounds. It turns out that yes "that would read the words," but I almost got it. - IndiGuy
  • @IndiGuy think: [a] the bottom of the letter m [o] zhet [a] b [a] mean different sounds. It is necessary to take into account firm and [m'yah'k'iye] consonants, stunning consonants at the end of words, letter combinations, shock, intonation ... Reading words is not reduced to β€œreading letters”, and pronouncing sentences - to reading words. - Nick Volynkin ♦

1 answer 1

Text-to-speech is, in general, a non-trivial task. There are ready-made implementations that can be divided into two groups:

  • Online services that implement this feature, such as Google.Translator or Yandex.Translate
  • Local programs for different OS

There are many online services and many local programs. Wrappers are written to some of them - python modules that work with api services / programs. For example, here is an incomplete list of python-modules that convert text to speech using Google.Translator.

The most simple and convenient of them is gTTS , consider an example program on it:

 from gtts import gTTS import pygame # ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ gTTS, ΠΎΠ΄Π½ΠΈΠΌ ΠΈΠ· способов: tts = gTTS(text='ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠΌΠΈΡ€', lang='ru') # русский тСкст tts = gTTS(text='Hello world', lang='en') # английский тСкст tts = gTTS(text='Hello world', lang='en', slow=True) # английский тСкст, ΠΌΠ΅Π΄Π»Π΅Π½Π½ΠΎΠ΅ ΠΏΡ€ΠΎΠΈΠ·Π½ΠΎΡˆΠ΅Π½ΠΈΠ΅ # ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅ΠΌ тСкст Π² Ρ€Π΅Ρ‡ΡŒ, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ google-ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄Ρ‡ΠΈΠΊ # ΠΈ сохраняСм ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠ²ΡˆΠ΅ΡŽΡΡ Ρ€Π΅Ρ‡ΡŒ Π² mp3 Ρ„Π°ΠΉΠ» tts.save('text.mp3') # инициализация pygame pygame.mixer.init() pygame.init() # Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ Ρ€Π΅Ρ‡ΡŒ ΠΈΠ· mp3 Ρ„Π°ΠΉΠ»Π° pygame.mixer.music.load('text.mp3') pygame.mixer.music.play() # music.play() β€” Π½Π΅Π±Π»ΠΎΠΊΠΈΡ€ΡƒΡŽΡ‰ΠΈΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄ # ΠΊΠΎΠ΄ Π½ΠΈΠΆΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΆΠ΄Π°Ρ‚ΡŒ, ΠΏΠΎΠΊΠ° Ρ€Π΅Ρ‡ΡŒ Π·Π°ΠΊΠΎΠ½Ρ‡ΠΈΡ‚ произносится pygame.mixer.music.set_endevent(pygame.USEREVENT) pygame.event.wait() 

This program gets speech using a google-translator, saves it to an mp3 file, then pygame plays this mp3 file. You can do without mp3 file using in-memory binary stream ( io.BytesIO ):

 from gtts import gTTS import pygame import io tts = gTTS(text='ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠΌΠΈΡ€', lang='ru') with io.BytesIO() as f: tts.write_to_fp(f) f.seek(0) pygame.mixer.init() pygame.init() pygame.mixer.music.load(f) pygame.mixer.music.play() pygame.mixer.music.set_endevent(pygame.USEREVENT) pygame.event.wait() 
  • one
    thanks) I have already solved the problem, just wrote down each letter in my own voice accelerated and removed spaces before and after the sound of the letter. And reproduce sounds using pygame. - IndiGuy
  • could you tell me (just 2 questions per day) see i have the code filename = 'dial.txt' fine = open(filename, mode ="r", encoding = 'utf-8') for line in fine: if x in line: print(line) I need to print a line after it was found, tell me how to do it - IndiGuy
  • @IndiGuy yes, first read the entire file into an array of lines: lines = open(...).readlines() , and then iterate over this array of lines using indices: for i in range(len(lines)): if x in lines[i]: print(lines[i+1]) - diraria
  • one
    Thank you very much) - IndiGuy