作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这两个类(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/
我是一名优秀的程序员,十分优秀!