【问题标题】:R quanteda library, error in corpus creationR quanteda 库,语料库创建错误
【发布时间】:2018-01-05 16:20:28
【问题描述】:

我有一个奇怪的错误,仅在我的同事 RStudio 运行代码时才会发生。代码是处理文本语料库,这就是我所做的:

ap.corpus <- corpus(raw.data$text) 
 ap.corpus
#Corpus consisting of 214,226 documents and 0 docvars.
ap.corpus <- Corpus(VectorSource(ap.corpus))
    ap.corpus <- tm_map(ap.corpus,tolower)
ap.corpus<-corpus(ap.corpus)

最后一步是在我进入模型之前重新格式化。我顺利运行此代码,没有任何问题。 另一方面,我的同事尝试在完全相同的数据上运行完全相同的代码,并在 ap.corpus

我们尝试重新启动 R studio,尝试在较小的语料库(只有 500 个文档)上运行,仍然出现同样的错误。 希望其他人遇到类似的错误。这似乎不是代码问题,因为我从未在我的 RStudio 中运行此代码或类似代码时遇到过此类错误。 注意:我的同事也在 R 中运行代码,避免使用 RStudio。同样的问题。

【问题讨论】:

  • 你有没有在每台机器上运行sessionInfo(),看看你和你同事的包版本是否有差异?另外,你能在你同事的机器上用 5 个文件重现错误吗?如果是这样,您能否使用dput() 并发布 5 个文档的数据,以便您的问题可以重现?
  • 感谢 Len 的建议。我会照办的。不幸的是,我今天无法尝试,因为另一台计算机在印度,但是一旦我们再次连接,我将首先对其进行测试。

标签: r corpus quanteda


【解决方案1】:

如果没有可重现的示例,这是无法验证的,但我在这里创建了一个,因为这可能是一个错误。但是,根据我尝试重现报告的错误,我认为不是。

这类问题最好在 quanteda GitHub 问题网站上提交,而不是 SO 问题。但是在这里很好解决,因为我还将向您展示一种避免使用 tm 的方法(即使您的示例没有指定这一点,很明显您正在使用它的一些功能)。

library("quanteda")
## quanteda version 0.99.22
## Using 7 of 8 threads for parallel computing

ap.corpus <- corpus(LETTERS[1:10])
ap.corpus
## Corpus consisting of 10 documents and 0 docvars.
texts(ap.corpus)
## text1  text2  text3  text4  text5  text6  text7  text8  text9 text10 
##   "A"    "B"    "C"    "D"    "E"    "F"    "G"    "H"    "I"    "J" 

ap.corpus <- tm::Corpus(tm::VectorSource(ap.corpus))
## <<SimpleCorpus>>
## Metadata:  corpus specific: 1, document level (indexed): 0
## Content:  documents: 10
ap.corpus <- tm::tm_map(ap.corpus, tolower)

corpus(ap.corpus)
## Corpus consisting of 10 documents and 0 docvars.
corpus(ap.corpus) %>% texts()
## text1  text2  text3  text4  text5  text6  text7  text8  text9 text10 
##   "a"    "b"    "c"    "d"    "e"    "f"    "g"    "h"    "i"    "j" 

所以看起来一切正常。

但是,没有必要为此使用 tm。您可以在 quanteda 中执行以下操作:

ap.corpus2 <- corpus(LETTERS[1:10])
texts(ap.corpus2) <- char_tolower(texts(ap.corpus2))
texts(ap.corpus2)
## text1  text2  text3  text4  text5  text6  text7  text8  text9 text10 
##   "a"    "b"    "c"    "d"    "e"    "f"    "g"    "h"    "i"    "j" 

但是我们不鼓励您直接修改您的语料库,因为这是一种破坏性的更改,如果您希望将其用于其他目的,您将无法恢复文本的大小写版本。

最好使用以下工作流程:

corpus(c("A B C", "C D E")) %>%
    tokens() %>%
    tokens_tolower()

## tokens from 2 documents.
## text1 :
## [1] "a" "b" "c"
## 
## text2 :
## [1] "c" "d" "e"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多