我正在尝试制作出版物关键词云。例如: 教育数据挖掘;协作学习;计算机科学等等
我目前的代码如下:
KeywordsCorpus <- Corpus(VectorSource(subset(Words$Author.Keywords, Words$Year==2012)))
KeywordsCorpus <- tm_map(KeywordsCorpus, removePunctuation)
KeywordsCorpus <- tm_map(KeywordsCorpus, removeNumbers)
# added tolower
KeywordsCorpus <- tm_map(KeywordsCorpus, tolower)
KeywordsCorpus <- tm_map(KeywordsCorpus, removeWords, stopwords("english"))
# moved stripWhitespace
KeywordsCorpus <- tm_map(KeywordsCorpus, stripWhitespace)
KeywordsCorpus <- tm_map(KeywordsCorpus, PlainTextDocument)
dtm4 <- TermDocumentMatrix(KeywordsCorpus)
m4 <- as.matrix(dtm4)
v4 <- sort(rowSums(m4),decreasing=TRUE)
d4 <- data.frame(word = names(v4),freq=v4)
但是,使用这段代码,它会将每个单词分开,但我需要的是组合单词/短语。例如:教育数据挖掘是我需要展示的一个短语,而不是正在发生的事情:&#34;教育&#34; &#34;数据&#34; &#34;挖掘&#34 ;.有没有办法将每个单词组合在一起?分号可能有助于作为分隔符。
感谢。
答案 0 :(得分:4)
这是一个使用不同文本包的解决方案,它允许您从统计检测到的搭配中形成多词表达式,或者仅通过形成所有二元组。该软件包名为 quanteda 。
library(quanteda)
packageVersion("quanteda")
## [1] ‘0.9.5.14’
首先,检测前1,500个bigram搭配的方法,并用单令牌版本(由"_"
字符连接)替换文本中的这些搭配。在这里,我使用的是美国总统就职演说文本中包的内置语料库。
### for just the top 1500 collocations
# detect the collocations
colls <- collocations(inaugCorpus, n = 1500, size = 2)
# remove collocations containing stopwords
colls <- removeFeatures(colls, stopwords("SMART"))
## Removed 1,224 (81.6%) of 1,500 collocations containing one of 570 stopwords.
# replace the phrases with single-token versions
inaugCorpusColl2 <- phrasetotoken(inaugCorpus, colls)
# create the document-feature matrix
inaugColl2dfm <- dfm(inaugCorpusColl2, ignoredFeatures = stopwords("SMART"))
## Creating a dfm from a corpus ...
## ... lowercasing
## ... tokenizing
## ... indexing documents: 57 documents
## ... indexing features: 9,741 feature types
## ... removed 430 features, from 570 supplied (glob) feature types
## ... complete.
## ... created a 57 x 9311 sparse dfm
## Elapsed time: 0.163 seconds.
# plot the wordcloud
set.seed(1000)
png("~/Desktop/wcloud1.png", width = 800, height = 800)
plot(inaugColl2dfm["2013-Obama", ], min.freq = 2, random.order = FALSE,
colors = sample(colors()[2:128]))
dev.off()
这导致以下图表。请注意搭配,例如“generation's_task”和“fellow_americans”。
与所有双字母组合形成的版本更容易,但会产生大量的低频双字母功能。对于词云,我选择了更多的文本,而不仅仅是2013年的奥巴马地址。
### version with all bi-grams
inaugbigramsDfm <- dfm(inaugCorpusColl2, ngrams = 2, ignoredFeatures = stopwords("SMART"))
## Creating a dfm from a corpus ...
## ... lowercasing
## ... tokenizing
## ... indexing documents: 57 documents
## ... removed 54,200 features, from 570 supplied (glob) feature types
## ... indexing features: 64,108 feature types
## ... created a 57 x 9908 sparse dfm
## ... complete.
## Elapsed time: 3.254 seconds.
# plot the bigram wordcloud - more texts because for a single speech,
# almost none occur more than once
png("~/Desktop/wcloud2.png", width = 800, height = 800)
plot(inaugbigramsDfm[40:57, ], min.freq = 2, random.order = FALSE,
colors = sample(colors()[2:128]))
dev.off()
这会产生:
答案 1 :(得分:-3)
最好的建议是按照短短的五分钟视频(链接如下):
如果您想直接使用R代码,请输入:
mycorpus <- Corpus(VectorSource(subset(Words$Author.Keywords,Words$Year==2012)))
文字清理 将文本转换为小写
mycorpus <- tm_map(mycorpus, content_transformer(tolower))
删除号码
mycorpus <- tm_map(mycorpus, removeNumbers)
删除英语常用术语
mycorpus <- tm_map(mycorpus, removeWords, stopwords("english"))
删除标点符号
mycorpus <- tm_map(mycorpus, removePunctuation)
消除额外的空白
mycorpus <- tm_map(mycorpus, stripWhitespace)
as.character(mycorpus[[1]])
Bigrams
minfreq_bigram<-2
token_delim <- " \\t\\r\\n.!?,;\"()"
bitoken <- NGramTokenizer(mycorpus, Weka_control(min=2,max=2, delimiters = token_delim))
two_word <- data.frame(table(bitoken))
sort_two <- two_word[order(two_word$Freq,decreasing=TRUE),]
wordcloud(sort_two$bitoken,sort_two$Freq,random.order=FALSE,scale = c(2,0.35),min.freq = minfreq_bigram,colors = brewer.pal(8,"Dark2"),max.words=150)