使用Python调用百度自然语言处理API

1.安装pip,执行pip install baidu-aip
2.然后在百度开放平台注册添加一个 自己的应用,生成自己的App ID,Api Key, Secret Key。然后就可以调用了,代码如下:下面是我的账号,在此隐去密码

from aip import AipNlp
APP_ID = '15367158'
API_KEY = '5t9m8RRSvwRck3adPWZVrqHO'
SECRET_KEY = '密码'

client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
text= '我爱宁宁宝贝'

#语法分析
client.lexer(text)#分词
{'log_id': 5365696327243057767,
 'text': '我爱宁宁宝贝',
 'items': [{'loc_details': [],
   'byte_offset': 0,
   'uri': '',
   'pos': 'r',
   'ne': '',
   'item': '我',
   'basic_words': ['我'],
   'byte_length': 2,
   'formal': ''},
  {'loc_details': [],
   'byte_offset': 2,
   'uri': '',
   'pos': 'v',
   'ne': '',
   'item': '爱',
   'basic_words': ['爱'],
   'byte_length': 2,
   'formal': ''},
  {'loc_details': [],
   'byte_offset': 4,
   'uri': '',
   'pos': '',
   'ne': 'PER',
   'item': '宁宁',
   'basic_words': ['宁宁'],
   'byte_length': 4,
   'formal': ''},
  {'loc_details': [],
   'byte_offset': 8,
   'uri': '',
   'pos': 'n',
   'ne': '',
   'item': '宝贝',
   'basic_words': ['宝贝'],
   'byte_length': 4,
   'formal': ''}]}
#词嵌入
client.wordEmbedding('我')
{'log_id': 1982318806902830151,
 'word': '我',
 'vec': [0.372384,
  0.403912,
  -0.389579,
  -0.156821,
  0.0858441,
  0.475236,
  ...]}
client.dnnlm(text)#语言模型
{'log_id': 3174176171138453703,
 'text': '我爱宁宁宝贝',
 'items': [{'word': '我', 'prob': 0.0161273},
  {'word': '爱', 'prob': 0.0125896},
  {'word': '宁宁', 'prob': 8.48159e-06},
  {'word': '宝贝', 'prob': 0.00672664}],
 'ppl': 542.048}
#词义相似度
word1 ='傻逼'
word2 ='智障'
client.wordSimEmbedding(word1,word2)

{'log_id': 8787237990510756615,
 'score': 0.109103,
 'words': {'word_2': '智障', 'word_1': '傻逼'}}
#短文本相似度
text2 = '宁宁爱我一辈子'
client.simnet(text,text2 )
{'log_id': 188821383576483399,
 'texts': {'text_2': '宁宁爱我一辈子', 'text_1': '我爱宁宁宝贝'},
 'score': 0.794989}
#评论观点抽取
client.commentTag(text)
{'log_id': 29687437575462791, 'error_code': 282130, 'error_msg': 'no result'}
#情感倾向分析
client.sentimentClassify(text)
{'log_id': 1674856818113157287,
 'text': '我爱宁宁宝贝',
 'items': [{'positive_prob': 0.782062,
   'confidence': 0.515693,
   'negative_prob': 0.217938,
   'sentiment': 2}]}
#文章标签分类
title = "iphone手机出现“白苹果”原因及解决办法,用苹果手机的可以看下"
content = "如果下面的方法还是没有解决你的问题建议来我们店看下成都市锦江区红星路三段99号银石广场24层01室。"
client.keyword(title,content)
{'log_id': 5437532772427730279,
 'items': [{'score': 0.99775, 'tag': 'iphone'},
  {'score': 0.862719, 'tag': '手机'},
  {'score': 0.845657, 'tag': '苹果'},
  {'score': 0.838071, 'tag': '苹果公司'},
  {'score': 0.811601, 'tag': '白苹果'},
  {'score': 0.798559, 'tag': '数码'}]}
#文本纠错
text = "百度是一家人工智障公司"
print(client.ecnet(text));
#为啥没纠错啊。。。。
{'log_id': 3468037021468636295, 'item': {'vec_fragment': [], 'score': 0, 'correct_query': '百度是一家人工智障公司'}, 'text': '百度是一家人工智障公司'}
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yang_daxia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值