如何解决文字必须是最大5000字符的有效文字,否则无法翻译

问题描述 投票:0回答:1

我正在使用谷歌翻译API来翻译一些文本。

该包是 from deep_translator import GoogleTranslator。

但是每次都会返回错误: --> 文本必须是最大5000字符的有效文本,否则无法翻译

有谁知道如何解决这个问题吗?是否可以有付费版本的api来扩展最大字符数?

非常感谢!

python google-translation-api
1个回答
2
投票

您可以将文本分成单词或句子的块。

使用NLTK模块:

pip install nltk
import nltk
#nltk.download('popular') -- only need to do this once
x = '''I am using google translate api to translate some text.

The package is from deep_translator import GoogleTranslator.

However, every time it would return the error: --> text must be a valid text with maximum 5000 character, otherwise it cannot be translated

Does anyone know how to solve this? Is it possible to have a paid version of api to expand the maximum characters?

Thanks a lot!'''

x = nltk.tokenize.sent_tokenize(x)
for sentence in x:
    print(sentence) #send each sentence to translator
© www.soinside.com 2019 - 2024. All rights reserved.