【问题标题】:Microsoft Translator in Python to translate entire JSON filePython 中的 Microsoft 翻译器用于翻译整个 JSON 文件
【发布时间】:2020-06-11 16:37:02
【问题描述】:

如何使用 Microsoft Translator 将超过 5000 个字符的整个 JSON 文件从不同的语言翻译成英语。每个请求有 5000 个字符的限制,请帮我翻译整个文件。

我收到此错误:

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

【问题讨论】:

  • 您可以在每个请求中子字符串少于 5K 字符的文本。但是因为你有句子,你应该用这样的词来溢出文本: text.split()[:500] 然后附加它们。终于发了
  • "attranslate" 是解决问题的现代工具:github.com/fkirc/attranslate 除了 Microsoft,它还可以与 Google 或 DeepL 一起使用。

标签: python translation microsoft-translator


【解决方案1】:

如果您正在寻找免费图书馆,Microsoft Translator 不是最佳选择。我建议你使用googletrans 模块。 要安装,只需从命令行编写 pip install googletrans。 以下是从文档中获取的示例:

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

或翻译一个列表:

>>> 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  ->  게으른 개

【讨论】:

    【解决方案2】:

    您可以使用 BreakSentence 功能将您的文本拆分为每个请求 5000 个字符。有关 API 的更多详细信息可以在这里找到 - https://docs.microsoft.com/en-gb/azure/cognitive-services/translator/reference/v3-0-break-sentence

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2021-01-11
      相关资源
      最近更新 更多