阅读 67

有道翻译API

有道翻译API使用起来非常简单。给定参数,发送请求,即可拿到结果。

# -*- coding: utf-8 -*-
# author: huihui
# date: 2020/2/21 5:19 下午 
import requests

url = "http://fanyi.youdao.com/translate"


def get_trans(line):
    data = {
        ‘doctype‘: ‘json‘,
        ‘type‘: ‘AUTO‘,
        ‘i‘: line
    }
    r = requests.get(url, params=data)
    result = r.json()
    print(result)


line = ‘安徽省亳州市漆园街道花园村,排列有序的光伏成为乡村一道靓丽的风景。近年来,安徽省亳州市蒙城县漆园街道通过与光伏发电企业及农业企业合作,建成运行40MW光伏发电项目,并建设290栋农业设施大棚,将太阳能发电与设施农业有机结合,实现棚顶光伏发电、棚内设施种植蔬菜、中药材等,最大限度发挥光伏电站的效益,不仅为当地农民增加收入,壮大村级集体经济,助力乡村振兴。‘
get_trans(line)

返回结果:

{‘type‘: ‘ZH_CN2EN‘, ‘errorCode‘: 0, ‘elapsedTime‘: 116, ‘translateResult‘: [
        [
            {‘src‘: ‘安徽省亳州市漆园街道花园村,排列有序的光伏成为乡村一道靓丽的风景。‘, ‘tgt‘: ‘Anhui province bozhou city paint garden street garden village, an orderly photovoltaic become village a beautiful beautiful scenery.‘
            },
            {‘src‘: ‘近年来,安徽省亳州市蒙城县漆园街道通过与光伏发电企业及农业企业合作,建成运行40MW光伏发电项目,并建设290栋农业设施大棚,将太阳能发电与设施农业有机结合,实现棚顶光伏发电、棚内设施种植蔬菜、中药材等,最大限度发挥光伏电站的效益,不仅为当地农民增加收入,壮大村级集体经济,助力乡村振兴。‘, ‘tgt‘: ‘In recent years, the anhui province bozhou city mengcheng paint garden street through cooperation with photovoltaic power generation enterprises and agricultural enterprises, the running 40 mw photovoltaic power generation project, and to build 290 greenhouses of agricultural facilities, the organic combination of solar and facility agriculture, the roof photovoltaic, relative facility cultivation of vegetables, medicinal herbs, etc., the maximum play to the benefit of the photovoltaic power station, not only for the local farmers to increase income, strengthen the village-level collective economy, boost rural revitalization.‘
            }
        ]
    ]
}

可以看到,API内部会进行分句。所以,如果是多个句子的话,需要重新join一下结果中的tgt。

本文仅供学习使用

原文:https://www.cnblogs.com/xuehuiping/p/15185897.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐