由于在学java,需要定义变量,拼音不好,英文也差,定义变量成了头痛的事,所以写这个脚本
#!/usr/bin/python3
# pip升级不了时可以 python3 get-pip.py
# 需要安装pinyin包 pip install pinyin translate
import pinyin
import pinyin.cedict
import sys
from translate import Translator
def ZhuanHuanPinYin(zhongwen):
# 如果全是英文就不做拼音的转换
if zhongwen < u'\u4e00' or zhongwen > u'\u9fff':
result = '请参考下面的翻译:...'
return result
else:
# 全拼小写
print("全拼小写: ",pinyin.get(zhongwen, format="strip",delimiter=''))
result = ''
shouzimu = ''
for c in zhongwen:
# 首字母
shouzimu += pinyin.get_initial(c)
# 全拼首字母大写
result += pinyin.get(c, format="strip").capitalize()
print("首字母: ", shouzimu)
result = print("全拼首字母大写: ", result)
return result
def ConvertEnglish(Chinese):
translator1 = ''
for chart in Chinese:
# 检查是否包括中文字符,不包括就是英转中
if chart < u'\u4e00' or chart > u'\u9fff':
translator = Translator(from_lang="english", to_lang="chinese")
translator1 = translator.translate(Chinese)
return translator1
else:
translator = Translator(from_lang="chinese", to_lang="english")
print("中文句子转英文: " ,translator.translate(Chinese))
result = pinyin.cedict.translate_word(Chinese)
if result != None:
return result
else:
result = list(pinyin.cedict.all_phrase_translations(Chinese))
return result
ZhuanHuanPinYin(sys.argv[1])
print(ConvertEnglish(sys.argv[1]))
标签:互译,Chinese,python,pinyin,zhongwen,英文单词,result,print,translate
From: https://www.cnblogs.com/yehaixiao/p/16853304.html