匹配文本中的确切单词列表

时间:2018-07-27 09:17:41

标签: r function text match string-matching

我想应用一个可以在文本中找到确切单词的函数。我有一个单词列表,我想在给定的文本中找到确切的单词。提供的代码与单词的字符匹配。我需要一个可以使用r匹配确切单词的解决方案。示例代码:

text <- c("crew reported during climb aircraft would not pressurize and elected to perform an air turn back to fill. crew also reported forward cargo door message at 200 feet",
       "cargo door malfunction because the actor was pressing the open button during take off",
       "It is a time for next flight",
       "flight")

keywords <- c("cargo door","aircraft","pressurize","malfunction","flight")

my_func <- function(x){
paste0(keywords[stringi::stri_detect_regex(x, keywords, case_insensitive=TRUE)], collapse = ", ")
}

outcome <- sapply(text, my_func, USE.NAMES = FALSE)
outcome
[1] "cargo door, aircraft, pressurize" "cargo door, malfunction"          "light"                                
[4] "light"

上述功能匹配单词的字符,但是我需要一个可以匹配确切单词的功能。任何想法如何用特定单词限制功能?

0 个答案:

没有答案