我有一个由正词和负词关联组成的数据表。我想创建两个词云,一个用于肯定词,一个用于否定词。
sentiment_words
表的示例:
element_id sentence_id negative positive
1115: 1 1115 limits agree,available
1116: 1 1116 slow strongly,agree
1117: 1 1117 management
1118: 1 1118
1119: 1 1119 concerns strongly,agree,better,
我正在使用library(wordcloud)
和library(sentimentr)
例如,如何仅从“正”列中拉出单词以创建wordcloud?我不确定如何解决每行有多个单词的事实(例如,“同意,可用”应视为两个条目)
我对wordcloud()
函数进行了不同的尝试,例如
wordcloud(words = sentiment_words$positive, freq = 3, min.freq = 1, max.words = 200, random.order = FALSE, rot.per=0.35, colors=brewer.pal(8, "Dark2"))
,但这只会返回术语在第一个条目中的云
编辑:我已经尝试过下面的tidyverse
答案,得到的结果是:
words n
<chr> <int>
1 " \"ability\"" 3
2 " \"ability\")" 1
3 " \"acceptable\")" 1
4 " \"accomplish\"" 1
5 " \"accomplished\")" 1
6 " \"accountability\"" 1
7 " \"accountability\")" 1
8 " \"accountable\"" 2
9 " \"accountable\")" 1
我已经尝试将gsub()
和apply
的变体相乘以删除多余的)
和c(
,但是还没有找到任何可行的方法。结果是应该一起计算的单词被分别计算(例如,“可接受的”和“可接受的”是单词云中的两个不同的单词)
编辑:为了使其能够正常工作,我必须按照以下建议先清理sentiment_words
。
for (j in seq(sentiment_words)) {
sentiment_words[[j]] <- gsub("character(0)", "", sentiment_words[[j]])
sentiment_words[[j]] <- gsub('"', "", sentiment_words[[j]])
sentiment_words[[j]] <- gsub("c\\(", "", sentiment_words[[j]])
sentiment_words[[j]] <- gsub(" ", "", sentiment_words[[j]])
sentiment_words[[j]] <- gsub("\\)", "", sentiment_words[[j]])
}
,而且我还必须过滤掉count_words
函数中其余的“ character(0”)字符串,请注意,它过滤了“ character(0”而不是“ character(0)”,因为我删除了右括号以上
filter(!!var != "character(0") %>%
实施上述操作可以根据文本的极性提供最干净的文字云
答案 0 :(得分:1)
这是一种基于tidyverse
的方法,可以帮助您入门。我同意Mr_Z的意见,因为我不清楚问题出在哪里。
让我们定义一个函数,该函数根据源数据data.frame
的特定列var
中逗号分隔的单词来生成一个单词计数的df
。
library(tidyverse)
count_words <- function(df, var) {
var <- enquo(var)
df %>%
separate_rows(!!var, sep = ",") %>%
filter(!!var != "") %>%
group_by(!!var) %>%
summarise(n = n()) %>%
rename(words = !!var)
}
然后我们可以为positive
和negative
列生成单词计数
df.pos <- count_words(df, positive)
df.neg <- count_words(df, negative)
让我们检查data.frame
df.pos
# A tibble: 5 x 2
words n
<chr> <int>
1 agree 3
2 available 1
3 better 1
4 management 1
5 strongly 2
df.neg
# A tibble: 3 x 2
words n
<chr> <int>
1 concerns 1
2 limits 1
3 slow 1
让我们画出云这个词
library(wordcloud)
wordcloud(words = df.pos$words, freq = df.pos$n, min.freq = 1,
max.words = 200, random.order = FALSE, rot.per = 0.35,
colors = brewer.pal(8, "Dark2"))
wordcloud(words = df.neg$words, freq = df.neg$n, min.freq = 1,
max.words = 200, random.order = FALSE, rot.per = 0.35,
colors = brewer.pal(8, "Dark2"))
答案 1 :(得分:0)
我强烈建议您不要在此处使用已接受的答案,因为它忽略了 sentimentr 已经为您返回了计算的计数(通过attributes(sentiment_words)$counts
)。 documentation for extract_sentiment_terms
shows examples使这一点更加清楚(存在改进返回的文档的空间,并且已在开发版本https://github.com/trinker/sentimentr/blob/master/R/extract_sentiment_terms.R中添加了该文档)。下面我展示了如何提取用于wordcloud和一些潜在布局的计数:
library(sentimentr)
library(wordcloud)
library(data.table)
set.seed(10)
x <- get_sentences(sample(hu_liu_cannon_reviews[[2]], 1000, TRUE))
sentiment_words <- extract_sentiment_terms(x)
sentiment_counts <- attributes(sentiment_words)$counts
sentiment_counts[polarity > 0,]
par(mfrow = c(1, 3), mar = c(0, 0, 0, 0))
## Positive Words
with(
sentiment_counts[polarity > 0,],
wordcloud(words = words, freq = n, min.freq = 1,
max.words = 200, random.order = FALSE, rot.per = 0.35,
colors = brewer.pal(8, "Dark2"), scale = c(4.5, .75)
)
)
mtext("Positive Words", side = 3, padj = 5)
## Negative Words
with(
sentiment_counts[polarity < 0,],
wordcloud(words = words, freq = n, min.freq = 1,
max.words = 200, random.order = FALSE, rot.per = 0.35,
colors = brewer.pal(8, "Dark2"), scale = c(4.5, 1)
)
)
mtext("Negative Words", side = 3, padj = 5)
sentiment_counts[,
color := ifelse(polarity > 0, 'red',
ifelse(polarity < 0, 'blue', 'gray70')
)]
## Together
with(
sentiment_counts[polarity != 0,],
wordcloud(words = words, freq = n, min.freq = 1,
max.words = 200, random.order = FALSE, rot.per = 0.35,
colors = color, ordered.colors = TRUE, scale = c(5, .75)
)
)
mtext("Positive (red) & Negative (blue) Words", side = 3, padj = 5)