【问题标题】:How two find two words in any order within a period delimited sentence两个人如何在句号分隔的句子中以任何顺序找到两个单词
【发布时间】:2018-12-08 18:05:21
【问题描述】:

我正在尝试提取任何包含两个单词columnBarr 的句子(定义为在两个句点之间)。这很棘手,因为目前我创建了一个正则表达式,它只能在句点之前以任何顺序查找两个单词,但如果这两个单词出现在两个句子中,则选择两个句子之间的所有文本。如何使正则表达式语句具体化?

输入

try<-c("I am a sentence.I am a sentence and I contain Barr. I contain other things. I contain column as well.","Here we go. I am a sentence and I contain column but also Barr. I only contain Barr. I am too.")

期望的输出

[1] NA
[2] "I am a sentence and I contain column but also Barr.

尝试

str_extract_all(try,"\..*column.Barr.?\.|.*Barr.column.?\.")

电流输出

[[1]]
[1] "I am a sentence.I am a sentence and I contain Barr. I contain other things. I contain column as well."

[[2]]
[1] ". I am a sentence and I contain column but also Barr. I only contain Barr."

【问题讨论】:

    标签: r


    【解决方案1】:

    为了找到以任意顺序出现的两个单词,您可以使用两个正向前瞻: 例如grepl((?=.*Barr)(?=.*column),x,perl=T) 将在每次出现两个单词时返回TRUE,无论它们的顺序如何,否则返回FALSE,但这没有考虑句子结构。 由于您要提取文本,并且要查找点之间的两个单词,我们可以将其更改为:

    library(stringr)
    ## Example data
    x <- c("I am a sentence.I am a sentence and I contain Barr. I contain other things. I contain column as well.","Here we go. I am a sentence and I contain column but also Barr. I only contain Barr. I am too.","Barr and column and also column. But just Barr. And just column. Now again column and Barr")
    > x
    [1] "I am a sentence.I am a sentence and I contain Barr. I contain other things. I contain column as well."
    [2] "Here we go. I am a sentence and I contain column but also Barr. I only contain Barr. I am too."       
    [3] "Barr and column and also column. But just Barr. And just column. Now again column and Barr"           
    
    str_extract_all(x,"(\\.|^)(?=[^\\.]*Barr)(?=[^\\.]*column)[^\\.]*(\\.|$)")
    

    这将查找句首或句点(\\.|^),后跟不是句点且包含Barr 和列(?=[^\\.]*Barr)(?=[^\\.]*column)[^\\.]* 的字符,后跟句号或句尾(\\.|$)。 这会返回一个列表:

    [[1]]
    character(0)
    
    [[2]]
    [1] ". I am a sentence and I contain column but also Barr."
    
    [[3]]
    [1] "Barr and column and also column." ". Now again column and Barr"
    

    【讨论】:

      【解决方案2】:

      这个正则表达式似乎可以满足您的需要:

      (\\.[^.]*column[^.]*Barr[^.]*)|(\\.[^.]*Barr[^.]*column[^.]*)
      

      它将从一个点 (.) 开始,并抓取任何不是点但也有 columnBarr 的东西。或者相同的两个词以不同的顺序排列。

      示例:

      try = c("I am a sentence.I am a sentence and I contain Barr. I contain other things. I contain column as well.",
              "Here we go. I am a sentence and I contain column but also Barr. I only contain Barr. I am too.",
              "I am a sentence and I contain column but also Barr. I only contain Barr. I am too.",
              "I contain column and Barr. I have Barr and column. I don't.",
              "Hello. I contain Barr and column but also Barr. I only contain Barr. I am too.") 
      
      k = sapply(try, function(x){
        str_extract(paste0(".",x), "(\\.[^.]*column[^.]*Barr[^.]*)|(\\.[^.]*Barr[^.]*column[^.]*)")
      })
      names(k) = NULL
      

      结果:

      [1] NA                                                    
      [2] ". I am a sentence and I contain column but also Barr"
      [3] ".I am a sentence and I contain column but also Barr" 
      [4] ".I contain column and Barr"                          
      [5] ". I contain Barr and column but also Barr"
      

      如果您使用str_extract_all,请记住它会返回匹配列表。

      [[1]]
      character(0)
      
      [[2]]
      [1] ". I am a sentence and I contain column but also Barr"
      
      [[3]]
      [1] ".I am a sentence and I contain column but also Barr"
      
      [[4]]
      [1] ".I contain column and Barr" ". I have Barr and column"  
      
      [[5]]
      [1] ". I contain Barr and column but also Barr"
      

      我添加了一个paste0(".",x) 以检测包含两个单词和第一个单词的句子(它们不以句点开头)。

      【讨论】:

        【解决方案3】:

        这是一个更一般的尝试,不需要创建所需单词的每个排列,当需要两个以上的作品时很有帮助。

        策略是找到每个单词的句子,然后找到结果的交集。

        #split the long text into individual sentences.
        sentences<-strsplit(try, "\\.")
        
        #create list of matches for each desired word
        columnlist<-lapply(sentences, function(x) {grep("(column)", x)})
        barrlist<-lapply(sentences, function(x) {grep("(Barr)", x)})
        
        #find intersection between lists
        intersection<-lapply(seq_along(columnlist), function(i){intersect(columnlist[[i]], barrlist[[i]])} )
        
        #extract the sentences out
        answer<-sapply(seq_along(intersection), function(i) { 
          if(length(intersection[[i]])) 
            {trimws(sentences[[i]][intersection[[i]] ])}  
          else {NA}
        })
        

        结果

        #[[1]]
        #[1] NA
        #
        #[[2]]
        #[1] "I am a sentence and I contain column but also Barr" 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-03
          • 1970-01-01
          • 2019-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多