2

我正在尝试制作出版物关键字的词云。例如:教育数据挖掘;协作学习;计算机科学...等

我当前的代码如下:

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)

但是,使用此代码,它会自行拆分每个单词,但我需要的是组合单词/短语。例如:教育数据挖掘是我需要展示的一个短语,而不是正在发生的事情:“教育”“数据”“挖掘”。有没有办法将每个单词组合在一起?分号可能有助于作为分隔符。

谢谢。

4

3 回答 3

4

这是一个使用不同文本包的解决方案,它允许您从统计检测到的搭配或仅通过形成所有二元组来形成多词表达。该软件包称为quanteda

library(quanteda)
packageVersion("quanteda")
## [1] ‘0.9.5.14’

首先,检测前 1,500 个二元组搭配的方法,并用它们的单标记版本(由"_"字符连接)替换文本中的这些搭配。在这里,我使用的是包的内置美国总统就职演说文本语料库。

### 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”。

wcloud1.png

由所有二元组组成的版本更容易,但会产生大量低频二元组特征。对于词云,我选择了更大的文本集,而不仅仅是 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()

这会产生:

wcloud2.png

于 2016-04-11T19:14:34.433 回答
0

好的..经过大量研究,我找到了完美的答案。首先,如果你想要 wordcloud 多个单词,这称为 bigrams。有 R 可用的软件包可以做到这一点,例如“tau”和“Rweka”。

此链接将为您提供帮助:

于 2016-04-11T12:46:21.603 回答
-3

对您的最佳建议是观看五分钟的短视频(下面的链接):

https://youtu.be/HellsQ2JF2k

如果你想要直接的 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]])

比格姆斯

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)
于 2017-08-09T20:14:40.377 回答