|
| 1 | +/** |
| 2 | + * Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.ibm.watson.developer_cloud.tone_analyzer.v1; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.logging.Logger; |
| 23 | + |
| 24 | +import org.apache.http.HttpResponse; |
| 25 | +import org.apache.http.client.methods.HttpRequestBase; |
| 26 | + |
| 27 | +import com.google.gson.Gson; |
| 28 | +import com.google.gson.JsonObject; |
| 29 | +import com.google.gson.reflect.TypeToken; |
| 30 | +import com.ibm.watson.developer_cloud.service.Request; |
| 31 | +import com.ibm.watson.developer_cloud.service.WatsonService; |
| 32 | +import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Scorecard; |
| 33 | +import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.SynonymResult; |
| 34 | +import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Tone; |
| 35 | +import com.ibm.watson.developer_cloud.util.ResponseUtil; |
| 36 | + |
| 37 | +/** |
| 38 | + * The IBM Watson The Tone Analyzer service uses linguistic analysis to detect |
| 39 | + * emotional tones, social propensities, and writing styles in written |
| 40 | + * communication. Then it offers suggestions to help the writer improve their |
| 41 | + * intended language tones. |
| 42 | + * |
| 43 | + * @author German Attanasio Ruiz (germanatt@us.ibm.com) |
| 44 | + * @version v1 |
| 45 | + * @see <a |
| 46 | + * href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/tone-analyzer.html"> |
| 47 | + * Tone Analyzer</a> |
| 48 | + */ |
| 49 | +public class ToneAnalyzer extends WatsonService { |
| 50 | + |
| 51 | + // parameters |
| 52 | + public static final String TEXT = null; |
| 53 | + public static final String SCORECARD = null; |
| 54 | + |
| 55 | + private static final Logger log = Logger.getLogger(ToneAnalyzer.class |
| 56 | + .getName()); |
| 57 | + private static final String URL = "https://gateway.watsonplatform.net/tone-analyzer-beta/api"; |
| 58 | + |
| 59 | + private static final Type listScorecardsType = new TypeToken<List<Scorecard>>() { |
| 60 | + }.getType(); |
| 61 | + |
| 62 | + /** |
| 63 | + * Instantiates a new Tone Analyzer service with the default url |
| 64 | + */ |
| 65 | + public ToneAnalyzer() { |
| 66 | + setEndPoint(URL); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Analyzes the "tone" of a piece of text. The message is analyzed from |
| 71 | + * several tones (social tone, emotional tone, writing tone), and for each |
| 72 | + * of them various traits are derived (such as conscientiousness, |
| 73 | + * agreeableness, openness). |
| 74 | + * |
| 75 | + * @param text |
| 76 | + * The text to analyze |
| 77 | + * @param scorecard |
| 78 | + * Name of the scorecard used to compute the tone. (business |
| 79 | + * messages by default) |
| 80 | + * @return the {@link Tone} with the response |
| 81 | + * |
| 82 | + */ |
| 83 | + public Tone getTone(final String text, final String scorecard) { |
| 84 | + |
| 85 | + if (text == null || text.isEmpty()) |
| 86 | + throw new IllegalArgumentException("text can not be null or empty"); |
| 87 | + |
| 88 | + JsonObject contentJson = new JsonObject(); |
| 89 | + contentJson.addProperty(TEXT, text); |
| 90 | + contentJson.addProperty(SCORECARD, text); |
| 91 | + |
| 92 | + HttpRequestBase request = Request.Post("/v1/tone") |
| 93 | + .withContent(contentJson).build(); |
| 94 | + |
| 95 | + try { |
| 96 | + HttpResponse response = execute(request); |
| 97 | + String toneAsJson = ResponseUtil.getString(response); |
| 98 | + Tone tone = getGson().fromJson(toneAsJson, Tone.class); |
| 99 | + return tone; |
| 100 | + } catch (IOException e) { |
| 101 | + throw new RuntimeException(e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Starts or continue conversations. |
| 107 | + * |
| 108 | + * @param words |
| 109 | + * (string[]), |
| 110 | + * @param traits |
| 111 | + * (string[], optional), |
| 112 | + * @param limit |
| 113 | + * (long, optional) Limits the number of words to return on each |
| 114 | + * trait. The most heavily correlated (positive and negative) to |
| 115 | + * each trait are returned; approximately limit/2 each of |
| 116 | + * positively correlated words negatively correlated words if |
| 117 | + * there are enough available. |
| 118 | + * @param hops |
| 119 | + * (long, optional): The number of 'hops' to explore for synonyms |
| 120 | + * or related words: 0 indicates words directly related to the |
| 121 | + * input; 1 gives words related to them, and so on. The greater |
| 122 | + * this number, the richer the word set returned (but at the same |
| 123 | + * time, less tightly related to the input). |
| 124 | + * @param context |
| 125 | + * (string[], optional) |
| 126 | + * @param index |
| 127 | + * (long, optional): The position (0-based) in `context` |
| 128 | + * parameter where the requested word appears. This parameter can |
| 129 | + * only be passed when `context` is also present and there is |
| 130 | + * only one word requested (length(`words`)=0), and it makes |
| 131 | + * sense only if `context[index]` is equal to `words[0]`. |
| 132 | + * |
| 133 | + * @param params |
| 134 | + * The map with the parameters described above |
| 135 | + * |
| 136 | + * @return {@link SynonymResult} |
| 137 | + */ |
| 138 | + public SynonymResult getSynynyms(Map<String, Object> params) { |
| 139 | + String[] words = (String[]) params.get("words"); |
| 140 | + String[] traits = (String[]) params.get("traits"); |
| 141 | + String[] context = (String[]) params.get("context"); |
| 142 | + |
| 143 | + if (words == null || words.length == 0) |
| 144 | + throw new IllegalArgumentException("words can not be null or empty"); |
| 145 | + |
| 146 | + JsonObject jsonObject = new JsonObject(); |
| 147 | + // TODO: add all the properties to the json object |
| 148 | + HttpRequestBase request = Request.Post("/v1/synonym") |
| 149 | + .withContent(jsonObject).build(); |
| 150 | + |
| 151 | + HttpResponse response = execute(request); |
| 152 | + try { |
| 153 | + String synonymResultJson = ResponseUtil.getString(response); |
| 154 | + SynonymResult synonymResult = getGson().fromJson(synonymResultJson, |
| 155 | + SynonymResult.class); |
| 156 | + return synonymResult; |
| 157 | + } catch (IOException e) { |
| 158 | + throw new RuntimeException(e); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Returns a list of available scorecards. Scorecards are implementations of |
| 164 | + * Tone evaluations for different domains. |
| 165 | + * |
| 166 | + * @return A list of {@link Scorecard } |
| 167 | + */ |
| 168 | + public List<Scorecard> getScorecards() { |
| 169 | + HttpRequestBase request = Request.Get("/v1/scorecards").build(); |
| 170 | + HttpResponse response = execute(request); |
| 171 | + |
| 172 | + try { |
| 173 | + JsonObject jsonObject = ResponseUtil.getJsonObject(response); |
| 174 | + List<Scorecard> scorecards = new Gson().fromJson( |
| 175 | + jsonObject.get("scorecards"), listScorecardsType); |
| 176 | + return scorecards; |
| 177 | + } catch (IOException e) { |
| 178 | + throw new RuntimeException(e); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments