【问题标题】:TermDocumentMatrix errors in RR中的TermDocumentMatrix错误
【发布时间】:2014-10-22 11:25:29
【问题描述】:

我一直在研究 R 中 {tm} 包的大量在线示例,试图创建一个 TermDocumentMatrix。创建和清理语料库非常简单,但是当我尝试创建矩阵时总是遇到错误。错误是:

UseMethod("meta", x) 中的错误: 没有适用于“元”的方法应用于“字符”类的对象 另外:警告信息: 在 mclapply(unname(content(x)), termFreq, control) : 所有计划的核心都在用户代码中遇到错误

例如,这里是来自 Jon Starkweather 的文本挖掘 example 的代码。提前为这么长的代码道歉,但这确实产生了一个可重现的例子。请注意,错误出现在 {tdm} 函数的末尾。

#Read in data
policy.HTML.page <- readLines("http://policy.unt.edu/policy/3-5")

#Obtain text and remove mark-up
policy.HTML.page[186:202]
id.1 <- 3 + which(policy.HTML.page == "                    TOTAL UNIVERSITY        </div>")
id.2 <- id.1 + 5
text.data <- policy.HTML.page[id.1:id.2]
td.1 <- gsub(pattern = "<p>", replacement = "", x = text.data, 
     ignore.case = TRUE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

td.2 <- gsub(pattern = "</p>", replacement = "", x = td.1, ignore.case = TRUE,
     perl = FALSE, fixed = FALSE, useBytes = FALSE)

text.d <- td.2; rm(text.data, td.1, td.2)

#Create corpus and clean 
library(tm)
library(SnowballC)
txt <- VectorSource(text.d); rm(text.d)
txt.corpus <- Corpus(txt)
txt.corpus <- tm_map(txt.corpus, tolower)
txt.corpus <- tm_map(txt.corpus, removeNumbers)
txt.corpus <- tm_map(txt.corpus, removePunctuation)
txt.corpus <- tm_map(txt.corpus, removeWords, stopwords("english"))
txt.corpus <- tm_map(txt.corpus, stripWhitespace); #inspect(docs[1])
txt.corpus <- tm_map(txt.corpus, stemDocument)

# NOTE ERROR WHEN CREATING TDM
tdm <- TermDocumentMatrix(txt.corpus)

【问题讨论】:

  • 我看过这篇文章,你的问题让我想起了这一点。看看this link。这可能很有用。
  • @jazzurro -- 非常感谢您将我重定向到这篇文章!在 tm_map 函数的 tolower 中添加 content_transformer 解决了问题
  • 我实际上遇到了同样的问题并看到了那个帖子。我很高兴您的脚本现在正在运行。

标签: r text-mining tm corpus term-document-matrix


【解决方案1】:

jazzurro 提供的链接指向解决方案。下面这行代码

 txt.corpus <- tm_map(txt.corpus, tolower)

必须改为

 txt.corpus <- tm_map(txt.corpus, content_transformer(tolower))

【讨论】:

    【解决方案2】:

    在 tm v0.6 中有两个原因。

    1. 如果您正在执行诸如tolower 等术语级别的转换,tm_map 返回字符向量而不是PlainTextDocument
      解决方案:通过content_transformer 调用tolower 或在tolower 之后立即致电tm_map(corpus, PlainTextDocument)
    2. 如果未安装 SnowballC 软件包,并且如果您试图阻止文档,那么也会发生这种情况。
      解决方案install.packages('SnowballC')

    【讨论】:

      【解决方案3】:

      无需申请 content_transformer。

      以这种方式创建语料库:

      trainData_corpus <- Corpus((VectorSource(trainData$Comments)))
      

      试试看。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-30
        • 1970-01-01
        • 2020-08-24
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多