R 中 tidytext 的情感分析
Sentiment analysis for tidytext in R
我正在尝试在 R 中执行情绪分析。
我想使用 afinn 或 bing 词典,但问题是我无法标记单词。
以下是我需要表达感情的词:
所以我想对 6 个词表达感情:
经过
失败
还没好
停业
通过 w/conditions
没有条目
我如何使用任何词典为这些词分配情感
这是我的代码:
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)
当 运行 我得到:
因为词典要分配情感,所以它必须是每行一个标记
所以我不得不将这些词合并在一起。
现在,当我使用 afinn 时,它无法理解什么是 outofbusiness 是显而易见的
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 个词进行情感分析?
嗯嗯,这听起来不像是情感分析问题。您有六个 words/phrases 是您确切知道的,并且您知道它们在您的上下文中的含义。这听起来好像您只想分配这些 words/phrases 分数,甚至只是一个因素的水平。
您可以像我在这里展示的那样做一些事情,其中 您 由分析师决定您的每个短语应该有什么分数。在这里,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
情感分析最适用于需要从大量非结构化文本中提取洞察力的情况。这里只有六个文本元素,您可以使用您对领域和上下文的了解来分配分数。
我正在尝试在 R 中执行情绪分析。 我想使用 afinn 或 bing 词典,但问题是我无法标记单词。
以下是我需要表达感情的词:
所以我想对 6 个词表达感情: 经过 失败 还没好 停业 通过 w/conditions 没有条目
我如何使用任何词典为这些词分配情感
这是我的代码:
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)
当 运行 我得到:
因为词典要分配情感,所以它必须是每行一个标记
所以我不得不将这些词合并在一起。 现在,当我使用 afinn 时,它无法理解什么是 outofbusiness 是显而易见的
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 个词进行情感分析?
嗯嗯,这听起来不像是情感分析问题。您有六个 words/phrases 是您确切知道的,并且您知道它们在您的上下文中的含义。这听起来好像您只想分配这些 words/phrases 分数,甚至只是一个因素的水平。
您可以像我在这里展示的那样做一些事情,其中 您 由分析师决定您的每个短语应该有什么分数。在这里,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
情感分析最适用于需要从大量非结构化文本中提取洞察力的情况。这里只有六个文本元素,您可以使用您对领域和上下文的了解来分配分数。