首页 > 编程问答 >DeepL API 无法从土耳其语翻译为乌克兰语/俄语

DeepL API 无法从土耳其语翻译为乌克兰语/俄语

时间:2024-07-21 04:59:45浏览次数:15  
标签:python translation text-processing deepl

我正在使用 Python 和 DeepL API 开发音频翻译应用程序。该程序成功识别土耳其语语音并对其进行转录,但无法使用 DeepL 将文本翻译为乌克兰语或俄语

以下是我的代码的相关部分:

from deep_translator import DeeplTranslator

class Translation:
    def __init__(self, logger, deepl_api_key):
        self.logger = logger
        self.deepl_translator = DeeplTranslator(api_key=deepl_api_key)

    def deepl_translate(self, text, dest_lang):
        try:
            lang_map = {'uk': 'UK', 'ru': 'RU', 'tr': 'TR'}
            deepl_lang = lang_map.get(dest_lang, dest_lang.upper())
            
            self.logger.info(f"Attempting translation with DeepL. Source: TR, Target: {deepl_lang}")
            translated = self.deepl_translator.translate(text, source_lang="TR", target_lang=deepl_lang)
            
            if translated == text:
                self.logger.warning("DeepL returned untranslated text. Attempting translation via intermediate language (English).")
                english_text = self.deepl_translator.translate(text, source_lang="TR", target_lang="EN-GB")
                translated = self.deepl_translator.translate(english_text, source_lang="EN-GB", target_lang=deepl_lang)
            
            if translated == text:
                self.logger.error("DeepL failed to translate the text even via an intermediate language.")
                return f"Error: DeepL did not translate the text. Original text: {text[:100]}..."
            
            self.logger.info(f"Successfully translated text (DeepL): {translated[:100]}...")
            return translated
        except Exception as e:
            self.logger.error(f"DeepL translation error: {str(e)}")
            return f"DeepL translation error: {str(e)}"`

DeepL API 密钥有效,并且翻译器已成功初始化。支持的语言包括乌克兰语(“uk”)和俄语(“ru”)

我希望 DeepL API 能够将土耳其语文本翻译成乌克兰语和俄语。然而,它始终未能这样做,返回原始文本不变 我尝试过以下操作:

从土耳其语直接翻译为乌克兰语/俄语。 两步翻译:土耳其语到英语,然后英语到乌克兰语/俄语。 不指定源语言,允许 DeepL 自动检测它。 预处理文本以删除特殊字符和多余空格。

我还检查了 DeepL 文档并确认我的 API 计划支持这些语言对之间的翻译。 日志输出显示:

2024-07-20 23:27:04,147 - AudioTranslator - INFO - Attempting translation with DeepL. Source: TR, Target: UK 2024-07-20 23:27:04,823 - AudioTranslator - WARNING - DeepL returned untranslated text. Attempting translation via intermediate language (English). 2024-07-20 23:27:05,970 - AudioTranslator - ERROR - DeepL failed to translate the text even via an intermediate language.

我期望 DeepL API 成功将土耳其语文本翻译为乌克兰语和俄语,或者至少在翻译过程出现问题时提供更具体的错误消息。


The problem you're facing is that while DeepL API officially supports Ukrainian and Russian, it might not support direct translation from Turkish to those languages . The API documentation can be a bit ambiguous sometimes, and certain language pairs, though listed individually, might not be directly compatible for translation.

Here are a few things you can try to troubleshoot and potentially fix the issue:

1. Contact DeepL Support:

  • The best course of action is to directly contact DeepL support and inquire about the specific language pair support (Turkish to Ukrainian/Russian).
  • They can confirm if this translation route is available and if there are any limitations or workarounds.

2. Experiment with other language pairs:

  • Try translating from Turkish to other less common language pairs DeepL supports to see if the issue persists.
  • This can help determine if the problem is specific to the Turkish-Ukrainian/Russian pair or if it's a broader issue with your implementation.

3. Consider alternative translation APIs:

  • If DeepL doesn't support the required translation route, explore alternative APIs like:
    • Google Translate API: Offers a wide range of language pairs and is known for its robust translation capabilities.
    • Microsoft Translator API: Another popular option with extensive language support and advanced features.

4. Implement fallback translation:

  • If you must use DeepL and direct translation isn't supported, consider a fallback mechanism:
    • Translate from Turkish to English.
    • Then, translate from English to Ukrainian/Russian.
    • This might not be ideal for accuracy but can serve as a temporary solution.

5. Enhance error handling:

  • Improve your error handling to provide more informative messages:
    • Instead of just logging "DeepL returned untranslated text," try catching specific exceptions thrown by the DeepL library.
    • This can offer more details about the failure reason, potentially pointing to unsupported language pairs or other issues.

Remember, always refer to the DeepL API documentation and reach out to their support team for the most accurate and up-to-date information on language pair support and any potential limitations.

标签:python,translation,text-processing,deepl
From: 78773900

相关文章

  • Python:对很高维的矩阵进行对角化?
    目前我正在研究一个涉及对角化矩阵以获得特征值和特征向量的问题。但现在我想将问题扩展到200,000x200,000的尺寸。我查找了如何将矩阵存储在numpy中,有人建议使用PyTables。看起来很有希望。但我想知道哪里有工具可以帮助对PyTables中的矩阵存储进行对角化。......
  • 除了curses之外,是否有一个python包可以轻松控制终端的输出?
    我现在正在处理一些小项目,我对GUI的偏好是终端中漂亮的文本界面。我宁愿不强迫用户处理Windowscurses二进制文件,所以我正在寻找不同的选项。我已经发现了asciimatics,但我想考虑所有可能的选择。如果有人有任何经验或知道解决此用例的包,我将不胜感激。谢谢你说的没错......
  • 当值来自函数 python unittest 时,如何模拟全局变量
    我必须在python中模拟全局变量,但变量值来自另一个函数。当我导入文件时,这个函数正在运行,但我想要那里的模拟值。secrets.pyimporttracebackimportloggingimportboto3importosimportjsonlogger=logging.getLogger()logger.setLevel(logging.INFO)secret_......
  • 使用 python print 和 gdb 时出现 BrokenPipeError
    我正在尝试在Linux中运行应用程序并使用Python生成输入:python3-c'print(".....")'|./someapp但出现下一个错误:Exceptionignoredin:<_io.TextIOWrappername='<stdout>'mode='w'encoding='utf-8'>BrokenPipeError:......
  • python 舰队容器
    我正在尝试使用容器在flet中制作一个菜单,它应该是半透明的,但其中的项目不是。我尝试将opacity=1分配给元素,但没有成功-它们与容器一样透明感谢任何帮助我的代码:nickname=ft.TextField(label="xxx",hint_text="xxx")column=ft.Column(controls=[nickname......
  • Python应用程序跨子包共享的配置文件
    我正在构建一个应用程序来控制一些硬件。我在包中实现了不同类型的硬件:电机和测量设备。我的文件结构如下:name_of_my_app/__init__.pymain.pyconfig.iniCONFIG.pymotors/__init__.pyone_kind_of_motor.pymeasurement_devices/......
  • python中时间序列数据的梯度计算
    我正在尝试编写一个函数,它可以从最适合下面的线返回梯度dataframe在浏览了谷歌的几个资源之后,我仍然不确定这是如何完成的。我明白最佳拟合线的计算公式为:y=mx+b将因变量(y)设置为foos,将自变量(x)设置为DateTimeDatafram......
  • 调试用 C 编写的 Python 扩展
    我非常熟悉编写C代码,并且很擅长编写Python代码。我正在尝试学习如何用C编写可以从OSX10.15.7上的Python-3.9.X调用的模块。我已经得到了几个“helloworld”类型的示例,但是对于复杂的示例,我正在努力弄清楚如何调试我编写的C扩展。MWE:src/add.c//......
  • 具有块大小选项的 Python pandas read_sas 因索引不匹配而失败并出现值错误
    我有一个非常大的SAS文件,无法容纳在我的服务器内存中。我只需要转换为镶木地板格式的文件。为此,我使用pandas中chunksize方法的read_sas选项分块读取它。它主要是在工作/做它的工作。除此之外,一段时间后它会失败并出现以下错误。此特定SAS文件有794......
  • 使用 requests 包 python 时打开文件太多
    我正在使用Pythonrequests包向API发出大量请求。然而,在某些时候,我的程序由于“打开的文件太多”而崩溃。当我明确关闭我的会话时,我真的不知道这是怎么回事。我使用以下代码:importrequestsimportmultiprocessingimportnumpyasnps=requests.session()s.keep......