Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a lapply-style function in R with Python code inside with package reticulate?

I built a simple translation function in R with Python code inside. It works well for one string. But how do should apply it for a list of strings?

string <- c("cat")
string <- c("cat", "dog")
translations.df <- TranslateEnglishStringToFrenchString(string)
View(translations.df)

TranslateEnglishStringToFrenchString <- function(string){
  functionToApply <- function(string){
    reticulate::py_run_string("from deep_translator import GoogleTranslator")
    reticulate::py_run_string("translatedString = GoogleTranslator(source='en', target='fr').translate(r.string)")
    translatedString <- py$translatedString
    .df <- data.frame(string, translatedString)
    return(.df)
  }
  toReturn.df <- do.call(rbind, lapply(string, functionToApply))
  return(toReturn.df)
}

For the moment, it returns the following error :

Error in py_run_string_impl(code, local, convert) : NotValidPayload: ['cat', 'dog'] --> text must be a valid text with maximum 5000 character, otherwise it cannot be translated

Nevertheless, it is clear that it is not a question of maximum characters here...

Thank you very much for your help !

like image 896
Ludovic Bocken Avatar asked Apr 27 '26 15:04

Ludovic Bocken


1 Answers

This version works on my computer with a conda environment called "rprog".

library(reticulate)
use_condaenv("rprog", required = TRUE)
py_run_string(
"
from deep_translator import GoogleTranslator
def google_translator(string):
    return GoogleTranslator(source='en', target='fr').translate(string)
"
)
lapply(c("hello", "coffee"), py$google_translator)

[[1]]
[1] "Bonjour"

[[2]]
[1] "café"

It is important to call library(reticulate). Otherwise, the object py is not made available and you can't access Python objects.

Instead of calling an R function, you create a Python function, and call that function from R.

like image 179
Tomas Capretto Avatar answered Apr 29 '26 05:04

Tomas Capretto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!