V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
xinmans
V2EX  ›  问与答

使用 Google Translate 翻译国家名称的时候容易翻译错

  •  
  •   xinmans · 6 天前 · 494 次点击

    def translate_text(text, target_language='zh-cn', max_retries=3): translation = None retries = 0

    translator = Translator(service_urls=['translate.googleapis.com'], user_agent=get_random_user_agent())
    
    while translation is None and retries < max_retries:
        try:
            translation = translator.translate(text, dest=target_language).text
            sleep(1)
        except Exception as e:
            if retries >= 2:
                sleep(5)
                retries += 1
                continue
    if translation is None:
        logging.warning(f"Translation <<<{text}>>> failed: {e}, have retry {retries} times, will jump to next line.")
        return text
    else:
        logging.info(f"Translation <<<{text}>>> success: {translation}")
        return translation
        
        
    

    2024-06-27 16:17:57 INFO Translation <<<Georgia>>> success: 乔治亚州 2024-06-27 16:17:58 INFO Translation <<<Portugal>>> success: 葡萄牙 2024-06-27 16:17:58 INFO Translated countries: ['乔治亚州', '葡萄牙']

    Georgia 没有正确的翻译成格鲁吉亚,而是翻译成了乔治亚州,Turkey 翻译成了火鸡

    这个有办法设置吗? Google 了一圈资料,没看到解决办法

    7 条回复    2024-06-28 14:06:01 +08:00
    alfchin
        1
    alfchin  
       6 天前 via iPhone
    要有上下文才不会错。加个上文进去翻译完取最后一段就好
    yyzh
        2
    yyzh  
       6 天前 via Android
    上下文联想+1
    riggzh
        3
    riggzh  
       6 天前
    本来乔治亚州也是 Georgia
    vacuitym
        4
    vacuitym  
       6 天前
    这种需要上下文,感觉很适合用 AI 处理
    LLaMA2
        5
    LLaMA2  
       6 天前
    笨点的方法

    Georgia 和 Turkey 等所有国家的名称 全部使用 "country xxxxxx"

    这样,得到的结果是 “国家 格鲁吉亚”和"国家 土耳其"
    Ericcccccccc
        6
    Ericcccccccc  
       6 天前
    Turkey 翻译成了火鸡 有啥问题
    xinmans
        7
    xinmans  
    OP
       5 天前
    @LLaMA2 有道理啊,好办法,我试试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1189 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:02 · PVG 07:02 · LAX 16:02 · JFK 19:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.