【问题标题】:Matching text tokens with list of word将文本标记与单词列表匹配
【发布时间】:2022-01-03 17:24:11
【问题描述】:

我想将单词列表中的单词与文本匹配并将它们提取到新列中。

我有这个数据

   df <- structure(list(ID = 1:3, Text = c(list("red car, car going, going to"),   list("red ball, ball on, on street"), list("to be, be or, or not"))), class = "data.frame", row.names = c(NA, -3L))


  ID                         Text
1  1 red car, car going, going to
2  2 red ball, ball on, on street
3  3         to be, be or, or not

还有我这个重要的单词列表

words <- c("car", "ball", "street", "dog", "frog")

我想要这样的df

  ID                         Text  Word
1  1 red car, car going, going to  c("car","car")
2  2 red ball, ball on, on street  c("ball", "ball", "street")
3  3         to be, be or, or not  NA

我的尝试

df$Word <- lapply(df$Text, function(x)  stringr::str_extract_all(x, "\\b"%s+%words+%"\\b"))

但它给了我一个长度为 5 的列表,而不仅仅是来自 Text 的单词。

【问题讨论】:

  • 为什么第二行只有一个ball,而第一行有两个car
  • 对不起,我的错误。

标签: r string string-matching


【解决方案1】:

一个可能的解决方案:

library(tidyverse)

df <- data.frame(
  stringsAsFactors = FALSE,
  ID = c(1L, 2L, 3L),
  Text = c("red car, car going, going to","red ball, ball on, on street",
           "to be, be or, or not")
)

words <- c("car", "ball", "street", "dog", "frog")

df %>%
  mutate(word = Text) %>% 
  separate_rows(word, sep = ",|\\s") %>% 
  mutate(word = ifelse(word %in% words, word, NA)) %>% 
  drop_na(word) %>% 
  group_by(ID) %>% 
  summarise(word = str_c(word, collapse = ", "), .groups = "drop") %>%  
  left_join(df,., by=c("ID"))

#>   ID                         Text               word
#> 1  1 red car, car going, going to           car, car
#> 2  2 red ball, ball on, on street ball, ball, street
#> 3  3         to be, be or, or not               <NA>

【讨论】:

  • 谢谢 tidyr 解决方案是我第一次尝试使用 str_split 和 unnest,但出于我的目的,这个解决方案太慢了。
  • 好吧,@onhalu,我已将您的原始数据框扩大到 900000 行(近 100 万行),并且在我的计算机上,解决方案是在 21 秒内计算出来的。如果您想要更快的速度,您可能想尝试data.table 甚至furrr
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多