【问题标题】:Getting count of keywords using tm package in R在 R 中使用 tm 包获取关键字计数
【发布时间】:2013-12-20 20:54:43
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

    标签: r text-mining tm text-analysis


    【解决方案1】:

    没有看到您的实际数据,这有点难以分辨,但由于您刚刚致电DocumentTermMatrix,我会尝试这样的事情:

    dtm.mat.mobile <- as.matrix(dtm.mobile)
    word.freqs <- sort(rowSums(dtm.mat.mobile), decreasing=TRUE)
    

    【讨论】:

    • 好吧,因为它是 DocumentTermMatrix 而不是 TermDocumentMatrix,所以 OP 可能在列总和之后。此外,此示例将 dtm.mobile 转换为非稀疏矩阵表示,这对于大型语料库来说可能很慢和/或不可能。
    • @josilber 我很想发布一张图片来澄清我的问题,但不幸的是我没有足够的声誉:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 2013-07-07
    • 2014-07-22
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    相关资源
    最近更新 更多