【问题标题】:R text mining filtering string from textR文本挖掘从文本中过滤字符串
【发布时间】:2015-09-19 17:06:16
【问题描述】:

我想知道是否存在将文本和字符串列表作为输入的现有 R 函数,是否会过滤掉在文本中找到的列表中的匹配字符串?

例如,

x <- "This is a new way of doing things."
mywords <- c("This is", "new", "not", "maybe", "things.")
filtered_words <- Rfunc(x, mywords)

然后filtered_words 将包含“This is”、“new”和“things”。

有这样的功能吗?

【问题讨论】:

    标签: r text-mining


    【解决方案1】:

    我们可以从library(stringr) 使用str_extract_all。输出将是list,可以将unlisted 转换为vector

    library(stringr)
    unlist(str_extract_all(x, mywords))
    #[1] "This is" "new"     "things."
    

    【讨论】:

      【解决方案2】:
      filterWords = function(x, mywords){
        splitwords = unlist(strsplit(x, split = " "))
        return(splitwords[splitwords%in%mywords])
      }
      

      这是一种方法。但是,这不会找到带有“this is”之类的两个子词的词。但我认为它可能会为您提供有关您所问内容的更多信息。

      【讨论】:

        猜你喜欢
        • 2015-06-11
        • 1970-01-01
        • 2015-07-17
        • 2011-11-10
        • 1970-01-01
        • 2013-04-29
        • 2019-03-27
        • 2016-02-22
        • 1970-01-01
        相关资源
        最近更新 更多