0
我試圖從R中的句子中提取精確的短語。它也提取了其部分匹配的句子。例如:無法從R中的句子中提取精確的短語
phrase <- c("r is not working","roster is not working")
sentence <- c("ABC is not working and roster is not working","CDE is working but printer is not working")
extract <- sapply(phrase, grepl, x = sentence)
extract
它使輸出爲:
r is not working roster is not working
[1,] TRUE TRUE
[2,] TRUE FALSE
我所需的輸出是:
r is not working roster is not working
[1,] FALSE TRUE
[2,] FALSE FALSE
短語 「R不工作」 不應該匹配兩個句子。有什麼辦法可以解決這個問題嗎?有什麼想法嗎?謝謝!!
可能會添加字邊界,如'sapply(paste0(「\\ b」,短語,「\\ b」),grepl,x =句子)' –
「r不工作」匹配兩個字符串,但添加一個空格在r:「r不工作」之前將阻止匹配。 – Dave2e