无法在整个数据集上运行Stanford Core NLP注释器

问题描述 投票:1回答:1

我一直试图在数据集上使用Stanford Core NLP,但它停在某些我无法找到的索引上。数据集可在Kaggle:https://www.kaggle.com/PromptCloudHQ/amazon-reviews-unlocked-mobile-phones/data上找到

这是一个通过获取单个句子的平均情绪值来输出段落情绪的函数。

import json
def funcSENT(paragraph):
    
    
    all_scores = []
    
    output = nlp.annotate(paragraph, properties={
        "annotators": "tokenize,ssplit,parse,sentiment",
        "outputFormat": "json",
        # Only split the sentence at End Of Line. We assume that this method only takes in one single sentence.
        #"ssplit.eolonly": "true",
        # Setting enforceRequirements to skip some annotators and make the process faster
        "enforceRequirements": "false"
    })
    
    
    all_scores = []
    for i in range(0,len(output['sentences'])):
        all_scores.append((int(json.loads(output['sentences'][i]['sentimentValue']))+1))
        

    final_score = sum(all_scores)/len(all_scores)
    
    return round(final_score)
 

现在,我使用此代码在“评论”列中为每个评论运行此代码。

import pandas as pd
data_file = 'C:\\Users\\SONY\\Downloads\\Amazon_Unlocked_Mobile.csv'
data = pd.read_csv( data_file)

from pandas import *
i = 0
my_reviews = data['Reviews'].tolist()
senti = []
while(i<data.shape[0]):
    senti.append(funcSENT(my_reviews[i]))
    i=i+1

但不知怎的,我得到这个错误,我无法找到问题。现在已经很多个小时了,请帮忙。

   [1]: https://i.stack.imgur.com/qFbCl.jpg

如何避免这个错误?

python typeerror stanford-nlp sentiment-analysis keyerror
1个回答
0
投票

据我了解,您正在使用pycorenlp与nlp = StanfordCoreNLP(...)和正在运行的StanfordCoreNLP服务器。我不会检查您使用的数据,因为它似乎需要一个Kaggle帐户。

运行相同的设置,但不同的段落显示单独打印“输出”显示java服务器的错误,在我的情况下:

java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Input word not tagged

我认为,因为没有词性注释器,服务器无法执行解析。每当你使用解析或解密时,我认为你也需要有“pos”注释器。

我不确定注释器需要什么,但你可能需要其他注释器,如“引理”来获得良好的情绪结果。

单独打印输出。如果你得到相同的java错误,请尝试添加“pos”注释器,看看你是否得到了预期的json。否则,尝试给出一个更简单的例子,可能使用您自己的小数据集,并评论或调整您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.