2

我正在尝试使用 R“tm”包计算我的语料库中的关键字。到目前为止,这是我的代码:

# get the data strings
f<-as.vector(forum[[1]])

# replace +
f<-gsub("+", " ", f ,fixed=TRUE)

# lower case
f<-tolower(f)

# show all strings that contain mobile
mobile<- f[grep("mobile", f, ignore.case = FALSE, perl = FALSE, value = FALSE,
     fixed = FALSE, useBytes = FALSE, invert = FALSE)]
text.corp.mobile <- Corpus(VectorSource(mobile))
text.corp.mobile <- tm_map(text.corp.mobile , removePunctuation) 
text.corp.mobile <- tm_map(text.corp.mobile , removeWords, c(stopwords("english"),"mobile")) 
dtm.mobile <- DocumentTermMatrix(text.corp.mobile)
dtm.mobile 
dtm.mat.mobile <- as.matrix(dtm.mobile)
dtm.mat.mobile

这将返回一个表格,其中包含天气的二进制结果,关键字是否出现在一个语料库文本中。我不想以二进制形式获得最终结果,而是想获得每个关键字的计数。例如:'car' 出现 5 次 'button' 出现 9 次

4

1 回答 1

1

在没有看到您的实际数据的情况下,这有点难以分辨,但既然您刚刚打电话DocumentTermMatrix,我会尝试这样的事情:

dtm.mat.mobile <- as.matrix(dtm.mobile)
word.freqs <- sort(rowSums(dtm.mat.mobile), decreasing=TRUE)
于 2013-12-20T21:04:50.483 回答