从文本字符串中获取字符串的唯一计数

Getting the unique count of strings from a text string

我想知道如何从文本字符串中获取唯一的字符数。假设我正在寻找这个字符串中苹果、香蕉、菠萝、葡萄这些词的重复次数。

 A<- c('I have a lot of pineapples, apples and grapes. One day the pineapples person gave the apples person two baskets of grapes')

 df<- data.frame(A) 

假设我想获得文本中列出的所有水果的唯一计数。

  library(stringr)
  df$fruituniquecount<- str_count(df$A, "apples|pineapples|grapes|bananas")

我试过了,但我得到了全部计数。我想答案为“3”。请提出你的想法。

不太优雅,但您可以像这样使用 str_detect

sum(str_detect(df$A, "apples"), 
    str_detect(df$A, "pineapples"), 
    str_detect(df$A, "grapes"), 
    str_detect(df$A, "bananas"))

或者,根据下面的评论,如果您将所有这些术语放入它们自己的向量中,您就可以使用应用函数:

fruits <- c("apples", "pineapples", "grapes", "bananas")
sum(sapply(fruits, function(x) str_detect(df$A, x)))

您可以使用 str_extract_all 然后计算唯一元素的长度。

输入:

A <- c('I have a lot of pineapples, apples and grapes. One day the pineapples person gave the apples person two baskets of grapes')
fruits <- "apples|pineapples|grapes|bananas"

结果

length(unique(c(stringr::str_extract_all(A, fruits, simplify = TRUE))))
# [1] 3

也许更好的方法是先分解单词,然后计算数量。

library(tokenizers)
library(magrittr)
df$fruituniquecount <- tokenize_words(A) %>% unlist(.) %>% unique(.) %>% 
       stringr::str_count(., "apples|pineapples|grapes|bananas") %>% sum(.)

一种基本可能性是:

length(unique(unlist(regmatches(A, gregexpr("apples|pineapples|grapes|bananas", A, perl = TRUE)))))

[1] 3

还可以:

A <- c('I have a lot of pineapples, apples and grapes. One day the pineapples person gave the apples person two baskets of grapes')

df <- data.frame(A) 

fruits <- c("apples", "pineapples", "grapes", "bananas")

df$count <- sum(tolower(unique(unlist(strsplit(as.character(df$A), "\.|,| ")))) %in% fruits)

输出:

[1] 3

好吧,这也是一个无正则表达式的基础 R 解决方案,

sum(unique(strsplit(A, ' ')[[1]]) %in% c('apples', 'pineapples', 'grapes', 'bananas'))
#[1] 3

我们可以使用 stringrstringi 的组合:

target<-"apples|pineapples|grapes|bananas"#inspired by @markus ' solution
length(stringi::stri_unique(stringr::str_extract_all(A,target,simplify=TRUE)))
#[1] 3

为什么要重新发明轮子? quanteda 软件包就是为此而构建的。

定义一个水果向量,作为奖励,我将其与(默认)glob 模式匹配类型一起使用,以捕获单数和复数形式。

A <- c("I have a lot of pineapples, apples and grapes. One day the pineapples person gave the apples person two baskets of grapes")
fruits <- c("apple*", "pineapple*", "grape*", "banana*")

library("quanteda", warn.conflicts = FALSE)
## Package version: 1.4.2
## Parallel computing: 2 of 12 threads used.
## See https://quanteda.io for tutorials and examples.

然后,一旦您使用 tokens() 将其标记为单词,您就可以使用向量 fruits 将结果发送到 tokens_select() 到 select 那些类型。

toks <- tokens(A) %>%
  tokens_select(pattern = fruits)
toks
## tokens from 1 document.
## text1 :
## [1] "pineapples" "apples"     "grapes"     "pineapples" "apples"    
## [6] "grapes"

最后,ntype()会告诉你单词的数量types(独特的单词),这是你想要的输出3.

ntype(toks)
## text1 
##     3

或者,您可以计算非唯一事件,称为 令牌

ntoken(toks)
## text1 
##     6

两个函数都被矢量化为 return 一个命名的整数向量,其中元素名称将是您的文档名称(此处,quanteda 默认值为 "text1"对于单个文档),因此这在大型语料库上也可以轻松高效地工作。

优点? 比正则表达式更容易(并且更易读),而且您还可以访问令牌的附加功能。例如,假设您想将单数和复数水果模式视为等效。在 quanteda 中,您可以通过两种方式执行此操作:使用 tokens_replace() 手动将模式替换为规范形式,或者使用 tokens_wordstem() 提取水果名称。

使用tokens_replace():

B <- "one apple, two apples, one grape two grapes, three pineapples."

toksrepl <- tokens(B) %>%
  tokens_select(pattern = fruits) %>%
  tokens_replace(
    pattern = fruits,
    replacement = c("apple", "pineapple", "grape", "banana")
  )
toksrepl
## tokens from 1 document.
## text1 :
## [1] "apple"     "apple"     "grape"     "grape"     "pineapple"
ntype(toksrepl)
## text1 
##     3

使用tokens_wordstem():

toksstem <- tokens(B) %>%
  tokens_select(pattern = fruits) %>%
  tokens_wordstem()
toksstem
## tokens from 1 document.
## text1 :
## [1] "appl"     "appl"     "grape"    "grape"    "pineappl"
ntype(toksstem)
## text1 
##     3