Skip to content

Commit 61a9909

Browse files
[speech-to-text] Added web socket example and code review for web
sockets
1 parent 34d305e commit 61a9909

File tree

18 files changed

+626
-353
lines changed

18 files changed

+626
-353
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* Copyright 2015 IBM Corp. All Rights Reserved.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
6-
*
6+
*
77
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
8+
*
99
* Unless required by applicable law or agreed to in writing, software distributed under the License
1010
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1111
* or implied. See the License for the specific language governing permissions and limitations under
@@ -15,7 +15,6 @@
1515

1616
import java.io.File;
1717

18-
import com.ibm.watson.developer_cloud.http.HttpMediaType;
1918
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechResults;
2019

2120

@@ -30,7 +29,7 @@ public static void main(String[] args) {
3029
service.setUsernameAndPassword("<username>", "<password>");
3130

3231
File audio = new File("src/test/resources/speech_to_text/sample1.wav");
33-
SpeechResults transcript = service.recognize(audio, HttpMediaType.AUDIO_WAV);
32+
SpeechResults transcript = service.recognize(audio);
3433

3534
System.out.println(transcript);
3635
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.service;
15+
16+
import com.ibm.watson.developer_cloud.http.HttpStatus;
17+
import com.squareup.okhttp.Response;
18+
19+
/**
20+
* 409 Conflict (HTTP/1.1 - RFC 2616)
21+
*/
22+
public class ConflictException extends ServiceResponseException {
23+
24+
/**
25+
* The Constant serialVersionUID.
26+
*/
27+
private static final long serialVersionUID = 1L;
28+
29+
/**
30+
* Instantiates a new Forbidden Exception.
31+
*
32+
* @param message the error message
33+
* @param response the HTTP response
34+
*/
35+
public ConflictException(String message, Response response) {
36+
super(HttpStatus.CONFLICT, message, response);
37+
}
38+
39+
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import com.google.gson.JsonSyntaxException;
2727
import com.ibm.watson.developer_cloud.http.HttpHeaders;
2828
import com.ibm.watson.developer_cloud.http.HttpStatus;
29+
import com.ibm.watson.developer_cloud.http.RequestBuilder;
2930
import com.ibm.watson.developer_cloud.service.model.GenericModel;
3031
import com.ibm.watson.developer_cloud.util.BluemixUtils;
3132
import com.ibm.watson.developer_cloud.util.RequestUtil;
3233
import com.ibm.watson.developer_cloud.util.ResponseUtil;
3334
import com.squareup.okhttp.Credentials;
3435
import com.squareup.okhttp.Headers;
36+
import com.squareup.okhttp.HttpUrl;
3537
import com.squareup.okhttp.OkHttpClient;
3638
import com.squareup.okhttp.Request;
3739
import com.squareup.okhttp.Request.Builder;
@@ -69,9 +71,9 @@ public WatsonService(String name) {
6971

7072

7173
/**
72-
* Configure HTTP client.
74+
* Configures the HTTP client.
7375
*
74-
* @return the okhttp client
76+
* @return the HTTP client
7577
*/
7678
protected OkHttpClient configureHttpClient() {
7779
final OkHttpClient client = new OkHttpClient();
@@ -147,6 +149,8 @@ protected Response execute(Request request) {
147149
case HttpStatus.NOT_ACCEPTABLE: // HTTP 406
148150
throw new ForbiddenException(error != null ? error
149151
: "Forbidden: Service refuse the request", response);
152+
case HttpStatus.CONFLICT: // HTTP 409
153+
throw new ConflictException(error != null ? error : "", response);
150154
case HttpStatus.REQUEST_TOO_LONG: // HTTP 413
151155
throw new RequestTooLargeException(error != null ? error
152156
: "Request too large: The request entity is larger than the server is able to process",
@@ -216,6 +220,20 @@ public String getEndPoint() {
216220
return endPoint;
217221
}
218222

223+
/**
224+
* Gets an authorization token that can be use to authorize API calls.
225+
*
226+
*
227+
* @return the token
228+
*/
229+
public String getToken() {
230+
HttpUrl url =
231+
HttpUrl.parse(getEndPoint()).newBuilder().setPathSegment(0, "authorization").build();
232+
Request request = RequestBuilder.get(url + "/v1/token").withQuery("url", getEndPoint()).build();
233+
Response response = execute(request);
234+
return ResponseUtil.getJsonObject(response).get("token").getAsString();
235+
}
236+
219237
/**
220238
* Gets the error message from a JSON response
221239
*

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@
1313
*/
1414
package com.ibm.watson.developer_cloud.speech_to_text.v1;
1515

16-
import java.util.List;
16+
import org.apache.commons.lang3.Validate;
1717

1818
import com.google.gson.annotations.SerializedName;
1919
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechSession;
20+
import com.squareup.okhttp.MediaType;
2021

2122

2223
/**
23-
* Recognize Options when using the
24-
* {@link SpeechToText#recognize(java.io.File, String, RecognizeOptions)} method.
24+
* Parameters to be use during a recognize call in the {@link SpeechToText} service.
2525
*/
2626
public class RecognizeOptions {
2727

28+
2829
@SerializedName("content-type")
2930
private String contentType;
3031
private Boolean continuous;
3132
private Integer inactivityTimeout;
3233

3334
@SerializedName("interim_results")
3435
private Boolean interimResults;
35-
private List<String> keywords;
36+
private String[] keywords;
3637

3738
@SerializedName("keywords_threshold")
3839
private Double keywordsThreshold;
@@ -100,7 +101,7 @@ public Boolean getInterimResults() {
100101
*
101102
* @return the keywords
102103
*/
103-
public List<String> getKeywords() {
104+
public String[] getKeywords() {
104105
return keywords;
105106
}
106107

@@ -180,7 +181,7 @@ public RecognizeOptions inactivityTimeout(Integer inactivityTimeout) {
180181

181182
/**
182183
* If true, the service sends interim results for the transcription. Otherwise, the recognition
183-
* ends after first "end of speech" is detected. The default is false..
184+
* ends after first "end of speech" is detected. The default is false.
184185
*
185186
* @param interimResults the interim results
186187
* @return the recognize options
@@ -198,7 +199,7 @@ public RecognizeOptions interimResults(Boolean interimResults) {
198199
* @param keywords the keywords
199200
* @return the recognize options
200201
*/
201-
public RecognizeOptions keywords(List<String> keywords) {
202+
public RecognizeOptions keywords(String[] keywords) {
202203
this.keywords = keywords;
203204
return this;
204205
}
@@ -293,12 +294,20 @@ public RecognizeOptions wordAlternativesThreshold(Double wordAlternativesThresho
293294
}
294295

295296
/**
296-
* Content type.
297+
* The format of the audio data specified as one of the following values: <br>
298+
* <ul>
299+
* <li><code>audio/flac</code> for Free Lossless Audio Codec (FLAC)</li>
300+
* <li><code>audio/l16</code> for Linear 16-bit Pulse-Code Modulation (PCM)</li>
301+
* <li><code>audio/wav</code> for Waveform Audio File Format (WAV)</li>
302+
* <li><code>audio/ogg;codecs=opus</code> for Ogg format files that use the opus codec</li>
303+
* </ul>
297304
*
298305
* @param contentType the content type
299306
* @return the recognize options
300307
*/
301308
public RecognizeOptions contentType(String contentType) {
309+
Validate.isTrue(MediaType.parse(contentType) != null,
310+
"contentType is not a valid mime audio format. Valid formats start with 'audio/'");
302311
this.contentType = contentType;
303312
return this;
304313
}

0 commit comments

Comments
 (0)