Skip to content
Merged

Dev #193

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ibm.watson.developer_cloud.http.HttpMediaType;
import com.ibm.watson.developer_cloud.http.RequestBuilder;
import com.ibm.watson.developer_cloud.service.WatsonService;
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Scorecard;
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.SynonymOptions;
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.SynonymResult;
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Tone;
import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis;
import com.ibm.watson.developer_cloud.util.GsonSingleton;
import com.ibm.watson.developer_cloud.util.ResponseUtil;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

/**
* The IBM Watson The Tone Analyzer service uses linguistic analysis to detect emotional tones,
* social propensities, and writing styles in written communication. Then it offers suggestions to
* help the writer improve their intended language tones.
*
* @version v1
* @see <a
* href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/tone-analyzer.html">
* Tone Analyzer</a>
*/
public class ToneAnalyzer extends WatsonService {

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";

/**
* Instantiates a new Tone Analyzer service.
*/
public ToneAnalyzer() {
super("tone_analyzer");
setEndPoint(URL);
}


/**
* 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).withBodyJson(contentJson).build();
return executeRequest(request, ToneAnalysis.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;

import java.util.ArrayList;
import java.util.List;

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.
*
* @author Hernan Badenes
*
*/
public class ElementTone extends GenericModel {

List<ToneCategory> tones = new ArrayList<ToneCategory>();

public List<ToneCategory> getTones() {
return tones;
}

public void setTones(List<ToneCategory> tones) {
this.tones = tones;
}

public void addTone(ToneCategory tone) {
this.tones.add(tone);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;

import java.util.ArrayList;
import java.util.List;

import com.ibm.watson.developer_cloud.service.model.GenericModel;

/**
* 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).
*
* @author Hernan Badenes
*
*/
public class SentenceAnalysis extends GenericModel {

long sentence_id;

int charFrom;

int charTo;

String text;

List<ToneCategory> tones = new ArrayList<ToneCategory>();

public SentenceAnalysis() {
}

public SentenceAnalysis(long sentence_id, int charFrom, int charTo, String text) {
super();
this.sentence_id = sentence_id;
this.charFrom = charFrom;
this.charTo = charTo;
this.text = text;
}

public long getSentence_id() {
return sentence_id;
}

public int getCharFrom() {
return charFrom;
}

public int getCharTo() {
return charTo;
}

public String getText() {
return text;
}

public void setTones(List<ToneCategory> tones) {
this.tones = tones;
}

public void addTone(ToneCategory tone) {
this.tones.add(tone);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;

import java.util.List;

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.
*
* @author Hernan Badenes
*
*/
public class ToneAnalysis extends GenericModel {

ElementTone documentTone;

List<SentenceAnalysis> sentencesTone;

public ElementTone getDocumentTone() {
return documentTone;
}

public void setDocumentTone(ElementTone documentTone) {
this.documentTone = documentTone;
}

public List<SentenceAnalysis> getSentencesTone() {
return sentencesTone;
}

public void setSentencesTone(List<SentenceAnalysis> sentencesTone) {
this.sentencesTone = sentencesTone;
}

public void addSentencesTone(SentenceAnalysis analysis) {
this.sentencesTone.add(analysis);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;

import java.util.ArrayList;
import java.util.List;

import com.ibm.watson.developer_cloud.service.model.GenericModel;

/**
* This object represents a top levle tone (or Tone Category) from the list of Writing Tone, Emotion Tone or Social Tone.
* It holds a list of scores for individual Tones.
*
* @author Hernan Badenes
*
*/
public class ToneCategory extends GenericModel {

// The ID of this category. It can referred to from several places in the API input/output.
String id;

// A human-readable, localized name for this category.
String name;

// The list of tone scores in this category
List<ToneScore> tones = new ArrayList<ToneScore>();

public ToneCategory() {
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<ToneScore> getTones() {
return tones;
}

public void setTones(List<ToneScore> tones) {
this.tones = tones;
}

public void addTone(ToneScore score) {
this.tones.add(score);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;

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.
*
* @author Hernan Badenes
*/
public class ToneScore extends GenericModel {

String tone_id;

String tone_name;

Double score;

public ToneScore() {

}

public ToneScore(String toneId, String toneName, Double score) {
super();
this.tone_id = toneId;
this.tone_name = toneName;
this.score = score;
}

public String getToneId() {
return tone_id;
}

public String getToneName() {
return tone_name;
}

public Double getScore() {
return score;
}

public void setToneId(String toneId) {
this.tone_id = toneId;
}

public void setToneName(String toneName) {
this.tone_name = toneName;
}

public void setScore(Double score) {
this.score = score;
}


}
Original file line number Diff line number Diff line change
@@ -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;

Loading