代码跟随b站up主“你好我是大河”的教程编写(有改动)。
流程:
import aip import time import pandas as pd from tqdm import tqdm content_list = [] positive_prob_list = [] negative_prob_list = [] sentiment_prob_list = [] def sentiment_classify(txt): client_appid = '**' ##**个人账号内容,token需要先通过AK和SK获取 client_ak = '**' client_sk = '**' my_nlp = aip.nlp.AipNlp(client_appid, client_ak, client_sk) token = '**' url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=UTF-8&access_token={}'.format(token) result = my_nlp.sentimentClassify(txt) # print(my_nlp.sentimentClassify(txt)) try: #记录错误值并继续执行 positive_prob = result['items'][0]['positive_prob'] negative_prob = result['items'][0]['negative_prob'] sentiment_prob = result['items'][0]['sentiment'] except: positive_prob ="Nan" negative_prob ="Nan" sentiment_prob="Nan" content_list.append(txt) positive_prob_list.append(positive_prob) negative_prob_list.append(negative_prob) sentiment_prob_list.append(sentiment_prob) return positive_prob df = pd.read_excel('D:/desktop/试.xlsx') #读取文件 txt_content = df["content"] #确定文本,表格第一行为content print(txt_content) positive_times = 0 negative_times = 0 for txt in tqdm(txt_content): time.sleep(1) #延时访问 if float(sentiment_classify(txt)) > 0.5: positive_times += 1 else: negative_times += 1 # print(sentiment_classify(txt)) df_res = pd.DataFrame({"content": content_list, "positive_prob": positive_prob_list, "negative_prob": negative_prob_list,"sentiment_prob": sentiment_prob_list}) df_res.to_excel("D:/desktop/结果.xlsx") #写入文件 print("{},{}".format(positive_times, negative_times)) # 正向{}条,负向{}条