Skip to contents

ac_count() returns the number of pattern matches in each document.

Usage

ac_count(ac, doc, ..., overlapping = FALSE, na = c("keep", "zero", "error"))

Arguments

ac

An <ac_automaton> object created by ac_build().

doc

A character vector of documents to search.

...

Must be empty. This is used to require optional arguments to be supplied by name.

overlapping

Default is FALSE. If TRUE, count overlapping matches. This is only supported when ac was built with match_kind = "standard".

na

How to handle NA documents. "keep" returns NA_integer_ (default); "zero" treats missing documents as zero matches; "error" fails.

Value

An integer vector with the same length as doc.

Examples

if (requireNamespace("dplyr", quietly = TRUE)) {
  ac <- ac_build(c("hello", "world"))
  docs <- data.frame(doc = c("hello world", "nothing", "world"))
  dplyr::mutate(docs, n_matches = ac_count(ac, doc))
}
#>           doc n_matches
#> 1 hello world         2
#> 2     nothing         0
#> 3       world         1