本文介绍了GoogleTrans Python无法翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用GoogleTrans.实际上,我在马拉地语中有一段文字,是从语音到文字"但是当我将该文本转换为英文时,翻译不正确.

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.

但是当我从网站上使用Google翻译时,它以100%的准确度正确翻译了吗?

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

图片

这是一段代码:

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:\Users\TOSHIBA\Documents\Mini Project-2 (Trimester 4)\Project 1- (Document Summary)\Dataset\Voice\Gaurav.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\n",text)

    print(type(text))
    
    translator = Translator()
    converted_text = translator.translate(text)
    print("Converted text\n",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?

推荐答案

https://pypi.org/project/googletrans 是非官方软件包(不是Google创建的).同样在免责声明中,它具有以下" ...此API不能保证该库在任何时候都能正常工作... ".如果需要稳定性,请使用官方的Google API https://cloud.google.com/translate/docs

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)

输出:

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:\Users\TOSHIBA\Documents\Mini Project-2 (Trimester 4)\Project 1- (Document Summary)\Dataset\Voice\Gaurav.wav")
print("Text\n", original_text)
print(type(original_text))
converted_text = translate(original_text)
print("Converted text\n",converted_text)
Save("D:/output.txt", original_text, converted_text) 

这篇关于GoogleTrans Python无法翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 14:40