重庆网站自己推广,网站开发怎么入账,wordpress后台主题无法管理,wordpress文章页面微信分享代码文章目录 1. 简介2. 使用步骤3. api调用实现4. 编码实现 1. 简介 前段时间在做视频语音识别生成多语种字幕时#xff0c;使用了百度翻译通用翻译api进行翻译。百度翻译平台经过个人认证之后#xff0c;每月有200万字符的免费翻译额度。还是比较舒服的。 百度翻译开放平台是百… 文章目录 1. 简介2. 使用步骤3. api调用实现4. 编码实现 1. 简介 前段时间在做视频语音识别生成多语种字幕时使用了百度翻译通用翻译api进行翻译。百度翻译平台经过个人认证之后每月有200万字符的免费翻译额度。还是比较舒服的。 百度翻译开放平台是百度翻译面向广大开发者提供开放服务的平台。服务涵盖通用翻译API、定制化翻译API、语音翻译SDK、拍照翻译SDK等。百度翻译平台地址
百度通用翻译API支持28种语言实时互译覆盖中、英、日、韩、西、法、泰、阿、俄、葡、德、意、荷、芬、丹等同时支持28种语言的语言检测。
2. 使用步骤
如果你是初次使用百度翻译开放平台你需要先注册一个百度账号。登录成功之后点击产品服务进入到通用翻译api界面。点击底部立即使用经过认证之后就可以使用了。个人能够申请标准版和高级版认证(推荐支持并发数高)。 认证完整之后你就能获取到对应的appid和secret密钥这些在调用api的时候需要使用。
3. api调用实现
可以通过以上网址查看通用翻译的API文档。api文档.我这里只展示几个重要的内容。 API调用网址https://fanyi-api.baidu.com/api/trans/vip/translate api调用参数 签名生成方法:签名对应api参数中的sign。 api支持get和post请求但我推荐使用post请求因为get请求存在url长度限制(服务器或浏览器限制)需要翻译的文本过长使用get请求就会出错而post请求就没有这个限制。 api返回结果值
4. 编码实现 基类定义规范,后期可以定义其他平台的翻译实现类 # encoding:utf-8
__author__ ObsessedCEfrom abc import ABC, abstractmethodclass Translation(ABC):abstractmethoddef translation(self, text, src_language auto, des_language zh):pass具体实现类具体实现调用百度通用翻译api的类 #codingutf-8
__author__ ObsessedCE
import requests
import file_util
import sys
import hashlib
import random
import string
import translationclass BAIDUTranslation(translation.ABC):def __init__(self, profile):self.appid profile.get(baidu_app_id)self.secret profile.get(baidu_secret)self.url profile.get(baidu_translation_url)self.session requests.Session()self.session.trust_env Falsedef translation(self, text, src_language auto, des_language zh):header {content-type:application/x-www-form-urlencoded}data {q : text,from : src_language,to : des_language,appid:self.appid,salt:,sign:}if not text:print(Unspecified content)return Nonesalt BAIDUTranslation.generate_random_string(6)data[salt] saltdata[sign] self.generate_sign(data)try:response self.session.post(url self.url, data data, headers header)if response.status_code ! 200:print(f调用百度翻译出错状态码为: {str(response.status_code)})return Noneresponse_data response.json()if response_data.get(code) and response_data.get(code )! 52000:print(f调用百度翻译出错返回错误代码为: {response_data.get(code)})return Nonetranslation_result list()trans_result response_data.get(trans_result)for result in trans_result:src result.get(src)des result.get(dst)translation_result.append(des)return translation_resultexcept Exception as e:print(f调用翻译请求时出现错误: {e})def generate_sign(self, data):生成签名:param data::return:str data.get(appid)str data.get(q)str data.get(salt)str self.secretreturn self.generate_md5(str)def generate_md5(self, content):进行内容md5加密发挥全小写的编码:param content::return:if content:md5_hash hashlib.md5()md5_hash.update(content.encode(utf-8))return md5_hash.hexdigest().lower()staticmethoddef generate_random_string(length6):# 可用字符大写字母、小写字母和数字chars string.ascii_letters string.digits# 随机选择字符并生成指定长度的字符串return .join(random.choices(chars, klength))if __name__ __main__:profile file_util.read_file(./profile.yml)if not profile:print(no profile)sys.exit(0)baidu_transltion BAIDUTranslation(profile)text You look so handsome today\nI think so, tooprint(baidu_transltion.translation(text, )) 文件辅助类读取配置文件 # encoding:utf-8
__author__ ObsessedCE
import yaml
import sysdef read_file(file_path):try:with open(file_path, r, encodingutf-8) as file:data yaml.safe_load(file)return dataexcept Exception as e:print(e)sys.exit(0)配置文件定义格式,文件类型为yml定义个人appid和密钥 baidu_app_id: 个人信息查看
baidu_secret: 个人信息中查看
baidu_translation_url: https://fanyi-api.baidu.com/api/trans/vip/translate最后看一下调用效果.
文章转载自: http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.mnsmb.cn.gov.cn.mnsmb.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.srzhm.cn.gov.cn.srzhm.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.yccnj.cn.gov.cn.yccnj.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.wdskl.cn.gov.cn.wdskl.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.hhqtq.cn.gov.cn.hhqtq.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.lmxzw.cn.gov.cn.lmxzw.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.fgtls.cn.gov.cn.fgtls.cn http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.nmnhs.cn.gov.cn.nmnhs.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.mszls.cn.gov.cn.mszls.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.lizimc.com.gov.cn.lizimc.com http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn