from aip import AipNlp
from openpyxl import load_workbook
import time

# 填写你的自然语言处理应用的App ID、API Key、Secret Key
APP_ID = ''
API_Key = ''
Secret_Key = ''
client = AipNlp(APP_ID, API_Key, Secret_Key)
wb = load_workbook('./data/商品评价.xlsx')
ws = wb.active
positiveSum = 0
negativeSum = 0
confidenceSum = 0
rows = 0
# 遍历全部商品评价
for row in range(2, ws.max_row + 1):
    comment = ws.cell(row=row, column=4).value
    result = client.sentimentClassify(comment)
    time.sleep(0.5)
    positive = result['items'][0]['positive_prob']
    negative = result['items'][0]['negative_prob']
    confidence = result['items'][0]['confidence']
    # 累计属于积极类别的概率、属于消极类别的概率、分类的置信度，统计评价数量
    positiveSum += positive
    negativeSum += negative
    confidenceSum += confidence
    rows += 1
# 输出属于积极类别的概率、属于消极类别的概率、分类的置信度的平均值
print('---笔记本电脑评价的情感倾向分析---')
print('评价数量：', rows)
print('属于积极类别的概率:', round(positiveSum / rows * 100, 4), '%')
print('属于消极类别的概率:', round(negativeSum / rows * 100, 4), '%')
print('分类的置信度:', round(confidenceSum / rows * 100, 4), '%')
