Skip to content

Commit a4ca38f

Browse files
replace getProfile function with a generic one that includes include_raw, content-language and accept-language for Personality Insights
1 parent 4ac2503 commit a4ca38f

File tree

4 files changed

+149
-85
lines changed

4 files changed

+149
-85
lines changed

examples/java/com/ibm/watson/developer_cloud/personality_insights/v2/PersonalityInsightsExample.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.ibm.watson.developer_cloud.personality_insights.v2;
1717

18+
import java.util.HashMap;
19+
import java.util.Map;
20+
1821
import com.ibm.watson.developer_cloud.personality_insights.v2.model.Profile;
1922

2023
public class PersonalityInsightsExample {
@@ -37,7 +40,10 @@ public static void main(String[] args) {
3740
+ "the street, and methodically knocking people's hats off-then, "
3841
+ "I account it high time to get to sea as soon as I can.";
3942

40-
Profile profile = service.getProfile(myProfile);
43+
Map<String, Object> params = new HashMap<String, Object>();
44+
params.put("text",myProfile);
45+
46+
Profile profile = service.getProfile(params);
4147
System.out.println(profile);
4248
}
4349
}

src/main/java/com/ibm/watson/developer_cloud/personality_insights/v2/PersonalityInsights.java

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
package com.ibm.watson.developer_cloud.personality_insights.v2;
1717

1818
import java.io.IOException;
19+
import java.util.Map;
20+
import java.util.logging.Logger;
1921

2022
import org.apache.http.HttpResponse;
21-
import org.apache.http.client.methods.HttpRequestBase;
2223
import org.apache.http.protocol.HTTP;
2324

2425
import com.ibm.watson.developer_cloud.personality_insights.v2.model.Content;
@@ -40,7 +41,10 @@
4041
* Personality Insights</a>
4142
*/
4243
public class PersonalityInsights extends WatsonService {
43-
44+
45+
/** The Constant log. */
46+
private static final Logger log = Logger.getLogger(PersonalityInsights.class.getName());
47+
4448
/** The url. */
4549
private static String URL = "https://gateway.watsonplatform.net/personality-insights/api";
4650

@@ -52,57 +56,43 @@ public PersonalityInsights() {
5256
}
5357

5458
/**
55-
* Accepts a {@link Content} object and analyzes the text. The response is
56-
* {@link Profile} with a tree of characteristics that include personality,
57-
* needs, and values. If you include either created or updated timestamps,
58-
* the response also includes a summary of the author's writing habits with
59-
* respect to time of day.
59+
* Accepts text or a {@link Content} object and responds with a {@link Profile}
60+
* with a tree of characteristics that include personality, needs, and values.
61+
* If you include either created or updated timestamps, the response also includes
62+
* a summary of the author's writing habits with respect to time of day.
6063
*
61-
* @param content
62-
* the content
63-
* @return the profile
64-
* @see Content
64+
* @param params
65+
* The parameters to generate the profile. Either text or content need
66+
* to be specified
67+
* @return The personality profile
6568
*/
66-
public Profile getProfile(Content content) {
67-
if (content == null)
68-
throw new IllegalArgumentException("content can not be null");
69-
70-
if (content.getContentItems() == null || content.getContentItems().isEmpty())
71-
throw new IllegalArgumentException("content needs to have contentItems.");
72-
73-
String contentJson = getGson().toJson(content);
74-
HttpRequestBase request = Request.Post("/v2/profile")
75-
.withContent(contentJson, MediaType.APPLICATION_JSON).build();
76-
77-
try {
78-
HttpResponse response = execute(request);
79-
String profileJson = ResponseUtil.getString(response);
80-
Profile profile = getGson().fromJson(profileJson, Profile.class);
81-
return profile;
82-
} catch (IOException e) {
83-
throw new RuntimeException(e);
69+
public Profile getProfile(Map<String, Object> params) {
70+
if (!params.containsKey("text") && !params.containsKey("content"))
71+
throw new IllegalArgumentException("text or content need to be specified");
72+
else if (params.containsKey("text") && params.containsKey("content"))
73+
log.warning("text and content were specified, only text will be used");
74+
75+
76+
Request request = Request.Post("/v2/profile");
77+
78+
if (params.containsKey("text")) {
79+
request.withContent(params.get("text").toString(), HTTP.PLAIN_TEXT_TYPE);
80+
} else {
81+
String contentJson = getGson().toJson(params.get("content"));
82+
request.withContent(contentJson, MediaType.APPLICATION_JSON);
8483
}
85-
}
84+
85+
if (params.containsKey("includeRaw"))
86+
request.withQuery("include_raw", params.get("includeRaw"));
87+
88+
if (params.containsKey("language"))
89+
request.withHeader("Content-Language", params.get("language"));
8690

87-
/**
88-
* Accepts text and responds with a {@link Profile} with a tree of
89-
* characteristics that include personality, needs, and values. If you
90-
* include either created or updated timestamps, the response also includes
91-
* a summary of the author's writing habits with respect to time of day.
92-
*
93-
* @param text
94-
* the text to analyze
95-
* @return the personality profile
96-
* @see Content
97-
*/
98-
public Profile getProfile(String text) {
99-
if (text == null)
100-
throw new IllegalArgumentException("text can not be null");
91+
if (params.containsKey("acceptLanguage"))
92+
request.withHeader("Accept-Language", params.get("acceptLanguage"));
10193

102-
HttpRequestBase request = Request.Post("/v2/profile")
103-
.withContent(text, HTTP.PLAIN_TEXT_TYPE).build();
94+
HttpResponse response = execute(request.build());
10495

105-
HttpResponse response = execute(request);
10696
try {
10797
String profileJson = ResponseUtil.getString(response);
10898
Profile profile = getGson().fromJson(profileJson, Profile.class);
@@ -111,7 +101,7 @@ public Profile getProfile(String text) {
111101
throw new RuntimeException(e);
112102
}
113103
}
114-
104+
115105
/*
116106
* (non-Javadoc)
117107
*

src/main/java/com/ibm/watson/developer_cloud/personality_insights/v2/model/Profile.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,36 @@ public class Profile {
3333
private Trait tree;
3434

3535
/** The word_count. */
36-
private int word_count;
36+
@SerializedName("word_count")
37+
private int wordCount;
3738

3839
/** The word count message. */
3940
@SerializedName("word_count_message")
4041
private String wordCountMessage;
4142

43+
/** The word count message. */
44+
@SerializedName("processed_lang")
45+
private String processedLanguage;
46+
47+
48+
/**
49+
* Gets the processed language.
50+
*
51+
* @return the processed language
52+
*/
53+
public String getProcessedLanguage() {
54+
return processedLanguage;
55+
}
56+
57+
/**
58+
* Sets the processed language.
59+
*
60+
* @param processedLanguage the new processed language
61+
*/
62+
public void setProcessedLanguage(String processedLanguage) {
63+
this.processedLanguage = processedLanguage;
64+
}
65+
4266
/**
4367
* Gets A message indicating the number of words found and where that value
4468
* falls in the range of required/suggested number of words.
@@ -93,8 +117,8 @@ public Trait getTree() {
93117
*
94118
* @return the number of words
95119
*/
96-
public int getWord_count() {
97-
return word_count;
120+
public int getWordCount() {
121+
return wordCount;
98122
}
99123

100124
/**
@@ -130,10 +154,10 @@ public void setTree(Trait tree) {
130154
/**
131155
* Sets the number of words found in the input.
132156
*
133-
* @param word_count the number of words
157+
* @param wordCount the number of words
134158
*/
135-
public void setWord_count(int word_count) {
136-
this.word_count = word_count;
159+
public void setWordCount(int wordCount) {
160+
this.wordCount = wordCount;
137161
}
138162

139163
/*

src/main/java/com/ibm/watson/developer_cloud/personality_insights/v2/model/Trait.java

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,32 @@
2525
*/
2626
public class Trait {
2727

28+
/** The category. */
29+
private String category;
30+
31+
/** The children. */
32+
private List<Trait> children;
33+
2834
/** The id. */
2935
private String id;
3036

3137
/** The name. */
3238
private String name;
3339

34-
/** The category. */
35-
private String category;
36-
3740
/** The percentage. */
3841
private double percentage;
42+
43+
/** The raw sampling error. */
44+
@SerializedName("raw_sampling_error")
45+
private Double rawSamplingError;
46+
47+
/** The raw score. */
48+
@SerializedName("raw_score")
49+
private Double rawScore;
3950

4051
/** The sampling error. */
4152
@SerializedName("sampling_error")
4253
private double samplingError;
43-
44-
/** The children. */
45-
private List<Trait> children;
46-
47-
/**
48-
* Indicates the sampling error of the percentage, based on the
49-
* number of words in the input. The number defines a 95% confidence
50-
* interval around the percentage. For example, the sampling error is
51-
* 4% and percentage is 61%. It is 95% likely that the actual percentage
52-
* value is between 57% and 65% if more words are given.
53-
*
54-
* @return the sampling error
55-
*/
56-
public double getSamplingError() {
57-
return samplingError;
58-
}
59-
60-
/**
61-
* Sets the sampling error of the percentage based on the
62-
* number of words in the input.
63-
*
64-
* @param samplingError error
65-
* the new sampling error
66-
*/
67-
public void setSamplingError(double samplingError) {
68-
this.samplingError = samplingError;
69-
}
7054

7155
/**
7256
* Gets the personality model category.
@@ -116,6 +100,37 @@ public double getPercentage() {
116100
return percentage;
117101
}
118102

103+
/**
104+
* Gets the raw sampling error.
105+
*
106+
* @return the raw sampling error
107+
*/
108+
public double getRawSamplingError() {
109+
return rawSamplingError;
110+
}
111+
112+
/**
113+
* Gets the raw score.
114+
*
115+
* @return the raw score
116+
*/
117+
public double getRawScore() {
118+
return rawScore;
119+
}
120+
121+
/**
122+
* Indicates the sampling error of the percentage, based on the
123+
* number of words in the input. The number defines a 95% confidence
124+
* interval around the percentage. For example, the sampling error is
125+
* 4% and percentage is 61%. It is 95% likely that the actual percentage
126+
* value is between 57% and 65% if more words are given.
127+
*
128+
* @return the sampling error
129+
*/
130+
public double getSamplingError() {
131+
return samplingError;
132+
}
133+
119134
/**
120135
* Sets personality model category.
121136
* e.g: "values", "needs" or "personality"
@@ -167,6 +182,35 @@ public void setPercentage(double percentage) {
167182
this.percentage = percentage;
168183
}
169184

185+
/**
186+
* Sets the raw sampling error.
187+
*
188+
* @param rawSamplingError the new raw sampling error
189+
*/
190+
public void setRawSamplingError(double rawSamplingError) {
191+
this.rawSamplingError = rawSamplingError;
192+
}
193+
194+
/**
195+
* Sets the raw score.
196+
*
197+
* @param rawScore the new raw score
198+
*/
199+
public void setRawScore(double rawScore) {
200+
this.rawScore = rawScore;
201+
}
202+
203+
/**
204+
* Sets the sampling error of the percentage based on the
205+
* number of words in the input.
206+
*
207+
* @param samplingError error
208+
* the new sampling error
209+
*/
210+
public void setSamplingError(double samplingError) {
211+
this.samplingError = samplingError;
212+
}
213+
170214
/* (non-Javadoc)
171215
* @see java.lang.Object#toString()
172216
*/

0 commit comments

Comments
 (0)