Skip to content

Commit 2c6b154

Browse files
#11 added support for /synonym and /tone along with an example
1 parent 499babd commit 2c6b154

File tree

20 files changed

+6024
-95
lines changed

20 files changed

+6024
-95
lines changed

README.md

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Watson Developer Cloud Java Wrapper
22
[![Build Status](https://secure.travis-ci.org/watson-developer-cloud/java-wrapper.png)](http://travis-ci.org/watson-developer-cloud/java-wrapper)
3+
34
Java code wrappers to quickly get started with the various [Watson Developer Cloud][wdc] services - A collection of REST APIs and SDKs that use cognitive computing to solve complex problems.
45

56
## Table of Contents
@@ -12,6 +13,7 @@ Java code wrappers to quickly get started with the various [Watson Developer Clo
1213
* [Getting the Service Credentials](#getting-the-service-credentials)
1314
* [IBM Watson Services](#ibm-watson-services)
1415
* [Concept Expansion](#concept-expansion)
16+
* [Dialog](#dialog)
1517
* [Language Identification](#language-identification)
1618
* [Language Translation](#language-translation)
1719
* [Machine Translation](#machine-translation)
@@ -22,6 +24,7 @@ Java code wrappers to quickly get started with the various [Watson Developer Clo
2224
* [Relationship Extraction](#relationship-extraction)
2325
* [Speech to Text](#speech-to-text)
2426
* [Text to Speech](#text-to-speech)
27+
* [Tone Analyzer](#tone-analyzer)
2528
* [Tradeoff Analytics](#tradeoff-analytics)
2629
* [Visual Recognition](#visual-recognition)
2730
* [Running in Bluemix](#running-in-bluemix)
@@ -48,13 +51,13 @@ Now, you are ready to see some [examples](https://github.com/watson-developer-cl
4851
<dependency>
4952
<groupId>com.ibm.watson.developer_cloud</groupId>
5053
<artifactId>java-wrapper</artifactId>
51-
<version>0.1.9</version>
54+
<version>1.0.1</version>
5255
</dependency>
5356
```
5457
##### Gradle
5558

5659
```gradle
57-
'com.ibm.watson.developer_cloud:java-wrapper:0.1.9'
60+
'com.ibm.watson.developer_cloud:java-wrapper:1.0.1'
5861
```
5962

6063
## Usage
@@ -108,6 +111,19 @@ while (service.getJobStatus(job) == Job.Status.AWAITING_WORK
108111
System.out.println(service.getJobResult(job));
109112
```
110113

114+
### Dialog
115+
Returns the dialog list using the [Dialog][dialog] service.
116+
117+
```java
118+
import com.ibm.watson.developer_cloud.dialog.v1.DialogService;
119+
import com.ibm.watson.developer_cloud.dialog.v1.model.Dialog;
120+
121+
DialogService service = new DialogService();
122+
service.setUsernameAndPassword("<username>", "<password>");
123+
124+
List<Dialog> dialogs = service.getDialogs();
125+
System.out.println(dialogs);
126+
```
111127

112128
### Language Identification
113129
Example: Identify a language using the [Language Identification][language_identification] service.
@@ -274,15 +290,15 @@ import java.io.File;
274290
SpeechToText service = new SpeechToText();
275291
service.setUsernameAndPassword("<username>", "<password>");
276292

277-
File audio = new File("sample1.wav");
293+
File audio = new File("src/test/resources/sample1.wav");
278294

279295
SpeechResults transcript = service.recognize(audio, "audio/l16; rate=44100");
280296
System.out.println(transcript);
281297
```
282298

283299
#### WebSocket support
284300

285-
Speech to Text supports WebSocket so you can use a websocket client like the one in: http://java-websocket.org/
301+
Speech to Text supports WebSocket so you can use a WebSocket client like the one in: http://java-websocket.org/
286302
The websocket url is: `wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize`
287303

288304
### Text to Speech
@@ -299,6 +315,30 @@ List<Voice> voices = service.getVoices();
299315
System.out.println(voices);
300316
```
301317

318+
### Tone Analyzer
319+
Use the [Tone Analyzer][tone_analyzer] service to get the tone of your email.
320+
321+
```java
322+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.ToneAnalyzer;
323+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Scorecard;
324+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.SynonymResult;
325+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Tone;
326+
327+
ToneAnalyzer service = new ToneAnalyzer();
328+
service.setUsernameAndPassword("<username>", "<password>");
329+
330+
String text = "I know the times are difficult! Our sales have been "
331+
+ "disappointing for the past three quarters for our data analytics "
332+
+ "product suite. We have a competitive data analytics product "
333+
+ "suite in the industry. But we need to do our job selling it! "
334+
+ "We need to acknowledge and fix our sales challenges.";
335+
336+
// Call the service and get the tone
337+
Tone tone = service.getTone(text, Scorecard.EMAIL);
338+
System.out.println(tone);
339+
```
340+
341+
302342
### Tradeoff Analytics
303343
Use the [Tradeoff Analytics][tradeoff_analytics] service to find the best
304344
phone that minimizes price and weight and maximizes screen size.
@@ -362,10 +402,10 @@ import java.io.File;
362402
VisualRecognition service = new VisualRecognition();
363403
service.setUsernameAndPassword("<username>", "<password>");
364404

365-
File image = new File("car.png");
405+
File image = new File("src/test/resources/car.png");
366406

367407
LabelSet labelSet = new LabelSet();
368-
labelSet.withLabelGroup("Animal").withLabelGroup("Food");
408+
labelSet.withLabelGroup("Auto Racing").withLabelGroup("Sports");
369409

370410
RecognizedImage recognizedImage = service.recognize(image, labelSet);
371411
System.out.println(recognizedImage);
@@ -387,7 +427,7 @@ Gradle:
387427

388428
```sh
389429
$ cd java-wrapper
390-
$ gradle jar # build jar file (build/libs/watson-developer-cloud-0.1.8.jar)
430+
$ gradle jar # build jar file (build/libs/watson-developer-cloud-1.0.1.jar)
391431
$ gradle test # run tests
392432
```
393433

@@ -440,6 +480,9 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
440480
[tradeoff_analytics]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/tradeoff-analytics/
441481
[text_to_speech]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/text-to-speech/
442482
[speech_to_text]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/speech-to-text/
483+
[tone-analyzer]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/tone-analyzer/
484+
[dialog]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/dialog/
485+
443486
[wdc]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/
444487
[vcap_environment]: http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/getting_started/index.html#EnvVars
445488
[bluemix]: https://console.ng.bluemix.net

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sourceCompatibility = 1.6
66
targetCompatibility = 1.6
77
group = 'com.ibm.watson.developercloud'
88
archivesBaseName = 'watson-developer-cloud'
9-
version = '0.1.9'
9+
version = '1.0.1'
1010

1111
description = 'Watson Developer Cloud Java Wrapper'
1212

examples/java/com/ibm/watson/developer_cloud/speech_to_text/v1/SpeechToTextExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void main(String[] args) throws URISyntaxException {
2727
SpeechToText service = new SpeechToText();
2828
service.setUsernameAndPassword("<username>", "<password>");
2929

30-
File audio = new File("sample1.wav");
30+
File audio = new File("src/test/resources/sample1.wav");
3131
SpeechResults transcript = service.recognize(audio, "audio/l16; rate=44100");
3232

3333
System.out.println(transcript);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.FileNotFoundException;
19+
import java.net.URISyntaxException;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Scorecard;
25+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.SynonymResult;
26+
import com.ibm.watson.developer_cloud.tone_analyzer.v1.model.Tone;
27+
28+
public class ToneAnalyzerExample {
29+
30+
31+
public static void main(String[] args) throws URISyntaxException, FileNotFoundException {
32+
ToneAnalyzer service = new ToneAnalyzer();
33+
service.setUsernameAndPassword("<username>", "<password>");
34+
35+
String text = "I know the times are difficult! Our sales have been "
36+
+ "disappointing for the past three quarters for our data analytics "
37+
+ "product suite. We have a competitive data analytics product "
38+
+ "suite in the industry. But we need to do our job selling it! "
39+
+ "We need to acknowledge and fix our sales challenges. "
40+
+ "We can’t blame the economy for our lack of execution! "
41+
+ "We are missing critical sales opportunities. "
42+
+ "Our product is in no way inferior to the competitor products. "
43+
+ "Our clients are hungry for analytical tools to improve their "
44+
+ "business outcomes. Economy has nothing to do with it.";
45+
46+
// Call the service and get the tone
47+
Tone tone = service.getTone(text, Scorecard.EMAIL);
48+
System.out.println(tone);
49+
50+
// Call the service and get the synonym for 'difficult'
51+
Map<String, Object> params = new HashMap<String, Object>();
52+
params.put(ToneAnalyzer.WORDS, new String[]{"difficult","inferior"});
53+
params.put(ToneAnalyzer.LIMIT, 3);
54+
params.put(ToneAnalyzer.HOPS, 3);
55+
56+
List<SynonymResult> synonyms = service.getSynonyms(params);
57+
System.out.println(synonyms);
58+
}
59+
}

examples/java/com/ibm/watson/developer_cloud/visual_recognition/v1/VisualRecognitionExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
import java.io.FileNotFoundException;
2020
import java.net.URISyntaxException;
2121

22-
import com.ibm.watson.developer_cloud.WatsonServiceTest;
2322
import com.ibm.watson.developer_cloud.visual_recognition.v1.model.RecognizedImage;
2423

25-
public class VisualRecognitionExample extends WatsonServiceTest {
24+
public class VisualRecognitionExample {
2625

2726
public static void main(String[] args) throws URISyntaxException, FileNotFoundException {
2827
VisualRecognition service = new VisualRecognition();
2928
service.setUsernameAndPassword("<username>", "<password>");
3029

31-
File image = new File("car.png");
30+
File image = new File("src/test/resources/car.png");
31+
3232
RecognizedImage recognizedImage = service.recognize(image);
33-
3433
System.out.println(recognizedImage);
34+
3535
}
3636
}

src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ protected HttpResponse execute(HttpRequestBase request) {
196196
// There was a Client Error 4xx or a Server Error 5xx
197197
// Get the error message and create the exception
198198
String error = getErrorMessage(response);
199-
log.log(Level.SEVERE, "HTTP Status: " + status);
200-
log.log(Level.SEVERE, "Error message from service: " + error);
199+
log.log(Level.SEVERE, "HTTP Status: " + status + ", message: "+ error);
201200

202201
switch (status) {
203202
case HttpStatus.SC_BAD_REQUEST: // HTTP 400
@@ -356,7 +355,7 @@ public HttpClient getThreadSafeClient() {
356355
* @return the user agent
357356
*/
358357
private final String getUserAgent() {
359-
return "watson-developer-cloud-java-wrapper-0.1.9";
358+
return "watson-developer-cloud-java-wrapper-1.0.1";
360359
}
361360

362361
/**

src/main/java/com/ibm/watson/developer_cloud/speech_to_text/v1/SpeechToText.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public void deleteSession(final SpeechSession session) {
127127
throw new IllegalArgumentException("session was not specified");
128128

129129
HttpRequestBase request = Request.Delete("/v1/sessions/" + session.getSessionId())
130+
.withHeader("Cookie", session.getCookieSession())
130131
.build();
131132
HttpResponse response = execute(request);
132133

0 commit comments

Comments
 (0)