gpt4 book ai didi

python - 在 PySpark 中使用字典进行情感分析

转载 作者:行者123 更新时间:2023-11-28 18:56:38 26 4
gpt4 key购买 nike

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

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

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

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

尽管花了几天时间在 SO 上寻找解决方案,但我没有找到任何符合我的问题的答案:( 我将不胜感激任何提示。

当前第一个数据框:

+---+--------------------+--------------------+----+-----+--------+---------+--------+
| 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|
+---------+--------+--------+-----------+---------+------------+-----------+-----------+-----+

最佳答案

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

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()

关于python - 在 PySpark 中使用字典进行情感分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57453094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com