📜  在Python中使用 Google API 的语言翻译器

📅  最后修改于: 2022-05-13 01:55:18.883000             🧑  作者: Mango

在Python中使用 Google API 的语言翻译器

API代表应用程序编程接口。它充当两个应用程序或软件之间的中间体。简单来说,API 充当信使,将您的请求发送到目的地,然后为您返回响应。 Google API 由 Google 开发,允许与他们的服务器通信并使用他们的 API 密钥来开发项目。

在本教程中,我们将使用 Google API 构建一个可以将一种语言翻译成另一种语言的语言翻译器。在互联网上,我们可以看到很多关于语音识别、语音到文本、文本到语音等的项目,但在这个项目中,我们将构建比这更先进的东西。

让我们假设一个场景,我们在西班牙旅行,我们不知道如何说西班牙语,或者我们在任何其他国家,我们不知道他们的母语,那么我们可以使用这个工具来解决这个问题。我们可以在谷歌翻译器中存在的所有语言之间进行翻译

安装

现在要检查它支持哪些语言,我们必须使用谷歌翻译库。我们可以使用 pip 来安装它。

pip install googletrans

现在检查它支持哪些语言来运行以下代码。

Python3
# To Print all the languages that google
# translator supports
import googletrans
 
 
print(googletrans.LANGUAGES)


Python3
# Importing necessary modules required
import speech_recognition as spr
from googletrans import Translator
from gtts import gTTS
import os
 
 
# Creating Recogniser() class object
recog1 = spr.Recognizer()
 
# Creating microphone instance
mc = spr.Microphone()
 
 
# Capture Voice
with mc as source:
    print("Speak 'hello' to initiate the Translation !")
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    recog1.adjust_for_ambient_noise(source, duration=0.2)
    audio = recog1.listen(source)
    MyText = recog1.recognize_google(audio)
    MyText = MyText.lower()
 
# Here initialising the recorder with
# hello, whatever after that hello it
# will recognise it.
if 'hello' in MyText:
     
    # Translator method for translation
    translator = Translator()
     
    # short form of english in which
    # you will speak
    from_lang = 'en'
     
    # In which we want to convert, short
    # form of hindi
    to_lang = 'hi'
     
    with mc as source:
         
        print("Speak a stentence...")
        recog1.adjust_for_ambient_noise(source, duration=0.2)
         
        # Storing the speech into audio variable
        audio = recog1.listen(source)
         
        # Using recognize.google() method to
        # convert audio into text
        get_sentence = recog1.recognize_google(audio)
 
        # Using try and except block to improve
        # its efficiency.
        try:
             
            # Printing Speech which need to
            # be translated.
            print("Phase to be Translated :"+ get_sentence)
 
            # Using translate() method which requires
            # three arguments, 1st the sentence which
            # needs to be translated 2nd source language
            # and 3rd to which we need to translate in
            text_to_translate = translator.translate(get_sentence,
                                                     src= from_lang,
                                                     dest= to_lang)
             
            # Storing the translated text in text
            # variable
            text = text_to_translate.text
 
            # Using Google-Text-to-Speech ie, gTTS() method
            # to speak the translated text into the
            # destination language which is stored in to_lang.
            # Also, we have given 3rd argument as False because
            # by default it speaks very slowly
            speak = gTTS(text=text, lang=to_lang, slow= False)
 
            # Using save() method to save the translated
            # speech in capture_voice.mp3
            speak.save("captured_voice.mp3")    
             
            # Using OS module to run the translated voice.
            os.system("start captured_voice.mp3")
 
        # Here we are using except block for UnknownValue
        # and Request Error and printing the same to
        # provide better service to the user.
        except spr.UnknownValueError:
            print("Unable to Understand the Input")
             
        except spr.RequestError as e:
            print("Unable to provide Required Output".format(e))


输出:

谷歌-trans-python

现在让我们开始构建语言翻译器。从编码部分开始,我们需要安装一些依赖项。在安装 Pyaudio 时,您可能会收到 portaudio 错误。有关 pyaudio 安装的详细信息,请单击此处。

pip install pyaudio
pip install SpeechRecognition
pip install gtts

下面是实现。

Python3

# Importing necessary modules required
import speech_recognition as spr
from googletrans import Translator
from gtts import gTTS
import os
 
 
# Creating Recogniser() class object
recog1 = spr.Recognizer()
 
# Creating microphone instance
mc = spr.Microphone()
 
 
# Capture Voice
with mc as source:
    print("Speak 'hello' to initiate the Translation !")
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    recog1.adjust_for_ambient_noise(source, duration=0.2)
    audio = recog1.listen(source)
    MyText = recog1.recognize_google(audio)
    MyText = MyText.lower()
 
# Here initialising the recorder with
# hello, whatever after that hello it
# will recognise it.
if 'hello' in MyText:
     
    # Translator method for translation
    translator = Translator()
     
    # short form of english in which
    # you will speak
    from_lang = 'en'
     
    # In which we want to convert, short
    # form of hindi
    to_lang = 'hi'
     
    with mc as source:
         
        print("Speak a stentence...")
        recog1.adjust_for_ambient_noise(source, duration=0.2)
         
        # Storing the speech into audio variable
        audio = recog1.listen(source)
         
        # Using recognize.google() method to
        # convert audio into text
        get_sentence = recog1.recognize_google(audio)
 
        # Using try and except block to improve
        # its efficiency.
        try:
             
            # Printing Speech which need to
            # be translated.
            print("Phase to be Translated :"+ get_sentence)
 
            # Using translate() method which requires
            # three arguments, 1st the sentence which
            # needs to be translated 2nd source language
            # and 3rd to which we need to translate in
            text_to_translate = translator.translate(get_sentence,
                                                     src= from_lang,
                                                     dest= to_lang)
             
            # Storing the translated text in text
            # variable
            text = text_to_translate.text
 
            # Using Google-Text-to-Speech ie, gTTS() method
            # to speak the translated text into the
            # destination language which is stored in to_lang.
            # Also, we have given 3rd argument as False because
            # by default it speaks very slowly
            speak = gTTS(text=text, lang=to_lang, slow= False)
 
            # Using save() method to save the translated
            # speech in capture_voice.mp3
            speak.save("captured_voice.mp3")    
             
            # Using OS module to run the translated voice.
            os.system("start captured_voice.mp3")
 
        # Here we are using except block for UnknownValue
        # and Request Error and printing the same to
        # provide better service to the user.
        except spr.UnknownValueError:
            print("Unable to Understand the Input")
             
        except spr.RequestError as e:
            print("Unable to provide Required Output".format(e))

输出:

Speak 'hello' to initiate the Translation !
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Speak a stentence...
Phase to be Translated :what are you doing