python翻元函数 python翻译函数

Python编程-翻译密码?区分大小写python翻元函数的凯撒密码 。
在凯撒密码python翻元函数的基础上针对大写与小字字符区分处理即可python翻元函数:
解密只需要将7换成19(因为26-7=19),或者使用-7也可以:
print(caesarcipher(caesarcipher('Student!', 7),19))
python语言翻译的过程是“输入文本”—“翻译”—“得到译文” 。详细步骤如下:
1、先输入文字 。
2、首先调用两个需要到的第三方库,设置请求头,因为百度翻译反爬机制,经过观察只加密了sign数据,由代码计算 , 将js文件保存在根目录下 。
3、设置data参数 , 获取cookies,发送 post请求 , 返回的是经过‘utf-8’编码后的字符串,我们要对其进行解码 , 并且转化为字典 , 直接对数据进行类型转换会报错“NameError: name 'null' is not defined” 。
4、在数据(字典)中将我们要的结果提取出来 。
python找错误,实现汉英翻译单词python翻元函数我改python翻元函数了一下
dictionary={}# 创建一个空字典
# 定义一个函数 , 功能:向字典中增加记录
# dictionary 是字典,en 是英文单词,ch 是对应中文单词
def add_dict(dictionary, en, ch):
dictionary[en] = ch# 增添or 更新一条记录
dictionary[ch] = en
print("添加成功")

# 定义一个函数,功能:翻译
# dictionary 是字典,string 是要查找python翻元函数的单词(中文or 英文)
def find(dictionary, string):
if string not in dictionary:# 如果string 不在dict 内,打印提示信息
print("该单词不在dict 内")
else:# 否则,给出对应中文释义
print("该单词", string, "的意思是:", dictionary[string])
# 向字典内增添几个记录,测试增加记录的功能,也可尝试用while 循环持续接收用户添加词条
for i in range(3):
en = input("增添的英文单词: ")# 接受输入
ch = input("对应的中文单词: ")
add_dict(dictionary, en, ch)# 调用add_dict 函数,往字典中添加内容
# 接收用户输入,调用find 函数实现翻译
string = input("请输入要查询的单词: ")
find(dictionary, string)
运行效果:
增添的英文单词: apple
对应的中文单词: 苹果
添加成功
增添的英文单词: banana
对应的中文单词: 香蕉
添加成功
增添的英文单词: peach
对应的中文单词: 桃子
添加成功
请输入要查询的单词: peach
该单词 peach 的意思是: 桃子
错误应该是python翻元函数你定义函数时的变量名(dictionary)和函数内部的变量名(dict)不一致导致的,还有python翻元函数你在测试add_dict的时候把add_dict的返回值None赋给了一个名叫dictionary的变量,这是完全没必要的,并且导致了和现有的dictionary的冲突 , 使得第二次循环添加单词时出现错误 。
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'):
6salt = random.randint(32768, 65536)
7sign = appidtextstr(salt)secretKey
8sign = hashlib.md5(sign.encode()).hexdigest()
9url = url_baidu'?appid='appid'q='urllib.parse.quote(text)'from='f'to='t\
10'salt='str(salt)'sign='sign
11response = urllib.request.urlopen(url)
12content = response.read().decode('utf-8')
13data = https://www.04ip.com/post/json.loads(content)
14result = str(data['trans_result'][0]['dst'])
15print(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):
13global dict
14dict['i'] = text
15data = https://www.04ip.com/post/urllib.parse.urlencode(dict).encode('utf-8')
16response = urllib.request.urlopen(url_youdao, data)
17content = response.read().decode('utf-8')
18data = https://www.04ip.com/post/json.loads(content)
19result = data['translateResult'][0][0]['tgt']
20print(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) ' \
4r'Chrome/44.0.2403.157 Safari/537.36'
5
6 def translateGoogle(text, f='ja', t='zh-cn'):
7values = {'hl': 'zh-cn', 'ie': 'utf-8', 'text': text, 'langpair': '%s|%s' % (f, t)}
8value = https://www.04ip.com/post/urllib.parse.urlencode(values)
9req = urllib.request.Request(url_google'?'value)
10req.add_header('User-Agent', user_agent)
11response = urllib.request.urlopen(req)
12content = response.read().decode('utf-8')
13data = https://www.04ip.com/post/reg_text.search(content)
14result = data.group(0).strip(';').strip('\'')
15print(result)
和上面两种方法一样 , 采用的是访问网页的形式来进行翻译 。
还有一种是利用python谷歌翻译模块Translator:
1 def translateGoogle2(text):
2result = translator.translate(text)
3print(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)):
9time1 = time.time()
10for text in text_list:
11translateBaidu(text)
12time2 = time.time()
13print('百度翻译第%s次时间:%s'%(i, time2 - time1))
14time_baidu= (time2 - time1)
15
16time1 = time.time()
17for text in text_list:
18translateYoudao(text)
19time2 = time.time()
20print('有道翻译第%s次时间:%s' % (i, time2 - time1))
21time_youdao= (time2 - time1)
22
23time1 = time.time()
24for text in text_list:
25translateGoogle(text)
26time2 = time.time()
27print('谷歌翻译第%s次时间:%s'%(i, time2 - time1))
28time_google= (time2 - time1)
29
30time1 = time.time()
31for text in text_list:
32translateGoogle2(text)
33time2 = time.time()
34print('谷歌2翻译第%s次时间:%s' % (i, time2 - time1))
35time_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翻译函数】python翻元函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python翻译函数、python翻元函数的信息别忘了在本站进行查找喔 。

    推荐阅读