Using google translate API in Matlab

13 views (last 30 days)
Amit Madahar
Amit Madahar on 27 Jun 2019
Commented: Amit Madahar on 3 Jul 2019
I am trying to use google translate API in matlab but struggling. I have implemented alternative method detailed below for the time being which works well but buggy with extra-long string, may be due to urlencoding, I am not sure. So want to implemented translation using Google Java APIs. I have done the following steps and hope experts here would be able to help me.
Google Translate API Set-up
1) Followed the google could translation API quick start guide at https://cloud.google.com/translate/docs/quickstart-client-libraries.
2) Set GCP project > set up environment variables on local machine > installed Google Cloud SDK > enabled Cloud Translation API > set up API credential key.
3) Downloaded google-could-translate API jar to provide Matlab /Java bridge to installed SDK and added it to dynamic path using javaaddpath.
Dry run in Java (IntelliJ)
4) Tried the following in java first to see if the above set-up works. It works!
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class gtAPI {
public static void main(String... args) throws Exception {
// Instantiates a client
Translate translate = TranslateOptions.getDefaultInstance().getService();
// The text to translate
String text = "Text to Translate";
// Translates some text into Russian
Translation translation =
translate.translate(
text,
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage("zh-CN"));
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.getTranslatedText());
}
}
Implementing above in Matlab Now I successfully imported the following:
import com.google.cloud.translate.*
import com.google.cloud.translate.Translate.*
now when I try to call
Translate.TranslateOptions.getDefaultInstance().getService();
I get the following error message.
Undefined variable "Translate" or class "Translate.TranslateOptions.getDefaultInstance"
which I am boggled by as the class does exist.
The idea is that I should be able use these directly into matlab just like other jar libraries, but somehow I am missing something. In Alternate Method below, I use Jsoup libraries to parse and query htmlDOM and all works great. So it is possible to do it directly in Matlab but I am missing something.
Alternatively I can write a java class that takes care of the translation and returns the translated string. Basically adapting the above java code to return me the translation but I wanted to do all that from within Matlab. Any ideas how?
Alternate Method - Currently Implemented
Currently working with the following methods, which works great but when it come to extra long strings, it struggles. I then have to chop the strings to be translated into manageable sizes for translation to proceed. That means I have to call this function many times adding to run time.
function output = translationAPI(foreignText,fromLang,toLang)
%% Uses google API to make the translations
% Input : foreignText - Text to be translated
% fromLang - Language of the text to be trnaslated, use google
% language tags. See GoogleTranslateAPI doccumentation.
% toLang - Language to be translated to
import org.jsoup.*
foreignText = char(java.net.URLEncoder.encode(foreignText,'UTF-8'));
qryStr = ['https://translation.googleapis.com/language/translate/v2?',...
'source=',fromLang,...
'&target=',toLang,...
'&key=yourKey',...
'&q=',foreignText];
conn = Jsoup.connect(qryStr);
conn = conn.timeout(20*1000);
conn = conn.referrer("http://www.google.com");
conn = conn.followRedirects(true);
conn = conn.userAgent("Chrome");
conn = conn.ignoreContentType(true); % Ignore conent type should be set to
% true when extracting rss xml to
% avoid errors
objectDOMStr = string(conn.get().toString);
objectDOMStr = strrep(objectDOMStr,'"','');
output = strtrim(string(regexp(objectDOMStr,'translatedText:(.*?)}','tokens')));
  2 Comments
Guillaume
Guillaume on 27 Jun 2019
Can you provide,
  • the portion of your code that works so far.
  • the bit that causes the error
  • the full text of the error message
Amit Madahar
Amit Madahar on 3 Jul 2019
Hi Guillaume - Sorry it was a very badly formed question the first time. I hope this time it explains the problem I am having.

Sign in to comment.

Answers (0)

Categories

Find more on External Language Interfaces in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!