Skip to content

Commit e0fbdab

Browse files
Merge branch 'concept-expansion' into dev
2 parents 95a12ac + 07d9e0e commit e0fbdab

File tree

9 files changed

+663
-0
lines changed

9 files changed

+663
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ APIs and SDKs that use cognitive computing to solve complex problems.
1919
* [Alchemy Language](#alchemy-language)
2020
* [Alchemy Vision](#alchemy-vision)
2121
* [Alchemy Data News](#alchemy-data-news)
22+
* [Concept Expansion](#concept-expansion)
2223
* [Concept Insights](#concept-insights)
2324
* [Dialog](#dialog)
2425
* [Document Conversion](#document-conversion)
@@ -158,6 +159,32 @@ DocumentsResult result = service.getNewsDocuments(params);
158159
System.out.println(result);
159160
```
160161

162+
### Concept Expansion
163+
Map euphemisms or colloquial terms to more commonly understood phrases using
164+
the [Concept Expansion][concept_expansion] service.
165+
Example: Create a job, wait for it to finish, and then retrieve results.
166+
167+
```java
168+
ConceptExpansion service = new ConceptExpansion();
169+
service.setUsernameAndPassword("<username>", "<password>");
170+
171+
String[] seeds = new String[]{ "motrin", "tylenol", "aspirin"};
172+
String label = "medicine";
173+
Job job = service.createJob(label, seeds);
174+
175+
while (service.getJobStatus(job) == Job.Status.AWAITING_WORK
176+
|| service.getJobStatus(job) == Job.Status.IN_FLIGHT) {
177+
try {
178+
Thread.sleep(4000);
179+
} catch (InterruptedException e) {
180+
e.printStackTrace();
181+
}
182+
}
183+
184+
System.out.println(service.getJobResult(job));
185+
```
186+
187+
161188
### Concept Insights
162189
Use the [Concept Insights][concept_insights] service to identify words in the text that
163190
correspond to concepts in a Wikipedia graph.
@@ -508,6 +535,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
508535
[concept_insights]: https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/concept-insights/
509536
[visual_insights]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/visual-insights/
510537
[retrieve_and_rank]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/retrieve-rank/
538+
[concept_expansion]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/concept-expansion/
511539

512540
[alchemy_language]: http://www.alchemyapi.com/products/alchemylanguage
513541
[sentiment_analysis]: http://www.alchemyapi.com/products/alchemylanguage/sentiment-analysis
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.concept_expansion.v1;
15+
16+
import com.ibm.watson.developer_cloud.concept_expansion.v1.model.Job;
17+
18+
public class ConceptExpansionExample {
19+
20+
21+
public static void main(String[] args) {
22+
final ConceptExpansion service = new ConceptExpansion();
23+
service.setUsernameAndPassword("<username>", "<password>");
24+
25+
final String[] seeds = new String[] {"nyc", "dc", "london", "big cities"};
26+
final String label = "demo";
27+
28+
final Job job = service.createJob(label, seeds);
29+
30+
while (service.getJobStatus(job) == Job.Status.AWAITING_WORK
31+
|| service.getJobStatus(job) == Job.Status.IN_FLIGHT) {
32+
try {
33+
Thread.sleep(4000);
34+
} catch (final InterruptedException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
39+
System.out.println(service.getJobResult(job));
40+
}
41+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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.concept_expansion.v1;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
19+
import com.google.gson.JsonArray;
20+
import com.google.gson.JsonElement;
21+
import com.google.gson.JsonObject;
22+
import com.google.gson.JsonParser;
23+
import com.google.gson.JsonPrimitive;
24+
import com.ibm.watson.developer_cloud.concept_expansion.v1.model.Concept;
25+
import com.ibm.watson.developer_cloud.concept_expansion.v1.model.Dataset;
26+
import com.ibm.watson.developer_cloud.concept_expansion.v1.model.Job;
27+
import com.ibm.watson.developer_cloud.concept_expansion.v1.model.Job.Status;
28+
import com.ibm.watson.developer_cloud.http.RequestBuilder;
29+
import com.ibm.watson.developer_cloud.service.WatsonService;
30+
import com.ibm.watson.developer_cloud.util.ResponseUtil;
31+
import com.ibm.watson.developer_cloud.util.Validate;
32+
import com.squareup.okhttp.Request;
33+
import com.squareup.okhttp.Response;
34+
35+
/**
36+
* The Concept Expansion service analyzes text and interprets its meaning based on usage in other
37+
* similar contexts.
38+
*
39+
* @version v1
40+
* @see <a
41+
* href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/concept-expansion.html">
42+
* Concept Expansion</a>
43+
*/
44+
public class ConceptExpansion extends WatsonService {
45+
private static final String DATASET = "dataset";
46+
private static final String LABEL = "label";
47+
private static final String PARAM_JOBID = "jobid";
48+
private static final String PARAM_STATE = "state";
49+
private static final String PREVALENCE = "prevalence";
50+
private static final String RESULT = "result";
51+
private static final String RETURN_SEEDS = "return_seeds";
52+
private static final String SEEDS = "seeds";
53+
private static String URL = "https://gateway.watsonplatform.net/concept-expansion-beta/api";
54+
private static final String V1_RESULT = "/v1/result";
55+
private static final String V1_STATUS = "/v1/status";
56+
private static final String V1_UPLOAD = "/v1/upload";
57+
58+
private Dataset dataset;
59+
60+
/**
61+
* Instantiates a new Concept Expansion service.
62+
*
63+
*/
64+
public ConceptExpansion() {
65+
super("concept_expansion");
66+
setEndPoint(URL);
67+
setDataset(Dataset.MT_SAMPLES);
68+
}
69+
70+
/**
71+
* Creates a {@link Job}.
72+
*
73+
* @param label A conceptual classification of the seed terms.
74+
* @param seeds List of terms to be used as seeds
75+
*
76+
* @return the {@link Job}
77+
*/
78+
public Job createJob(final String label, final String[] seeds) {
79+
Validate.notEmpty(label, "label cannot be null or empty");
80+
Validate.notEmpty(seeds, "seeds cannot be null or empty");
81+
Validate.notNull(dataset, "dataset cannot be null");
82+
83+
final JsonArray seedJsonArray = new JsonArray();
84+
for (final String seed : seeds) {
85+
seedJsonArray.add(new JsonPrimitive(seed));
86+
}
87+
88+
final JsonObject payload = new JsonObject();
89+
payload.addProperty(LABEL, label);
90+
payload.addProperty(DATASET, dataset.getId());
91+
payload.add(SEEDS, seedJsonArray);
92+
93+
final Request request = RequestBuilder.post(V1_UPLOAD).withBodyJson(payload).build();
94+
return executeRequest(request, Job.class);
95+
}
96+
97+
/**
98+
* Formats a concept {@link JsonObject} as a {@link Concept} object
99+
*
100+
* @param conceptJson the concept as {@link JsonObject}
101+
*
102+
* <pre>
103+
* <code> { result:"apple", prevalence:"20" }</code>
104+
* </pre>
105+
* @return the concept as POJO Object
106+
*/
107+
private Concept formatConcept(JsonObject conceptJson) {
108+
return new Concept(conceptJson.get(RESULT).getAsString(), conceptJson.get(PREVALENCE)
109+
.getAsDouble());
110+
}
111+
112+
/**
113+
* Map JSON concepts to POJO Objects.
114+
*
115+
* @param conceptsJson the concepts as json
116+
* @return the list
117+
*/
118+
private List<Concept> formatConcepts(JsonObject conceptsJson) {
119+
final JsonArray conceptArray = conceptsJson.get(RETURN_SEEDS).getAsJsonArray();
120+
final List<Concept> concepts = new ArrayList<Concept>();
121+
122+
for (final JsonElement conceptJson : conceptArray) {
123+
concepts.add(formatConcept(conceptJson.getAsJsonObject()));
124+
}
125+
return concepts;
126+
}
127+
128+
/**
129+
* Gets the dataset.
130+
*
131+
* @return the dataset
132+
*/
133+
public Dataset getDataset() {
134+
return dataset;
135+
}
136+
137+
/**
138+
* Gets the {@link Job} result.
139+
*
140+
* @param job the {@link Job}
141+
*
142+
* @return the list of concepts
143+
* @see Concept
144+
* @see Job
145+
*/
146+
public List<Concept> getJobResult(final Job job) {
147+
Validate.notNull(job, "job cannot be null");
148+
149+
final JsonObject payload = new JsonObject();
150+
payload.addProperty(PARAM_JOBID, job.getId());
151+
152+
final Request request = RequestBuilder.put(V1_RESULT).withBodyJson(payload).build();
153+
154+
final String resultAsString = ResponseUtil.getString(execute(request));
155+
return formatConcepts(new JsonParser().parse(resultAsString).getAsJsonObject());
156+
}
157+
158+
/**
159+
* Gets the {@link Job} status.
160+
*
161+
* @param job the {@link Job}
162+
*
163+
* @return the job status
164+
*/
165+
public Status getJobStatus(final Job job) {
166+
Validate.notNull(job, "job cannot be null");
167+
168+
final Request request =
169+
RequestBuilder.get(V1_STATUS).withQuery(PARAM_JOBID, job.getId()).build();
170+
171+
final Response response = execute(request);
172+
final JsonObject jsonObject = ResponseUtil.getJsonObject(response);
173+
return Status.fromString(jsonObject.get(PARAM_STATE).getAsString());
174+
}
175+
176+
/**
177+
* Sets the Dataset to run against.
178+
*
179+
* @param dataset the new dataset
180+
*/
181+
public void setDataset(final Dataset dataset) {
182+
this.dataset = dataset;
183+
}
184+
185+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
15+
package com.ibm.watson.developer_cloud.concept_expansion.v1.model;
16+
17+
import com.ibm.watson.developer_cloud.concept_expansion.v1.ConceptExpansion;
18+
import com.ibm.watson.developer_cloud.service.model.GenericModel;
19+
20+
/**
21+
* This class map a Concept returned by {@link ConceptExpansion}.
22+
*
23+
* @author German Attanasio Ruiz (germanatt@us.ibm.com)
24+
*/
25+
public class Concept extends GenericModel {
26+
27+
/** The name. */
28+
private final String name;
29+
30+
/** The prevalence. */
31+
private final Double prevalence;
32+
33+
/**
34+
* Instantiates a new concept.
35+
*
36+
* @param name the name
37+
* @param prevalence the prevalence
38+
*/
39+
public Concept(String name, Double prevalence) {
40+
super();
41+
this.name = name;
42+
this.prevalence = prevalence;
43+
}
44+
45+
/**
46+
* Gets the name.
47+
*
48+
*
49+
* @return the name
50+
*/
51+
public String getName() {
52+
return name;
53+
}
54+
55+
/**
56+
* Gets the prevalence.
57+
*
58+
*
59+
* @return the prevalence
60+
*/
61+
public Double getPrevalence() {
62+
return prevalence;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)