'list'对象没有属性'encode':情感分析

'list' object has no attribute 'encode': sentiment analysis

我想使用 Vader 对一些文本进行情感分析(但我在这里描述的问题也适用于任何词典,除了 Vader)。 但是,在完成所有数据处理(包括标记化和转换为小写字母(我未在此处提及))之后,出现以下错误:

知道如何处理文档以便词典可以阅读文本吗?谢谢。

AttributeError: 'list' object has no attribute 'encode'

with open('data_1.txt') as g:
    data_1 = g.read()
with open('data_2.txt') as g:
    data_2 = g.read()
with open('data_3.txt') as g:
    data_3 = g.read()

df_1 = pd.DataFrame({"text":[data_1, data_2, data_3]})

df_1.head()
                                                 text
#0  [[bangladesh, education, commission, report, m...
#1  [[english, version, glis, ministry, of, educat...
#2  [[national, education, policy, 2010, ministry,...

from nltk.sentiment.vader import SentimentIntensityAnalyzer
vader = SentimentIntensityAnalyzer()

df_1['Vader_sentiment'] = df_1.text.apply(lambda x: vader.polarity_scores(x)['compound'])

AttributeError: 'list' object has no attribute 'encode'

df_1.text 是一系列列表列表。您不能将 VADER 应用于任何列表,尤其是列表的列表。将列表转换为字符串,然后应用 VADER:

df_1['text_as_string'] = df_1['text'].str[0].str.join(" ")
df_1['text_as_string'].apply(lambda x: vader.polarity_scores(x)['compound'])