样本代码:使用 Watson Natural Language Classifier 服务识别自然语言的语境

栏目: Java · 发布时间: 7年前

内容简介:样本代码:使用 Watson Natural Language Classifier 服务识别自然语言的语境

import java.util.List;

import com.google.gson.JsonObject; import com.google.gson.JsonParser;

import com.ibm.watson.developer_cloud.natural_language_classifier.v1.NaturalLanguageClassifier; import com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification; import com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifiedClass;

public class LanguageClassifier {

private static String data = "{"textToClassify" :"Will it rain tomorrow?"," + " "contextId" :"359f41x201-nlc-180573"," + " "username" :""," + " "password" :""," + " "endpoint" :"https://sandbox-watson-proxy.mybluemix.net/natural-language-classifier/api"," + " "skip_authentication" :"true"}";

public static void main(String[] args) { JsonParser parser = new JsonParser(); JsonObject jsonArgs = parser.parse(data).getAsJsonObject(); main(jsonArgs); }

public static JsonObject main(JsonObject args) { JsonParser parser = new JsonParser();

NaturalLanguageClassifier service = new NaturalLanguageClassifier();

service.setUsernameAndPassword
    (args.get("username").getAsString(),
     args.get("password").getAsString());

if (args.get("endpoint") != null)
  service.setEndPoint(args.get("endpoint").getAsString());

if (args.get("skip_authentication") != null)
  service.setSkipAuthentication((args.get("skip_authentication")
  .getAsString() == "true") ? true : false);

Classification result = service.classify
    (args.get("contextId").getAsString(),
     args.get("textToClassify").getAsString()).
    execute();

System.out.println("The most likely classification is " +
                   result.getTopClass() + "\n");
System.out.println("The complete list is:");
List<ClassifiedClass> classifications = result.getClasses();
for (ClassifiedClass nextClassification : classifications) {
  System.out.println("  Classification:" + nextClassification.getName() +
                     " (confidence:" +
                     (int)(nextClassification.getConfidence() * 100) +
                     "%)");
}

JsonObject returnObject = parser.parse(result.toString()).getAsJsonObject();
return returnObject;

} }


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

让创意更有黏性

让创意更有黏性

[美] 奇普·希思、[美] 丹·希思 / 姜奕晖 / 中信出版社 / 2014-1-8 / 49.00元

你或许相信在太空中唯一能看到的人工建筑就是万里长城,可乐能腐蚀人体骨骼,我们的大脑使用了10%;与此同时,你却记不得上周例会上领导的安排,昨天看过的那本书里写了什么,上次参加培训的主要内容…… 为什么? 这就引发出《让创意更有黏性》的核心问题:什么样的观点或创意具有强有力的黏性,能被他人牢牢记住? 国际知名行为心理学家希思兄弟根据大量的社会心理学研究案例,揭示了让创意或观点具有黏......一起来看看 《让创意更有黏性》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试