Skip to content

Commit d8a1066

Browse files
committed
Revise handwritten speech to text methods
1 parent ad82c66 commit d8a1066

File tree

1 file changed

+97
-53
lines changed

1 file changed

+97
-53
lines changed

src/IBM.WatsonDeveloperCloud.SpeechToText.v1/SpeechToTextServiceExtension.cs

Lines changed: 97 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -231,71 +231,78 @@ private SpeechRecognitionResults Recognize(string sessionId, string contentType,
231231
try
232232
{
233233
string urlService = string.Empty;
234-
IRequest request = null;
234+
IRequest restRequest = null;
235+
236+
IClient client;
237+
if (_tokenManager == null)
238+
{
239+
client = this.Client.WithAuthentication(this.UserName, this.Password);
240+
241+
}
242+
else
243+
{
244+
client = this.Client.WithAuthentication(_tokenManager.GetToken());
245+
}
235246

236247
if (string.IsNullOrEmpty(sessionId))
237248
{
238-
request =
239-
this.Client.WithAuthentication(this.UserName, this.Password)
240-
.PostAsync($"{this.Endpoint}/v1/recognize");
249+
restRequest = client.PostAsync($"{this.Endpoint}/v1/recognize");
241250
}
242251
else
243252
{
244-
request =
245-
this.Client.WithAuthentication(this.UserName, this.Password)
246-
.PostAsync($"{this.Endpoint}/v1/sessions/{sessionId}")
253+
restRequest = client.PostAsync($"{this.Endpoint}/v1/sessions/{sessionId}")
247254
.WithHeader("Cookie", sessionId);
248255
}
249256

250257
if (!string.IsNullOrEmpty(transferEncoding))
251-
request.WithHeader("Transfer-Encoding", transferEncoding);
258+
restRequest.WithHeader("Transfer-Encoding", transferEncoding);
252259

253260
if (metaData == null)
254261
{
255262
// if a session exists, the model should not be sent
256263
if (string.IsNullOrEmpty(sessionId))
257-
request.WithArgument("model", model);
264+
restRequest.WithArgument("model", model);
258265

259266
if (!string.IsNullOrEmpty(customizationId))
260-
request.WithArgument("customization_id", customizationId);
267+
restRequest.WithArgument("customization_id", customizationId);
261268

262269
if (continuous.HasValue)
263-
request.WithArgument("continuous", continuous.Value);
270+
restRequest.WithArgument("continuous", continuous.Value);
264271

265272
if (inactivityTimeout.HasValue && inactivityTimeout.Value > 0)
266-
request.WithArgument("inactivity_timeout", inactivityTimeout.Value);
273+
restRequest.WithArgument("inactivity_timeout", inactivityTimeout.Value);
267274

268275
if (keywords != null && keywords.Length > 0)
269-
request.WithArgument("keywords", keywords);
276+
restRequest.WithArgument("keywords", keywords);
270277

271278
if (keywordsThreshold.HasValue && keywordsThreshold.Value > 0)
272-
request.WithArgument("keywords_threshold", keywordsThreshold.Value);
279+
restRequest.WithArgument("keywords_threshold", keywordsThreshold.Value);
273280

274281
if (maxAlternatives.HasValue && maxAlternatives.Value > 0)
275-
request.WithArgument("max_alternatives", maxAlternatives.Value);
282+
restRequest.WithArgument("max_alternatives", maxAlternatives.Value);
276283

277284
if (wordAlternativesThreshold.HasValue && wordAlternativesThreshold.Value > 0)
278-
request.WithArgument("word_alternatives_threshold", wordAlternativesThreshold.Value);
285+
restRequest.WithArgument("word_alternatives_threshold", wordAlternativesThreshold.Value);
279286

280287
if (wordConfidence.HasValue)
281-
request.WithArgument("word_confidence", wordConfidence.Value);
288+
restRequest.WithArgument("word_confidence", wordConfidence.Value);
282289

283290
if (timestamps.HasValue)
284-
request.WithArgument("timestamps", timestamps.Value);
291+
restRequest.WithArgument("timestamps", timestamps.Value);
285292

286293
if (profanityFilter)
287-
request.WithArgument("profanity_filter", profanityFilter);
294+
restRequest.WithArgument("profanity_filter", profanityFilter);
288295

289296
if (smartFormatting.HasValue)
290-
request.WithArgument("smart_formatting", smartFormatting.Value);
297+
restRequest.WithArgument("smart_formatting", smartFormatting.Value);
291298

292299
if (speakerLabels.HasValue)
293-
request.WithArgument("speaker_labels", speakerLabels.Value);
300+
restRequest.WithArgument("speaker_labels", speakerLabels.Value);
294301

295302
StreamContent bodyContent = new StreamContent(audio);
296303
bodyContent.Headers.Add("Content-Type", contentType);
297304

298-
request.WithBodyContent(bodyContent);
305+
restRequest.WithBodyContent(bodyContent);
299306
}
300307
else
301308
{
@@ -311,16 +318,15 @@ private SpeechRecognitionResults Recognize(string sessionId, string contentType,
311318

312319
// if a session exists, the model should not be sent
313320
if (string.IsNullOrEmpty(sessionId))
314-
request.WithArgument("model", model);
321+
restRequest.WithArgument("model", model);
315322

316323
formData.Add(metadata, "metadata");
317324
formData.Add(audioContent, "upload");
318325

319-
request.WithBodyContent(formData);
326+
restRequest.WithBodyContent(formData);
320327
}
321328

322-
result =
323-
request.As<SpeechRecognitionResults>()
329+
result = restRequest.As<SpeechRecognitionResults>()
324330
.Result;
325331

326332
}
@@ -350,18 +356,27 @@ public List<SpeechRecognitionResults> ObserveResult(string sessionId, int? seque
350356

351357
try
352358
{
353-
var request =
354-
this.Client.WithAuthentication(this.UserName, this.Password)
355-
.GetAsync($"{this.Endpoint}/v1/sessions/{sessionId}/observe_result");
359+
IRequest restRequest = null;
360+
IClient client;
361+
if (_tokenManager == null)
362+
{
363+
client = this.Client.WithAuthentication(this.UserName, this.Password);
364+
}
365+
else
366+
{
367+
client = this.Client.WithAuthentication(_tokenManager.GetToken());
368+
}
369+
370+
restRequest = client.GetAsync($"{this.Endpoint}/v1/sessions/{sessionId}/observe_result");
356371

357372
if (sequenceId.HasValue)
358-
request.WithArgument("sequence_id", sequenceId);
373+
restRequest.WithArgument("sequence_id", sequenceId);
359374

360375
if (interimResults)
361-
request.WithArgument("interim_results", interimResults);
376+
restRequest.WithArgument("interim_results", interimResults);
362377

363378
var strResult =
364-
request.AsString()
379+
restRequest.AsString()
365380
.Result;
366381
var serializer = new JsonSerializer();
367382

@@ -403,18 +418,28 @@ public SpeechSession CreateSession(string model, string customizationId = null,
403418
{
404419
if (string.IsNullOrEmpty(model))
405420
throw new ArgumentNullException(nameof(model));
406-
407-
var request = this.Client.WithAuthentication(this.UserName, this.Password)
408-
.PostAsync($"{this.Endpoint}/v1/sessions");
409-
request.WithArgument("model", model);
421+
422+
IRequest restRequest = null;
423+
IClient client;
424+
if (_tokenManager == null)
425+
{
426+
client = this.Client.WithAuthentication(this.UserName, this.Password);
427+
}
428+
else
429+
{
430+
client = this.Client.WithAuthentication(_tokenManager.GetToken());
431+
}
432+
433+
restRequest = client.PostAsync($"{this.Endpoint}/v1/sessions");
434+
restRequest.WithArgument("model", model);
410435
if (!string.IsNullOrEmpty(customizationId))
411-
request.WithArgument("customization_id", customizationId);
436+
restRequest.WithArgument("customization_id", customizationId);
412437
if (!string.IsNullOrEmpty(acousticCustomizationId))
413-
request.WithArgument("acoustic_customization_id", acousticCustomizationId);
438+
restRequest.WithArgument("acoustic_customization_id", acousticCustomizationId);
414439
if (!string.IsNullOrEmpty(baseModelVersion))
415-
request.WithArgument("base_model_version", baseModelVersion);
416-
request.WithHeader("accept", HttpMediaType.APPLICATION_JSON);
417-
result = request.As<SpeechSession>().Result;
440+
restRequest.WithArgument("base_model_version", baseModelVersion);
441+
restRequest.WithHeader("accept", HttpMediaType.APPLICATION_JSON);
442+
result = restRequest.As<SpeechSession>().Result;
418443
}
419444
catch (AggregateException ae)
420445
{
@@ -448,13 +473,23 @@ public SessionStatus GetSessionStatus(string sessionId)
448473

449474
try
450475
{
451-
result =
452-
this.Client.WithAuthentication(this.UserName, this.Password)
453-
.GetAsync($"{this.Endpoint}{string.Format("/v1/sessions/{0}/recognize", sessionId)}")
454-
.WithHeader("Cookie", sessionId)
455-
.WithHeader("accept", HttpMediaType.APPLICATION_JSON)
456-
.As<SessionStatus>()
457-
.Result;
476+
IRequest restRequest = null;
477+
478+
IClient client;
479+
if (_tokenManager == null)
480+
{
481+
client = this.Client.WithAuthentication(this.UserName, this.Password);
482+
}
483+
else
484+
{
485+
client = this.Client.WithAuthentication(_tokenManager.GetToken());
486+
}
487+
488+
restRequest = client.GetAsync($"{this.Endpoint}{string.Format("/v1/sessions/{0}/recognize", sessionId)}");
489+
restRequest.WithHeader("Cookie", sessionId);
490+
restRequest.WithHeader("accept", HttpMediaType.APPLICATION_JSON);
491+
result = restRequest.As<SessionStatus>().Result;
492+
458493
}
459494
catch (AggregateException ae)
460495
{
@@ -488,11 +523,20 @@ public object DeleteSession(string sessionId)
488523

489524
try
490525
{
491-
result =
492-
this.Client.WithAuthentication(this.UserName, this.Password)
493-
.DeleteAsync(string.Format("{0}{1}/{2}", this.Endpoint, "/v1/sessions", sessionId))
494-
.AsMessage()
495-
.Result;
526+
IRequest restRequest = null;
527+
IClient client;
528+
if (_tokenManager == null)
529+
{
530+
client = this.Client.WithAuthentication(this.UserName, this.Password);
531+
}
532+
else
533+
{
534+
client = this.Client.WithAuthentication(_tokenManager.GetToken());
535+
}
536+
537+
restRequest = client.DeleteAsync(string.Format("{0}{1}/{2}", this.Endpoint, "/v1/sessions", sessionId));
538+
539+
result = restRequest.AsMessage().Result;
496540
}
497541
catch (AggregateException ae)
498542
{

0 commit comments

Comments
 (0)