IBM Watson Text Translator 示例语法错误。如何自动检测语言

IBM Watson Text Translator example syntactically wrong. How to auto detect language

根据 Translator API reference,识别语言使用以下代码:

LanguageTranslator service = new LanguageTranslator();
service.setUsernameAndPassword("{username}","{password}");

List <IdentifiedLanguage> langs = service.identify("this is a test");
System.out.println(langs);

但是正如您在随附的屏幕截图中看到的那样,它导致了语法错误。我已经通过将一行更改为 :

来更正了这一点
ServiceCall<List<IdentifiedLanguage>> langs = service.identify("this is a test");

如果能更新文档就好了。 错误消失了,但现在如何处理这个 ServiceCall?如何获取语言?

此外,如果 link 提供所有模型 ID,我们将不胜感激,因为这有助于 API 的初始评估。另外,我在哪里可以找到目前支持的语言?

service.identify("...") 调用需要 .execute(),而不是其他类型:

List<IdentifiedLanguage> langs = service.identify("this is a test").execute();

然后 returns 预期的 IdentifiedLanguages 列表。这是一个完整的示例,它记录了列表,然后从列表中选择置信度最高的语言,并记录了:

package com.watson.example;

import java.util.Collections;
import com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator;
import com.ibm.watson.developer_cloud.language_translator.v2.model.IdentifiedLanguage;

import java.util.Comparator;
import java.util.List;

public class ItentifyLanguage {
    public ItentifyLanguage() {
        LanguageTranslator service = new LanguageTranslator();
        service.setUsernameAndPassword("{username}","{password}");

        // identify returns a list of potential languages with confidence scores
        List<IdentifiedLanguage> langs = service.identify("this is a test").execute();
        System.out.println("language confidence scores:");
        System.out.println(langs);

        // this narrows the list down to a single language
        IdentifiedLanguage lang = Collections.max(langs, new Comparator<IdentifiedLanguage>() {
            public int compare (IdentifiedLanguage a, IdentifiedLanguage b) {
                return a.getConfidence().compareTo(b.getConfidence());
            }
        });

        System.out.println("Language " + lang.getLanguage() + " has the highest confidence score at " + lang.getConfidence());
    }


    public static void main(String[] args) {
        new ItentifyLanguage();
    }
}
  1. 简单地注释该行,因为我没有看到局部变量“langs”在该方法的任何地方进一步使用。

  2. 如果您需要它,请调用 service.identify("this is a test") 上的执行方法,然后将其初始化为变量“langs" 如下所示:

    列表语言 = service.identify("this is a test").execute();