R/tokens_recompile.R

Defines functions tokens_recompile.tokens tokens_recompile.tokens_xptr tokens_recompile.default tokens_recompile

Documented in tokens_recompile

#' Remove redundant or unused tokens IDs
#'
#' This function reassign tokens IDs after changes in removal or transformation of 
#' tokens. It update token IDs to ensure that they are unique and dense. 
#' Usually, this function is only applied to [tokens_xptr] objects at the end of 
#' text pre-processing.
#' @param x the [tokens_xptr] object.
#' @param force if `TRUE`, update tokens IDs even when it may not be necessary.
#' @keywords tokens internal
#' @export
#' @examples
#' toks <- tokens(c(one = "a b c d A B C D",
#'                  two = "A B C d"), xptr = TRUE)
#' toks <- tokens_tolower(toks)
#' types(toks)
#' types(tokens_recompile(toks))
tokens_recompile <- function(x, force = FALSE) {
    UseMethod("tokens_recompile")
}

#' @export
tokens_recompile.default <- function(x, force = FALSE) {
    check_class(class(x), "tokens_recompile")
}

#' @export
tokens_recompile.tokens_xptr <- function(x, force = FALSE) {
    cpp_recompile(x, force = force)
}

#' @export
tokens_recompile.tokens <- function(x, force = FALSE) {
    as.tokens(tokens_recompile(as.tokens_xptr(x), force))
}

Try the quanteda package in your browser

Any scripts or data that you put into this service are public.

quanteda documentation built on Aug. 5, 2026, 1:09 a.m.