样本代码:使用 Watson Tone Analysis 服务识别书面文本的语气

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

内容简介:样本代码:使用 Watson Tone Analysis 服务识别书面文本的语气

import java.util.List;

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

import com.ibm.watson.developer_cloud.tone_analyzer.v3.ToneAnalyzer; import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ElementTone; import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis; import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneCategory; import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneScore;

public class CheckTheTone {

private static String textToAnalyze = "I know the times are difficult!Our sales have been " + "disappointing for the past three quarters for our data analytics " + "product suite.We have a competitive data analytics product " + "suite in the industry.But we need to do our job selling it!" + "We need to acknowledge and fix our sales challenges." + "We can't blame the economy for our lack of execution!" + "We are missing critical sales opportunities." + "Our product is in no way inferior to the competitor products." + "Our clients are hungry for analytical tools to improve their " + "business outcomes.Economy has nothing to do with it.";

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

ToneAnalyzer service = new ToneAnalyzer("2016-05-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);

ToneAnalysis result =
    service.getTone(args.get("textToAnalyze").getAsString(), null).
    execute();

ElementTone elementTone = result.getDocumentTone();
List<ToneCategory> toneCategories = elementTone.getTones();
for (ToneCategory nextCategory : toneCategories) {
  System.out.println("Analysis for " + nextCategory.getName());

  List<ToneScore> toneScores = nextCategory.getTones();
  for (ToneScore nextScore : toneScores) {
    System.out.println("    " + nextScore.getName() + " = " +
                       (int)(nextScore.getScore() * 100) + "%");
  }
  System.out.println();
}

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

} }


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

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

计算机组成:结构化方法

计算机组成:结构化方法

坦嫩鲍姆 / 刘卫东 / 人民邮电出版社 / 2006-1 / 65.00元

本书采用结构化方法来介绍计算机系统,书的内容完全建立在“计算机是由层次结构组成的,每层完成规定的功能”这一概念之上。作者对本版进行了彻底的更新,以反映当今最重要的计算机技术以及计算机组成和体系结构方面的最新进展。书中详细讨论了数字逻辑层、微体系结构层、指令系统层、操作系统层和汇编语言层,并涵盖了并行体系结构的内容,而且每一章结尾都配有丰富的习题。 本书适合作为计算机专业本科生计算机组......一起来看看 《计算机组成:结构化方法》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具