1

我有两个数据框。一个(txt.df)有一列包含我想从(text)中提取短语的文本。另一个(wrd.df)有一个包含短语(短语)的列。两者都是具有复杂文本和字符串的大数据框,但可以说:

txt.df <- data.frame(id = c(1, 2, 3, 4, 5),
                     text = c("they love cats and dogs", "he is drinking juice", 
                              "the child is having a nap on the bed", "they jump on the bed and break it",
                              "the cat is sleeping on the bed"))


wrd.df <- data.frame(label = c('a', 'b', 'c', 'd', 'e', 'd'),
                     phrase = c("love cats", "love dogs", "juice drinking", "nap on the bed", "break the bed",
                              "sleeping on the bed"))

我最终需要的是一个txt.df,其中包含检测到的短语标签的另一列。

我尝试的是在 wrd.df 中创建一个列,在其中我标记了这样的短语

wrd.df$token <- sapply(wrd.df$phrase, function(x) unlist(strsplit(x, split = " ")))

然后尝试编写一个自定义函数以使用 grepl/str_detect 对令牌列进行 sapply 获取所有正确的名称(标签)

Extract.Fun <- function(text, df, label, token){
  for (i in token) {
  truefalse[i] <- sapply(token[i], function (x) grepl(x, text))
  truenames[i] <- names(which(truefalse[i] == T))
  removedup[i] <- unique(truenames[i])
  return(removedup)
}

然后在我的 txt.df$text 上应用这个自定义函数,以获得一个带有标签的新列。

txt.df$extract <- sapply(txt.df$text, function (x) Extract.Fun(x, wrd.df, "label", "token"))

但我不擅长自定义功能,而且我真的被卡住了。我将不胜感激任何帮助。PS如果我也能有部分匹配,比如“喝果汁”和“打破床”,那将是非常好的……但这不是优先事项……与原来的匹配。

4

1 回答 1

2

如果您需要匹配确切的短语,regex_join()fuzzyjoin-package 中的 是您所需要的。

fuzzyjoin::regex_join( txt.df, wrd.df, by = c(text = "phrase"), mode = "left" )

  id                                 text label              phrase
1  1              they love cats and dogs     a           love cats
2  2                 he is drinking juice  <NA>                <NA>
3  3 the child is having a nap on the bed     d      nap on the bed
4  4    they jump on the bed and break it  <NA>                <NA>
5  5       the cat is sleeping on the bed     d sleeping on the bed

如果您想匹配所有单词,我想您可以从涵盖此类行为的短语中构建一个正则表达式...

更新

#build regex for phrases
#done by splitting the phrases to individual words, and then paste the regex together
wrd.df$regex <- unlist( lapply( lapply( strsplit( wrd.df$phrase, " "), 
                                        function(x) paste0( "(?=.*", x, ")", collapse = "" ) ),
                                function(x) paste0( "^", x, ".*$") ) )


fuzzyjoin::regex_join( txt.df, wrd.df, by = c(text = "regex"), mode = "left" )

  id                                 text label              phrase                                        regex
1  1              they love cats and dogs     a           love cats                     ^(?=.*love)(?=.*cats).*$
2  1              they love cats and dogs     b           love dogs                     ^(?=.*love)(?=.*dogs).*$
3  2                 he is drinking juice     c      juice drinking                ^(?=.*juice)(?=.*drinking).*$
4  3 the child is having a nap on the bed     d      nap on the bed      ^(?=.*nap)(?=.*on)(?=.*the)(?=.*bed).*$
5  4    they jump on the bed and break it     e       break the bed            ^(?=.*break)(?=.*the)(?=.*bed).*$
6  5       the cat is sleeping on the bed     d sleeping on the bed ^(?=.*sleeping)(?=.*on)(?=.*the)(?=.*bed).*$
于 2020-06-16T14:58:21.293 回答