样本代码:使用 Watson Personality Insights 服务分析文本

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

内容简介:样本代码:使用 Watson Personality Insights 服务分析文本

import java.util.List;

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

import com.ibm.watson.developer_cloud.personality_insights.v3.PersonalityInsights; import com.ibm.watson.developer_cloud.personality_insights.v3.model.Profile; import com.ibm.watson.developer_cloud.personality_insights.v3.model.Trait;

public class GetInsights {

private static String textToAnalyze = "Call me Ishmael.Some years ago-never mind how long precisely-having " + "little or no money in my purse, and nothing particular to interest " + "me on shore, I thought I would sail about a little and see the " + "watery part of the world.It is a way I have of driving off the " + "spleen and regulating the circulation.Whenever I find myself " + "growing grim about the mouth; whenever it is a damp, drizzly " + "November in my soul; whenever I find myself involuntarily pausing " + "before coffin warehouses, and bringing up the rear of every " + "funeral I meet; and especially whenever my hypos get such an upper " + "hand of me, that it requires a strong moral principle to prevent " + "me from deliberately stepping into the street, and methodically " + "knocking people's hats off-then, I account it high time to get to " + "sea as soon as I can.This is my substitute for pistol and ball." + "With a philosophical flourish Cato throws himself upon his sword; " + "I quietly take to the ship.There is nothing surprising in this." + "If they but knew it, almost all men in their degree, some time or " + "other, cherish very nearly the same feelings towards the ocean " + "with me.There now is your insular city of the Manhattoes, belted " + "round by wharves as Indian isles by coral reefs-commerce surrounds " + "it with her surf.Right and left, the streets take you waterward.";

private static String data = "{"textToAnalyze":"" + textToAnalyze + ""," + " "username" :""," + " "password" :""," + " "endpoint" :"https://sandbox-watson-proxy.mybluemix.net/personality-insights/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();

PersonalityInsights service = new
  PersonalityInsights(PersonalityInsights.VERSION_DATE_2016_10_19);
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);

Profile result =
  service.getProfile(args.get("textToAnalyze").getAsString()).execute();

System.out.println("Watson's analysis\n\nComment:" +
                   result.getWordCountMessage() + "\n");

System.out.println("Personality traits:");
List<Trait> traits = result.getPersonality();
for (Trait nextTrait : traits) {
  System.out.println("- " + nextTrait.getName() + " - (" +
                     (int) (nextTrait.getPercentile() * 100) +
                     " percentile)");
}
System.out.println("\nEmotional needs:");
List<Trait> needs = result.getNeeds();
for (Trait nextNeed : needs) {
  System.out.println("- " + nextNeed.getName() + " - (" +
                     (int) (nextNeed.getPercentile() * 100) +
                     " percentile)");
}
System.out.println("\nPersonal values:");
List<Trait> values = result.getValues();
for (Trait nextValue : values) {
  System.out.println("- " + nextValue.getName() + " - (" +
                     (int) (nextValue.getPercentile() * 100) +
                     " percentile)");
}

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

} }


以上所述就是小编给大家介绍的《样本代码:使用 Watson Personality Insights 服务分析文本》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

链接

链接

[美]艾伯特-拉斯洛•巴拉巴西 (Albert-László Barabási) / 沈华伟 / 浙江人民出版社 / 2013-8-1 / 59.90元

[内容简介] ★《链接》是《爆发》的作者,艾伯特-拉斯洛•巴拉巴西的成名之作,同时也是复杂网络的奠基之作,社交网络的入门之作。巴拉巴西之前,随机网络理论一直主导者我们的网络思维,是巴拉巴西第一个证明了,我们不是生活在随机世界里,真实网络是无尺度的。 ★巴拉巴西在书中追溯了网络的数学起源,分析了社会学家在此基础上得出的研究成果,最后提出自己的观点:我们周围的复杂网络,从鸡尾酒会、恐怖组织......一起来看看 《链接》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码