Extract n Words Around Defined Term (Multicase)

╄→尐↘猪︶ㄣ 提交于 2019-12-13 02:04:09

问题


I have a vector of text strings, such as:

Sentences <- c("I would have gotten the promotion, but TEST my attendance wasn’t good enough.Let me help you with your baggage.",
               "Everyone was busy, so I went to the movie alone. Two seats were vacant.",
               "TEST Rock music approaches at high velocity.",
               "I am happy to take your TEST donation; any amount will be greatly TEST appreciated.",
               "A purple pig and a green donkey TEST flew a TEST kite in the middle of the night and ended up sunburnt.",
               "Rock music approaches at high velocity TEST.")

I would like to extract n (for example: three) words (a word is characterized by a whitespace before and after character(s)) AROUND (i.e., before and after) a particular term (e.g., 'TEST'). Improtant: Several matches should be allowed (i.e., if a particular term occurs more than one times, the intended solution should capture those cases).

The result might look like this (the format can be improved):

S1  <- c(before = "the promotion, but", after = "my attendance wasn’t")
S2  <- c(before = "",                   after = "")
S3  <- c(before = "",                   after = "Rock music approaches")
S4a <- c(before = "to take your",       after = "donation; any amount")
S4b <- c(before = "will be greatly",    after = "appreciated.")
S5a <- c(before = "a green donkey",     after = "flew a TEST")
S5b <- c(before = "TEST flew",          after = "kite in the")
S6  <- c(before = "at high velocit",    after = "")  

How can I do this? I already figured out other psots, which are either only for one-case-matches or relate to fixed sentence structures.


回答1:


The quanteda package has a great function for this: kwic() (keywords in context).

Out of the box, this works pretty well on your example:

library("quanteda")
names(Sentences) <- paste0("S", seq_along(Sentences))
(kw <- kwic(Sentences, "TEST", window = 3))
# 
# [S1, 9]   promotion, but | TEST | my attendance wasn't 
# [S3, 1]                  | TEST | Rock music approaches
# [S4, 7]     to take your | TEST | donation; any        
# [S4, 15] will be greatly | TEST | appreciated.         
# [S5, 8]   a green donkey | TEST | flew a TEST          
# [S5, 11]     TEST flew a | TEST | kite in the          
# [S6, 7] at high velocity | TEST | .               

(kw2 <- as.data.frame(kw)[, c("docname", "pre", "post")])
#   docname              pre                  post
# 1      S1  promotion , but  my attendance wasn't
# 2      S3                  Rock music approaches
# 3      S4     to take your        donation ; any
# 4      S4  will be greatly         appreciated .
# 5      S5   a green donkey           flew a TEST
# 6      S5      TEST flew a           kite in the
# 7      S6 at high velocity                     .

That's probably a better format than the separate objects you ask for you in the question. But to get as close as possible to your target, you can further transform it as follows.

# this picks up the empty matching sentence S2
(kw3 <- merge(kw2, 
              data.frame(docname = names(Sentences), stringsAsFactors = FALSE), 
              all.y = TRUE))
# replaces the NA with the empty string
kw4 <- as.data.frame(lapply(kw3, function(x) { x[is.na(x)] <- ""; x} ), 
                     stringsAsFactors = FALSE)
# renames pre/post to before/after
names(kw4)[2:3] <- c("before", "after")
# makes the docname unique
kw4$docname <- make.unique(kw4$docname)

kw4
#   docname           before                 after
# 1      S1  promotion , but  my attendance wasn't
# 2      S2                                       
# 3      S3                  Rock music approaches
# 4      S4     to take your        donation ; any
# 5    S4.1  will be greatly         appreciated .
# 6      S5   a green donkey           flew a TEST
# 7    S5.1      TEST flew a           kite in the
# 8      S6 at high velocity                     .


来源:https://stackoverflow.com/questions/48727546/extract-n-words-around-defined-term-multicase

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!