Learn R Programming

quanteda (version 1.3.13)

pattern2id: Convert regex and glob patterns to type IDs or fixed patterns

Description

pattern2id converts regex or glob to type IDs to allow C++ function to perform fast searches in tokens object. C++ functions use a list of type IDs to construct a hash table, against which sub-vectors of tokens object are matched. This function constructs an index of glob patterns for faster matching.

pattern2fixed converts regex and glob patterns to fixed patterns.

index_types is an auxiliary function for pattern2id that constructs an index of "glob" or "fixed" patterns to avoid expensive sequential search. For example, a type "cars" is index by keys "cars", "car?", "c*", "ca*", "car*" and "cars*" when valuetype="glob".

Usage

pattern2id(pattern, types = NULL, valuetype = NULL,
  case_insensitive = NULL, index = NULL)

pattern2fixed(pattern, types = NULL, valuetype = NULL, case_insensitive = NULL, index = NULL)

index_types(types, valuetype, case_insensitive, max_len = NULL)

Arguments

pattern

a character vector, list of character vectors, dictionary, collocations, or dfm. See pattern for details.

types

unique types of tokens obtained by types

valuetype

the type of pattern matching: "glob" for "glob"-style wildcard expressions; "regex" for regular expressions; or "fixed" for exact matching. See valuetype for details.

case_insensitive

if TRUE, ignores case when matching

index

If NULL, index is constructed automatically. It also accept index constructed by index_types. In that case, types, valuetype and case_insensitive should be NULL.

max_len

maximum length of types to be indexed

Value

pattern2id returns a list of integer vectors containing type IDs

pattern2fixed returns a list of character vectors containing types

index_types returns a list of integer vectors containing type IDs with index keys as an attribute

Examples

Run this code
# NOT RUN {
types <- c("A", "AA", "B", "BB", "BBB", "C", "CC")

pats_regex <- list(c("^a$", "^b"), c("c"), c("d"))
pattern2id(pats_regex, types, "regex", case_insensitive = TRUE)

pats_glob <- list(c("a*", "b*"), c("c"), c("d"))
pattern2id(pats_glob, types, "glob", case_insensitive = TRUE)

pattern <- list(c("^a$", "^b"), c("c"), c("d"))
types <- c("A", "AA", "B", "BB", "BBB", "C", "CC")
pattern2fixed(pattern, types, "regex", case_insensitive = TRUE)
index <- index_types(types, "regex", case_insensitive = TRUE)
pattern2fixed(pattern, index = index)
index <- index_types(c("xxx", "yyyy", "ZZZ"), "glob", FALSE, 3)
quanteda:::search_glob("yy*", attr(index, "type_search"), index)
# }

Run the code above in your browser using DataLab