【问题标题】:text back to R object in tm package文本返回到 tm 包中的 R 对象
【发布时间】:2016-08-03 21:46:03
【问题描述】:

我是tm 软件包的新手,我将不胜感激。我有一堆帖子,我从中提取了不必要的符号和停用词,我使用 tm 包的各种功能完成了这些操作(见下文)。最后,我留下了 201 个包含我需要的干净字符串的文档,但是,它不是 R 对象,而是VCorpus 对象。我应该如何将这些处理过的文档全部拼接到一个文本文件中,使其成为一个长字符串?

换句话说,如何将 VCorpus 对象转换为数据框或列表或另一个 R 对象?

corpus <-iconv(posts$message, "latin1", "ASCII", sub="")

corpus <- Corpus(VectorSource(docs))
corpus <- tm_map(corpus, PlainTextDocument)
corpus <- tm_map(corpus, removePunctuation)

corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, tolower)

#remove speical characters for emails

for(j in seq(corpus))   
{   
  corpus[[j]] <- gsub("/", " ", corpus[[j]])   
  corpus[[j]] <- gsub("@", " ", corpus[[j]])   
  corpus[[j]] <- gsub("\\|", " ", corpus[[j]])   
}   


library(SnowballC)

corpus <- tm_map(corpus, stemDocument)  

#remove common English stopwords 
docs <- tm_map(docs, removeWords, stopwords("english"))

#remove words that will be common in our given context
docs <- tm_map(docs, removeWords, c("department", "email", "job", "fresher", "internship"))

#removeUrls
removeURL <- function(x) gsub("http[[:alnum:]]*", "", x)

corpus <- tm_map(corpus, removeURL)

> corpus
<<VCorpus>>
Metadata:  corpus specific: 0, document level (indexed): 0
Content:  documents: 201

【问题讨论】:

  • 不要将您的问题编辑为完全不同的问题。改为打开一个新问题。

标签: r text-mining tm corpus


【解决方案1】:

语料库是纯文本文档的列表。如果要将所有内容提取为字符数组,可以使用sapplycontent 循环遍历列表以提取所有内容

测试使用

# library(tm)
data("crude")
x <- tm_map(crude, stemDocument, lazy = TRUE)
x <- tm_map(x, content_transformer(tolower))

xx <- sapply(x, content)
str(xx)

如果需要列表,请使用 lapply 而不是 sapply

【讨论】:

  • 当我用自己的数据替换您的数据时收到以下错误:Error in UseMethod("meta", x) : no applicable method for 'meta' applied to an object of class "character"
  • 那么您应该在您的问题中提供一个可重现的示例,以便我可以看到您的语料库与我创建的示例有何不同。看起来您的 removeURL 应该包含在 content_transformer 中。我建议你阅读后一个函数的文档。
猜你喜欢
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多