Skip to content

Instantly share code, notes, and snippets.

@PaulSayantan
Created June 13, 2020 19:34
Show Gist options
  • Save PaulSayantan/1002f09871a43888e57b409a60da1317 to your computer and use it in GitHub Desktop.
Save PaulSayantan/1002f09871a43888e57b409a60da1317 to your computer and use it in GitHub Desktop.
Python Snippets
# # A Python program to convert speech to text
# Pakages to install:
# pip install SpeechRecognition
# pip install pipwin
# pipwin install pyaudio
import speech_recognition as sr
# initialize a recognizer object
# this object will record the audio and recognize the language of the audio, i.e, english, russian, chinese, etc.
r = sr.Recognizer()
# initialize the Microphone object as the source of audio file
with sr.Microphone() as source:
print("Please speak anything :")
# calling the recognizer object to listen the audio from the source
# you can also pass your google cloud speech API key through the key parameter(key="YOUR_GOOGLE_CLOUD_SPPECH_API_KEY")
audio = r.listen(source)
try:
# now the audio will be recognized by google cloud speech API and converted to text
text = r.recognize_google(audio)
print(text)
except :
print("Sorry ! Speech Recognition failed !")
# A Python program to convert text to speech
# Packages to install:
# pip install gtts
# pip install pydub
# pip install ffmpeg-python
from gtts import gTTS
from pydub import AudioSegment
from pydub.playback import play
import time, os
def tts(text, lang):
# gTTS object that will create the audio file of the given text
TextAudio = gTTS(text = text, lang=lang)
# filepath to save the audio file in the desired directory
filePath = './tmp/audio.mp3'
TextAudio.save(filePath)
# playback of the audio and removal on exit
audio = AudioSegment.from_mp3(filePath)
print('Playing...')
play(audio)
os.remove(filePath)
if __name__ == "__main__":
tts('India is a beautiful country', 'en')
tts('India ek sundar desh hai', 'hi')
# A Python program to translate text in different languages
# Packages to install:
# pip install googletrans
from googletrans import Translator
def translate(text, lang):
# an instance of Translator object
translator = Translator(service_urls=['translate.google.com', 'translate.google.co.in'])
# Translate text from source language to destination language
trans = translator.translate(text = text, dest = lang)
# to check if the user is passing a list of strings
if isinstance(trans, list):
for translations in trans:
if translations.pronunciation == None:
print(translations.text, '\n')
else:
print(translations.text, '->', translations.pronunciation, '\n')
else:
if trans.pronunciation == None:
print(trans.text, '\n')
else:
print(trans.text, '->', trans.pronunciation, '\n')
if __name__ == "__main__":
# My favourite quote by El profesor from Money Heist
translate( 'When someone is in love, they look through rose-tinted glasses. Everything is wonderful. They transform into a soft teddy bear that is smiling all the time.', 'es')
# Breaking down that quote into list
translate(['When someone is in love', 'they look through rose-tinted glasses', 'Everything is wonderful.', 'They transform into a soft teddy bear that is smiling all the time.'], 'es')
# some other translation examples
translate('I am a young man', 'es')
translate('この文章は日本語で書かれました。', 'en')
translate('안녕하세요.', 'ja')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment