r - R 中文本的部分匹配

标签 r

我有一个数据集,其中包含 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/

相关文章:

r - R中的欧几里德距离使用矩阵中的两个变量

python - 使用 Python 和 Rpy2 进行统计测试(Kolmogorov 和 T 测试)

r - 否则 : combine 3 categories into one

arrays - 使用索引向量从另一个向量生成向量

r - 隐藏最终链接时,下载一个保留原始文件名的文件

r - 如何使用 tm_map 将元数据添加到 tm Corpus 对象

R 版本 4.0.0 上的 ROracle

r - 汇总到矢量输出

r - 获取 "mlm"返回的 `lm()` 对象的回归系数的置信区间

r - Fisher.test R 崩溃,并出现 *** 捕获段错误 *** 错误