无法从 R 中的句子中提取确切的短语
Not able to extract exact phrase from the sentence in 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 is not working"不应与两个句子都匹配。有什么办法可以解决这个问题。有什么想法吗?谢谢!!
grepl
计算正则表达式。
如果您想坚持使用这些,将您的搜索模式锚定到字符串的开头和结尾:
phrase <- c("^r is not working$", "^roster is not working$")
如果您想检查完全匹配,只需使用
extract <- sapply(sentence, `%in%`, phrase)
我正在尝试从 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 is not working"不应与两个句子都匹配。有什么办法可以解决这个问题。有什么想法吗?谢谢!!
grepl
计算正则表达式。
如果您想坚持使用这些,将您的搜索模式锚定到字符串的开头和结尾:
phrase <- c("^r is not working$", "^roster is not working$")
如果您想检查完全匹配,只需使用
extract <- sapply(sentence, `%in%`, phrase)