我正在尝试找到一个代码,该代码实际上可以在 R 文本挖掘包中找到最常用的两个和三个单词短语(也许还有另一个我不知道的包)。我一直在尝试使用标记器,但似乎没有运气。
如果您过去处理过类似的情况,您可以发布经过测试且实际有效的代码吗?太感谢了!
您可以将自定义标记化函数传递给tm
's DocumentTermMatrix
功能,所以如果你有包tau
安装起来相当简单。
library(tm); library(tau);
tokenize_ngrams <- function(x, n=3) return(rownames(as.data.frame(unclass(textcnt(x,method="string",n=n)))))
texts <- c("This is the first document.", "This is the second file.", "This is the third text.")
corpus <- Corpus(VectorSource(texts))
matrix <- DocumentTermMatrix(corpus,control=list(tokenize=tokenize_ngrams))
Where n
in the tokenize_ngrams
函数是每个短语的单词数。此功能也在包中实现RTextTools
,这进一步简化了事情。
library(RTextTools)
texts <- c("This is the first document.", "This is the second file.", "This is the third text.")
matrix <- create_matrix(texts,ngramLength=3)
这会返回一个类DocumentTermMatrix
与包装一起使用tm
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)