R中tidytext的情感分析

时间:2017-12-03 22:01:15

标签: r text-mining sentiment-analysis tidyverse tidytext

我正在尝试在R中执行情绪分析 我想使用afinn或bing lexicon,但问题是我无法将这些词语标记出来。

以下是我需要的观点:

sentiment_words

所以有6个单词我想要他们的情绪: 通过 失败 没有准备好 出于商业 通过w /条件 没有条目

如何使用任何词典为这些词语分配情绪

这是我的代码:

d<- as.data.frame(data$Results)
d<- as.data.frame(d[1:2000,])

colnames(d) <- "text"



#Making preprocessed file for raw data
preprocess<-data.frame(text=sapply(tweet_corpus_clean, identity), 
                       stringsAsFactors=F)

# tokenize
tokens <- data_frame(text = preprocess$text) %>% unnest_tokens(word, text)

运行时我得到:

senti_new

因为对于分配情绪的词典,它必须是每行一个标记

所以我不得不将这些词合并在一起。 现在,当我使用afinn时,它无法理解什么是业务是显而易见的

tokens <- data_frame(text = preprocess$text) %>% unnest_tokens(word, text)


contributions = tokens %>%ungroup()%>%
  inner_join(get_sentiments("afinn"), by = "word") %>%
  group_by(word) %>%
  summarize(score = as.numeric(sum(score * n) / sum(n))) %>%
  arrange(desc(sentiment))

我如何对这6个单词进行情绪分析?

1 个答案:

答案 0 :(得分:0)

嗯,这对我来说听起来不像情绪分析问题。您有六个完全了解的单词/短语,并且您知道它们在您的上下文中的含义。这听起来像你只想分配这些单词/短语分数,甚至只是分配一个因子的水平。

您可以执行类似我在此处显示的内容,其中,因为分析师会决定您的每个短语的分数。在这里,scores是您作为分析师构造的数据框,每个文本选项都有合理选择的分数,df是您正在分析的数据。

library(dplyr)

scores <- data_frame(text = c("pass",
                              "fail",
                              "not ready",
                              "out of business",
                              "pass w/conditions",
                              "no entry"),
                     score = c(3, -1, 0, 0, 2, 1))

scores
#> # A tibble: 6 x 2
#>   text              score
#>   <chr>             <dbl>
#> 1 pass               3.00
#> 2 fail              -1.00
#> 3 not ready          0   
#> 4 out of business    0   
#> 5 pass w/conditions  2.00
#> 6 no entry           1.00

df <- data_frame(text = c("pass",
                          "pass",
                          "fail",
                          "not ready",
                          "out of business",
                          "no entry",
                          "fail",
                          "pass w/conditions",
                          "fail",
                          "no entry",
                          "pass w/conditions"))

df %>%
  left_join(scores)
#> Joining, by = "text"
#> # A tibble: 11 x 2
#>    text              score
#>    <chr>             <dbl>
#>  1 pass               3.00
#>  2 pass               3.00
#>  3 fail              -1.00
#>  4 not ready          0   
#>  5 out of business    0   
#>  6 no entry           1.00
#>  7 fail              -1.00
#>  8 pass w/conditions  2.00
#>  9 fail              -1.00
#> 10 no entry           1.00
#> 11 pass w/conditions  2.00

情感分析最适合您需要从中提取大量非结构化文本的地方。在这里,您只有六个文本元素,您可以使用您对域和上下文的了解来分配分数。