zumbov2 / deeplr

An R wrapper for the DeepL Translator API
Other
38 stars 4 forks source link

`detect` fails when provided with NA #6

Open sebastian-c opened 1 year ago

sebastian-c commented 1 year ago

When detect2 is given NA, it fails before authentication as there's a missing value at the utf8 check. The expected functionality would be to note the position of the NAs and return a vector with translations and the NAs intact in their correct positions.

> detect2(NA_character_, "")

Error in if (!utf8::utf8_valid(text)) stop("Text input cannot be translated to a valid UTF-8 string.") : 
  missing value where TRUE/FALSE needed
sebastian-c commented 1 year ago

Here's my workaround function:

Detect <- function(text, auth_key = DEEPL_API_KEY){

  text_positions <- which(!is.na(text))

  language <- detect2(text[text_positions], auth_key)

  new_vec <- rep(NA_character_, length(text))
  new_vec[text_positions] <- language

  return(new_vec)
}