Filtering of Stop Words for Tokens VariablesSource:
step_stopwords() creates a specification of a recipe step that will
token variable for stop words.
step_stopwords( recipe, ..., role = NA, trained = FALSE, columns = NULL, language = "en", keep = FALSE, stopword_source = "snowball", custom_stopword_source = NULL, skip = FALSE, id = rand_id("stopwords") )
A recipe object. The step will be added to the sequence of operations for this recipe.
One or more selector functions to choose which variables are affected by the step. See
recipes::selections()for more details.
Not used by this step since no new variables are created.
A logical to indicate if the quantities for preprocessing have been estimated.
A character string of variable names that will be populated (eventually) by the
termsargument. This is
NULLuntil the step is trained by
A character to indicate the language of stop words by ISO 639-1 coding scheme.
A logical. Specifies whether to keep the stop words or discard them.
A character to indicate the stop words source as listed in
A character vector to indicate a custom list of words that cater to the users specific problem.
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 = FALSE.
A character string that is unique to this step to identify it.
An updated version of
recipe with the new step added
to the sequence of existing steps (if any).
Stop words are words which sometimes are remove before natural language processing tasks. While stop words usually refers to the most common words in the language there is no universal stop word list.
custom_stopword_source allows you to pass a character vector
to filter against. With the
keep argument one can specify to keep the words
instead of removing thus allowing you to select words with a combination of
these two arguments.
tidy() this step, a tibble with columns
(the selectors or variables selected),
value (name of stop word list), and
keep (whether stop words are removed or kept).
library(recipes) library(modeldata) data(tate_text) tate_rec <- recipe(~., data = tate_text) %>% step_tokenize(medium) %>% step_stopwords(medium) tate_obj <- tate_rec %>% prep() bake(tate_obj, new_data = NULL, medium) %>% slice(1:2) #> # A tibble: 2 × 1 #> medium #> <tknlist> #> 1 [6 tokens] #> 2 [2 tokens] bake(tate_obj, new_data = NULL) %>% slice(2) %>% pull(medium) #> <textrecipes_tokenlist> #>  [2 tokens] #> # Unique Tokens: 2 tidy(tate_rec, number = 2) #> # A tibble: 1 × 4 #> terms value keep id #> <chr> <chr> <lgl> <chr> #> 1 medium NA NA stopwords_GK5iC tidy(tate_obj, number = 2) #> # A tibble: 1 × 4 #> terms value keep id #> <chr> <chr> <lgl> <chr> #> 1 medium snowball FALSE stopwords_GK5iC # With a custom stop words list tate_rec <- recipe(~., data = tate_text) %>% step_tokenize(medium) %>% step_stopwords(medium, custom_stopword_source = c("twice", "upon")) tate_obj <- tate_rec %>% prep(traimomg = tate_text) bake(tate_obj, new_data = NULL) %>% slice(2) %>% pull(medium) #> <textrecipes_tokenlist> #>  [3 tokens] #> # Unique Tokens: 3