【问题标题】:DocumentTermMatrix with Sparsity 0%稀疏度为 0% 的 DocumentTermMatrix
【发布时间】:2021-02-18 00:00:24
【问题描述】:

我正在尝试从意大利语书中获取文档术语矩阵。我有这本书的pdf文件,我写了几行代码:

#install.packages("pdftools")
library(pdftools)
library(tm)
text <- pdf_text("IoRobot.pdf")
# collapse pdf pages into 1
text <- paste(unlist(text), collapse ="")
myCorpus <- VCorpus(VectorSource(text))
mydtm <-DocumentTermMatrix(myCorpus,control = list(removeNumbers = TRUE, removePunctuation = TRUE,
                                 stopwords=stopwords("it"), stemming=TRUE))
inspect(mydtm)

我在最后一行之后得到的结果是:

<<DocumentTermMatrix (documents: 1, terms: 10197)>>
Non-/sparse entries: 10197/0
Sparsity           : 0%
Maximal term length: 39
Weighting          : term frequency (tf)
Sample             :
    Terms
Docs calvin cosa donovan esser piú poi powel prima quando robot
   1    201  191     254   193 288 211   287   166    184   62

我注意到稀疏度为 0%。这正常吗?

【问题讨论】:

    标签: r pdf tm


    【解决方案1】:

    是的,这似乎是正确的。
    document term matrix 是一个矩阵,它以行为文档,以列为术语,如果术语在文档中的行 (1) 或不在 (0) 中,则为 0 或 1。
    稀疏性是指出文档术语矩阵中“0 的数量”的指标。
    您可以定义一个稀疏术语,当它不在文档中时,从here 查找。

    为了理解这些要点,让我们看一个可重现的示例,该示例会创建类似于您的情况:

    library(tm)
    text <- c("here some text")
    corpus <- VCorpus(VectorSource(text))
    DTM <- DocumentTermMatrix(corpus)
    DTM
    
    <<DocumentTermMatrix (documents: 1, terms: 3)>>
    Non-/sparse entries: 3/0
    Sparsity           : 0%
    Maximal term length: 4
    Weighting          : term frequency (tf)
    

    查看输出,我们可以看到您有一个文档(因此具有该语料库的 DTM 由一行组成)。
    看看它:

    as.matrix(DTM)
        Terms
    Docs here some text
       1    1    1    1
    

    现在可以更容易理解输出了:

    • 您有一个包含三个术语的文档:

      >

    • 你的非稀疏(即!= 0 in DTM)是3,而sparse == 0

      非/稀疏条目:3/0

    所以你的稀疏度是== 0%,因为你不能在一个文档语料库中有一些0;每个术语都属于唯一的文档,因此您将拥有所有术语:

      Sparsity           : 0%
    

    看一个不同的例子,它有稀疏的术语:

    text <- c("here some text", "other text")
    
    corpus <- VCorpus(VectorSource(text))
    DTM <- DocumentTermMatrix(corpus)
    
    DTM
    <<DocumentTermMatrix (documents: 2, terms: 4)>>
    Non-/sparse entries: 5/3
    Sparsity           : 38%
    Maximal term length: 5
    Weighting          : term frequency (tf)
    
    as.matrix(DTM)
        Terms
    Docs here other some text
       1    1     0    1    1
       2    0     1    0    1
    

    现在您有 3 个稀疏项 (3/5),如果您使用 3/8 = 0.375,即 38% 的稀疏度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-30
      • 2013-05-28
      • 2018-01-19
      • 1970-01-01
      • 2019-08-05
      • 2018-08-22
      • 2020-07-30
      • 2021-09-21
      相关资源
      最近更新 更多