Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

google translate - GoogleTrans Python not translating

I am using GoogleTrans for my project. Actually I have a text in Marathi which I got from "speech to text" but when I am converting that text into English it is not translating properLY.

But when I am using Google Translate from website, it is translating properly with 100% accuracy ?

Image

Here is the piece of Code :

import speech_recognition as sr
from cltk.corpus.utils.importer import CorpusImporter
from googletrans import Translator
import goslate
import googletrans

print("Hello World")

filename = r"C:UsersTOSHIBADocumentsMini Project-2 (Trimester 4)Project 1- (Document Summary)DatasetVoiceGaurav.wav"
r = sr.Recognizer()

with sr.AudioFile(filename) as source:
    audio_data = r.record(source)

    text = r.recognize_google(audio_data,language='mr')
    print("Text
",text)

    print(type(text))
    
    translator = Translator()
    converted_text = translator.translate(text)
    print("Converted text
",converted_text)

    with open("D:/output.txt",'w',encoding="utf-8") as f:
        f.write(text)
        f.write(str(converted_text))

Please suggest, what should I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

https://pypi.org/project/googletrans is an unofficial package (not created by Google). Also in the disclaimer, it has the following "...this API does not guarantee that the library would work properly at all times...". If you want stability, use official Google API https://cloud.google.com/translate/docs

Anyway below is a test to show the translator work:

from googletrans import Translator

translator = Translator()
results =translator.translate('???? ??????')
print(results.text)

Output:

Hello World

I'm not sure what the issue you're experiencing. However I would suggest refactoring your code into methods and then you can test in isolation.

The code below has not been tested, but should help debug:

import speech_recognition as sr
#from cltk.corpus.utils.importer import CorpusImporter
from googletrans import Translator
#import goslate
#import googletrans

def provideAudioToText(filename):
    r = sr.Recognizer()
    with sr.AudioFile(filename) as source:
    audio_data = r.record(source)
    return r.recognize_google(audio_data,language='mr')

def translate(text):
    translator = Translator()
    results = translator.translate(text)
    return results.text

def Save(filename, original_text, converted_text):
    with open(filename,'w',encoding="utf-8") as f:
        f.write(original_text)
        f.write(str(converted_text))

original_text = provideAudioToText(r"C:UsersTOSHIBADocumentsMini Project-2 (Trimester 4)Project 1- (Document Summary)DatasetVoiceGaurav.wav")
print("Text
", original_text)
print(type(original_text))
converted_text = translate(original_text)
print("Converted text
",converted_text)
Save("D:/output.txt", original_text, converted_text) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...