Skip to content

Commit d8b8699

Browse files
[alchemy] added emotion analysis #197
1 parent 69b3bb3 commit d8b8699

File tree

5 files changed

+232
-4
lines changed

5 files changed

+232
-4
lines changed

src/main/java/com/ibm/watson/developer_cloud/alchemy/v1/AlchemyLanguage.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.ibm.watson.developer_cloud.alchemy.v1.model.Concepts;
2424
import com.ibm.watson.developer_cloud.alchemy.v1.model.Dates;
2525
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentAuthors;
26+
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentEmotion;
2627
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentPublicationDate;
2728
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentSentiment;
2829
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentText;
@@ -275,12 +276,23 @@ public DocumentTitle getTitle(Map<String, Object> params) {
275276
return executeRequest(params, AlchemyAPI.title, DocumentTitle.class, HTML, URL);
276277
}
277278

279+
/**
280+
* Detects emotions in a text, URL or HTML.
281+
*
282+
* @param params The parameters to be used in the service call, text, html or url should be
283+
* specified
284+
* @return {@link DocumentEmotion}
285+
*/
286+
public DocumentEmotion getEmotion(Map<String, Object> params) {
287+
return executeRequest(params, AlchemyAPI.emotion, DocumentEmotion.class, TEXT, HTML, URL);
288+
}
289+
278290
/**
279291
* Extracts dates for text, a URL or HTML.
280292
*
281293
* @param params The parameters to be used in the service call, text, html or url should be
282294
* specified.
283-
* @return {@link DocumentTitle}
295+
* @return {@link Dates}
284296
*/
285297
public Dates getDates(Map<String, Object> params) {
286298

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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.alchemy.v1.model;
15+
16+
import com.google.gson.annotations.SerializedName;
17+
import com.ibm.watson.developer_cloud.alchemy.v1.AlchemyLanguage;
18+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
19+
20+
/**
21+
* The document emotion from {@link AlchemyLanguage#getEmotion(java.util.Map)}
22+
*
23+
*/
24+
public class DocumentEmotion extends AlchemyLanguageGenericModel {
25+
26+
/**
27+
* The extracted emotion from {@link AlchemyLanguage#getEmotion(java.util.Map)}
28+
*/
29+
public static class Emotion extends GenericModel {
30+
private Double anger;
31+
private Double disgust;
32+
private Double fear;
33+
private Double joy;
34+
private Double sadness;
35+
36+
/**
37+
* Gets the anger.
38+
*
39+
* @return the anger
40+
*/
41+
public Double getAnger() {
42+
return anger;
43+
}
44+
45+
/**
46+
* Gets the disgust.
47+
*
48+
* @return the disgust
49+
*/
50+
public Double getDisgust() {
51+
return disgust;
52+
}
53+
54+
/**
55+
* Gets the fear.
56+
*
57+
* @return the fear
58+
*/
59+
public Double getFear() {
60+
return fear;
61+
}
62+
63+
/**
64+
* Gets the joy.
65+
*
66+
* @return the joy
67+
*/
68+
public Double getJoy() {
69+
return joy;
70+
}
71+
72+
/**
73+
* Gets the sadness.
74+
*
75+
* @return the sadness
76+
*/
77+
public Double getSadness() {
78+
return sadness;
79+
}
80+
81+
/**
82+
* Sets the anger.
83+
*
84+
* @param anger the new anger
85+
*/
86+
public void setAnger(Double anger) {
87+
this.anger = anger;
88+
}
89+
90+
/**
91+
* Sets the disgust.
92+
*
93+
* @param disgust the new disgust
94+
*/
95+
public void setDisgust(Double disgust) {
96+
this.disgust = disgust;
97+
}
98+
99+
/**
100+
* Sets the fear.
101+
*
102+
* @param fear the new fear
103+
*/
104+
public void setFear(Double fear) {
105+
this.fear = fear;
106+
}
107+
108+
/**
109+
* Sets the joy.
110+
*
111+
* @param joy the new joy
112+
*/
113+
public void setJoy(Double joy) {
114+
this.joy = joy;
115+
}
116+
117+
/**
118+
* Sets the sadness.
119+
*
120+
* @param sadness the new sadness
121+
*/
122+
public void setSadness(Double sadness) {
123+
this.sadness = sadness;
124+
}
125+
126+
}
127+
128+
@SerializedName("docEmotions")
129+
private Emotion emotion;
130+
131+
private String text;
132+
133+
/**
134+
* Gets the emotion.
135+
*
136+
* @return the emotion
137+
*/
138+
public Emotion getEmotion() {
139+
return emotion;
140+
}
141+
142+
/**
143+
* Gets the text.
144+
*
145+
* @return the text
146+
*/
147+
public String getText() {
148+
return text;
149+
}
150+
151+
/**
152+
* Sets the emotion.
153+
*
154+
* @param emotion the new emotion
155+
*/
156+
public void setEmotion(Emotion emotion) {
157+
this.emotion = emotion;
158+
}
159+
160+
/**
161+
* Sets the text.
162+
*
163+
* @param text the new text
164+
*/
165+
public void setText(String text) {
166+
this.text = text;
167+
}
168+
}
169+

src/main/java/com/ibm/watson/developer_cloud/alchemy/v1/util/AlchemyEndPoints.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ public enum AlchemyAPI {
9898

9999
/** The publication date */
100100
publication_date,
101-
101+
102102
/** dates */
103-
dates
103+
dates,
104+
105+
/** emotion */
106+
emotion
104107
}
105108

106109
/** The file where alchemy endpoints are described. */

src/main/resources/alchemy_endpoints.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"text": "/text/TextGetTextSentiment",
2525
"html": "/html/HTMLGetTextSentiment"
2626
},
27+
"emotion": {
28+
"url": "/url/URLGetEmotion",
29+
"text": "/text/TextGetEmotion",
30+
"html": "/html/HTMLGetEmotion"
31+
},
2732
"sentiment_targeted": {
2833
"url": "/url/URLGetTargetedSentiment",
2934
"text": "/text/TextGetTargetedSentiment",

src/test/java/com/ibm/watson/developer_cloud/alchemy/v1/AlchemyLanguageIT.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.ibm.watson.developer_cloud.alchemy.v1.model.Concepts;
2828
import com.ibm.watson.developer_cloud.alchemy.v1.model.Dates;
2929
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentAuthors;
30+
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentEmotion;
3031
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentPublicationDate;
3132
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentSentiment;
3233
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentText;
@@ -128,7 +129,7 @@ public void testGetConceptsHTML() {
128129
}
129130

130131
/**
131-
* Test get concepts HTML.
132+
* Test get concepts Tet.
132133
*/
133134
@Test
134135
public void testGetConceptsText() {
@@ -427,4 +428,42 @@ public void testGetDates() {
427428
Assert.assertFalse(dates.getDates().isEmpty());
428429
}
429430

431+
/**
432+
* Test get emotion from HTML.
433+
*/
434+
@Test
435+
public void testGetEmotionHTML() {
436+
final Map<String, Object> params = new HashMap<String, Object>();
437+
params.put(AlchemyLanguage.HTML, htmlExample);
438+
final DocumentEmotion emotion = service.getEmotion(params);
439+
Assert.assertNotNull(emotion);
440+
Assert.assertNotNull(emotion.getEmotion());
441+
System.out.println(emotion);
442+
}
443+
444+
/**
445+
* Test get emotion from text.
446+
*/
447+
@Test
448+
public void testGetEmotionText() {
449+
final Map<String, Object> params = new HashMap<String, Object>();
450+
params.put(AlchemyLanguage.TEXT, htmlExample);
451+
final DocumentEmotion emotion = service.getEmotion(params);
452+
Assert.assertNotNull(emotion);
453+
Assert.assertNotNull(emotion.getEmotion());
454+
System.out.println(emotion);
455+
}
456+
457+
/**
458+
* Test Get emotion from URL.
459+
*/
460+
@Test
461+
public void testGetEmotionUrl() {
462+
final Map<String, Object> params = new HashMap<String, Object>();
463+
params.put(AlchemyLanguage.URL, "http://www.techcrunch.com/");
464+
final DocumentEmotion emotion = service.getEmotion(params);
465+
Assert.assertNotNull(emotion);
466+
Assert.assertNotNull(emotion.getEmotion());
467+
System.out.println(emotion);
468+
}
430469
}

0 commit comments

Comments
 (0)