R文本挖掘文本中的过滤字符串

时间:2015-09-19 03:33:25

标签: r text-mining

我想知道是否有一个现有的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。”。

有没有这样的功能?

2 个答案:

答案 0 :(得分:1)

我们可以使用str_extract_all中的library(stringr)。输出为list,可以unlist将其转换为vector

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

答案 1 :(得分:0)

filterWords = function(x, mywords){
  splitwords = unlist(strsplit(x, split = " "))
  return(splitwords[splitwords%in%mywords])
}

这是一种方法。然而,这将找不到像“这是”这样的两个子词的单词。但我认为它可能会给你更多关于你问的内容的信息。