R文本从文本中挖掘过滤字符串
R text mining filtering string from text
我想知道是否存在给定文本和字符串列表作为输入的现有 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."。
有没有这个功能?
我们可以使用 library(stringr)
中的 str_extract_all
。输出将是 list
,可以 unlist
ed 将其转换为 vector
。
library(stringr)
unlist(str_extract_all(x, mywords))
#[1] "This is" "new" "things."
filterWords = function(x, mywords){
splitwords = unlist(strsplit(x, split = " "))
return(splitwords[splitwords%in%mywords])
}
这是一种方法。但是,这不会找到带有两个子词的词,如 "this is"。但我认为它可能会给你更多关于你所问内容的信息。
我想知道是否存在给定文本和字符串列表作为输入的现有 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."。
有没有这个功能?
我们可以使用 library(stringr)
中的 str_extract_all
。输出将是 list
,可以 unlist
ed 将其转换为 vector
。
library(stringr)
unlist(str_extract_all(x, mywords))
#[1] "This is" "new" "things."
filterWords = function(x, mywords){
splitwords = unlist(strsplit(x, split = " "))
return(splitwords[splitwords%in%mywords])
}
这是一种方法。但是,这不会找到带有两个子词的词,如 "this is"。但我认为它可能会给你更多关于你所问内容的信息。