gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:30:49 24 4
gpt4 key购买 nike

我有这两个类(class),我想让它们发挥作用。问题出在这一行:

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)

最佳答案

您应该只为您的方法提供参数,而不是使用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();
}
}

关于java - 如何让translate()方法接收字符串或与字符串类型兼容的给定列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38123406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com