2021-07-11

实体识别_基于百度AI

# -*- coding:utf-8 -*-
import requests
import json

from tornado.escape import json_decode


def Tag(data):
    tag = ''

    APIKey = '官网apikey'
    secret = '官网secretkey'
    host ='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+APIKey+'&client_secret='+secret
    response = requests.get(host)
    if response:
        print(response.json()['access_token'])

    kv = {
        'accept-encoding': 'gzip, deflate',
        'connection': 'keep-alive',
        'content-type': 'application/json'
    }

    params = {
        "data": data
    }

    text = json.dumps(params)
    # print(type(text))
    url = 'https://aip.baidubce.com/rpc/2.0/kg/v1/cognitive/entity_annotation?charset=UTF-8&access_token=' + response.json()['access_token']
    response1=requests.post(url, headers=kv, timeout=5, data=text)
    if response1:
        result=json.dumps(response1.json(), sort_keys=True, indent=4, ensure_ascii=False)
        print(result)
        result1=json_decode(result)

if __name__=="__main__":
    print(Tag("C语言的祖先是BCPL语言。1967年英国剑桥大学的Martin Richards推出了没有类型的BCPL语言"))

参考文献:https://ai.baidu.com/ai-doc/NLP/7k6z52ggx#%E8%BF%94%E5%9B%9E%E6%A0%BC%E5%BC%8F
https://www.jianshu.com/p/d2fc896dff37
https://cloud.baidu.com/doc/KG/s/jk3h9ztgd
结果:在这里插入图片描述


版权声明:本文为z1077762121原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。