我正在尝试从一本意大利语书中获取文档术语矩阵。我有这本书的pdf文件,我写了几行代码:
\n#install.packages("pdftools")\nlibrary(pdftools)\nlibrary(tm)\ntext <- pdf_text("IoRobot.pdf")\n# collapse pdf pages into 1\ntext <- paste(unlist(text), collapse ="")\nmyCorpus <- VCorpus(VectorSource(text))\nmydtm <-DocumentTermMatrix(myCorpus,control = list(removeNumbers = TRUE, removePunctuation = TRUE,\n stopwords=stopwords("it"), stemming=TRUE))\ninspect(mydtm)\nRun Code Online (Sandbox Code Playgroud)\n我在最后一行之后得到的结果是:
\n<<DocumentTermMatrix (documents: 1, terms: 10197)>>\nNon-/sparse entries: 10197/0\nSparsity : 0%\nMaximal term length: 39\nWeighting : term frequency (tf)\nSample :\n Terms\nDocs calvin cosa donovan esser pi\xc3\xba poi powel prima quando robot\n 1 201 191 254 193 288 211 287 166 184 62\nRun Code Online (Sandbox Code Playgroud)\n我注意到稀疏度是 0%。这是正常的吗?
\n是的,这似乎是正确的。
文档术语矩阵是以文档为行、术语为列的矩阵,如果该术语在文档中的行 (1) 或不在行 (0) 中,则为 0 或 1。
稀疏性是指出文档术语矩阵中“0 的数量”的指标。
您可以定义一个稀疏术语,当它不在文档中时,请从此处查看。
为了理解这些要点,让我们看一个可重现的示例,它创建了与您类似的情况:
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)
Run Code Online (Sandbox Code Playgroud)
查看输出,我们可以看到您有一个文档(因此具有该语料库的 DTM 由一行组成)。
看看它:
as.matrix(DTM)
Terms
Docs here some text
1 1 1 1
Run Code Online (Sandbox Code Playgroud)
现在可以更容易理解输出:
您有一份包含三个术语的文档:
<<DocumentTermMatrix(文档:1,术语:3)>>
你的非稀疏(即!= 0 in DTM)是 3,并且sparse == 0:
非/稀疏条目:3/0
所以你的稀疏性是== 0%,因为在一个文档语料库中不能有一些 0;每个术语都属于唯一的文档,因此您将拥有所有术语:
Sparsity : 0%
Run Code Online (Sandbox Code Playgroud)
看一下另一个例子,它的术语稀疏:
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
Run Code Online (Sandbox Code Playgroud)
现在你有 3 个稀疏项 (3/5),如果你这样做,则 3/8 = 0.375 即稀疏度的 38%。