有道翻译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。

本文仅供学习使用

posted on 2021-08-25 16:59  宋岳庭  阅读(991)  评论(0编辑  收藏  举报