from aip import AipNlp
from openpyxl import load_workbook

# 填写你的自然语言处理应用的App ID、API Key、Secret Key
APP_ID = ''
API_Key = ''
Secret_Key = ''
client = AipNlp(APP_ID, API_Key, Secret_Key)
# 读取第一条商品评价（D4单元格）
wb = load_workbook('./data/商品评价.xlsx')
ws = wb.active
comment = ws.cell(row=2, column=4).value
# 调用评论观点抽取接口
options = {'type': 11}
result = client.commentTag(comment, options)
# 输出观点抽取接口返回值的关键信息
print('第1条商品评价的观点抽取：')
print('-----------------------------------')
if len(result['items']) == 0:
    print('该评论观点与本商品无关')
else:
    for item in result['items']:
        if item['sentiment'] == 1:
            print('观点中性，中性的词语为：', item['prop'], item['adj'])
        elif item['sentiment'] == 2:
            print('观点积极，积极的词语为：', item['prop'], item['adj'])
        else:
            print('观点消极，消极的词语为：', item['prop'], item['adj'])
