gpt4 book ai didi

python - GoogleTrans Python 不翻译

转载 作者:行者123 更新时间:2023-12-05 09:08:45 27 4
gpt4 key购买 nike

我正在为我的项目使用 GoogleTrans。实际上我有一段马拉地语文本,它是从“语音到文本”中获得的,但是当我将该文本转换为英语时,它没有正确翻译。

但是当我在网站上使用谷歌翻译时,它翻译正确且准确率 100%?

Image

这是一段代码:

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))

请指教,我该怎么做?

最佳答案

https://pypi.org/project/googletrans是一个非官方包(不是由谷歌创建)。同样在免责声明中,它具有以下内容“...此 API 不保证库在任何时候都能正常工作...”。如果您想要稳定性,请使用官方 Google API https://cloud.google.com/translate/docs

无论如何,下面是一个显示翻译工作的测试:

from googletrans import Translator

translator = Translator()
results =translator.translate('हॅलो वर्ल्ड')
print(results.text)

输出:

Hello World

我不确定您遇到了什么问题。但是,我建议将您的代码重构为方法,然后您可以单独进行测试。

下面的代码经过测试,但应该有助于调试:

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)

关于python - GoogleTrans Python 不翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63077115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com