获取提取词的上下文

Get context around extracted word

我已经从句子数据框中提取了关键词。我需要了解一些词前和 post- 关键字来理解上下文并能够进行一些基本计数。

我已经尝试了多个 stringr 和 stringi 函数以及其他人针对类似问题在 SO 上建议的 grepl 函数。但是,没有找到适合我的情况的任何东西。

以下是我想要的。假设它是一个数据框或 tibble,其中列出了前两个字段。我need/want创建最右边的列(keyword_w_context)。

在示例中,我提取了关键字后面的三个词。但是,我想修改任何解决方案,以便获得 1、2、n。如果我能以同样的方式做 post 字也很好。

基本上,想要做一些类似 mutate 的事情,它使用关键字周围的上下文词(before/after,见下文)创建一个新变量。

Sentence Keyword Keyword_w_context
The yellow lab dog is so cute. dog The yellow lab dog
The fluffy black cat purrs loudly. cat The fluffy black cat

非常感谢!

dat = read.table(text = 'Sentence   | Keyword | Keyword_w_context
The yellow lab dog is so cute.|dog|The yellow lab dog
The fluffy black cat purrs loudly.|cat|The fluffy black cat',sep="|",header=TRUE)

    
n_before = 3
n_after = 2


# Note: This will give an error if you don't have enough words before or after
dat %>% 
  mutate(Keyword_w_context_before = str_extract(string=Sentence,
                                              pattern=paste0("(([A-Za-z]+)\s){",n_before,"}",Keyword)),
         
         Keyword_w_context_after = str_extract(string=Sentence,
                                               pattern=paste0(Keyword,"(\s([A-Za-z]+)){",n_after,"}"))
         )


                            Sentence Keyword    Keyword_w_context Keyword_w_context_before Keyword_w_context_after
1     The yellow lab dog is so cute.     dog   The yellow lab dog       The yellow lab dog               dog is so
2 The fluffy black cat purrs loudly.     cat The fluffy black cat     The fluffy black cat        cat purrs loudly

您可能想要采用自然语言处理 (NLP) 方法,而不是基于正则表达式的方法。这有很多框架。一个足够简单的是 tidytext。下面是一个关于如何抓取关键字周围的一堆词的示例。

您可能想尝试一下这个以获得您想要的。听起来你想从中得到几样东西,所以我只是挑了一个。

library(tidytext)
library(dplyr)
library(tibble)

df <- tibble(Sentence = c("The yellow lab dog is so cute.",
                          "The fluffy black cat purrs loudly."))
keywords <- tibble(word = c("dog", "cat"), keyword = TRUE)

df %>% 
  rowid_to_column() %>% 
  unnest_tokens("trigram", Sentence, token = "ngrams", n = 3, n_min = 2) %>%
  unnest_tokens("word", trigram, drop = FALSE) %>% 
  left_join(keywords, by = "word") %>% 
  filter(keyword)
# A tibble: 10 x 4
   rowid trigram          word  keyword
   <int> <chr>            <chr> <lgl>  
 1     1 yellow lab dog   dog   TRUE   
 2     1 lab dog          dog   TRUE   
 3     1 lab dog is       dog   TRUE   
 4     1 dog is           dog   TRUE   
 5     1 dog is so        dog   TRUE   
 6     2 fluffy black cat cat   TRUE   
 7     2 black cat        cat   TRUE   
 8     2 black cat purrs  cat   TRUE   
 9     2 cat purrs        cat   TRUE   
10     2 cat purrs loudly cat   TRUE

您可以在此基础上进行构建的示例如下所示。在这里,您可以跟踪找到每个单词的 n-gram 中的句子和位置。因此,您可以过滤关键字是第一个 word_pos 或其他关键字的位置。

df %>% 
  rowid_to_column("sentence_id") %>% 
  unnest_tokens("trigram", Sentence, token = "ngrams", n = 3, n_min = 3) %>%
  rowid_to_column("trigram_id") %>% 
  unnest_tokens("word", trigram, drop = FALSE) %>% 
  group_by(trigram_id) %>% 
  mutate(word_pos = row_number()) %>% 
  left_join(keywords, by = "word") %>%
  relocate(sentence_id, trigram_id, word_pos, trigram, word) %>% 
  filter(keyword, word_pos == 1)
# A tibble: 2 x 6
# Groups:   trigram_id [2]
  sentence_id trigram_id word_pos trigram          word  keyword
        <int>      <int>    <int> <chr>            <chr> <lgl>  
1           1          4        1 dog is so        dog   TRUE   
2           2          9        1 cat purrs loudly cat   TRUE