在PySp中使用词典进行情感分析

2024-09-29 02:25:21 发布

您现在位置:Python中文网/ 问答频道 /正文

一开始我想说的是,我是编程新手。我花了很多时间转换我的数据集,但后来我卡住了。目标是在Pypark对2011-2019年的一段时间进行情绪分析。你知道吗

我想做的是检查Body列中的语句是否有负面情绪或正面情绪。此数据存储在一个数据帧中。为了得到正确的情绪分析,我将使用Loughran-McDonald情绪词列表,因为Body中的文本将包含一些(或许多)金融术语。包含单词和指定情感的词典存储在第二数据帧中。每个数据帧(一个带有“Body”列,第二个带有LM dictionary)包含数千行(约80行)。每个)。你知道吗

要进行情感分析,我必须使用第二个数据帧中的单词,逐列遍历第一个数据帧中的每一行Body——查看“Body”列中存储的句子中是否存在特定单词。考虑到一个句子中可能有否定词和肯定词,让我们假设一个“否定”词等于-1,一个句子中的一个肯定词等于+1。最终结果(n(-1)/(+1)p字之和)将存储在第一个数据帧的新列中。你知道吗

例如-如果Body中的某一行包含单词abandon,该单词被标记为negative(在第二个df中,数字不等于0(在本例中为2009)表示该单词被分配给情绪的特定列-在本例中为负),则新列中的结果应为-1。希望我用一种可以理解的方式描述我的问题。你知道吗

尽管我花了好几天的时间在寻找解决方案,所以我还没有找到任何与我的问题相匹配的答案:(如果有任何提示,我将不胜感激。你知道吗

当前第一个数据帧:

+---+--------------------+--------------------+----+-----+--------+---------+--------+
| Id|        CreationDate|                Body|Year|Month|Day_of_Y|Week_of_Y|Year_adj|
+---+--------------------+--------------------+----+-----+--------+---------+--------+
|  1|2011-08-30 21:12:...|What open source ...|2011|    8|     242|       35|    2011|
|  2|2011-08-30 21:14:...|GPU mining is the...|2011|    8|     242|       35|    2011|
|  8|2011-08-30 21:18:...|I would like to d...|2011|    8|     242|       35|    2011|
|  9|2011-08-30 21:18:...|I didn't get it. ...|2011|    8|     242|       35|    2011|
| 10|2011-08-30 21:19:...|Poclbm: An open s...|2011|    8|     242|       35|    2011|
+---+--------------------+--------------------+----+-----+--------+---------+--------+

第二个数据帧(Loughran-McDonald字典):

+---------+--------+--------+-----------+---------+------------+-----------+-----------+-----+
|     Word|Negative|Positive|Uncertainty|Litigious|Constraining|Superfluous|Interesting|Modal|
+---------+--------+--------+-----------+---------+------------+-----------+-----------+-----+
| aardvark|       0|       0|          0|        0|           0|          0|          0|    0|
| abalones|       0|       0|          0|        0|           0|          0|          0|    0|
|  abandon|    2009|       0|          0|        0|           0|          0|          0|    0|
+---------+--------+--------+-----------+---------+------------+-----------+-----------+-----+

Tags: of数据编程时间bodyopen单词year
1条回答
网友
1楼 · 发布于 2024-09-29 02:25:21

一种方法(不确定它是否是最有效的)是从您的情感词典创建一个实际的python词典,并将其应用到用户定义函数(UDF)中。考虑到你的情感词典大约有8万行,这应该是可行的。另外,通过先删除中性词,可以进一步加快速度。
代码大纲如下:

from pyspark.sql import functions as f
# filter neutral words
filtered_sentiment_df = sentiment_df.filter((f.col("negative") > 0) | (f.col("positive") > 0))
# the following assumes that there are no words both positive and negative
sentiments = filtered_sentiment_df.select(f.col("word"), f.when(f.col("negative") > 0, -1).otherwise(1).alias("sentiment"))

# now we got the dict and can apply it via a UDF
sentiment_dict = {row["word"]: row["sentiment"] for row in sentiments.collect()}

def calculate_sentiment_score(sentence, sentiment_dict):
    return sum([sentiment_dict.get(w, 0) for w in sentence.split(" ")])

sentiment_udf = f.udf(lambda x: calculate_sentiment_score(x, sentiment_dict))
bodies_df = bodies_df.withColumn("total_sentiment", sentiment_udf(f.col("body")))
bodies_df.show()

相关问题 更多 >