diff --git a/.utility/travis.sh b/.utility/travis.sh index 00c94ca73f6..5ec8a6f0f35 100755 --- a/.utility/travis.sh +++ b/.utility/travis.sh @@ -2,8 +2,8 @@ if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then echo '$TRAVIS_PULL_REQUEST is false, running all tests' - mvn clean cobertura:cobertura-integration-test coveralls:report + mvn clean cobertura:cobertura-integration-test else echo '$TRAVIS_PULL_REQUEST is not false ($TRAVIS_PULL_REQUEST), running unit tests' mvn clean test -fi +fi \ No newline at end of file diff --git a/README.md b/README.md index bda666c3ad3..cc549e5f1cb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Watson Developer Cloud Java SDK [![Build Status](https://travis-ci.org/watson-developer-cloud/java-sdk.svg?branch=master)](https://travis-ci.org/watson-developer-cloud/java-sdk) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.ibm.watson.developer_cloud/java-sdk/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.ibm.watson.developer_cloud/java-sdk) -[![Coverage Status](https://coveralls.io/repos/watson-developer-cloud/java-sdk/badge.svg)](https://coveralls.io/github/watson-developer-cloud/java-sdk) +[![codecov.io](https://codecov.io/github/watson-developer-cloud/java-sdk/coverage.svg?branch=master)](https://codecov.io/github/watson-developer-cloud/java-sdk?branch=master) Java client library to use the [Watson Developer Cloud][wdc] services, a collection of REST APIs and SDKs that use cognitive computing to solve complex problems. @@ -14,7 +14,6 @@ APIs and SDKs that use cognitive computing to solve complex problems. * [Usage](#usage) * [Getting the Service Credentials](#getting-the-service-credentials) * [Questions](#questions) - * [Examples](#examples) * [IBM Watson Services](#ibm-watson-services) * [Alchemy Language](#alchemy-language) * [Alchemy Vision](#alchemy-vision) @@ -23,9 +22,7 @@ APIs and SDKs that use cognitive computing to solve complex problems. * [Concept Insights](#concept-insights) * [Dialog](#dialog) * [Document Conversion](#document-conversion) - * [Language Identification](#language-identification) * [Language Translation](#language-translation) - * [Machine Translation](#machine-translation) * [Natural Language Classifier](#natural-language-classifier) * [Personality Insights](#personality-insights) * [Relationship Extraction](#relationship-extraction) @@ -38,9 +35,7 @@ APIs and SDKs that use cognitive computing to solve complex problems. * [Visual Recognition](#visual-recognition) * [Android](#android) * [Running in Bluemix](#running-in-bluemix) - * [Build + Test](#build--test) * [Eclipse and Intellij](#working-with-eclipse-and-intellij-idea) - * [Open Source @ IBM](#open-source--ibm) * [License](#license) * [Contributing](#contributing) @@ -53,13 +48,13 @@ APIs and SDKs that use cognitive computing to solve complex problems. com.ibm.watson.developer_cloud java-sdk - 2.7.1 + 2.8.0 ``` ##### Gradle ```gradle -'com.ibm.watson.developer_cloud:java-sdk:2.7.1' +'com.ibm.watson.developer_cloud:java-sdk:2.8.0' ``` Now, you are ready to see some [examples](https://github.com/watson-developer-cloud/java-sdk/tree/master/examples/java/com/ibm/watson/developer_cloud). @@ -129,7 +124,7 @@ service.setApiKey(""); File image = new File("src/test/resources/alchemy/obama.jpg"); Boolean forceShowAll = false; Boolean knowledgeGraph = false; -ImageKeywords keywords = service.getImageKeywords(, forceShowAll, knowledgeGraph); +ImageKeywords keywords = service.getImageKeywords(image, forceShowAll, knowledgeGraph); System.out.println(keywords); ``` @@ -365,9 +360,10 @@ System.out.println(voices); Use the [Tone Analyzer][tone_analyzer] service to get the tone of your email. ```java -ToneAnalyzer service = new ToneAnalyzer(); +ToneAnalyzer service = new ToneAnalyzer(ToneAnalyzer.VERSION_DATE_2016_02_11); service.setUsernameAndPassword("", ""); + String text = "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 " @@ -377,6 +373,10 @@ String text = "I know the times are difficult! Our sales have been " // Call the service and get the tone Tone tone = service.getTone(text, Scorecard.EMAIL); System.out.println(tone); + + +ToneAnalysis tone = service.getTone(text); +System.out.println(tone); ``` @@ -481,7 +481,7 @@ Gradle: ```sh $ cd java-sdk - $ gradle jar # build jar file (build/libs/watson-developer-cloud-2.7.1.jar) + $ gradle jar # build jar file (build/libs/watson-developer-cloud-2.8.0.jar) $ gradle test # run tests ``` @@ -551,4 +551,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md). [apache_maven]: http://maven.apache.org/ [releases]: https://github.com/watson-developer-cloud/java-sdk/releases -[jar]: https://github.com/watson-developer-cloud/java-sdk/releases/download/java-sdk-2.7.1/java-sdk-2.7.1-jar-with-dependencies.jar +[jar]: https://github.com/watson-developer-cloud/java-sdk/releases/download/java-sdk-2.8.0/java-sdk-2.8.0-jar-with-dependencies.jar diff --git a/examples/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerExample.java b/examples/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerExample.java new file mode 100644 index 00000000000..0ad13a5e617 --- /dev/null +++ b/examples/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerExample.java @@ -0,0 +1,42 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3; + +import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis; + +public class ToneAnalyzerExample { + + + public static void main(String[] args) { + ToneAnalyzer service = new ToneAnalyzer(ToneAnalyzer.VERSION_DATE_2016_02_11); + service.setUsernameAndPassword("", ""); + + String text = + "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."; + + // Call the service and get the tone + ToneAnalysis tone = service.getTone(text); + System.out.println(tone); + + } +} diff --git a/examples/retrieve-and-rank-solrj/pom.xml b/examples/retrieve-and-rank-solrj/pom.xml index be6efbcccfa..11d348e8d4f 100644 --- a/examples/retrieve-and-rank-solrj/pom.xml +++ b/examples/retrieve-and-rank-solrj/pom.xml @@ -27,7 +27,7 @@ com.ibm.watson.developer_cloud java-sdk - 2.7.1 + 2.8.0 diff --git a/pom.xml b/pom.xml index f2f705c884e..856a512f39a 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.ibm.watson.developer_cloud - 2.7.2-SNAPSHOT + 2.8.1-SNAPSHOT jar java-sdk Watson Developer Cloud Java SDK @@ -128,23 +128,6 @@ deploy - - - org.codehaus.mojo - cobertura-maven-plugin - 2.7 - - xml - - - com/ibm/watson/developer_cloud/*/v*/model/**/*.class - - - com/ibm/watson/developer_cloud/*/**/*Example.class - - - - org.eluder.coveralls coveralls-maven-plugin @@ -174,51 +157,23 @@ - - org.jacoco - jacoco-maven-plugin - 0.7.5.201505241946 - - - com/ibm/watson/developer_cloud/*/v*/model/**/*.class - com/ibm/watson/developer_cloud/**/*Example.* - - - - - - prepare-agent - - - - report - test - - report - - - - - + diff --git a/src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/ConceptInsights.java b/src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/ConceptInsights.java index 2175014db5d..65b843d889f 100644 --- a/src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/ConceptInsights.java +++ b/src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/ConceptInsights.java @@ -213,7 +213,7 @@ public ConceptInsights() { * @return {@link Annotations} */ public Annotations annotateText(final Graph graph, final String text) { - final String graphId = IDHelper.getGraphId(graph, getAccountId()); + final String graphId = IDHelper.getGraphId(graph, getFirstAccountId()); Validate.notEmpty(text, "text cannot be empty"); final Request request = @@ -243,7 +243,7 @@ public Annotations annotateText(final Graph graph, final String text) { */ public QueryConcepts conceptualSearch(Corpus corpus, Map parameters) { Validate.notNull(parameters.get(IDS), "ids cannot be null"); - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); final Map queryParams = new HashMap(); final String[] queryParameters = new String[] {CURSOR, LIMIT}; @@ -283,7 +283,7 @@ public QueryConcepts conceptualSearch(Corpus corpus, Map paramet * @param corpus Corpus the corpus object. */ public void createCorpus(final Corpus corpus) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); final Request request = RequestBuilder .put(API_VERSION + corpusId) @@ -314,7 +314,7 @@ public void createDocument(final Document document) { * @param corpus Corpus the corpus object. */ public void deleteCorpus(final Corpus corpus) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); final Request request = RequestBuilder.delete(API_VERSION + corpusId).build(); executeWithoutResponse(request); } @@ -352,11 +352,11 @@ private T executeRequest(final String resourcePath, } /** - * Gets the account id. + * Returns the first account id. * * @return the account id */ - private String getAccountId() { + public String getFirstAccountId() { if (accountId == null) { final Accounts accounts = getAccountsInfo(); if (accounts != null && accounts.getAccounts() != null && !accounts.getAccounts().isEmpty()) { @@ -426,7 +426,7 @@ public Concepts getConceptRelatedConcepts(final Concept concept, * @return the Corpus */ public Corpus getCorpus(final Corpus corpus) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); return executeRequest(API_VERSION + corpusId, null, Corpus.class); } @@ -437,7 +437,7 @@ public Corpus getCorpus(final Corpus corpus) { * @return {@link CorpusProcessingState} The processing state of a given corpus. */ public CorpusProcessingState getCorpusProcessingState(final Corpus corpus) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); return executeRequest(API_VERSION + corpusId + PROCESSING_STATE_PATH, null, CorpusProcessingState.class); } @@ -457,7 +457,7 @@ public CorpusProcessingState getCorpusProcessingState(final Corpus corpus) { * @return {@link Concepts} */ public Concepts getCorpusRelatedConcepts(final Corpus corpus, final Map parameters) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); final Map queryParameters = new HashMap(); final String[] params = new String[] {LEVEL, LIMIT}; @@ -483,7 +483,7 @@ public Concepts getCorpusRelatedConcepts(final Corpus corpus, final Map concepts) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); Validate.notEmpty(concepts, "concepts cannot be empty"); final Map queryParameters = new HashMap(); @@ -505,7 +505,7 @@ public Scores getCorpusRelationScores(final Corpus corpus, final List c * @return the {@link CorpusStats} */ public CorpusStats getCorpusStats(final Corpus corpus) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); return executeRequest(API_VERSION + corpusId + STATS_PATH, null, CorpusStats.class); } @@ -618,7 +618,7 @@ public Scores getDocumentRelationScores(final Document document, final List concepts, final Map parameters) { - final String graphId = IDHelper.getGraphId(graph, getAccountId()); + final String graphId = IDHelper.getGraphId(graph, getFirstAccountId()); Validate.notEmpty(concepts, "concepts cannot be empty"); final Map queryParameters = new HashMap(); @@ -721,7 +721,7 @@ public Corpora listCorpora(final String accountId) { * @return {@link Documents} */ public Documents listDocuments(final Corpus corpus, final Map parameters) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); final Map queryParameters = new HashMap(); final String[] queryParams = new String[] {CURSOR, LIMIT}; @@ -766,7 +766,7 @@ public Graphs listGraphs() { * @return {@link Matches} */ public Matches searchCorpusByLabel(final Corpus corpus, final Map parameters) { - final String corpusId = IDHelper.getCorpusId(corpus, getAccountId()); + final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId()); Validate.notEmpty((String) parameters.get(QUERY), "query cannot be empty"); final Map queryParameters = new HashMap(); @@ -809,7 +809,7 @@ public Matches searchCorpusByLabel(final Corpus corpus, final Map parameters) { - final String graphId = IDHelper.getGraphId(graph, getAccountId()); + final String graphId = IDHelper.getGraphId(graph, getFirstAccountId()); Validate.notEmpty((String) parameters.get(QUERY), "query cannot be empty"); final Map queryParameters = new HashMap(); @@ -833,7 +833,7 @@ public Matches searchGraphsConceptByLabel(final Graph graph, final Map Tone + * Analyzer + */ +public class ToneAnalyzer extends WatsonService { + + private static final String VERSION_DATE = "version"; + private static final String PATH_TONE = "/v3/tone"; + private static final String TEXT = "text"; + private static final String URL = "https://gateway.watsonplatform.net/tone-analyzer-beta/api"; + private String versionDate; + + /** The version date */ + public static final String VERSION_DATE_2016_02_11 = "2016-02-11"; + + + /** + * Instantiates a new tone analyzer. + * + * @param versionDate The version date (yyyy-MM-dd) of the REST API to use. Specifying this value + * will keep your API calls from failing when the service introduces breaking changes. + */ + public ToneAnalyzer(String versionDate) { + super("tone_analyzer"); + setEndPoint(URL); + this.versionDate = versionDate; + } + + + + /** + * Analyzes the "tone" of a piece of text. The message is analyzed from several tones (social + * tone, emotional tone, writing tone), and for each of them various traits are derived (such as + * conscientiousness, agreeableness, openness). + * + * @param text The text to analyze + * @return the {@link Tone} with the response + * + */ + public ToneAnalysis getTone(final String text) { + + if (text == null || text.isEmpty()) + throw new IllegalArgumentException("text cannot be null or empty"); + + final JsonObject contentJson = new JsonObject(); + contentJson.addProperty(TEXT, text); + + final Request request = RequestBuilder.post(PATH_TONE) + .withQuery(VERSION_DATE, versionDate) + .withBodyJson(contentJson) + .build(); + return executeRequest(request, ToneAnalysis.class); + } + +} diff --git a/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ElementTone.java b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ElementTone.java new file mode 100644 index 00000000000..e427276f406 --- /dev/null +++ b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ElementTone.java @@ -0,0 +1,59 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3.model; + +import java.util.List; + +import com.google.gson.annotations.SerializedName; +import com.ibm.watson.developer_cloud.service.model.GenericModel; + +/** + * This object represents the results of Tone analysis on an element; which may be a document or a sentence. + * Its structure is a 2-level tree, with tone categories in the top level and the individual tones (and their + * scores) in leaves. + * + */ +public class ElementTone extends GenericModel { + + @SerializedName("tone_categories") + private List tones; + + /** + * Gets the tones. + * + * @return the tones + */ + public List getTones() { + return tones; + } + + /** + * Sets the tones. + * + * @param tones the new tones + */ + public void setTones(List tones) { + this.tones = tones; + } + + /** + * Adds the tone. + * + * @param tone the tone + */ + public void addTone(ToneCategory tone) { + this.tones.add(tone); + } + +} diff --git a/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/SentenceTone.java b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/SentenceTone.java new file mode 100644 index 00000000000..85df0800212 --- /dev/null +++ b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/SentenceTone.java @@ -0,0 +1,111 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3.model; + +import com.google.gson.annotations.SerializedName; + +/** + * This element contains the result of analyzing an individual sentence. It contains a list of + * ToneCategory objects which is the actual result, and also some metadata about the sentence: The + * original text (if it needs to be tracked back), and the position of the sentence in the original + * text (as index of first and last character). + * + */ +public class SentenceTone extends ElementTone { + + @SerializedName("sentence_id") + private Integer id; + + @SerializedName("input_from") + private Integer inputFrom; + + @SerializedName("input_to") + private Integer inputTo; + + @SerializedName("text") + private String text; + + /** + * Gets the id. + * + * @return the id + */ + public Integer getId() { + return id; + } + + /** + * Sets the id. + * + * @param id the new id + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * Gets the input from. + * + * @return the input from + */ + public Integer getInputFrom() { + return inputFrom; + } + + /** + * Sets the input from. + * + * @param inputFrom the new input from + */ + public void setInputFrom(Integer inputFrom) { + this.inputFrom = inputFrom; + } + + /** + * Gets the input to. + * + * @return the input to + */ + public Integer getInputTo() { + return inputTo; + } + + /** + * Sets the input to. + * + * @param inputTo the new input to + */ + public void setInputTo(Integer inputTo) { + this.inputTo = inputTo; + } + + /** + * Gets the text. + * + * @return the text + */ + public String getText() { + return text; + } + + /** + * Sets the text. + * + * @param text the new text + */ + public void setText(String text) { + this.text = text; + } + +} diff --git a/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneAnalysis.java b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneAnalysis.java new file mode 100644 index 00000000000..f632660aefd --- /dev/null +++ b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneAnalysis.java @@ -0,0 +1,81 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3.model; + +import java.util.List; + +import com.google.gson.annotations.SerializedName; +import com.ibm.watson.developer_cloud.service.model.GenericModel; + +/** + * + * Main object containing the result of running Tone Analyzer on a document. It contains both the + * sentence-level and document-level results. + * + */ +public class ToneAnalysis extends GenericModel { + + @SerializedName("document_tone") + private ElementTone documentTone; + + @SerializedName("sentences_tone") + private List sentencesTone; + + /** + * Gets the document tone. + * + * @return the document tone + */ + public ElementTone getDocumentTone() { + return documentTone; + } + + /** + * Sets the document tone. + * + * @param documentTone the new document tone + */ + public void setDocumentTone(ElementTone documentTone) { + this.documentTone = documentTone; + } + + /** + * Gets the sentences tone. + * + * @return the sentences tone + */ + public List getSentencesTone() { + return sentencesTone; + } + + /** + * Sets the sentences tone. + * + * @param sentencesTone the new sentences tone + */ + public void setSentencesTone(List sentencesTone) { + this.sentencesTone = sentencesTone; + } + + /** + * Adds the sentences tone. + * + * @param analysis the analysis + */ + public void addSentencesTone(SentenceTone analysis) { + this.sentencesTone.add(analysis); + } + + +} diff --git a/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneCategory.java b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneCategory.java new file mode 100644 index 00000000000..695ced3af5a --- /dev/null +++ b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneCategory.java @@ -0,0 +1,96 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3.model; + +import java.util.List; + +import com.google.gson.annotations.SerializedName; +import com.ibm.watson.developer_cloud.service.model.GenericModel; + +/** + * Top level tone (or Tone Category) from the list of Writing Tone, Emotion Tone or Social Tone. It + * holds a list of scores for individual Tones. + * + */ +public class ToneCategory extends GenericModel { + + @SerializedName("category_id") + private String id; + @SerializedName("category_name") + private String name; + private List tones; + + /** + * Gets the id. + * + * @return the id + */ + public String getId() { + return id; + } + + /** + * Sets the id. + * + * @param id the new id + */ + public void setId(String id) { + this.id = id; + } + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Sets the name. + * + * @param name the new name + */ + public void setName(String name) { + this.name = name; + } + + /** + * Gets the tones. + * + * @return the tones + */ + public List getTones() { + return tones; + } + + /** + * Sets the tones. + * + * @param tones the new tones + */ + public void setTones(List tones) { + this.tones = tones; + } + + /** + * Adds the tone. + * + * @param score the score + */ + public void addTone(ToneScore score) { + this.tones.add(score); + } +} diff --git a/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneScore.java b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneScore.java new file mode 100644 index 00000000000..0d7570226a5 --- /dev/null +++ b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ToneScore.java @@ -0,0 +1,95 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3.model; + +import com.google.gson.annotations.SerializedName; +import com.ibm.watson.developer_cloud.service.model.GenericModel; + +/** + * Object representing scoring of a single Tone (of any category) on our responses. It contains the + * Tone ID, a score, and optionally a list of evidences. + */ +public class ToneScore extends GenericModel { + + @SerializedName("tone_id") + private String id; + + @SerializedName("tone_name") + private String name; + + /** The score. */ + private Double score; + + /** + * Instantiates a new tone score. + */ + public ToneScore() { + + } + + /** + * Gets the id. + * + * @return the id + */ + public String getId() { + return id; + } + + /** + * Sets the id. + * + * @param id the new id + */ + public void setId(String id) { + this.id = id; + } + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Sets the name. + * + * @param name the new name + */ + public void setName(String name) { + this.name = name; + } + + /** + * Gets the score. + * + * @return the score + */ + public Double getScore() { + return score; + } + + /** + * Sets the score. + * + * @param score the new score + */ + public void setScore(Double score) { + this.score = score; + } + +} diff --git a/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/package-info.java b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/package-info.java new file mode 100644 index 00000000000..8f90dc1dfad --- /dev/null +++ b/src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/package-info.java @@ -0,0 +1,15 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3; + diff --git a/src/main/java/com/ibm/watson/developer_cloud/util/ResponseUtil.java b/src/main/java/com/ibm/watson/developer_cloud/util/ResponseUtil.java index 2f509e5b477..2973a48308f 100644 --- a/src/main/java/com/ibm/watson/developer_cloud/util/ResponseUtil.java +++ b/src/main/java/com/ibm/watson/developer_cloud/util/ResponseUtil.java @@ -109,7 +109,7 @@ public static T getObject(Response response, Class t try { response.body().close(); } catch (IOException e) { - log.log(Level.SEVERE,"Error closing the HTTP Response", e); + log.log(Level.SEVERE, "Error closing the HTTP Response", e); } } } @@ -120,7 +120,7 @@ public static T getObject(Response response, Class t * * @param response an HTTP response * @return the content body as String - * */ + */ public static String getString(Response response) { try { return response.body().string(); diff --git a/src/test/java/com/ibm/watson/developer_cloud/WatsonServiceTest.java b/src/test/java/com/ibm/watson/developer_cloud/WatsonServiceTest.java index 7d20687dee4..df4bac5819a 100755 --- a/src/test/java/com/ibm/watson/developer_cloud/WatsonServiceTest.java +++ b/src/test/java/com/ibm/watson/developer_cloud/WatsonServiceTest.java @@ -52,11 +52,12 @@ public WatsonServiceTest() { setupLogging(); } - protected Map getDefaultHeaders() { - Map headers = new HashMap(); + protected Map getDefaultHeaders() { + Map headers = new HashMap(); headers.put(HttpHeaders.X_WATSON_LEARNING_OPT_OUT, String.valueOf(true)); return headers; } + /** * The Class EmptyPropertyException. */ diff --git a/src/test/java/com/ibm/watson/developer_cloud/service/GenericServiceTest.java b/src/test/java/com/ibm/watson/developer_cloud/service/GenericServiceTest.java index b8963ec65b1..fd06a32c154 100644 --- a/src/test/java/com/ibm/watson/developer_cloud/service/GenericServiceTest.java +++ b/src/test/java/com/ibm/watson/developer_cloud/service/GenericServiceTest.java @@ -179,7 +179,7 @@ public void testUserAgentIsSet() { mockAPICall(); service.getProfile(sampleText); mockServer.verify(new HttpRequest().withMethod("POST").withHeader( - new Header(HttpHeaders.USER_AGENT, "watson-developer-cloud-java-sdk-2.7.1"))); + new Header(HttpHeaders.USER_AGENT, "watson-developer-cloud-java-sdk-2.8.0"))); } @Test diff --git a/src/test/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerIT.java b/src/test/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerIT.java new file mode 100644 index 00000000000..75148e67c43 --- /dev/null +++ b/src/test/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerIT.java @@ -0,0 +1,67 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.ibm.watson.developer_cloud.WatsonServiceTest; +import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis; + +/** + * Tone Analyzer Integration tests + */ +public class ToneAnalyzerIT extends WatsonServiceTest { + + /** The service. */ + private ToneAnalyzer service; + + /* + * (non-Javadoc) + * + * @see com.ibm.watson.developer_cloud.WatsonServiceTest#setUp() + */ + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + service = new ToneAnalyzer(ToneAnalyzer.VERSION_DATE_2016_02_11); + service.setUsernameAndPassword(getValidProperty("tone_analyzer.username"), + getValidProperty("tone_analyzer.password")); + service.setEndPoint(getValidProperty("tone_analyzer.url")); + service.setDefaultHeaders(getDefaultHeaders()); + + } + + @Test + public void testGetTone() { + final String text = + "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! "; + + + // Call the service and get the tone + final ToneAnalysis tone = service.getTone(text); + Assert.assertNotNull(tone); + Assert.assertNotNull(tone.getDocumentTone()); + Assert.assertEquals(3, tone.getDocumentTone().getTones().size()); + Assert.assertNotNull(tone.getSentencesTone()); + Assert.assertEquals(4, tone.getSentencesTone().size()); + Assert.assertEquals("I know the times are difficult!", tone.getSentencesTone().get(0).getText()); + } + +} diff --git a/src/test/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerTest.java b/src/test/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerTest.java new file mode 100644 index 00000000000..2bfaa086e2b --- /dev/null +++ b/src/test/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzerTest.java @@ -0,0 +1,99 @@ +/** + * Copyright 2015 IBM Corp. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.ibm.watson.developer_cloud.tone_analyzer.v3; + +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +import java.io.FileNotFoundException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.google.gson.JsonObject; +import com.ibm.watson.developer_cloud.WatsonServiceUnitTest; +import com.ibm.watson.developer_cloud.http.HttpHeaders; +import com.ibm.watson.developer_cloud.http.HttpMediaType; +import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis; + +/** + * Tone Analyzer unit test + */ +public class ToneAnalyzerTest extends WatsonServiceUnitTest { + + private static final String VERSION_DATE = "version"; + private static final String TEXT = "text"; + private static final String FIXTURE = "src/test/resources/tone_analyzer/tone.json"; + private final static String TONE_PATH = "/v3/tone"; + + /** The service. */ + private ToneAnalyzer service; + + /* + * (non-Javadoc) + * + * @see com.ibm.watson.developer_cloud.WatsonServiceTest#setUp() + */ + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + service = new ToneAnalyzer(ToneAnalyzer.VERSION_DATE_2016_02_11); + service.setApiKey(""); + service.setEndPoint(MOCK_SERVER_URL); + + } + + /** + * Test get tone. + * + * @throws FileNotFoundException + */ + @Test + public void testGetTone() throws FileNotFoundException { + final String text = "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! "; + + ToneAnalysis response = + loadFixture(FIXTURE, ToneAnalysis.class); + + final JsonObject contentJson = new JsonObject(); + contentJson.addProperty(TEXT, text); + + mockServer + .when(request() + .withMethod(POST) + .withPath(TONE_PATH) + .withQueryStringParameter(VERSION_DATE, ToneAnalyzer.VERSION_DATE_2016_02_11) + .withBody(contentJson.toString())) + .respond(response() + .withHeader(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_JSON) + .withBody(response.toString())); + + // Call the service and compare the result + Assert.assertEquals(response, service.getTone(text)); + } + + /** + * Test tone with null. + */ + @Test(expected = IllegalArgumentException.class) + public void testGetToneWithNull() { + service.getTone(null); + } + +} diff --git a/src/test/resources/tone_analyzer/message.txt b/src/test/resources/tone_analyzer/message.txt new file mode 100644 index 00000000000..7a8fa798f85 --- /dev/null +++ b/src/test/resources/tone_analyzer/message.txt @@ -0,0 +1 @@ +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! \ No newline at end of file diff --git a/src/test/resources/tone_analyzer/tone.json b/src/test/resources/tone_analyzer/tone.json new file mode 100644 index 00000000000..73cbaa0e297 --- /dev/null +++ b/src/test/resources/tone_analyzer/tone.json @@ -0,0 +1,455 @@ +{ + "document_tone": { + "tone_categories": [ + { + "tones": [ + { + "score": 0.105802, + "tone_id": "anger", + "tone_name": "Anger" + }, + { + "score": 0.280862, + "tone_id": "disgust", + "tone_name": "Disgust" + }, + { + "score": 0.299966, + "tone_id": "fear", + "tone_name": "Fear" + }, + { + "score": 0.222444, + "tone_id": "joy", + "tone_name": "Joy" + }, + { + "score": 0.090926, + "tone_id": "sadness", + "tone_name": "Sadness" + } + ], + "category_id": "emotion_tone", + "category_name": "Emotion Tone" + }, + { + "tones": [ + { + "score": 0.951, + "tone_id": "analytical", + "tone_name": "Analytical" + }, + { + "score": 0.999, + "tone_id": "confident", + "tone_name": "Confident" + }, + { + "score": 0.999, + "tone_id": "tentative", + "tone_name": "Tentative" + } + ], + "category_id": "writing_tone", + "category_name": "Writing Tone" + }, + { + "tones": [ + { + "score": 0.037, + "tone_id": "openness_big5", + "tone_name": "Openness" + }, + { + "score": 0.172, + "tone_id": "conscientiousness_big5", + "tone_name": "Conscientiousness" + }, + { + "score": 0.263, + "tone_id": "extraversion_big5", + "tone_name": "Extraversion" + }, + { + "score": 0.831, + "tone_id": "agreeableness_big5", + "tone_name": "Agreeableness" + }, + { + "score": 0.967, + "tone_id": "neuroticism_big5", + "tone_name": "Emotional Range" + } + ], + "category_id": "social_tone", + "category_name": "Social Tone" + } + ] + }, + "sentences_tone": [ + { + "sentence_id": 0, + "text": "I know the times are difficult!", + "input_from": 0, + "input_to": 31, + "tone_categories": [ + { + "tones": [ + { + "score": 0.276294, + "tone_id": "anger", + "tone_name": "Anger" + }, + { + "score": 0.280268, + "tone_id": "disgust", + "tone_name": "Disgust" + }, + { + "score": 0.236519, + "tone_id": "fear", + "tone_name": "Fear" + }, + { + "score": 0.15242, + "tone_id": "joy", + "tone_name": "Joy" + }, + { + "score": 0.054499, + "tone_id": "sadness", + "tone_name": "Sadness" + } + ], + "category_id": "emotion_tone", + "category_name": "Emotion Tone" + }, + { + "tones": [ + { + "score": 0.892, + "tone_id": "analytical", + "tone_name": "Analytical" + }, + { + "score": 0.999, + "tone_id": "confident", + "tone_name": "Confident" + }, + { + "score": 0.999, + "tone_id": "tentative", + "tone_name": "Tentative" + } + ], + "category_id": "writing_tone", + "category_name": "Writing Tone" + }, + { + "tones": [ + { + "score": 0.07, + "tone_id": "openness_big5", + "tone_name": "Openness" + }, + { + "score": 0.291, + "tone_id": "conscientiousness_big5", + "tone_name": "Conscientiousness" + }, + { + "score": 0.37, + "tone_id": "extraversion_big5", + "tone_name": "Extraversion" + }, + { + "score": 0.165, + "tone_id": "agreeableness_big5", + "tone_name": "Agreeableness" + }, + { + "score": 0.959, + "tone_id": "neuroticism_big5", + "tone_name": "Emotional Range" + } + ], + "category_id": "social_tone", + "category_name": "Social Tone" + } + ] + }, + { + "sentence_id": 1, + "text": "Our sales have been disappointing for the past three quarters for our data analytics product suite.", + "input_from": 32, + "input_to": 131, + "tone_categories": [ + { + "tones": [ + { + "score": 0.144036, + "tone_id": "anger", + "tone_name": "Anger" + }, + { + "score": 0.235218, + "tone_id": "disgust", + "tone_name": "Disgust" + }, + { + "score": 0.18726, + "tone_id": "fear", + "tone_name": "Fear" + }, + { + "score": 0.141991, + "tone_id": "joy", + "tone_name": "Joy" + }, + { + "score": 0.291495, + "tone_id": "sadness", + "tone_name": "Sadness" + } + ], + "category_id": "emotion_tone", + "category_name": "Emotion Tone" + }, + { + "tones": [ + { + "score": 0.379, + "tone_id": "analytical", + "tone_name": "Analytical" + }, + { + "score": 0.999, + "tone_id": "confident", + "tone_name": "Confident" + }, + { + "score": 0.999, + "tone_id": "tentative", + "tone_name": "Tentative" + } + ], + "category_id": "writing_tone", + "category_name": "Writing Tone" + }, + { + "tones": [ + { + "score": 0.174, + "tone_id": "openness_big5", + "tone_name": "Openness" + }, + { + "score": 0.367, + "tone_id": "conscientiousness_big5", + "tone_name": "Conscientiousness" + }, + { + "score": 0.41, + "tone_id": "extraversion_big5", + "tone_name": "Extraversion" + }, + { + "score": 0.753, + "tone_id": "agreeableness_big5", + "tone_name": "Agreeableness" + }, + { + "score": 0.803, + "tone_id": "neuroticism_big5", + "tone_name": "Emotional Range" + } + ], + "category_id": "social_tone", + "category_name": "Social Tone" + } + ] + }, + { + "sentence_id": 2, + "text": "We have a competitive data analytics product suite in the industry.", + "input_from": 132, + "input_to": 199, + "tone_categories": [ + { + "tones": [ + { + "score": 0.209645, + "tone_id": "anger", + "tone_name": "Anger" + }, + { + "score": 0.193127, + "tone_id": "disgust", + "tone_name": "Disgust" + }, + { + "score": 0.22004, + "tone_id": "fear", + "tone_name": "Fear" + }, + { + "score": 0.206854, + "tone_id": "joy", + "tone_name": "Joy" + }, + { + "score": 0.170334, + "tone_id": "sadness", + "tone_name": "Sadness" + } + ], + "category_id": "emotion_tone", + "category_name": "Emotion Tone" + }, + { + "tones": [ + { + "score": 0.608, + "tone_id": "analytical", + "tone_name": "Analytical" + }, + { + "score": 0.999, + "tone_id": "confident", + "tone_name": "Confident" + }, + { + "score": 0.999, + "tone_id": "tentative", + "tone_name": "Tentative" + } + ], + "category_id": "writing_tone", + "category_name": "Writing Tone" + }, + { + "tones": [ + { + "score": 0.627, + "tone_id": "openness_big5", + "tone_name": "Openness" + }, + { + "score": 0.928, + "tone_id": "conscientiousness_big5", + "tone_name": "Conscientiousness" + }, + { + "score": 0.035, + "tone_id": "extraversion_big5", + "tone_name": "Extraversion" + }, + { + "score": 0.526, + "tone_id": "agreeableness_big5", + "tone_name": "Agreeableness" + }, + { + "score": 0.548, + "tone_id": "neuroticism_big5", + "tone_name": "Emotional Range" + } + ], + "category_id": "social_tone", + "category_name": "Social Tone" + } + ] + }, + { + "sentence_id": 3, + "text": "But we need to do our job selling it!", + "input_from": 200, + "input_to": 237, + "tone_categories": [ + { + "tones": [ + { + "score": 0.056075, + "tone_id": "anger", + "tone_name": "Anger" + }, + { + "score": 0.226992, + "tone_id": "disgust", + "tone_name": "Disgust" + }, + { + "score": 0.252165, + "tone_id": "fear", + "tone_name": "Fear" + }, + { + "score": 0.315444, + "tone_id": "joy", + "tone_name": "Joy" + }, + { + "score": 0.149324, + "tone_id": "sadness", + "tone_name": "Sadness" + } + ], + "category_id": "emotion_tone", + "category_name": "Emotion Tone" + }, + { + "tones": [ + { + "score": 0.722, + "tone_id": "analytical", + "tone_name": "Analytical" + }, + { + "score": 0.999, + "tone_id": "confident", + "tone_name": "Confident" + }, + { + "score": 0.999, + "tone_id": "tentative", + "tone_name": "Tentative" + } + ], + "category_id": "writing_tone", + "category_name": "Writing Tone" + }, + { + "tones": [ + { + "score": 0.02, + "tone_id": "openness_big5", + "tone_name": "Openness" + }, + { + "score": 0.024, + "tone_id": "conscientiousness_big5", + "tone_name": "Conscientiousness" + }, + { + "score": 0.828, + "tone_id": "extraversion_big5", + "tone_name": "Extraversion" + }, + { + "score": 0.946, + "tone_id": "agreeableness_big5", + "tone_name": "Agreeableness" + }, + { + "score": 0.969, + "tone_id": "neuroticism_big5", + "tone_name": "Emotional Range" + } + ], + "category_id": "social_tone", + "category_name": "Social Tone" + } + ] + } + ] +} \ No newline at end of file