【问题标题】:Finding the number of specific character vector values in another character vector in R在R中的另一个字符向量中查找特定字符向量值的数量
【发布时间】:2017-11-21 09:03:07
【问题描述】:

我正在寻找一种使用另一个字符向量扫描字符向量的方法。我已经在这方面投入了很多时间,但似乎无法做到正确,分别。我找不到可以做我打算做的功能。但我确信有一个简单的方法可以解决这个问题

假设我有以下向量:

    c <- c("bread", "milk", "oven", "salt")

另一方面,我有一个包含句子的向量。

    text <- c("The BREAD is in the oven. Wonderful!!",
    "We don't only need Milk to bake a yummy bread, but a pinch of salt as 
    well.", "Oven, oven, oven, why not just eat it raw.")

现在我想使用我的 c 向量的内容来扫描文本块。输出应该是这样的:

                                             text bread milk oven salt
    1       The BREAD is in the oven. Wonderful!!    1    0    1    0
    2        We don't only need Milk... as well."    0    1    0    1
    3 Oven, oven, oven, why not just eat it raw.     0    0    3    0

我想做的另一件事是搜索组合,而不仅仅是单个单词。

    c <- c("need milk", "oven oven", "eat it")

得到相同的输出:

                                             text need milk oven oven eat it
    1       The BREAD is in the oven. Wonderful!!     0         0        0
    2        We don't only need Milk... as well."     1         0        1
    3 Oven, oven, oven, why not just eat it raw.      0         2        1

如果有人可以帮助我,那就太好了! :) 非常感谢!

【问题讨论】:

  • 试试data.frame(text, +(sapply(c, grepl, tolower(text))))
  • 太棒了。你能帮助了解+在这里做什么吗?
  • @amrrs grepl 返回一个逻辑矩阵。通过添加+* 1,它强制转换为二进制
  • 您的示例“文本”和显示的输出“文本”不匹配。第二部分不清楚。在您的文字中,它是Oven, oven, oven,您正在尝试匹配“烤箱”
  • 我不确定 OP 是否在那里提到了二元组 - 出现了 2 次烤箱烤箱。

标签: r vector find text-mining


【解决方案1】:

我们可以使用str_count来统计'string'中每个pattern的出现次数

library(stringr)
data.frame(text, sapply(c, str_count, string = tolower(text)))

【讨论】:

    【解决方案2】:

    这里使用stringi 包的另一种解决方案,至少在速度(不考虑简单性)方面优于其他方法。当然,这取决于“节拍”在这里的含义,如果您考虑速度而不是简单性并使用基础 R。

    另一件要提的事情是grepl 解决方案不返回实际计数,而是返回二进制计数,如上述注释所示。所以不能直接比较。但是,根据您的需要,这可能就足够了。

    library(stringi)
    library(stringr)
    library(microbenchmark)
    
    c <- c("bread", "milk", "oven", "salt")
    text <- c("The BREAD is in the oven. Wonderful!!",
              "We don't only need Milk to bake a yummy bread, but a pinch of salt as 
              well.", "Oven, oven, oven, why not just eat it raw.")
    
    
    stringi_approach <- function() {
    
      matches <- sapply(c, function(w) {stri_count_fixed(text,w, case_insensitive = TRUE)})
      rownames(matches) <- text
    
    }
    
    grepl_approach <- function() {
    
      df <- data.frame(text, +(sapply(c, grepl, tolower(text))))
    
    }
    
    stringr_approach <- function() {
    
      df <- data.frame(text, sapply(c, str_count, string = tolower(text)))
    
    }
    
    microbenchmark(
      grepl_approach(),
      stringr_approach(),
      stringi_approach()
    )
    
    # Unit: microseconds
    #         expr       min      lq     mean   median       uq     max neval
    # grepl_approach() 309.091 338.500 351.3017 347.5790 352.7105 565.679   100
    # stringr_approach() 380.541 418.634 437.7599 429.2925 441.7275 814.767   100
    # stringi_approach() 101.057 113.492 126.9763 129.4790 133.8215 217.903   100
    

    【讨论】:

      【解决方案3】:

      您可以为此使用 corpus 库:

      library(corpus)
      library(Matrix)
      
      text <- c("The BREAD is in the oven. Wonderful!!",
          "We don't only need Milk to bake a yummy bread, but a pinch of salt as 
          well.", "Oven, oven, oven, why not just eat it raw.")
      
      term_matrix(text, select = c("bread", "milk", "oven", "salt"))
      ## 3 x 4 sparse Matrix of class "dgCMatrix"
      ##      bread milk oven salt
      ## [1,]     1    .    1    .
      ## [2,]     1    1    .    1
      ## [3,]     .    .    3    .
      
      term_matrix(text, select = c("need milk", "oven oven", "eat it"), drop_punct = TRUE)
      ## 3 x 3 sparse Matrix of class "dgCMatrix"
      ##      need milk oven oven eat it
      ## [1,]         .         .      .
      ## [2,]         1         .      .
      ## [3,]         .         2      1
      

      或者,您可以修改 Manuel Bickel 的答案之一,使用 text_count 而不是 str_count

      【讨论】:

        猜你喜欢
        • 2020-09-17
        • 2013-06-12
        • 1970-01-01
        • 1970-01-01
        • 2015-09-22
        • 1970-01-01
        • 2014-02-14
        • 1970-01-01
        • 2019-06-03
        相关资源
        最近更新 更多