R grepl检查字符串是否包含我们所有的单词

时间:2018-02-23 13:30:46

标签: r regex nlp

寻找快速且非详细的解决方案来检查字符串是否包含 all 给定的单词向量的元素。我想出了一些想法,但感觉我遗漏了一些东西,特别是因为检查字符串是否包含任何的单词都有一个非常简洁的解决方案。

我尝试过的事情:

# Example data
strings <- c(
  "never going to do this again",
  "never again", 
  "will repeat", 
  "never repeat", 
  "again tomorrow"
)

# Words we are looking for
ourWords <- c("never", "again")

# Check if string contains any of our words
grepl(paste0(ourWords, collapse = "|"), strings, , fixed = TRUE)
# Very neat solution but **not** what I am looking for    

# Check if string contains **all** of our words
grepl(ourWords[1], strings, fixed = TRUE) & 
  grepl(ourWords[2], strings, fixed = TRUE)
# This is verbose, not very scalable, and seems inefficient

# Even less efficient alternative
vapply(
  strsplit(strings, split = " "), 
  function(x)  sum(ourWords %in% x) == length(ourWords),
  logical(1)
)

1 个答案:

答案 0 :(得分:1)

您可以使用foo()和多个前瞻的组合:

template <typename ...Args>
std::enable_if_t<false == fIsCToS<Args...>{}>
   foo(std::string, std::string, Args...) 
 { std::cout << "TWO STRINGS\n"; }

在这种情况下产生哪个

sprintf

这里的想法是使用多个前瞻。