3

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 !

  • I think the issue is this bit ... `.translate(r.string)` in that it doesn't get the `string` from the function argument of `functionToApply` but from the global workspace. So `lapply` won't be able to pass values along to the function – user20650 Mar 28 '21 at 01:39
  • ... not answering your question but using `.translate_batch(r.string)` accepts vectors. So you can then run `string <- c("cat", "dog"); functionToApply(string)` without the outer wrapper – user20650 Mar 28 '21 at 01:53

2 Answers2

4

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.

Tomas Capretto
  • 721
  • 5
  • 6
1

Try lapply(c("cat", "dog"), FUN = TranslateEnglishStringToFrenchString)

Chriss Paul
  • 1,101
  • 6
  • 19