Home > Software engineering >  Matching multiple strings in R/nlp/spacyr
Matching multiple strings in R/nlp/spacyr

Time:06-02

I have a data frame

myDataframe <- data.frame(keyword = c(c("meeting", "laptop"),c("attend a meeting", "fan")))
description <- "I have to attend a meeting."

I have to match the description with the column keyword in the dataframe and return "attend a meeting". Is there any solution to get a particular output

CodePudding user response:

Using grepl will return the keywords in myDataframe which contains in description

myDataframe <- data.frame(keyword = c(c("meeting", "laptop"),c("attend a meeting", "fan")))
description <- "I have to attend a meeting."

found <- as.logical(lapply(myDataframe$keyword ,
                       function(x) grepl(x , description)))

myDataframe[found , ]
#> [1] "meeting"          "attend a meeting"

Created on 2022-06-02 by the reprex package (v2.0.1)

  • Related