Skip to content

Commit 6102075

Browse files
[concept-insights] fix for concepts not being correctly serialized #163
1 parent 4b0093d commit 6102075

File tree

5 files changed

+106
-85
lines changed

5 files changed

+106
-85
lines changed

src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/ConceptInsights.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818
import java.util.Map;
1919

20+
import com.google.gson.Gson;
2021
import com.google.gson.JsonArray;
2122
import com.google.gson.JsonObject;
2223
import com.google.gson.JsonPrimitive;
@@ -263,13 +264,13 @@ public QueryConcepts conceptualSearch(Corpus corpus, Map<String, Object> paramet
263264
if (parameters.get(CONCEPT_FIELDS) != null) {
264265
final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
265266
if (fields != null && !fields.isEmpty())
266-
queryParams.put(CONCEPT_FIELDS, fields.toString());
267+
queryParams.put(CONCEPT_FIELDS, toJson(fields.getFields()));
267268
}
268269

269270
if (parameters.get(DOCUMENT_FIELDS) != null) {
270271
final RequestedFields fields = (RequestedFields) parameters.get(DOCUMENT_FIELDS);
271272
if (fields != null && !fields.isEmpty())
272-
queryParams.put(DOCUMENT_FIELDS, fields.toString());
273+
queryParams.put(DOCUMENT_FIELDS, toJson(fields.getFields()));
273274
}
274275

275276
return executeRequest(API_VERSION + corpusId + CONCEPTUAL_SEARCH_PATH, queryParams,
@@ -412,7 +413,7 @@ public Concepts getConceptRelatedConcepts(final Concept concept,
412413
if (parameters.get(CONCEPT_FIELDS) != null) {
413414
final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
414415
if (fields != null && !fields.isEmpty())
415-
queryParameters.put(CONCEPT_FIELDS, fields.toString());
416+
queryParameters.put(CONCEPT_FIELDS, toJson(fields.getFields()));
416417
}
417418
return executeRequest(API_VERSION + conceptId + RELATED_CONCEPTS_PATH, queryParameters,
418419
Concepts.class);
@@ -467,7 +468,7 @@ public Concepts getCorpusRelatedConcepts(final Corpus corpus, final Map<String,
467468
if (parameters.get(CONCEPT_FIELDS) != null) {
468469
final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
469470
if (fields != null && !fields.isEmpty())
470-
queryParameters.put(CONCEPT_FIELDS, fields.toString());
471+
queryParameters.put(CONCEPT_FIELDS, toJson(fields.getFields()));
471472
}
472473
return executeRequest(API_VERSION + corpusId + RELATED_CONCEPTS_PATH, queryParameters,
473474
Concepts.class);
@@ -569,7 +570,7 @@ public Concepts getDocumentRelatedConcepts(final Document document,
569570
if (parameters.get(CONCEPT_FIELDS) != null) {
570571
final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
571572
if (fields != null && !fields.isEmpty())
572-
queryParams.put(CONCEPT_FIELDS, fields.toString());
573+
queryParams.put(CONCEPT_FIELDS, toJson(fields.getFields()));
573574
}
574575
return executeRequest(API_VERSION + documentId + RELATED_CONCEPTS_PATH, queryParams,
575576
Concepts.class);
@@ -629,7 +630,7 @@ public Concepts getGraphRelatedConcepts(final Graph graph, final List<Concept> c
629630
if (parameters.get(CONCEPT_FIELDS) != null) {
630631
final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
631632
if (fields != null && !fields.isEmpty())
632-
queryParameters.put(CONCEPT_FIELDS, fields.toString());
633+
queryParameters.put(CONCEPT_FIELDS, toJson(fields.getFields()));
633634
}
634635
final JsonObject contentJson = new JsonObject();
635636
final JsonArray conceptsJson = new JsonArray();
@@ -642,6 +643,17 @@ public Concepts getGraphRelatedConcepts(final Graph graph, final List<Concept> c
642643
Concepts.class);
643644
}
644645

646+
/**
647+
* Returns the JSON representation of @param object This method is being use to transform into
648+
* JSON query parameters for some of the {@link ConceptInsights} calls
649+
*
650+
* @param object the object to transform
651+
* @return the JSON representation of the object
652+
*/
653+
private String toJson(Object object) {
654+
return new Gson().toJson(object);
655+
}
656+
645657
/**
646658
* Returns a list of scores that denotes how related a source concept is to a list of individual
647659
* concepts.
@@ -767,13 +779,13 @@ public Matches searchCorpusByLabel(final Corpus corpus, final Map<String, Object
767779
if (parameters.get(CONCEPT_FIELDS) != null) {
768780
final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
769781
if (fields != null && !fields.isEmpty())
770-
queryParameters.put(CONCEPT_FIELDS, fields.toString());
782+
queryParameters.put(CONCEPT_FIELDS, toJson(fields.getFields()));
771783
}
772784

773785
if (parameters.get(DOCUMENT_FIELDS) != null) {
774786
final RequestedFields fields = (RequestedFields) parameters.get(DOCUMENT_FIELDS);
775787
if (fields != null && !fields.isEmpty())
776-
queryParameters.put(DOCUMENT_FIELDS, fields.toString());
788+
queryParameters.put(DOCUMENT_FIELDS, toJson(fields.getFields()));
777789
}
778790
return executeRequest(API_VERSION + corpusId + LABEL_SEARCH_PATH, queryParameters,
779791
Matches.class);
@@ -810,7 +822,7 @@ public Matches searchGraphsConceptByLabel(final Graph graph, final Map<String, O
810822
if (parameters.get(CONCEPT_FIELDS) != null) {
811823
final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
812824
if (fields != null && !fields.isEmpty())
813-
queryParameters.put(CONCEPT_FIELDS, fields.toString());
825+
queryParameters.put(CONCEPT_FIELDS, toJson(fields.getFields()));
814826
}
815827
return executeRequest(API_VERSION + graphId + LABEL_SEARCH_PATH, queryParameters, Matches.class);
816828
}

src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/model/Concept.java

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
package com.ibm.watson.developer_cloud.concept_insights.v2.model;
1515

16+
import java.util.List;
17+
18+
import com.google.gson.annotations.SerializedName;
1619
import com.ibm.watson.developer_cloud.concept_insights.v2.ConceptInsights;
1720
import com.ibm.watson.developer_cloud.service.model.GenericModel;
1821
import com.ibm.watson.developer_cloud.util.Validate;
@@ -23,15 +26,24 @@
2326
*/
2427
public class Concept extends GenericModel {
2528

29+
@SerializedName("abstract")
30+
private String _abstract;
31+
2632
/** The id. */
2733
private String id;
2834

2935
/** The label. */
3036
private String label;
3137

38+
private String link;
39+
3240
/** The concept name. */
3341
private String name;
3442

43+
private List<String> ontology;
44+
45+
private String thumbnail;
46+
3547
/**
3648
* Instantiates a new concept.
3749
*/
@@ -63,6 +75,13 @@ public Concept(final String accountId, final String graphName, final String conc
6375
setId(new Graph(accountId, graphName).getId() + "/concepts/" + concept);
6476
}
6577

78+
/**
79+
* @return the abstract. Brief description of the concept. Typically 1-3 sentences. ,
80+
*/
81+
public String getAbstract() {
82+
return _abstract;
83+
}
84+
6685
/**
6786
* Gets the id.
6887
*
@@ -73,14 +92,21 @@ public String getId() {
7392
}
7493

7594
/**
76-
* Gets the label.
95+
* Gets the Human-readable title of the concept
7796
*
7897
* @return The label
7998
*/
8099
public String getLabel() {
81100
return label;
82101
}
83102

103+
/**
104+
* @return the Link to external resource for this concept (for example, a wikipedia page)
105+
*/
106+
public String getLink() {
107+
return link;
108+
}
109+
84110
/**
85111
* Gets the name.
86112
*
@@ -90,6 +116,27 @@ public String getName() {
90116
return name;
91117
}
92118

119+
/**
120+
* @return the list of potential categories for a concept
121+
*/
122+
public List<String> getOntology() {
123+
return ontology;
124+
}
125+
126+
/**
127+
* @return the URL of a small image of the concept
128+
*/
129+
public String getThumbnail() {
130+
return thumbnail;
131+
}
132+
133+
/**
134+
* @param abs the brief description of the concept. Typically 1-3 sentences. ,
135+
*/
136+
public void setAbstract(String abs) {
137+
this._abstract = abs;
138+
}
139+
93140
/**
94141
* Sets the id.
95142
*
@@ -100,14 +147,21 @@ public void setId(String id) {
100147
}
101148

102149
/**
103-
* Sets the label.
150+
* Sets the Human-readable title of the concept
104151
*
105152
* @param label The label
106153
*/
107154
public void setLabel(String label) {
108155
this.label = label;
109156
}
110157

158+
/**
159+
* @param link the Link to external resource for this concept (for example, a wikipedia page)
160+
*/
161+
public void setLink(String link) {
162+
this.link = link;
163+
}
164+
111165
/**
112166
* Sets the name.
113167
*
@@ -116,4 +170,18 @@ public void setLabel(String label) {
116170
public void setName(String name) {
117171
this.name = name;
118172
}
173+
174+
/**
175+
* @param ontology the list of potential categories for a concept
176+
*/
177+
public void setOntology(List<String> ontology) {
178+
this.ontology = ontology;
179+
}
180+
181+
/**
182+
* @param thumbnail the URL of a small image of the concept
183+
*/
184+
public void setThumbnail(String thumbnail) {
185+
this.thumbnail = thumbnail;
186+
}
119187
}

src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/model/Match.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/model/Matches.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
public class Matches extends GenericModel {
2626

2727
/** The matches. */
28-
private List<Match> matches;
28+
private List<Concept> matches;
2929

3030
/**
3131
* Gets the matches.
3232
*
3333
* @return The matches
3434
*/
35-
public List<Match> getMatches() {
35+
public List<Concept> getMatches() {
3636
return matches;
3737
}
3838

@@ -41,7 +41,7 @@ public List<Match> getMatches() {
4141
*
4242
* @param matches The matches
4343
*/
44-
public void setMatches(List<Match> matches) {
44+
public void setMatches(List<Concept> matches) {
4545
this.matches = matches;
4646
}
4747
}

src/test/java/com/ibm/watson/developer_cloud/concept_insights/v2/ConceptInsightsIT.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ public void testGetConceptRelatedConcepts() {
144144
final RequestedFields fs = new RequestedFields();
145145
fs.include("abstract");
146146
params.put("concept_fields", fs);
147-
final Concepts concepts = service.getConceptRelatedConcepts(EXAMPLE_CONCEPT, params);
147+
Concepts concepts = service.getConceptRelatedConcepts(EXAMPLE_CONCEPT, params);
148148
Assert.assertNotNull(concepts);
149-
149+
Assert.assertNotNull(concepts.getConcepts().get(0).getConcept().getAbstract());
150150
}
151151

152152
/**
@@ -295,10 +295,15 @@ public void testGetGraphRelatedConcepts() {
295295
params.put(ConceptInsights.LEVEL, 1);
296296
final RequestedFields fs = new RequestedFields();
297297
fs.include("abstract");
298-
params.put("concept_fields", fs);
299-
final Concepts conceptResults =
300-
service.getGraphRelatedConcepts(Graph.WIKIPEDIA, concepts, params);
298+
fs.include("link");
299+
fs.include("name");
300+
params.put(ConceptInsights.CONCEPT_FIELDS, fs);
301+
Concepts conceptResults = service.getGraphRelatedConcepts(Graph.WIKIPEDIA, concepts, params);
301302
Assert.assertNotNull(conceptResults);
303+
Assert.assertTrue(!conceptResults.getConcepts().isEmpty());
304+
Assert.assertNotNull(conceptResults.getConcepts().get(0).getConcept().getAbstract());
305+
306+
302307
}
303308

304309
/**
@@ -347,6 +352,8 @@ public void testGetGraphsLabelSearch() {
347352
final Matches matches = service.searchGraphsConceptByLabel(Graph.WIKIPEDIA, params);
348353
Assert.assertNotNull(matches);
349354
Assert.assertFalse(matches.getMatches().isEmpty());
355+
Assert.assertNotNull(matches.getMatches().get(0).getAbstract());
356+
350357
}
351358

352359
/**

0 commit comments

Comments
 (0)