我正在使用 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