【问题标题】:IBM Watson Text Translator example syntactically wrong. How to auto detect languageIBM Watson Text Translator 示例语法错误。如何自动检测语言
【发布时间】:2017-02-24 10:27:27
【问题描述】:

根据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?如何获取语言?

此外,任何提供所有模型 ID 的链接都将不胜感激,因为这有助于对 API 进行初始评估。另外我在哪里可以找到目前支持的语言?

【问题讨论】:

    标签: java ibm-watson language-translation


    【解决方案1】:
    1. 只需注释该行,因为我没有看到局部变量“langs”在该方法的任何其他地方使用。

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

      List langs = service.identify("这是一个测试").execute();

    【讨论】:

      【解决方案2】:

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

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

      然后它返回预期的 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();
          }
      }
      

      【讨论】:

        【解决方案3】:

        【讨论】:

          猜你喜欢
          • 2019-12-19
          • 2016-03-11
          • 1970-01-01
          • 1970-01-01
          • 2017-12-23
          • 1970-01-01
          • 2017-08-11
          • 2020-07-25
          • 2016-10-06
          相关资源
          最近更新 更多