【问题标题】:R: Data mining. Replacing words containing a substringR:数据挖掘。替换包含子字符串的单词
【发布时间】:2017-04-22 17:05:59
【问题描述】:

我想将语料库中包含“kind”的每个单词都替换为“Kindertoekomst”。我可以正常做:

Woorden<-c("kinderen", "kleinkind")
Woorden[grepl("kind", Woorden)]<-"Kindertoekomst"

但我想在我的 Corpus 中进行。

我设法用

做到了这一点
Kind<-grepl("kind", Woorden)
docs <- tm_map(docs, function(x) stri_replace_all_fixed(x, Woorden[as.logical(Kind)], "kindertoekomst", vectorize_all = FALSE))

但是我不能再使用其他功能了:

dtm <- DocumentTermMatrix(docs)

错误:inherits(doc, "TextDocument") 不是 TRUE

并且 corpus_clean

请帮帮我:)

【问题讨论】:

    标签: r string replace text-mining tm


    【解决方案1】:

    tm 包中使用content_transformer() 函数包装器的替代方法

    library(tm)
    
    Woorden<-c("kinderen", "kleinkind")
    
    rep_kind <- function(x){ 
      gsub("\b.*kind.*\b","Kindertoekomst",x)
    }
    
    docs <- Corpus(VectorSource(as.list(Woorden)))
    docs <- tm_map(docs, content_transformer(rep_kind))
    dtm <- DocumentTermMatrix(docs)
    inspect(dtm)
    

    【讨论】:

      【解决方案2】:

      这应该可行:

      docs <- tm_map(docs, function(x) stri_replace_all_fixed(x, Woorden[as.logical(Kind)], "kindertoekomst", vectorize_all = FALSE))
      docs <- tm_map(docs, PlainTextDocument) 
      dtm <- DocumentTermMatrix(docs)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-19
        • 2022-01-22
        • 2021-05-26
        • 2012-05-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-20
        • 1970-01-01
        相关资源
        最近更新 更多