Skip to content

Commit 2a96d6a

Browse files
Merge pull request #193 from herchu/dev
Dev
2 parents 6429c3f + de7e64e commit 2a96d6a

File tree

9 files changed

+574
-0
lines changed

9 files changed

+574
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.ibm.watson.developer_cloud.tone_analyzer.v3;
15+
16+
import java.lang.reflect.Type;
17+
import java.util.List;
18+
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.reflect.TypeToken;
21+
import com.ibm.watson.developer_cloud.http.HttpMediaType;
22+
import com.ibm.watson.developer_cloud.http.RequestBuilder;
23+
import com.ibm.watson.developer_cloud.service.WatsonService;
24+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Scorecard;
25+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.SynonymOptions;
26+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.SynonymResult;
27+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Tone;
28+
import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis;
29+
import com.ibm.watson.developer_cloud.util.GsonSingleton;
30+
import com.ibm.watson.developer_cloud.util.ResponseUtil;
31+
import com.squareup.okhttp.Request;
32+
import com.squareup.okhttp.Response;
33+
34+
/**
35+
* The IBM Watson The Tone Analyzer service uses linguistic analysis to detect emotional tones,
36+
* social propensities, and writing styles in written communication. Then it offers suggestions to
37+
* help the writer improve their intended language tones.
38+
*
39+
* @version v1
40+
* @see <a
41+
* href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/tone-analyzer.html">
42+
* Tone Analyzer</a>
43+
*/
44+
public class ToneAnalyzer extends WatsonService {
45+
46+
private static final String PATH_TONE = "/v3/tone";
47+
private static final String TEXT = "text";
48+
private static final String URL =
49+
"https://gateway.watsonplatform.net/tone-analyzer-beta/api";
50+
51+
/**
52+
* Instantiates a new Tone Analyzer service.
53+
*/
54+
public ToneAnalyzer() {
55+
super("tone_analyzer");
56+
setEndPoint(URL);
57+
}
58+
59+
60+
/**
61+
* Analyzes the "tone" of a piece of text. The message is analyzed from several tones (social
62+
* tone, emotional tone, writing tone), and for each of them various traits are derived (such as
63+
* conscientiousness, agreeableness, openness).
64+
*
65+
* @param text The text to analyze
66+
* @return the {@link Tone} with the response
67+
*
68+
*/
69+
public ToneAnalysis getTone(final String text) {
70+
71+
if (text == null || text.isEmpty())
72+
throw new IllegalArgumentException("text cannot be null or empty");
73+
74+
final JsonObject contentJson = new JsonObject();
75+
contentJson.addProperty(TEXT, text);
76+
77+
final Request request = RequestBuilder.post(PATH_TONE).withBodyJson(contentJson).build();
78+
return executeRequest(request, ToneAnalysis.class);
79+
}
80+
81+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
7+
8+
/**
9+
* This object represents the results of Tone analysis on an element; which may be a document or a sentence.
10+
* Its structure is a 2-level tree, with tone categories in the top level and the individual tones (and their
11+
* scores) in leaves.
12+
*
13+
* @author Hernan Badenes
14+
*
15+
*/
16+
public class ElementTone extends GenericModel {
17+
18+
List<ToneCategory> tones = new ArrayList<ToneCategory>();
19+
20+
public List<ToneCategory> getTones() {
21+
return tones;
22+
}
23+
24+
public void setTones(List<ToneCategory> tones) {
25+
this.tones = tones;
26+
}
27+
28+
public void addTone(ToneCategory tone) {
29+
this.tones.add(tone);
30+
}
31+
32+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
7+
8+
/**
9+
* This element contains the result of analyzing an individual sentence. It contains a list of ToneCategory
10+
* objects which is the actual result, and also some metadata about the sentence: The original text (if it needs
11+
* to be tracked back), and the position of the sentence in the original text (as index of first and last
12+
* character).
13+
*
14+
* @author Hernan Badenes
15+
*
16+
*/
17+
public class SentenceAnalysis extends GenericModel {
18+
19+
long sentence_id;
20+
21+
int charFrom;
22+
23+
int charTo;
24+
25+
String text;
26+
27+
List<ToneCategory> tones = new ArrayList<ToneCategory>();
28+
29+
public SentenceAnalysis() {
30+
}
31+
32+
public SentenceAnalysis(long sentence_id, int charFrom, int charTo, String text) {
33+
super();
34+
this.sentence_id = sentence_id;
35+
this.charFrom = charFrom;
36+
this.charTo = charTo;
37+
this.text = text;
38+
}
39+
40+
public long getSentence_id() {
41+
return sentence_id;
42+
}
43+
44+
public int getCharFrom() {
45+
return charFrom;
46+
}
47+
48+
public int getCharTo() {
49+
return charTo;
50+
}
51+
52+
public String getText() {
53+
return text;
54+
}
55+
56+
public void setTones(List<ToneCategory> tones) {
57+
this.tones = tones;
58+
}
59+
60+
public void addTone(ToneCategory tone) {
61+
this.tones.add(tone);
62+
}
63+
64+
65+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;
2+
3+
import java.util.List;
4+
5+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
6+
7+
/**
8+
*
9+
* Main object containing the result of running Tone Analyzer on a document. It contains both
10+
* the sentence-level and document-level results.
11+
*
12+
* @author Hernan Badenes
13+
*
14+
*/
15+
public class ToneAnalysis extends GenericModel {
16+
17+
ElementTone documentTone;
18+
19+
List<SentenceAnalysis> sentencesTone;
20+
21+
public ElementTone getDocumentTone() {
22+
return documentTone;
23+
}
24+
25+
public void setDocumentTone(ElementTone documentTone) {
26+
this.documentTone = documentTone;
27+
}
28+
29+
public List<SentenceAnalysis> getSentencesTone() {
30+
return sentencesTone;
31+
}
32+
33+
public void setSentencesTone(List<SentenceAnalysis> sentencesTone) {
34+
this.sentencesTone = sentencesTone;
35+
}
36+
37+
public void addSentencesTone(SentenceAnalysis analysis) {
38+
this.sentencesTone.add(analysis);
39+
}
40+
41+
42+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
7+
8+
/**
9+
* This object represents a top levle tone (or Tone Category) from the list of Writing Tone, Emotion Tone or Social Tone.
10+
* It holds a list of scores for individual Tones.
11+
*
12+
* @author Hernan Badenes
13+
*
14+
*/
15+
public class ToneCategory extends GenericModel {
16+
17+
// The ID of this category. It can referred to from several places in the API input/output.
18+
String id;
19+
20+
// A human-readable, localized name for this category.
21+
String name;
22+
23+
// The list of tone scores in this category
24+
List<ToneScore> tones = new ArrayList<ToneScore>();
25+
26+
public ToneCategory() {
27+
}
28+
29+
public String getId() {
30+
return id;
31+
}
32+
33+
public void setId(String id) {
34+
this.id = id;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
45+
public List<ToneScore> getTones() {
46+
return tones;
47+
}
48+
49+
public void setTones(List<ToneScore> tones) {
50+
this.tones = tones;
51+
}
52+
53+
public void addTone(ToneScore score) {
54+
this.tones.add(score);
55+
}
56+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;
2+
3+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
4+
5+
/*
6+
* Object representing scoring of a single Tone (of any category) on our responses. It contains the Tone ID, a score, and optionally
7+
* a list of evidences.
8+
*
9+
* @author Hernan Badenes
10+
*/
11+
public class ToneScore extends GenericModel {
12+
13+
String tone_id;
14+
15+
String tone_name;
16+
17+
Double score;
18+
19+
public ToneScore() {
20+
21+
}
22+
23+
public ToneScore(String toneId, String toneName, Double score) {
24+
super();
25+
this.tone_id = toneId;
26+
this.tone_name = toneName;
27+
this.score = score;
28+
}
29+
30+
public String getToneId() {
31+
return tone_id;
32+
}
33+
34+
public String getToneName() {
35+
return tone_name;
36+
}
37+
38+
public Double getScore() {
39+
return score;
40+
}
41+
42+
public void setToneId(String toneId) {
43+
this.tone_id = toneId;
44+
}
45+
46+
public void setToneName(String toneName) {
47+
this.tone_name = toneName;
48+
}
49+
50+
public void setScore(Double score) {
51+
this.score = score;
52+
}
53+
54+
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.ibm.watson.developer_cloud.tone_analyzer.v3;
15+

0 commit comments

Comments
 (0)