Точное совпадение из списка слов из текста в R - PullRequest
0 голосов
/ 31 марта 2020

У меня есть список слов, и я ищу слова, которые есть в тексте. В результате в последнем столбце всегда находится поиск шаблонов. Я ищу точное совпадение, которое есть в словах. Не комбинации. Для первых трех записей это не должно быть найдено. Пожалуйста, направьте, где я иду не так.

col_1 <- c(1,2,3,4,5)
col_2 <- c("work instruction change", 
           "technology npi inspections", 
           " functional locations",
           "Construction has started",
           " there is going to be constn coon")


df <- as.data.frame(cbind(col_1,col_2))
df$col_2 <- tolower(df$col_2)
words <- c("const","constn","constrction","construc",
                    "construct","construction","constructs","consttntype","constypes","ct","ct#",
                    "ct2"
                    )


pattern_words  <- paste(words, collapse = "|")
df$result<- ifelse(str_detect(df$col_2, regex(pattern_words)),"Found","Not Found")

1 Ответ

2 голосов
/ 31 марта 2020

Используйте границы слов вокруг words.

library(stringr)

pattern_words  <- paste0('\\b', words, '\\b', collapse = "|")
df$result <- c('Not Found', 'Found')[str_detect(df$col_2, pattern_words) + 1]
#OR with `ifelse`
#df$result <- ifelse(str_detect(df$col_2, pattern_words), "Found", "Not Found")

df
#  col_1                             col_2    result
#1     1           work instruction change Not Found
#2     2        technology npi inspections Not Found
#3     3              functional locations Not Found
#4     4          construction has started     Found
#5     5  there is going to be constn coon     Found

Вы также можете использовать grepl здесь, чтобы сохранить его в базе R:

grepl(pattern_words, df$col_2)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...