Skip to content

Commit 38a8e02

Browse files
[tone-analyzer] Add support for v3
1 parent 2a96d6a commit 38a8e02

File tree

11 files changed

+888
-300
lines changed

11 files changed

+888
-300
lines changed

src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/ToneAnalyzer.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,49 @@
1313
*/
1414
package com.ibm.watson.developer_cloud.tone_analyzer.v3;
1515

16-
import java.lang.reflect.Type;
17-
import java.util.List;
18-
1916
import com.google.gson.JsonObject;
20-
import com.google.gson.reflect.TypeToken;
21-
import com.ibm.watson.developer_cloud.http.HttpMediaType;
2217
import com.ibm.watson.developer_cloud.http.RequestBuilder;
2318
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;
2719
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Tone;
2820
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;
3121
import com.squareup.okhttp.Request;
32-
import com.squareup.okhttp.Response;
3322

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

35+
private static final String VERSION_DATE = "version";
4636
private static final String PATH_TONE = "/v3/tone";
4737
private static final String TEXT = "text";
48-
private static final String URL =
49-
"https://gateway.watsonplatform.net/tone-analyzer-beta/api";
38+
private static final String URL = "https://gateway.watsonplatform.net/tone-analyzer-beta/api";
39+
private String versionDate;
40+
41+
/** The version date */
42+
public static final String VERSION_DATE_2016_02_11 = "2016-02-11";
43+
5044

5145
/**
52-
* Instantiates a new Tone Analyzer service.
46+
* Instantiates a new tone analyzer.
47+
*
48+
* @param versionDate The version date (yyyy-MM-dd) of the REST API to use. Specifying this value
49+
* will keep your API calls from failing when the service introduces breaking changes.
5350
*/
54-
public ToneAnalyzer() {
51+
public ToneAnalyzer(String versionDate) {
5552
super("tone_analyzer");
5653
setEndPoint(URL);
54+
this.versionDate = versionDate;
5755
}
5856

5957

58+
6059
/**
6160
* Analyzes the "tone" of a piece of text. The message is analyzed from several tones (social
6261
* tone, emotional tone, writing tone), and for each of them various traits are derived (such as
@@ -74,7 +73,10 @@ public ToneAnalysis getTone(final String text) {
7473
final JsonObject contentJson = new JsonObject();
7574
contentJson.addProperty(TEXT, text);
7675

77-
final Request request = RequestBuilder.post(PATH_TONE).withBodyJson(contentJson).build();
76+
final Request request = RequestBuilder.post(PATH_TONE)
77+
.withQuery(VERSION_DATE, versionDate)
78+
.withBodyJson(contentJson)
79+
.build();
7880
return executeRequest(request, ToneAnalysis.class);
7981
}
8082

src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/ElementTone.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,57 @@
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+
*/
114
package com.ibm.watson.developer_cloud.tone_analyzer.v3.model;
215

3-
import java.util.ArrayList;
416
import java.util.List;
517

18+
import com.google.gson.annotations.SerializedName;
619
import com.ibm.watson.developer_cloud.service.model.GenericModel;
720

821
/**
922
* This object represents the results of Tone analysis on an element; which may be a document or a sentence.
1023
* Its structure is a 2-level tree, with tone categories in the top level and the individual tones (and their
1124
* scores) in leaves.
1225
*
13-
* @author Hernan Badenes
14-
*
1526
*/
1627
public class ElementTone extends GenericModel {
1728

18-
List<ToneCategory> tones = new ArrayList<ToneCategory>();
29+
@SerializedName("tone_categories")
30+
private List<ToneCategory> tones;
1931

32+
/**
33+
* Gets the tones.
34+
*
35+
* @return the tones
36+
*/
2037
public List<ToneCategory> getTones() {
2138
return tones;
2239
}
2340

41+
/**
42+
* Sets the tones.
43+
*
44+
* @param tones the new tones
45+
*/
2446
public void setTones(List<ToneCategory> tones) {
2547
this.tones = tones;
2648
}
2749

50+
/**
51+
* Adds the tone.
52+
*
53+
* @param tone the tone
54+
*/
2855
public void addTone(ToneCategory tone) {
2956
this.tones.add(tone);
3057
}

src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v3/model/SentenceAnalysis.java

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.model;
15+
16+
import com.google.gson.annotations.SerializedName;
17+
18+
/**
19+
* This element contains the result of analyzing an individual sentence. It contains a list of
20+
* ToneCategory objects which is the actual result, and also some metadata about the sentence: The
21+
* original text (if it needs to be tracked back), and the position of the sentence in the original
22+
* text (as index of first and last character).
23+
*
24+
*/
25+
public class SentenceTone extends ElementTone {
26+
27+
@SerializedName("sentence_id")
28+
private Integer id;
29+
30+
@SerializedName("input_from")
31+
private Integer inputFrom;
32+
33+
@SerializedName("input_to")
34+
private Integer inputTo;
35+
36+
@SerializedName("text")
37+
private String text;
38+
39+
/**
40+
* Gets the id.
41+
*
42+
* @return the id
43+
*/
44+
public Integer getId() {
45+
return id;
46+
}
47+
48+
/**
49+
* Sets the id.
50+
*
51+
* @param id the new id
52+
*/
53+
public void setId(Integer id) {
54+
this.id = id;
55+
}
56+
57+
/**
58+
* Gets the input from.
59+
*
60+
* @return the input from
61+
*/
62+
public Integer getInputFrom() {
63+
return inputFrom;
64+
}
65+
66+
/**
67+
* Sets the input from.
68+
*
69+
* @param inputFrom the new input from
70+
*/
71+
public void setInputFrom(Integer inputFrom) {
72+
this.inputFrom = inputFrom;
73+
}
74+
75+
/**
76+
* Gets the input to.
77+
*
78+
* @return the input to
79+
*/
80+
public Integer getInputTo() {
81+
return inputTo;
82+
}
83+
84+
/**
85+
* Sets the input to.
86+
*
87+
* @param inputTo the new input to
88+
*/
89+
public void setInputTo(Integer inputTo) {
90+
this.inputTo = inputTo;
91+
}
92+
93+
/**
94+
* Gets the text.
95+
*
96+
* @return the text
97+
*/
98+
public String getText() {
99+
return text;
100+
}
101+
102+
/**
103+
* Sets the text.
104+
*
105+
* @param text the new text
106+
*/
107+
public void setText(String text) {
108+
this.text = text;
109+
}
110+
111+
}

0 commit comments

Comments
 (0)