R/callbacks.R

Defines functions print.callback .construct_callback combine_metrics convergence_metric diff_metric perplexity_metric coherence_metric

Documented in coherence_metric combine_metrics convergence_metric diff_metric perplexity_metric

#' Callbacks
#' 
#' Callbacks can be used to observe the training process.
#' 
#' @param ... Any option from href{https://radimrehurek.com/gensim/models/callbacks.html#gensim.models.callbacks.CoherenceMetric}{official documentation}.
#' 
#' @name callbacks
#' @export
coherence_metric <- function(...) {
  cb <- gensim$models$callbacks$CoherenceMetric(...) %>% 
    .construct_callback()
  invisible(cb)
}

#' @rdname callbacks
#' @export
perplexity_metric <- function(...) {
  cb <- gensim$models$callbacks$PerplexityMetric(...) %>% 
    .construct_callback()
  invisible(cb)
}

#' @name callbacks
#' @export
diff_metric <- function(...) {
  cb <- gensim$models$callbacks$DiffMetric(...) %>% 
    .construct_callback()
  invisible(cb)
}

#' @name callbacks
#' @export
convergence_metric <- function(...) {
  cb <- gensim$models$callbacks$ConvergenceMetric(...) %>% 
    .construct_callback()
  invisible(cb)
}

#' @rdname callbacks
#' @export
combine_metrics <- function(...) {
  metrics <- reticulate::tuple(...) %>% 
    .construct_callback()
  invisible(metrics)
}

.construct_callback <- function(x){
  structure(x, class = c("callback", class(x)))
}

#' @export
print.callback <- function(x, ...){
  cat(
    crayon::blue(cli::symbol$info),
    "A callback function\n",
    ...
  )
}
news-r/gensimr documentation built on Jan. 9, 2021, 5:55 a.m.