简体   繁体   English

Python 中的 Microsoft Translator 翻译整个 JSON 文件

[英]Microsoft Translator in Python to translate entire JSON file

How to translate entire JSON file having more than 5000 characters, from different language to English using Microsoft Translator.如何使用 Microsoft Translator 将具有 5000 多个字符的整个 JSON 文件从不同的语言翻译成英语。 There is a limit of 5000 characters per request, please help me translate the entire file.每个请求有 5000 个字符的限制,请帮我翻译整个文件。

I am getting this error:我收到此错误:

{"error": {"code": 400077, "message": "The maximum request size has been exceeded."}}

If you are looking for a free library, Microsoft Translator is not the best option to pick.如果您正在寻找免费图书馆,Microsoft Translator 不是最佳选择。 I suggest you use the googletrans module.我建议你使用googletrans模块。 To install simply write pip install googletrans from the command line.要安装,只需编写pip install googletrans Here's an example taken from the documentation:这是从文档中获取的示例:

>>> from googletrans import Translator
>>> translator = Translator()
>>> translator.translate('안녕하세요.')
# <Translated src=ko dest=en text=Good evening. pronunciation=Good evening.>
>>> translator.translate('안녕하세요.', dest='ja')
# <Translated src=ko dest=ja text=こんにちは。 pronunciation=Kon'nichiwa.>
>>> translator.translate('veritas lux mea', src='la')
# <Translated src=la dest=en text=The truth is my light pronunciation=The truth is my light>

or to translate a list:或翻译列表:

>>> translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko')
>>> for translation in translations:
...    print(translation.origin, ' -> ', translation.text)
# The quick brown fox  ->  빠른 갈색 여우
# jumps over  ->  이상 점프
# the lazy dog  ->  게으른 개

You can use the BreakSentence functionality to break your text to 5000 chars per request.您可以使用 BreakSentence 功能将您的文本拆分为每个请求 5000 个字符。 More details on the API can be found here - https://docs.microsoft.com/en-gb/azure/cognitive-services/translator/reference/v3-0-break-sentence可以在此处找到有关 API 的更多详细信息 - https://docs.microsoft.com/en-gb/azure/cognitive-services/translator/reference/v3-0-break-sentence

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM