[英]How to remove words from wordcloud R package so that they can be included in the output?
I'm using package "wordcloud" with description "Word Cloud" from the R Packages repository. 我正在使用R Packages存储库中的描述为“ Word Cloud”的软件包“ wordcloud”。 When I create wordcloud from some random text, some words are omitted automatically as they should not be a part of wordcloud.
当我从一些随机文本创建wordcloud时,某些单词会自动省略,因为它们不应该是wordcloud的一部分。
Code: 码:
library(RColorBrewer)
library(NLP)
library(wordcloud)
library(tm)
wordcloud("foo bar oh oh by by bye bingo hell no", scale=c(3,1), colors=brewer.pal(6,"Dark2"),random.order=FALSE)
Output: 输出:
I want to keep words like "oh" and "by" in the wordcloud. 我想在wordcloud中保留“ oh”和“ by”之类的词。 How?
怎么样?
Edit: I prefer doing so by removing these words from set of stopwords from wordcloud package, instead of using frequency. 编辑:我更喜欢通过从wordcloud软件包中的停用词集中删除这些单词,而不是使用频率。
Here's one way: 这是一种方法:
library(wordcloud)
library(tm)
txt <- "foo bar oh oh by by bye bingo hell no"
corp <- Corpus(VectorSource(txt))
tdm <- TermDocumentMatrix(corp, control = list(wordLengths = c(-Inf, Inf)))
m <- as.matrix(tdm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
wordcloud(d$word,d$freq,min.freq=1)
There are two ways to use wordcloud(): 有两种使用wordcloud()的方式:
The first input forces wordcloud() to call tm, constitute a corpus, remove the stopwords and this is the step where you lose the two-letter words. 第一个输入将迫使wordcloud()调用tm,构成语料库,删除停用词,这是您丢失两个字母的单词的步骤。
A simple way is to revert to the use of wordcloud that does not require the tm package, by treating your string before feeding it to wordcloud(): 一种简单的方法是,通过在将字符串提供给wordcloud()之前对其进行处理,来恢复不需要tm软件包的wordcloud的使用:
library(stringr)
library(wordcloud)
library(RColorBrewer)
## The initial string
mystring <- "foo bar oh oh by by bye bingo hell no"
## Split it and count frequencies
tabl <- table(str_split(mystring,pattern=" "))
## Make the wordcloud: all words are there!
wordcloud(names(tabl),tabl,scale=c(3,1), colors=brewer.pal(6,"Dark2"),random.order=FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.