step_textfeature creates a specification of a recipe step that will extract a number of numeric features of a text column.

step_textfeature(
  recipe,
  ...,
  role = "predictor",
  trained = FALSE,
  columns = NULL,
  extract_functions = textfeatures::count_functions,
  prefix = "textfeature",
  skip = FALSE,
  id = rand_id("textfeature")
)

# S3 method for step_textfeature
tidy(x, ...)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

One or more selector functions to choose variables. For step_textfeature, this indicates the variables to be encoded into a tokenlist. See recipes::selections() for more details. For the tidy method, these are not currently used.

role

For model terms created by this step, what analysis role should they be assigned?. By default, the function assumes that the new columns created by the original variables will be used as predictors in a model.

trained

A logical to indicate if the recipe has been baked.

columns

A list of tibble results that define the encoding. This is NULL until the step is trained by recipes::prep.recipe().

extract_functions

A named list of feature extracting functions. default to count_functions from the textfeatures package. See details for more information.

prefix

A prefix for generated column names, default to "textfeature".

skip

A logical. Should the step be skipped when the recipe is baked by recipes::bake.recipe()? While all operations are baked when recipes::prep.recipe() is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using skip = TRUE as it may affect the computations for subsequent operations.

id

A character string that is unique to this step to identify it

x

A step_textfeature object.

Value

An updated version of recipe with the new step added to the sequence of existing steps (if any).

Details

This step will take a character column and returns a number of numeric columns equal to the number of functions in the list passed to the extract_functions argument. The default is a list of functions from the textfeatures package.

All the functions passed to extract_functions must take a character vector as input and return a numeric vector of the same length, otherwise an error will be thrown.

See also

Other character to numeric steps: step_lda(), step_sequence_onehot()

Examples

if (requireNamespace("textfeatures", quietly = TRUE)) { library(recipes) library(modeldata) data(okc_text) okc_rec <- recipe(~ ., data = okc_text) %>% step_textfeature(essay0) okc_obj <- okc_rec %>% prep() juice(okc_obj) %>% slice(1:2) juice(okc_obj) %>% pull(textfeature_essay0_n_words) tidy(okc_rec, number = 1) tidy(okc_obj, number = 1) # Using custom extraction functions nchar_round_10 <- function(x) round(nchar(x) / 10) * 10 recipe(~ ., data = okc_text) %>% step_textfeature(essay0, extract_functions = list(nchar10 = nchar_round_10)) %>% prep() %>% juice() }
#> # A tibble: 750 x 10 #> essay1 essay2 essay3 essay4 essay5 essay6 essay7 essay8 essay9 #> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> <fct> #> 1 "i ju… "writ… "that… "musi… "frie… "roma… "usua… "i ha… "you'… #> 2 "eati… "pick… "i lo… "non-… "dese… "ever… "maki… "this… "you'… #> 3 "rais… "proc… "my s… "sush… "my c… "movi… "at h… "on m… "you … #> 4 "i ju… "i've… "my f… "nove… "fami… "how … "havi… "is t… "you'… #> 5 "i'm … "bein… "that… "book… "- fr… "the … "prob… "uhh.… "you … #> 6 "i'm … "i'm … "the … "book… "guit… "a li… "hang… "i'm … "if y… #> 7 "well… "well… "eith… "i do… "1) m… "the … "out … "i ow… "you … #> 8 "i wo… "well… "dimp… "book… "1-la… "sex<… "depe… "i li… "you … #> 9 "phot… "eati… "my g… "donn… "sush… "love… "drin… "hm..… "." #> 10 "occu… "livi… "long… "film… "yuzu… "menu… "ofte… "but … "you … #> # … with 740 more rows, and 1 more variable: textfeature_essay0_nchar10 <dbl>