Skip to content

Commit b89709f

Browse files
committed
fix(Speech to Text V1): Parsing response to double because of casting issues
1 parent 9be22a0 commit b89709f

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

Scripts/Services/SpeechToText/v1/SpeechToText.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,10 +1176,10 @@ private SpeakerRecognitionEvent ParseSpeakerRecognitionResponse(IDictionary resp
11761176
continue;
11771177

11781178
SpeakerLabelsResult result = new SpeakerLabelsResult();
1179-
result.confidence = (double)iresult["confidence"];
1179+
result.confidence = Utility.StringToDouble(iresult["confidence"].ToString());
11801180
result.final = (bool)iresult["final"];
1181-
result.from = (double)iresult["from"];
1182-
result.to = (double)iresult["to"];
1181+
result.from = Utility.StringToDouble(iresult["from"].ToString());
1182+
result.to = Utility.StringToDouble(iresult["to"].ToString());
11831183
result.speaker = (Int64)iresult["speaker"];
11841184

11851185
results.Add(result);
@@ -1229,9 +1229,9 @@ private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp)
12291229

12301230
WordAlternativeResults wordAlternativeResults = new WordAlternativeResults();
12311231
if (iwordAlternative.Contains("start_time"))
1232-
wordAlternativeResults.start_time = (double)iwordAlternative["start_time"];
1232+
wordAlternativeResults.start_time = Utility.StringToDouble(iwordAlternative["start_time"].ToString());
12331233
if (iwordAlternative.Contains("end_time"))
1234-
wordAlternativeResults.end_time = (double)iwordAlternative["end_time"];
1234+
wordAlternativeResults.end_time = Utility.StringToDouble(iwordAlternative["end_time"].ToString());
12351235
if (iwordAlternative.Contains("alternatives"))
12361236
{
12371237
List<WordAlternativeResult> wordAlternativeResultList = new List<WordAlternativeResult>();
@@ -1246,7 +1246,7 @@ private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp)
12461246
if (ialternative.Contains("word"))
12471247
wordAlternativeResult.word = (string)ialternative["word"];
12481248
if (ialternative.Contains("confidence"))
1249-
wordAlternativeResult.confidence = (double)ialternative["confidence"];
1249+
wordAlternativeResult.confidence = Utility.StringToDouble(ialternative["confidence"].ToString());
12501250
wordAlternativeResultList.Add(wordAlternativeResult);
12511251
}
12521252

@@ -1273,7 +1273,7 @@ private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp)
12731273
SpeechRecognitionAlternative alternative = new SpeechRecognitionAlternative();
12741274
alternative.transcript = (string)ialternative["transcript"];
12751275
if (ialternative.Contains("confidence"))
1276-
alternative.confidence = (double)ialternative["confidence"];
1276+
alternative.confidence = Utility.StringToDouble(ialternative["confidence"].ToString());
12771277

12781278
if (ialternative.Contains("timestamps"))
12791279
{
@@ -1288,8 +1288,8 @@ private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp)
12881288

12891289
TimeStamp ts = new TimeStamp();
12901290
ts.Word = (string)itimestamp[0];
1291-
ts.Start = (double)itimestamp[1];
1292-
ts.End = (double)itimestamp[2];
1291+
ts.Start = Utility.StringToDouble(itimestamp[1].ToString());
1292+
ts.End = Utility.StringToDouble(itimestamp[2].ToString());
12931293
timestamps[i] = ts;
12941294
}
12951295

@@ -1307,9 +1307,7 @@ private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp)
13071307

13081308
WordConfidence wc = new WordConfidence();
13091309
wc.Word = (string)iwordconf[0];
1310-
string wordConf = iwordconf[1].ToString();
1311-
double.TryParse(wordConf, out double wordConfDouble);
1312-
wc.Confidence = wordConfDouble;
1310+
wc.Confidence = Utility.StringToDouble(iwordconf[1].ToString());
13131311
confidence[i] = wc;
13141312
}
13151313

@@ -1340,9 +1338,9 @@ private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp)
13401338
IDictionary iKeywordDictionary = k as IDictionary;
13411339
KeywordResult keywordResult = new KeywordResult();
13421340
keywordResult.keyword = keyword;
1343-
keywordResult.confidence = (double)iKeywordDictionary["confidence"];
1344-
keywordResult.end_time = (double)iKeywordDictionary["end_time"];
1345-
keywordResult.start_time = (double)iKeywordDictionary["start_time"];
1341+
keywordResult.confidence = Utility.StringToDouble(iKeywordDictionary["confidence"].ToString());
1342+
keywordResult.end_time = Utility.StringToDouble(iKeywordDictionary["end_time"].ToString());
1343+
keywordResult.start_time = Utility.StringToDouble(iKeywordDictionary["start_time"].ToString());
13461344
keywordResult.normalized_text = (string)iKeywordDictionary["normalized_text"];
13471345
keywordResults.Add(keywordResult);
13481346
}

Scripts/Utilities/Utility.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,15 @@ public static List<string> GetCredentialsPaths()
13491349
return filePathsToLoad;
13501350
}
13511351
#endregion
1352+
1353+
#region String to Double
1354+
public static double StringToDouble(string input)
1355+
{
1356+
double output;
1357+
double.TryParse(input, out output);
1358+
return output;
1359+
}
1360+
#endregion
13521361
}
13531362

13541363
/// <summary>

0 commit comments

Comments
 (0)