一开始我想说的是,我是编程新手。我花了很多时间转换我的数据集,但后来我卡住了。目标是在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|
+---------+--------+--------+-----------+---------+------------+-----------+-----------+-----+
一种方法(不确定它是否是最有效的)是从您的情感词典创建一个实际的python词典,并将其应用到用户定义函数(UDF)中。考虑到你的情感词典大约有8万行,这应该是可行的。另外,通过先删除中性词,可以进一步加快速度。
代码大纲如下:
相关问题 更多 >
编程相关推荐