Created
June 7, 2018 06:28
-
-
Save 2niuhe/0c7fe368c16a2ee4680f7c747f1ded55 to your computer and use it in GitHub Desktop.
It's a python translation script which can works on linux shell. 擅长中英互译。http://blog.niuhemoon.xyz/pages/2018/06/06/Python-CommandLine-Translator/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# encoding:utf-8 | |
import requests | |
import json | |
import sys | |
import argparse | |
from googletrans import Translator | |
translator = Translator(service_urls=[ | |
'translate.google.cn', | |
]) | |
# 扇贝单词 只能翻译一个单词 | |
shanbei = 'https://www.shanbay.com/api/v1/bdc/search/?version=2&word;=' | |
def shanbei_trans(word): | |
''' | |
只能翻译单个单词 | |
''' | |
try: | |
response = requests.get(shanbei + word).text | |
response = json.loads(response) | |
if response and response['msg'] == 'SUCCESS': | |
print('\033[1;31;40m') # 在shell里显示彩色字体 | |
print(word) | |
print('\033[0m') | |
print('CN:%s' % response['data']['definitions']['cn'][0]['defn']) | |
print('EN:%s' % response['data']['definitions']['en'][0]['defn']) | |
return True | |
else: | |
return False | |
except Exception: | |
return False | |
def google_trans(word,dest="zh-CN"): | |
print(translator.translate(word,dest=dest).text) | |
def detect_lang(word): | |
lang_info = translator.detect(word) | |
return lang_info.lang if lang_info.confidence > 0.9 else None | |
def args_parser(): | |
parser = argparse.ArgumentParser(description='Translate Some Shit') | |
parser.add_argument('-v', dest='verbose', action='store_true', | |
help='trans chinese to english') | |
parser.add_argument('-s', dest='shanbei', action='store_true', | |
help='use Shanbei to translate single word') | |
parser.add_argument('-d', dest='dest', action='store', | |
help='specify destination language,zh-CN default') | |
parser.add_argument(dest='source', metavar='source', nargs='*', | |
help='source word to translate') | |
args = parser.parse_args() | |
return args | |
if __name__ == '__main__': | |
args = args_parser() | |
source = ' '.join(args.source) | |
language = detect_lang(source) | |
if language == None: | |
print("No legal language detected") | |
sys.exit() | |
if args.shanbei == True : | |
shanbei_trans(args.source[0]) | |
if args.dest != None: | |
google_trans(source, dest=args.dest) | |
elif args.verbose == True or language == "zh-CN": | |
google_trans(source,dest='en') | |
else: | |
google_trans(source) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment