大段的聊天记录存储于csv文件的text列,生成词云的原理在Jerry的文章 在SAP分析云里根据业务数据绘制词云(Word Cloud) 里有过介绍。
生成的效果见下图:
R脚本源代码:
library(wordcloud)
library(tm)
texts = as.character(sapTweets$text)
texts <- iconv(texts, 'UTF-8', 'ASCII')
texts <- gsub("http[s]?://[[:alnum:].\\/]+", "", texts)
corpus <- Corpus(VectorSource(texts))
tdm <- TermDocumentMatrix(corpus,
control = list(removePunctuation = TRUE,
stopwords = c("amp", "will", "sap", "via", "can", "just", stopwords("english")),
removeNumbers = TRUE, tolower = TRUE))
m <- as.matrix(tdm)
word_freqs <- sort(rowSums(m), decreasing=TRUE)
dm <- data.frame(word=names(word_freqs), freq=word_freqs)
wordcloud(dm$word, dm$freq, scale = c(5, 1), max.words = 50, random.order = FALSE,
rot.per = 0.2, use.r.layout = FALSE, colors=brewer.pal(8, "Dark2"))
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
网友评论