step_stopwords()
creates a specification of a recipe step that will
filter a token
variable for stop words.
Usage
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")
)
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 which variables are affected by the step. See
recipes::selections()
for more details.- role
Not used by this step since no new variables are created.
- trained
A logical to indicate if the quantities for preprocessing have been estimated.
- columns
A character string of variable names that will be populated (eventually) by the
terms
argument. This isNULL
until the step is trained byrecipes::prep.recipe()
.- language
A character to indicate the language of stop words by ISO 639-1 coding scheme.
- keep
A logical. Specifies whether to keep the stop words or discard them.
- stopword_source
A character to indicate the stop words source as listed in
stopwords::stopwords_getsources
.- custom_stopword_source
A character vector to indicate a custom list of words that cater to the users specific problem.
- skip
A logical. Should the step be skipped when the recipe is baked by
recipes::bake.recipe()
? While all operations are baked whenrecipes::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 usingskip = FALSE
.- id
A character string that is unique to this step to identify it.
Value
An updated version of recipe
with the new step added
to the sequence of existing steps (if any).
Details
Stop words are words which sometimes are removed 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.
The argument custom_stopword_source
allows you to pass a character vector
to filter against. With the keep
argument one can specify words to keep
instead of removing thus allowing you to select words with a combination of
these two arguments.
Tidying
When you tidy()
this step, a tibble is returned with
columns terms
, value
, keep
, and id
:
- terms
character, the selectors or variables selected
- value
character, name of stop word list
- keep
logical, whether stop words are removed or kept
- id
character, id of this step
See also
step_tokenize()
to turn characters into tokens
Other Steps for Token Modification:
step_lemma()
,
step_ngram()
,
step_pos_filter()
,
step_stem()
,
step_tokenfilter()
,
step_tokenmerge()
Examples
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[1]>
#> [1] [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_jUJMM
tidy(tate_obj, number = 2)
#> # A tibble: 1 × 4
#> terms value keep id
#> <chr> <chr> <lgl> <chr>
#> 1 medium snowball FALSE stopwords_jUJMM
# 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[1]>
#> [1] [3 tokens]
#> # Unique Tokens: 3