百度云语义分析+mongodb:try-except需要continue

# -*- coding: UTF-8 -*-
_author_ = 'zy'
_date_ = '2018/12/12 0012 14:41'
import json ,requests,urllib,pymongo,time
API_KEY=
SEC_KEY=
def get_access_token():
    """
    获取百度AI平台的Access Token
    """
    host.format(APIK=API_KEY,SECK=SEC_KEY)
    print(host)
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    rdata = json.loads(content)
    print(rdata['access_token'])
    return rdata['access_token']

def sentiment_classify(text):
    """
    获取文本的感情偏向(消极 or 积极 or 中立)
    参数:
    text:str 本文
    """
    raw = {"text":"内容"}
    raw['text'] = text
    data = json.dumps(raw).encode('utf-8')
   # AT = get_access_token()#"Access Token"
 
    host = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=UTF-8&access_token="+AT
    request = urllib.request.Request(url=host,data=data)
    request.add_header('Content-Type', 'application/json')
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    rdata = json.loads(content)
    #print(rdata['items'])#0:负向,1:中性,2:正向['items'][0]['sentiment']

    return rdata

if __name__=='__main__':
    #get_access_token()
    #result=sentiment_classify('武汉大学')
    dbname='comment1213'
    #sentiment_analysis
    #positive_prob
    #confidence
    #negative_prob
    #sentiment_value
    client = pymongo.MongoClient('127.0.0.1', 27017)  # 缺少一步骤进行属性的清洗操作,确定是否有这个值
    db = client.weibo
    cursor = db[dbname].find({'sentiment':{'$exists':False}})  # segmentation#
    for i in cursor:
        text=i['text']
        try:
            if text != None:
                result=sentiment_classify(text)
                sentimentcode=result['items'][0]['sentiment']
                if sentimentcode==1:
                    sentiment='中性'
                if sentimentcode==0:
                    sentiment = '消极'
                if sentimentcode==2:
                    sentiment = '积极'
                data={
                    'positive_prob':result['items'][0]['positive_prob'],
                    'confidence': result['items'][0]['confidence'],
                    'negative_prob': result['items'][0]['negative_prob'],
                    'sentiment': sentiment
                }
                mongoid = i['_id']
                myquery = {"_id": mongoid}
                newvalues = {"$set": data}
                db[dbname].update_one(myquery, newvalues)
                print('插入一个')
        except KeyError:
            time.sleep(1)
        continue
# except KeyError:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PDF文件大小为130M,PDF带目录索引,高清版 这是Power BI 官方中文教程的高清无删减版 连接到 Oracle数据库 在 Power b| Desktop中运行 Python脚本 在 Power查询编辑器中使用 Python 将外部 Python|DE与 Power bl一起使用 使用 Python创建 Power b|视觉村象 在 Power b| Desktop中运行R脚本 在查询编辑器中使用R 将外部R|DE与 Power b|一起使用 受支持的R包 值接在 Power b| Desktop中输入数据 在 Power Bl Desktop中连接到Exce Power b| Desktop中的 Analysis Services多维数据 通过 Power b| Desktop连接到CS∨文件 在 Power b| Desktop中连接到 Google BigQuery数据库 在 Power BI Desktop中连接到 Impala数据库 通过 Power BI Desktop连接到 OData数据源 在 Power b| Desktop中连接到 Amazon redshift数据库 通过 Power BI Desktop连接到网页 连接到 Power BI Desktop中的雪花型计算仓库 连接到 Microsoft Azure Consumption Insights 在 Power Bl Desktop中使用 SAP HANA Power BI Desktop中的 Analysis Services表格数据 在 Power b| Desktop中使用 DirectQuery Oracle和 Teradata数据库的 DirectQuery DirectQuery FA SAP Business Warehouse(BW) DirectQuery和 SAP HANA Power BI Desktop中的假设引用完整性设置 在 Power b| Desktop中使用 SAP BW连接器(预览) 在 Power b| Desktop中使用 One Drive for business链接 第三方服务:适用于 Power b| Desktop的 Facebook连接器 第三方服务:适用于 Power b| Desktop的 Google Analytics连接器 Project Online:通过 Power BI Desktop连接到数据
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值