- 1、有道接口
- 2、微软接口
- 3、谷歌接口
本文主要分享三个免费的机器翻译接口,方便有需求的人学习(本人应用于机器学习数据增强): 1、有道接口
‘type’默认是 汉译英 英译汉
import requests data1 = { 'doctype': 'json', 'type': 'auto','i': '你吃饭了吗?' } data2 = { 'doctype': 'json', 'type': 'auto','i': 'Did you eat?' } r = requests.get("http://fanyi.youdao.com/translate",params=data1) result = r.json() print(result) r = requests.get("http://fanyi.youdao.com/translate",params=data2) result = r.json() print(result)
结果:
{'type': 'ZH_CN2EN', 'errorCode': 0, 'elapsedTime': 1, 'translateResult': [[{'src': '你吃饭了吗?', 'tgt': 'Did you eat?'}]]} {'type': 'EN2ZH_CN', 'errorCode': 0, 'elapsedTime': 0, 'translateResult': [[{'src': 'Did you eat?', 'tgt': '你吃了吗?'}]]}
type 可选:
ZH_CN2EN 中文 » 英语 ZH_CN2JA 中文 » 日语 ZH_CN2KR 中文 » 韩语 EN2ZH_CN 英语 » 中文 JA2ZH_CN 日语 » 中文 KR2ZH_CN 韩语 » 中文
其它语种没调通,可能收费,暂时未知原因。
2、微软接口from translate import Translator translator=Translator(from_lang="chinese",to_lang="english") translation = translator.translate("你吃了吗?") print(translation) translator2=Translator(from_lang="english",to_lang="chinese") translation = translator2.translate("Did you eat?") print(translation)
更改 from_lang 和 to_lang 就好了
3、谷歌接口注意:谷歌会自定识别输入文本,dest 是目标语言,下列代码需要访问谷歌网址,如果访问不通,不然会显示超时:
from googletrans import Translator translator = Translator() title_alternative = translator.translate('你吃了吗?', dest=‘en').text print (title_alternative)
如无法访问,更改第二行为:
translator = Translator(service_urls=['translate.google.cn'])
目标语言列表:
‘af’: ‘afrikaans’,
‘ar’: ‘arabic’,
‘be’: ‘belarusian’,
‘bg’: ‘bulgarian’,
‘ca’: ‘catalan’,
‘cs’: ‘czech’,
‘cy’: ‘welsh’,
‘da’: ‘danish’,
‘de’: ‘german’,
‘el’: ‘greek’,
‘en’: ‘english’,
‘eo’: ‘esperanto’,
‘es’: ‘spanish’,
‘et’: ‘estonian’,
‘fa’: ‘persian’,
‘fi’: ‘finnish’,
‘fr’: ‘french’,
‘ga’: ‘irish’,
‘gl’: ‘galician’,
‘hi’: ‘hindi’,
‘hr’: ‘croatian’,
‘hu’: ‘hungarian’,
‘id’: ‘indonesian’,
‘is’: ‘icelandic’,
‘it’: ‘italian’,
‘iw’: ‘hebrew’,
‘ja’: ‘japanese’,
‘ko’: ‘korean’,
‘la’: ‘latin’,
‘lt’: ‘lithuanian’,
‘lv’: ‘latvian’,
‘mk’: ‘macedonian’,
‘ms’: ‘malay’,
‘mt’: ‘maltese’,
‘nl’: ‘dutch’,
‘no’: ‘norwegian’,
‘pl’: ‘polish’,
‘pt’: ‘portuguese’,
‘ro’: ‘romanian’,
‘ru’: ‘russian’,
‘sk’: ‘slovak’,
‘sl’: ‘slovenian’,
‘sq’: ‘albanian’,
‘sr’: ‘serbian’,
‘sv’: ‘swedish’,
‘sw’: ‘swahili’,
‘th’: ‘thai’,
‘tl’: ‘filipino’,
‘tr’: ‘turkish’,
‘uk’: ‘ukrainian’,
‘vi’: ‘vietnamese’,
‘yi’: ‘yiddish’,
‘zh-CN’: ‘chinese_simplified’,
‘zh-TW’: ‘chinese_traditional’
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)