在文本挖掘中,合并变形词的词频是很必要的。虽然步骤较简单,但很多人都没有注意。
下面比较一下合并跟不合并的差别:
library("tm") library("wordcloud") data(crude) crude <- tm_map(crude,removePunctuation) crude <- tm_map(crude, function(x) removeWords(x, stopwords())) tdm <- TermDocumentMatrix(crude) m <- as.matrix(tdm) v <- sort(rowSums(m), decreasing = TRUE) d <- data.frame(word = names(v), freq = v) cls <- c("gray50", brewer.pal(8, "Dark2"), "orangeRed") wordcloud(d$word,d$freq, scale = c(6, 0.5), color = cls, random.order = FALSE) subfix <- c("s", "es", "ed", "ing", "y", "ive", "ic", "al", "ous", "ious", "ish", "able", "ible", "ize", "ise") del <- 0 for (ss in subfix) { w1 <- d$word w2 <- paste0(w1,ss) sel <- w2 %in% w1 pls <- w1 %in% w2 if (sum(pls) > 0) { f1 <- d$freq f1[sel] <- f1[sel] + f1[pls] d$freq <- f1 d <- d[!pls,] del <- del + sum(pls) } } del
## [1] 104
wordcloud(d$word, random.order = FALSE)
可以看到合并后opec(欧佩克),market(市场),Kuwait(科威特)等词的重要性明显提高。
安装 SnowballC 软件包后也可以用?tm_map(x,stemDocument) 合并变形词,但效果很差,可以试试。