参考了CSDN上的一篇帖子https://blog.csdn.net/fei347795790/article/details/89737951
目前已经可以返回值,但是只有对第一条数据进行评论观点抽取的结果。
总共list里酒店数据有1W多条,但是只返回了第一条数据的处理结果
{'log_id': 8924813165379044540, 'items': [{'sentiment': 2, 'abstract': '酒店交通便利', 'prop': '交通', 'begin_pos': 4, 'end_pos': 12, 'adj': '方便'}, {'sentiment': 2, 'abstract': '购物便利', 'prop': '交通', 'begin_pos': 8, 'end_pos': 8, 'adj': '方便'}, {'sentiment': 2, 'abstract': '前台的小伙子服务很热情', 'prop': '服务', 'begin_pos': 12, 'end_pos': 22, 'adj': '好'}]}
酒店交通便利
定义commentTag()里面的代码我基本上不太看得懂,就只改了个range参数。有大佬可以给我说说怎么改吗?
具体代码如下
from aip import AipNlp
import pandas as pd
APP_ID='*********'
API_KEY='**'*************‘
SECRET_KEY = '**************'
client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
def output():
urls=[]
data=pd.read_csv("C:\\Users\\MSI\\Desktop\\HotelCommentData.csv",encoding='utf-8')
urls=data.comment.tolist()
print(urls)
return urls
def commentTag():
x=output()
all={}
abst='’
for i in range(10390):
text=x[i]
options={}
options["type"]=1
result=client.commentTag(text,options)
print(result)
if "error_code" in result.keys():
abst+=''
all['abstract']=abst
else:
data=result['items']
items=data[0]
abst+=items['abstract']
all['abstract']=abst
return abst
if __name__=='__main__':
ALL=commentTag()
print(ALL)