gonz
gonz

Reputation: 123

How can I get the translate () method to receive Strings or a given list compatible with the string type?

I have these two classes and I want to make them work. The problem is this line:

TranslationResult translationResult = service.translate(lista.get(0).toString(), Language.PORTUGUESE, Language.ENGLISH).execute();

I want it to receive a list containing a list value something like:

TranslationResult translationResult = service.translate(lista.get(0).toString(), lista.get(1).toString() , lista.get(2).toString()).execute();

I want it to receive a list containing a list value something like:

import com.ibm.watson.developer_cloud.language_translation.v2.model.TranslationResult;
import Teste.Watson;

import java.util.Map;

public class Cognitive implements Serializable {

    static public String Translate(ArrayList lista) {

        LanguageTranslation service = new LanguageTranslation();
        service.setUsernameAndPassword(lista.get(3).toString(), lista.get(4).toString());
        TranslationResult translationResult = service.translate(lista.get(0).toString(), Language.PORTUGUESE, Language.ENGLISH).execute();

        String translation = translationResult.getFirstTranslation();
        return translation;
    }
}

Class Test:

public class Watson {

    public static void main(String[] args) {

        ArrayList<Object> lista = new ArrayList<>();

        lista.add("Isse texto vai virar ingles"); 
        lista.add(Language.PORTUGUESE);
        lista.add(Language.ITALIAN);
        lista.add("adm");
        lista.add("password");

        String result= Cognitive.Translate(lista);
        System.out.println(result);
    }
}

I tried that way but gives this error:

static public String Translate(ArrayList lista){


    LanguageTranslation service = new LanguageTranslation();
    final Language srcLang;
    final Language srcDest;

    srcLang = (Language) lista.get(1);
    srcDest = (Language) lista.get(2);
    service.setUsernameAndPassword(lista.get(3).toString(), lista.get(4).toString());
    TranslationResult translationResult = service.translate(lista.get(0).toString(), srcLang, srcDest).execute();

    String translation = translationResult.getFirstTranslation();

    return translation;
}

Error:

jun 30, 2016 10:09:50 AM com.ibm.watson.developer_cloud.service.WatsonService processServiceCall
GRAVE: POST https://gateway.watsonplatform.net/language-  translation/api/v2/translate, status: 404, error: cannot find service matching   the request data
Exception in thread "main"   com.ibm.watson.developer_cloud.service.exception.NotFoundException: cannot find service matching the request data
at   com.ibm.watson.developer_cloud.service.WatsonService.processServiceCall(WatsonService.java:381)
at com.ibm.watson.developer_cloud.service.WatsonService$1.execute(WatsonService.java:170)
at ibm.Cognitive.Translate(Cognitive.java:28)
at Teste.Watson.main(Watson.java:21)

Upvotes: 0

Views: 936

Answers (1)

explv
explv

Reputation: 2759

Instead of using a List which is a strange and hacky solution, you should instead just have parameters for your method:

public class Cognitive implements Serializable {

    public static String translate(final String text, final Language srcLang, final Language destLang, final String username, final String password) {

        LanguageTranslation service = new LanguageTranslation();
        service.setUsernameAndPassword(username, password);
        TranslationResult translationResult = service.translate(text, srcLang, destLang).execute();
        return translationResult.getFirstTranslation();
    }
}

Upvotes: 2

Related Questions