From 3227b0d7dba8785949c1d66c83d232bb38438c08 Mon Sep 17 00:00:00 2001 From: Angelo D'Ambrosio Date: Fri, 11 Oct 2024 20:08:06 +0200 Subject: [PATCH] fix: use rlang to check and install suggested pkgs --- R/NLP_operations.R | 8 +++--- R/speech_to_text.R | 9 ++----- R/utils.R | 37 --------------------------- man/check_and_install_dependencies.Rd | 18 ------------- 4 files changed, 6 insertions(+), 66 deletions(-) delete mode 100644 man/check_and_install_dependencies.Rd diff --git a/R/NLP_operations.R b/R/NLP_operations.R index f54114d..c47aa4c 100644 --- a/R/NLP_operations.R +++ b/R/NLP_operations.R @@ -11,7 +11,7 @@ #' tokenize_text <- function(text) { - check_and_install_dependencies("text2vec") + rlang::check_installed("text2vec") # Convert text to lowercase, remove special characters and extra spaces, and # tokenize @@ -50,7 +50,7 @@ generate_glove_model <- function( overwrite = FALSE ) { - check_and_install_dependencies("parallel") + rlang::check_installed("parallel") # Create a hash of the data and the model parameters data_hash <- rlang::hash(list( @@ -177,7 +177,7 @@ compute_text_sim <- function(x_text, y_texts, embedding_matrix) { # # if (method == "word2vec") { # -# check_and_install_dependencies("word2vec") +# rlang::check_installed("word2vec") # # # Word2vec returns NA if a segment is a single word with a punctuation # # mark at the end, don't know why @@ -191,7 +191,7 @@ compute_text_sim <- function(x_text, y_texts, embedding_matrix) { # word2vec::doc2vec(model, x_text)) |> # as.vector() # } else { -# check_and_install_dependencies("stringdist") +# rlang::check_installed("stringdist") # # y_probes$similarity <- stringdist::stringsim( # x_text, y_probes$text, method = "cosine") diff --git a/R/speech_to_text.R b/R/speech_to_text.R index 84f594a..eaad207 100644 --- a/R/speech_to_text.R +++ b/R/speech_to_text.R @@ -212,7 +212,7 @@ split_audio <- function( ) { # Check if the av package is installed and ask to install it if not - check_and_install_dependencies("av") + rlang::check_installed("av") # Calculate segment length in seconds segment_length_sec <- segment_duration * 60 @@ -278,7 +278,7 @@ use_whisper_ctranslate2_stt <- function( n_threads = NULL ) { - check_and_install_dependencies("parallel") + rlang::check_installed("parallel") if (is.null(n_threads)) { n_threads <- parallel::detectCores() @@ -315,11 +315,6 @@ use_whisper_ctranslate2_stt <- function( jsonlite::read_json(output_file_path) - # In console, synchronous version, no logging - # p <- processx::process$run( - # "whisper-ctranslate2", - # args = args, - # echo_cmd = T, stdout = "|") } #' Use Azure Whisper Model for Speech-to-Text diff --git a/R/utils.R b/R/utils.R index c054efb..614a501 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,40 +1,3 @@ -#' Check and install dependencies -#' -#' Checks if a list of packages are installed and installs them if not. Trigger -#' an error if the user chooses not to install a package. -#' -#' @param deps A character vector of package names. -#' -#' @return Nothing. -#' -check_and_install_dependencies <- function(deps) { - for (dep in deps) { - stop_message <- paste0(dep, " is required but was not installed.") - # Check if the package is installed - is_installed <- requireNamespace(dep, quietly = TRUE) - - if (!is_installed) { - # If not, ask the user if they want to install it - if (interactive()) { - # Only in interactive sessions, otherwise just stop - do_install <- utils::menu( - c("Yes", "No"), - title = paste0(dep, " is not installed. Install it now?")) == 1 - - if(do_install) { - try({ - utils::install.packages(dep) - # After successful installation, recheck if the package is now installed - is_installed <- requireNamespace(dep, quietly = FALSE) - }) - } - } - } - - # Stop if the package is not installed - if (!is_installed) stop(stop_message) - } -} #' Check if transcript segments are silent #' diff --git a/man/check_and_install_dependencies.Rd b/man/check_and_install_dependencies.Rd deleted file mode 100644 index 27e5eb0..0000000 --- a/man/check_and_install_dependencies.Rd +++ /dev/null @@ -1,18 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{check_and_install_dependencies} -\alias{check_and_install_dependencies} -\title{Check and install dependencies} -\usage{ -check_and_install_dependencies(deps) -} -\arguments{ -\item{deps}{A character vector of package names.} -} -\value{ -Nothing. -} -\description{ -Checks if a list of packages are installed and installs them if not. Trigger -an error if the user chooses not to install a package. -}