# -*- coding: utf-8 -*-
import requests
import json
#test data
test_data = """ blabla
"""
#print (test_data)
#get token url
access_token_url = "https://aip.baidubce.com/oauth/2.0/token"
#hard code
grant_type = "client_credentials"
#API Key
client_id = "lDBDRR7tulfQIr3t0d8e4d4d"
#Secret Key
client_secret = "rUCFe0jlzi9bUQnKY90xcH7BqPEwEF9s"
#sample url: https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&
payload = { 'grant_type': 'client_credentials', 'client_id': 'lDBDRR7tulfQIr3t0d8e4d4d', 'client_secret': 'rUCFe0jlzi9bUQnKY90xcH7BqPEwEF9s'}
rsp = requests.get(access_token_url, payload)
rspdata = json.loads(json.dumps(rsp.json()))
access_token = rspdata['access_token']
print (access_token)
print ("-------------------------------------------------------------------")
#词法分析
header = {'Content-Type': 'application/json'}
body = {'text': test_data}
post_url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer" + "?access_token=" + access_token
print('词法分析')
rsp = requests.post(post_url, headers=header, data=json.dumps(body))
rspdata = json.loads(json.dumps(rsp.json()))
data_str = json.dumps(rspdata, sort_keys=False, ensure_ascii=False, indent=4, separators=(',', ':'))
print (type(data_str))
print (data_str)
print ("-------------------------------------------------------------------")
#文章标签
header = {'Content-Type': 'application/json'}
body = {'title': '案件综述', 'content': test_data}
post_url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/keyword" + "?access_token=" + access_token
print ("-------------------------------------------------------------------")
print('文章标签')
rsp = requests.post(post_url, headers=header, data=json.dumps(body))
rspdata = json.loads(json.dumps(rsp.json()))
data_str = json.dumps(rspdata, sort_keys=False, ensure_ascii=False, indent=4, separators=(',', ':'))
print (type(data_str))
print (data_str)
print ("-------------------------------------------------------------------")
#文章分类
post_url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/topic" + "?access_token=" + access_token
print('文章分类')
rsp = requests.post(post_url, headers=header, data=json.dumps(body))
rspdata = json.loads(json.dumps(rsp.json()))
data_str = json.dumps(rspdata, sort_keys=False, ensure_ascii=False, indent=4, separators=(',', ':'))
print (type(data_str))
print (data_str)
print ("-------------------------------------------------------------------")
百度云 词法分析python
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...