本文目錄一覽:
- 1、Python有哪些好用的語言翻譯方法
- 2、python中文是什麼意思
- 3、po文件自動翻譯程序.. Python腳本問題
- 4、請設計一個程式,輸入key:value為英文:中文的英中翻譯字典,直至輸入0:0結束(請用 dicts )
Python有哪些好用的語言翻譯方法
1 import re
2 import urllib.parse, urllib.request
3 import hashlib
4 import urllib
5 import random
6 import json
7 import time
8 from translate import Translator
非python自帶的庫,如python google translator,需要手動安裝,命令pip install module_name。
1. 百度翻譯
1 appid = ‘your_appid’
2 secretKey = ‘your_secretKey’
3 url_baidu = ”
4
5 def translateBaidu(text, f=’ja’, t=’zh’):
6 salt = random.randint(32768, 65536)
7 sign = appid + text + str(salt) + secretKey
8 sign = hashlib.md5(sign.encode()).hexdigest()
9 url = url_baidu + ‘?appid=’ + appid + ‘q=’ + urllib.parse.quote(text) + ‘from=’ + f + ‘to=’ + t + \
10 ‘salt=’ + str(salt) + ‘sign=’ + sign
11 response = urllib.request.urlopen(url)
12 content = response.read().decode(‘utf-8’)
13 data = json.loads(content)
14 result = str(data[‘trans_result’][0][‘dst’])
15 print(result)
參數:text–待翻文本,f–初始語言,t–目標語言,後面方法類似。
2. 有道翻譯
1 url_youdao = ‘;smartresult=rulesmartresult=ugcsessionFrom=’ \
2 ”
3 dict = {}
4 dict[‘type’] = ‘AUTO’
5 dict[‘doctype’] = ‘json’
6 dict[‘xmlVersion’] = ‘1.8’
7 dict[‘keyfrom’] = ‘fanyi.web’
8 dict[‘ue’] = ‘UTF-8’
9 dict[‘action’] = ‘FY_BY_CLICKBUTTON’
10 dict[‘typoResult’] = ‘true’
11
12 def translateYoudao(text):
13 global dict
14 dict[‘i’] = text
15 data = urllib.parse.urlencode(dict).encode(‘utf-8’)
16 response = urllib.request.urlopen(url_youdao, data)
17 content = response.read().decode(‘utf-8’)
18 data = json.loads(content)
19 result = data[‘translateResult’][0][0][‘tgt’]
20 print(result)
參數主要由字典dict指定,發現沒有地方可以指定語言(可能是我沒找到),測試結果是不管輸入什麼語言的文本,輸出均是中文。
3. 谷歌翻譯
1 url_google = ”
2 reg_text = re.compile(r'(?=TRANSLATED_TEXT=).*?;’)
3 user_agent = r’Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ‘ \
4 r’Chrome/44.0.2403.157 Safari/537.36′
5
6 def translateGoogle(text, f=’ja’, t=’zh-cn’):
7 values = {‘hl’: ‘zh-cn’, ‘ie’: ‘utf-8’, ‘text’: text, ‘langpair’: ‘%s|%s’ % (f, t)}
8 value = urllib.parse.urlencode(values)
9 req = urllib.request.Request(url_google + ‘?’ + value)
10 req.add_header(‘User-Agent’, user_agent)
11 response = urllib.request.urlopen(req)
12 content = response.read().decode(‘utf-8’)
13 data = reg_text.search(content)
14 result = data.group(0).strip(‘;’).strip(‘\”)
15 print(result)
和上面兩種方法一樣,採用的是訪問網頁的形式來進行翻譯。
還有一種是利用python谷歌翻譯模塊Translator:
1 def translateGoogle2(text):
2 result = translator.translate(text)
3 print(result)
4. 測試代碼
測試過程:
翻譯5個字串為一個小的單位,輸出消耗時間;
循環10次為一個大的單位,輸出消耗時間;
對不同的語言字串和循環次數做過多次測試,發現情況基本類似,所以這裡選擇了10次。
1 text_list = [‘こんにちは’, ‘こんばんは’, ‘おはようございます’, ‘お休(やす)みなさい’, ‘お元気(げんき)ですか’]
2
3 time_baidu = 0
4 time_youdao = 0
5 time_google = 0
6 time_google2 = 0
7
8 for i in list(range(1, 11)):
9 time1 = time.time()
10 for text in text_list:
11 translateBaidu(text)
12 time2 = time.time()
13 print(‘百度翻譯第%s次時間:%s’ % (i, time2 – time1))
14 time_baidu += (time2 – time1)
15
16 time1 = time.time()
17 for text in text_list:
18 translateYoudao(text)
19 time2 = time.time()
20 print(‘有道翻譯第%s次時間:%s’ % (i, time2 – time1))
21 time_youdao += (time2 – time1)
22
23 time1 = time.time()
24 for text in text_list:
25 translateGoogle(text)
26 time2 = time.time()
27 print(‘谷歌翻譯第%s次時間:%s’ % (i, time2 – time1))
28 time_google += (time2 – time1)
29
30 time1 = time.time()
31 for text in text_list:
32 translateGoogle2(text)
33 time2 = time.time()
34 print(‘谷歌2翻譯第%s次時間:%s’ % (i, time2 – time1))
35 time_google2 += (time2 – time1)
36
37
38 print(‘百度翻譯時間:%s’ % (time_baidu / 10))
39 print(‘有道翻譯時間:%s’ % (time_youdao / 10))
40 print(‘谷歌翻譯時間:%s’ % (time_google / 10))
41 print(‘谷歌2翻譯時間:%s’ % (time_google2 / 10))
python中文是什麼意思
英文翻譯是「蟒蛇」的意思。
而實際上這個名字的來歷是發明者為了紀念他喜愛馬戲團的一個角色,角色名就是python
po文件自動翻譯程序.. Python腳本問題
比如你的第一個Python腳本叫做fanyi.py,放在D盤,而且你需要翻譯的po文件比如abc.po文件也放在D盤。
點擊開始-》運行-輸入cmd-輸入D: – 輸入 python fanyi.py abc.po
這樣就行了。
請設計一個程式,輸入key:value為英文:中文的英中翻譯字典,直至輸入0:0結束(請用 dicts )
#-*-coding:utf-8-*-
engbdict={}
d=raw_input(“請輸入英文:中文字典翻譯字彙(輸入0:0結束)-“)
while d.split(“:”)[0]!=”0″ or d.split(“:”)[1]!=”0″:
key,value=d.split(“:”)
engbdict[key]=value
d=raw_input(“請輸入英文:中文字典翻譯字彙(輸入0:0結束)-“)
for key,value in engbdict.items():
print key,value
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/244664.html