可以尝试 BingTranslator
argos-translate 是一款基于 OpenNMT(神经网络机器翻译框架)的离线翻译库,不依赖第三方翻译接口,支持翻译包括中文在内的 30 多种语言。
pip install argostranslate
import argostranslate.package
import argostranslate.translate
from_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.package
import argostranslate.translate
from_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.package
argostranslate.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, translate
from_code = 'en'
to_code = 'zh'
filename = f'translate-{from_code}_{to_code}.argosmodel'
path = settings.downloads_dir / filename
package.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? | 你们的时代是什么? |