我有一个数据集,其中包含 ids 及其对应的短语。一个 ID 可以包含 2 或 3 个单词的短语。在一个Id内,如果有2个或3个单词的短语,则将2个单词的短语与3个单词的短语进行匹配。如果匹配,则保留2个词的短语并删除3个词的短语。
Data:
id text
11 XYX not working
11 cant find anything
11 wont let go
11 wont let open
11 not working
11 let open
12 no music store
12 no sound store
12 not playing
12 not printing
12 no music
13 paper issue
13 charger issue
14 no issue found
示例:在 id(11) 中,“let open”与“wont let open”匹配。因此删除“不会让打开”并保留“让打开”。 “不工作”与“XYX 不工作”匹配,因此保留“不工作”。还保留其他不匹配的短语。当我们有 2 和 3 个词的短语(特别是 id)时,我们总是需要匹配短语。
Expected output:
id text
11 cant find anything
11 wont let go
11 not working
11 let open
12 no sound store
12 not playing
12 not printing
12 no music
13 paper issue
13 charger issue
14 no issue found
提前谢谢您!
最佳答案
这是使用 tidyverse
系列软件包的解决方案:
library(stringr)
library(tidyverse)
is_long_phrase <- function(x) {
map_lgl(x, ~ !any(str_detect(.x, setdiff(x, .x))))
}
data %>%
group_by(id) %>%
filter(is_long_phrase(text)) %>%
ungroup()
关于r - R 中文本的部分匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41509951/