Any equivalent of Google translate (like in gsheet) for DataFrames?

Hello there,

I’m used to googletranslating cell in google sheet, a solution that works quite well.

I wonder if there is any simple solution that can be used with DataFrames to translate some cell ?

The fact is i need to translate some hebrew to english.
I currently use formula like =GOOGLETRANSLATE(A1, "he", "en")
But this involve constantly shifting cell slices horizontally and vertically, which is very error prone.

A more reproducible research will be appreciated with DataFrames

These are two separate tasks. You need a function that translates a hebrew string into an english string.

https://github.com/sonicrules1234/GoogleTrans.jl

Then you can apply it to each of the values in the dataframe using transform.

The GoogleTrans.jl package seems to be broken?

FWIW, the solution posted here does work:

using HTTP, JSON, DataFrames

function gtranslate(text, targetlang, sourcelang = "auto")
    url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" * sourcelang * "&tl=" * targetlang * "&dt=t&q=" * HTTP.escapeuri(text)
    result = JSON.parse(String(HTTP.request("GET", url).body))
    join([s[1] for s in result[1]], "")
end

messages = ["Hello Julia." "Thank you for everything"; "You are welcome!" "Bye now"]
df = DataFrame(messages, :auto)

 Row │ x1                x2
─────┼────────────────────────────────────────────
   1 │ Hello Julia.      Thank you for everything
   2 │ You are welcome!  Bye now

gtranslate.(df,"fr")

 Row │ x1                x2
─────┼────────────────────────────────────────────
   1 │ Bonjour Julia.    Merci pour tout
   2 │ Je vous en prie!  Et maintenant, au revoir

gtranslate.(df,"iw")

 Row │ x1            x2
─────┼───────────────────────────
   1 │ שלום ג'וליה.  תודה על הכל
   2 │ בבקשה!        להתראות
3 Likes

Your snippet works for me too. Thanks.

A little remark about coding style concerning

join([s[1] for s in result[1]], "")

I prefer the form

result[1] .|> first |> join

.|> is not known enough (sometimes it right-associates eagerly, then block it with parenthesis or separated exprs)

Thanks anyway. Happy New Year to all

1 Like

This seems to be equivalent to the shorter: join(first.(result[1]))

Absolutely. But the shorter form is not the only criteria IMO. I like a balance of concision and readability with context. The pipe conveys a sense of narrowing as used in modern query language for document db, graph db, stylesheet selector etc.
A game changer when refactoring / testing big document

1 Like