|
| 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 | +} |
0 commit comments