如何让translate()方法接收字符串或与字符串类型兼容的给定列表?

时间:2016-06-30 12:40:39

标签: java ibm-cloud ibm-watson

我有这两个课程,我想让它们发挥作用。问题是这一行:

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

我希望它能够收到一个包含列表值的列表,如:

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

我希望它能够收到一个包含列表值的列表,如:

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;
    }
}

班级考试:

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);
    }
}

我尝试过这种方式,但却出现了这个错误:

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;
}

错误:

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)

1 个答案:

答案 0 :(得分:2)

而不是使用List,这是一个奇怪的hacky解决方案,你应该只为你的方法提供参数:

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();
    }
}