Matches text against a table of regular expressions and returns extracted matches with optional metadata.
Usage
extract(
data,
col_name = "text",
regex_table,
pattern_col = "pattern",
data_return_cols = NULL,
regex_return_cols = NULL,
date_col = NULL,
date_start = NULL,
date_end = NULL,
remove_acronyms = FALSE,
do_clean_text = TRUE,
verbose = TRUE,
unique_match = FALSE,
cl = NULL,
use_ner = FALSE,
ner_entity_types = c("ORG")
)Arguments
- data
A data frame or character vector containing the text to search. If a character vector is provided, it is internally converted to a data frame and
col_nameis ignored.- col_name
Character string specifying the column in
datathat contains text to search. Default is "text".- regex_table
A data frame containing regular expression patterns and optional metadata columns.
- pattern_col
Character string specifying the column in
regex_tablethat contains regex patterns. Default is "pattern".- data_return_cols
Optional vector of column names to include from
data. Default isNULL(onlyrow_idis returned).- regex_return_cols
Optional vector of column names to include from
regex_table. Default isNULL(no metadata columns added).- date_col
Optional column in
datafor date filtering. If provided, rows are filtered bydate_startanddate_endbefore pattern matching.- date_start
Optional start date (Date object or string like "YYYY-MM-DD") for filtering
datawhendate_colis specified.- date_end
Optional end date (Date object or string like "YYYY-MM-DD") for filtering
datawhendate_colis specified.- remove_acronyms
Logical; if TRUE, removes patterns consisting only of uppercase letters (2 or more characters) from
regex_table.- do_clean_text
Logical; if TRUE, applies basic text cleaning to the input before matching.
- verbose
Logical; if TRUE, displays progress messages.
- unique_match
Logical; if TRUE, stops searching after the first match to find at most one match per row (evaluated in the order patterns appear in
regex_table). If FALSE, returns all matches for all patterns.- cl
A cluster object created by
parallel::makeCluster(), or an integer to indicate number of child processes (integer values are ignored on Windows). Passed topbapply::pblapply().- use_ner
Logical; if TRUE, uses the 'spacyr' package to validate that matches are actual Named Entities (e.g., organizations). Note:
spacyrmust be initialized (e.g., viaspacyr::spacy_initialize()) before calling this function.- ner_entity_types
Character vector; the types of Named Entities to keep if
use_neris TRUE. Default is "ORG".
Value
A tibble with the following columns:
row_id: Integer identifier corresponding to rows in the input data.Additional columns from
dataifdata_return_colsis specified.Additional columns from
regex_tableifregex_return_colsis specified.pattern: The matched regular expression pattern(s).match: The extracted text from the data (original casing preserved).
Details
Pattern matching is performed using R's regular expression engine and is
case-insensitive by default. For each input row, the function checks patterns
in regex_table and returns matches based on the unique_match parameter.
Examples
# Create sample data
data <- data.frame(
id = 1:3,
text = c("I love apples", "Bananas are great", "Oranges and apples"),
stringsAsFactors = FALSE
)
# Create regex patterns
patterns <- data.frame(
pattern = c("apples", "bananas", "oranges"),
category = c("fruit", "fruit", "fruit")
)
# Extract all matches
extract(data, "text", patterns)
#> Scanning 3 patterns against 3 text entries...
#>
| | 0 % ~calculating
|+++++++++++++++++ | 33% ~00s
|++++++++++++++++++++++++++++++++++ | 67% ~00s
|++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=00s
#> Number of rows with matches: 4
#> # A tibble: 4 × 3
#> row_id pattern match
#> <int> <chr> <chr>
#> 1 1 apples apples
#> 2 2 bananas Bananas
#> 3 3 apples apples
#> 4 3 oranges Oranges
# Extract one match per row
extract(data, "text", patterns, unique_match = TRUE)
#> Scanning: 3 patterns against 3 text entries...
#>
| | 0 % ~calculating
|+++++++++++++++++ | 33% ~00s
|++++++++++++++++++++++++++++++++++ | 67% ~00s
|++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=00s
#> Number of rows with matches: 3
#> # A tibble: 3 × 3
#> row_id pattern match
#> <int> <chr> <chr>
#> 1 1 apples apples
#> 2 2 bananas Bananas
#> 3 3 apples apples
