文章目录

  • **截至 2023-09-28 中英互译效果巨差,不推荐!!!**
  • 简介
  • 安装
  • 初试
  • 列出语言列表
  • 直接翻译
  • 参考文献

截至 2023-09-28 中英互译效果巨差,不推荐!!!

可以尝试 BingTranslator

简介

argos-translate 是一款基于 OpenNMT(神经网络机器翻译框架)的离线翻译库,不依赖第三方翻译接口,支持翻译包括中文在内的 30 多种语言。

  • 在线试用
  • 开源免费离线翻译引擎调研及测试

安装

pip install argostranslate

初试

import argostranslate.packageimport argostranslate.translatefrom_code = 'en'to_code = 'zh'argostranslate.package.update_package_index()# 更新索引available_packages = argostranslate.package.get_available_packages()# 可翻译的语言package_to_install = next(filter(lambda x: x.from_code == from_code and x.to_code == to_code, available_packages))path = package_to_install.download()# 下载包argostranslate.package.install_from_path(path)# 安装translated_text = argostranslate.translate.translate('friends', from_code, to_code)print(translated_text)# 朋友

装不上的话可以尝试手动装,将文件改名,并放到路径如 C:\Users\Administrator\.local\cache\argos-translate\downloads\translate-en_zh.argosmodel

import argostranslate.packageimport argostranslate.translatefrom_code = 'en'to_code = 'zh'argostranslate.package.update_package_index()# 更新索引available_packages = argostranslate.package.get_available_packages()# 可翻译的语言package_to_install = next(filter(lambda x: x.from_code == from_code and x.to_code == to_code, available_packages))print(package_to_install.links)# 列出下载地址# ['https://pub-dbae765fb25a4114aac1c88b90e94178.r2.dev/v1/translate-en_zh-1_7.argosmodel', 'ifps://QmZ9QmyRdwsYebNS4HPnfXKUsaREisoRe1Vd8W63F4MDiR']

列出语言列表

import argostranslate.packageargostranslate.package.update_package_index()# 更新索引available_packages = argostranslate.package.get_available_packages()# 可翻译的语言for package in available_packages:print(package, package.from_code, package.to_code)# Chinese → English zh en# English → Chinese en zh# 其余略

直接翻译

from argostranslate import package, settings, translatefrom_code = 'en'to_code = 'zh'filename = f'translate-{from_code}_{to_code}.argosmodel'path = settings.downloads_dir / filenamepackage.install_from_path(path)while True:q = input()translated_text = translate.translate(q, from_code, to_code)print(translated_text)
英文译文
How are you?你们如何?
I’m fine. Thank you. And you?我的罚款。 谢谢。 你们?
What’s your age?你们的时代是什么?

参考文献

  1. argos-translate GitHub
  2. argos-translate Documentation
  3. Poor Chinese translation