美文网首页
2018-06-13python使用腾讯的情感分析api

2018-06-13python使用腾讯的情感分析api

作者: ifqu | 来源:发表于2018-06-13 09:48 被阅读0次
# -*- coding: utf-8 -*-
import hashlib
import time
import random
import string
import urllib
from urllib.parse import quote
import sys
import requests
import json

def get_params(plus_item):
    t=time.time()
    time_stamp=int(t)
    nonce_str=''.join(random.sample(string.ascii_letters+string.digits,10))
    app_id="1106854879"
    app_key="z1BoKczrUMtsWyMC"
    text1=plus_item
    text=urllib.parse.quote(text1.encode('utf8')).upper()
    sign_before='app_id='+app_id+'&nonce_str='+nonce_str+'&text='+text+'&time_stamp='+str(time_stamp)+'&app_key='+app_key
    m=hashlib.md5()
    m.update(sign_before.encode('utf8'))
    sign=m.hexdigest()
    sign=sign.upper()
    params='app_id='+app_id+'&time_stamp='+str(time_stamp)+'&nonce_str='+nonce_str+'&sign='+sign+'&text='+text
    return params


def get_content(plus_item):
    url='https://api.ai.qq.com/fcgi-bin/nlp/nlp_textpolar'
    params =get_params(plus_item)
    url=url+'?'+params
    r=requests.get(url)
    doc=json.loads(r.text)
    return doc['data']['polar'],doc['data']['confd'],doc['data']['text']
if __name__ == '__main__':
    words=input('请输入查询内容(少于60个词):')
    a,b,c=get_content(words)
    print('情感:'+str(a)+'\n'+'程度:'+str(b)+'\n'+'文本内容:'+str(c)+'\n'+'(注:1代表正面情感;0代表中性;-1代表负面情感)')

相关文章

网友评论

      本文标题:2018-06-13python使用腾讯的情感分析api

      本文链接:https://www.haomeiwen.com/subject/vlodeftx.html