step_tokenfilter creates a specification of a recipe step that will convert a tokenlist to be filtered based on frequency.

step_tokenfilter(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  columns = NULL,
  max_times = Inf,
  min_times = 0,
  percentage = FALSE,
  max_tokens = 100,
  res = NULL,
  skip = FALSE,
  id = rand_id("tokenfilter")
)

# S3 method for step_tokenfilter
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_tokenfilter, 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

Not used by this step since no new variables are created.

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().

max_times

An integer. Maximal number of times a word can appear before getting removed.

min_times

An integer. Minimum number of times a word can appear before getting removed.

percentage

A logical. Should max_times and min_times be interpreded as a percentage instead of count.

max_tokens

An integer. Will only keep the top max_tokens tokens after filtering done by max_times and min_times. Defaults to 100.

res

The words that will be keep will be stored here once this preprocessing step has be trained by prep.recipe().

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_tokenfilter object.

Value

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

Details

This step allow you to limit the tokens you are looking at by filtering on their occurrence in the corpus. You are able to exclude tokens if they appear too many times or too fews times in the data. It can be specified as counts using max_times and min_times or as percentages by setting percentage as TRUE. In addition one can filter to only use the top max_tokens used tokens. If max_tokens is set to Inf then all the tokens will be used. This will generally lead to very large datasets when then tokens are words or trigrams. A good strategy is to start with a low token count and go up according to how much RAM you want to use.

It is strongly advised to filter before using step_tf or step_tfidf to limit the number of variables created.

See also

step_tokenize() to turn character into tokenlist.

Other tokenlist to tokenlist steps: step_lemma(), step_ngram(), step_pos_filter(), step_stem(), step_stopwords(), step_tokenmerge()

Examples

library(recipes) library(modeldata) data(okc_text) okc_rec <- recipe(~ ., data = okc_text) %>% step_tokenize(essay0) %>% step_tokenfilter(essay0) okc_obj <- okc_rec %>% prep() juice(okc_obj, essay0) %>% slice(1:2)
#> # A tibble: 2 x 1 #> essay0 #> <tknlist> #> 1 [83 tokens] #> 2 [13 tokens]
juice(okc_obj) %>% slice(2) %>% pull(essay0)
#> <textrecipes_tokenlist[1]> #> [1] [13 tokens] #> # Unique Tokens: 7
tidy(okc_rec, number = 2)
#> # A tibble: 1 x 3 #> terms value id #> <chr> <int> <chr> #> 1 essay0 NA tokenfilter_TxTTv
tidy(okc_obj, number = 2)
#> # A tibble: 1 x 3 #> terms value id #> <quos> <list> <chr> #> 1 essay0 <int [1]> tokenfilter_TxTTv