Èç¹ûÒª»ùÓÚPythonÖÆ×÷Ò»¸öÎı¾·ÒëÆ÷£¬ÎÒÃÇ¿ÉÒÔʹÓÃPythonµÄ»úÆ÷·Òë¿â¡ª¡ªGoogletransºÍBaiduTranslate£¬ÕâÁ½¸ö¿â¿ÉÒÔʵÏÖÎı¾µÄ×Ô¶¯·Òë¡£
ÒÔÏÂÊÇÖÆ×÷Îı¾·ÒëÆ÷µÄÍêÕû¹¥ÂÔ£º
ÎÒÃÇÐèÒªÏÂÔغͰ²×°PythonµÄ»úÆ÷·Òë¿â¡ª¡ªGoogletransºÍBaiduTranslate¡£ÆäÖУ¬Googletrans¿ÉÒÔÖ±½Ó´Ópip°²×°£¬BaiduTranslateÐèÒª´ÓGitHubÉÏÏÂÔØÔ´´úÂë²¢ÊÖ¶¯°²×°¡£
ʾÀý´úÂ룺
# °²×° Googletrans
pip install googletrans==4.0.0rc1
# ÏÂÔØ BaiduTranslate Ô´´úÂë²¢ÊÖ¶¯°²×°
git clone https://github.com/Mouse0w0/YouDaoTranslate.git
cd YouDaoTranslate
python setup.py install
ÎÒÃÇÐèÒªÔÚPython´úÂëÖе¼ÈëGoogletransºÍBaiduTranslate¿â¡£
ʾÀý´úÂ룺
# µ¼ÈëGoogletrans
from googletrans import Translator
# µ¼ÈëBaiduTranslate
from youdao_trans import Translator as YoudaoTranslator
ÎÒÃÇÐèÒª±àдһ¸ö·ÒëµÄº¯Êý£¬Õâ¸öº¯ÊýÐèÒª½«ÊäÈëµÄÎı¾½øÐзÒ룬Ȼºó·µ»Ø·Òë½á¹û¡£
ʾÀý´úÂ룺
# ¶¨Òå·Ò뺯Êý£¬Ê¹Óà Googletrans ½øÐзÒë
def google_trans(text, dest='zh-CN'):
translator = Translator(service_urls=['translate.google.cn'])
trans = translator.translate(text, dest)
return trans.text
# ¶¨Òå·Ò뺯Êý£¬Ê¹Óà BaiduTranslate ½øÐзÒë
def baidu_trans(text, dest='zh'):
APP_ID = 'ÄãµÄ°Ù¶È·ÒëAPP_ID'
SECRET_KEY = 'ÄãµÄ°Ù¶È·ÒëSECRET_KEY'
translator = YoudaoTranslator(APP_ID, SECRET_KEY)
trans = translator.translate(text, dest)
return trans
±àдºÃ·Ò뺯Êýºó£¬ÎÒÃÇ¿ÉÒÔ±àдһ¸ö²âÊÔº¯ÊýÀ´²âÊÔ·Ò뺯ÊýÊÇ·ñÕý³£¹¤×÷¡£
ʾÀý´úÂ룺
# ²âÊÔ·Ò뺯Êý
def test_trans():
# ²âÊÔ Googletrans
print(google_trans('hello')) # ÄãºÃ
print(google_trans('hello', 'ja')) # ¤³¤ó¤Ë¤Á¤Ï
# ²âÊÔ BaiduTranslate
print(baidu_trans('hello')) # {'translation': ['ÄãºÃ'], 'dict': None, 'webdict': None, 'query': 'hello', 'errorCode': '0'}
print(baidu_trans('hello', 'ja')) # {'translation': ['ÄãºÃ'], 'dict': None, 'webdict': None, 'query': 'hello', 'errorCode': '0'}
ÒÔÏÂÊÇÍêÕûµÄÎı¾·ÒëÆ÷´úÂ룬¿ÉÒÔ¸ù¾ÝÐèÒª½øÐÐÐ޸ĺÍÓÅ»¯¡£
from googletrans import Translator
from youdao_trans import Translator as YoudaoTranslator
# ¶¨Òå·Ò뺯Êý£¬Ê¹Óà Googletrans ½øÐзÒë
def google_trans(text, dest='zh-CN'):
translator = Translator(service_urls=['translate.google.cn'])
trans = translator.translate(text, dest)
return trans.text
# ¶¨Òå·Ò뺯Êý£¬Ê¹Óà BaiduTranslate ½øÐзÒë
def baidu_trans(text, dest='zh'):
APP_ID = 'ÄãµÄ°Ù¶È·ÒëAPP_ID'
SECRET_KEY = 'ÄãµÄ°Ù¶È·ÒëSECRET_KEY'
translator = YoudaoTranslator(APP_ID, SECRET_KEY)
trans = translator.translate(text, dest)
return trans
# ²âÊÔ·Ò뺯Êý
def test_trans():
# ²âÊÔ Googletrans
print(google_trans('hello')) # ÄãºÃ
print(google_trans('hello', 'ja')) # ¤³¤ó¤Ë¤Á¤Ï
# ²âÊÔ BaiduTranslate
print(baidu_trans('hello')) # {'translation': ['ÄãºÃ'], 'dict': None, 'webdict': None, 'query': 'hello', 'errorCode': '0'}
print(baidu_trans('hello', 'ja')) # {'translation': ['ÄãºÃ'], 'dict': None, 'webdict': None, 'query': 'hello', 'errorCode': '0'}
# Ö´ÐвâÊÔº¯Êý
test_trans()
ÒÔÉϾÍÊÇÖÆ×÷Îı¾·ÒëÆ÷µÄÍêÕû¹¥ÂÔ¡£