0

我有一个推文数据框。给定的推文有多个句子。当我使用感测者的感悟函数时,它会为每个函数返回一个分数,如下所示:

sentiment(as.character(tweets$text[1]))$sentiment
>>> [1] 0.2474874 0.0000000

但是如果我想要整个推文的一个分数,我可以通过取平均分来实现这个效果

mean(sentiment(as.character(tweets$text[1]))$sentiment)
>>>[1] 0.1237437

所以,我想我可以将相同的逻辑应用于整个数据帧

tweets$sentiment <- mean(sentiment(as.character((tweets$text)))$sentiment)

但是...这将为所有推文返回相同的值。如果我放弃mean()我会得到NULL,因为有太多的句子/乐谱需要解压。

如何获得分配给数据框每一行的单个值?

4

2 回答 2

2

我们可以使用sapplysentiment功能单独应用于每个text

library(sentimentr)

tweets$text <- as.character(tweets$text)
tweets$sentiment_score <- sapply(tweets$text, function(x) 
                             mean(sentiment(x)$sentiment))
于 2020-03-10T00:27:20.700 回答
2

如果您更喜欢感性/整洁的方式,您可以执行以下操作。get_sentences()将每条推文分成句子。然后,您使用sentiment_by(). 在这里,我用作id分组变量并获得每条推文的平均情绪得分。

library(magrittr)
library(dplyr)

mytweets <- tibble(id = 1:3,
                   mytext = c("do you like it?  But I hate really bad dogs",
                              "I think the sentimentr package is great. But I need to learn how to use it",
                              "Do you like data science? I do!"))

mutate(mytweets,
      sentence_split = get_sentences(mytext)) %$%
sentiment_by(sentence_split, list(id))

   id word_count        sd ave_sentiment
1:  1         10 1.4974654    -0.8088680
2:  2         16 0.2906334     0.3944911
3:  3          7 0.1581139     0.1220192
于 2020-03-10T03:53:00.650 回答