我正在尝试将自定义函数添加到软件包tm_map
的{{1}}函数中,但是它将数据转换为无法继续的其他格式。
例如,我使用
tm
错误:
library(tm)
library(qdapRegex)
docs <- data.frame(doc_id = c("doc_1", "doc_2"),
text = c("This is a text. With some more text, www.yahoo.com", "This another one. with some different text www.google.com"),
dmeta1 = 1:2, dmeta2 = letters[1:2],
stringsAsFactors = FALSE)
docs = VCorpus(DataframeSource(docs))
content(docs[[1]])
docs <- tm_map(docs, content_transformer(tolower)) # This Works fine
content(docs[[1]])
nchar_rm <- function(x){
gsub(" *\\b[[:alpha:]]{1,2}\\b *", " ", x)
} # Custom function to remove characters less than 2
docs <- tm_map(docs, nchar_rm) # implement custom function
content(docs[[1]]) # returns an error.
还使用Error in UseMethod("content", x) :
no applicable method for 'content' applied to an object of class "character"
软件包中的docs <- tm_map(docs, rm_url)
函数rm_url
返回错误。
答案 0 :(得分:2)
以相同的方式使用content_transformer
library(tm)
docs <- tm_map(docs, content_transformer(nchar_rm))
content(docs[[1]])
#[1] "This text. With some more text, www.yahoo.com"
它也可以与rm_url
library(qdapRegex)
docs <- tm_map(docs, content_transformer(rm_url))
content(docs[[1]])
#[1] "This is a text. With some more text,"
但是,您可能知道可以完全不使用tm
/ lapply
/ sapply
等来使用map
函数来做到这一点。
lapply(docs$text, rm_url)
lapply(docs$text, nchar_rm)