使用if-else语句对数据帧进行情感分析



我使用这个函数获得了形容词:

def getAdjectives(text):
blob=TextBlob(text)
return [ word for (word,tag) in blob.tags if tag == "JJ"]
dataset['adjectives'] = dataset['text'].apply(getAdjectives)`

我使用以下代码从json文件中获得了数据帧:

with open('reviews.json') as project_file:    
data = json.load(project_file)
dataset=pd.json_normalize(data) 
print(dataset.head())

我已经使用以下代码对数据帧进行了情绪分析:

dataset[['polarity', 'subjectivity']] = dataset['text'].apply(lambda text: pd.Series(TextBlob(text).sentiment))
print(dataset[['adjectives', 'polarity']])

这是输出:


adjectives  polarity
0                                                 []  0.333333
1  [right, mad, full, full, iPad, iPad, bad, diff...  0.209881
2                             [stop, great, awesome]  0.633333
3                                          [awesome]  0.437143
4                        [max, high, high, Gorgeous]  0.398333
5                                     [decent, easy]  0.466667
6  [it’s, bright, wonderful, amazing, full, few...  0.265146
7                                       [same, same]  0.000000
8         [old, little, Easy, daily, that’s, late]  0.161979
9                       [few, huge, storage.If, few]  0.084762

我试着过滤形容词,以确定这个代码中具有正极性、中性性和负极性的形容词:

if dataset['polarity']> 0:
print(dataset[['adjectives', 'polarity']], "Positive")

elif dataset['polarity'] == 0:
print(dataset[['adjectives', 'polarity']], "Neutral")   
else: 
print(dataset[['adjectives', 'polarity']], "Negative")

我得到了错误:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

请帮忙。

尝试使用np.select根据极性确定情绪:

df['sentiment'] = np.select(
[
dataset['polarity'] > 0,
dataset['polarity'] == 0
],
[
"Positive",
"Neutral"
],
default="Negative"
)

一个衬垫:

df['sentiment'] = np.select([dataset['polarity'] > 0, dataset['polarity'] == 0], ["Positive", "Neutral"], "Negative")

最新更新