[英]Removing words featured in character vector from string
I have a character vector of stopwords in R: 我在R中有一个停用词的字符向量:
stopwords = c("a" ,
"able" ,
"about" ,
"above" ,
"abst" ,
"accordance" ,
...
"yourself" ,
"yourselves" ,
"you've" ,
"z" ,
"zero")
Let's say I have the string: 假设我有字符串:
str <- c("I have zero a accordance")
How can remove my defined stopwords from str
? 如何从
str
删除我定义的停用词?
I think gsub
or another grep
tool could be a good candidate to pull this off, although other recommendations are welcome. 我认为
gsub
或其他grep
工具可能是一个很好的选择,尽管其他建议是受欢迎的。
Try this: 试试这个:
str <- c("I have zero a accordance")
stopwords = c("a", "able", "about", "above", "abst", "accordance", "yourself",
"yourselves", "you've", "z", "zero")
x <- unlist(strsplit(str, " "))
x <- x[!x %in% stopwords]
paste(x, collapse = " ")
# [1] "I have"
Addition: Writing a "removeWords" function is simple so it is not necessary to load an external package for this purpose: 增加:编写“removeWords”函数很简单,因此不需要为此目的加载外部包:
removeWords <- function(str, stopwords) {
x <- unlist(strsplit(str, " "))
paste(x[!x %in% stopwords], collapse = " ")
}
removeWords(str, stopwords)
# [1] "I have"
If stopwords
is long, the removeWords()
solution should be much faster than any regex based solution. 如果
stopwords
很长,则removeWords()
解决方案应该比任何基于正则表达式的解决方案快得多。
For completeness, in case str
is a vector of strings, one can write: 为了完整性,如果
str
是一个字符串向量,可以写:
library("magrittr")
library("stringr")
library("purrr")
remove_words <- function(x, .stopwords) {
x %>%
stringr::str_split(" ") %>%
purrr::flatten_chr() %>%
setdiff(.stopwords) %>%
stringr::str_c(collapse = " ")
}
purrr::map_chr(str, remove_words, .stopwords = stopwords)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.