如何提取特定单词旁边的单词/句子?例如:
“6月28日,简去电影院吃爆米花”
我想选择“简”,并获得[-2,2],意思是:
“6月28日,简去了”
“6月28日,简去电影院吃爆米花”
我想选择“简”,并获得[-2,2],意思是:
“6月28日,简去了”
我们可以编写一个函数来帮忙。这可以使它更加动态。
library(tidyverse)
txt <- "On June 28, Jane went to the cinema and ate popcorn"
grab_text <- function(text, target, before, after){
min <- which(unlist(map(str_split(text, "\\s"), ~grepl(target, .x))))-before
max <- which(unlist(map(str_split(text, "\\s"), ~grepl(target, .x))))+after
paste(str_split(text, "\\s")[[1]][min:max], collapse = " ")
}
grab_text(text = txt, target = "Jane", before = 2, after = 2)
#> [1] "June 28, Jane went to"
首先我们将句子分割,然后确定目标的位置,接着获取目标之前或之后的单词(函数中指定的数量),最后将句子重新拼合在一起。
我有一个使用 stringr
的 str_extract
更简短的版本。
library(stringr)
txt <- "On June 28, Jane went to the cinema and ate popcorn"
str_extract(txt,"([^\\s]+\\s+){2}Jane(\\s+[^\\s]+){2}")
[1] "June 28, Jane went to"
str_extract
从字符串中提取模式。正则表达式\\s
用于匹配空格,而[^\\s]
则是它的否定,即匹配除空格以外的任何字符。因此整个模式是在Jane之前和之后各有两个空格,并由除空格以外的任何内容组成。str_extract_all
:s <- c("On June 28, Jane went to the cinema and ate popcorn.
The next day, Jane hiked on a trail.",
"an indeed Jane loved it a lot")
str_extract_all(s,"([^\\s]+\\s+){2}Jane(\\s+[^\\s]+){2}")
[[1]]
[1] "June 28, Jane went to" "next day, Jane hiked on"
[[2]]
[1] "an indeed Jane loved it"
\\w
获取单词。str_extract(txt, "\\w")
将提取所有单词。 - deniss <- "On June 28, Jane went to the cinema and ate popcorn. The next day, Jane hiked on a trail."
words <- strsplit(s, '\\s+')[[1]]
inds <- grep('Jane', words)
lapply(inds, FUN = function(i) {
paste(words[max(1, i-2):min(length(words), i+2)], collapse = ' ')
})
#> [[1]]
#> [1] "June 28, Jane went to"
#>
#> [[2]]
#> [1] "next day, Jane hiked on"
本文创建于2019年9月17日,使用reprex软件包(版本0.3.0)。
这应该可以工作:
stringr::str_extract(text, "(?:[^\\s]+\\s){5}Jane(?:\\s[^\\s]+){5}")