Skip to content

Commit ddd25b7

Browse files
committed
chore(speech to text v1): Added grammars support
1 parent bf89c0a commit ddd25b7

File tree

3 files changed

+574
-14
lines changed

3 files changed

+574
-14
lines changed

Examples/ServiceExamples/Scripts/ExampleSpeechToText.cs

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class ExampleSpeechToText : MonoBehaviour
5858
private byte[] _oggResourceData;
5959
private string _oggResourceMimeType;
6060
private bool _isOggLoaded = false;
61+
private string _grammarFilePath;
6162

6263
private bool _recognizeTested = false;
6364
private bool _recognizeOggTested = false;
@@ -94,11 +95,20 @@ public class ExampleSpeechToText : MonoBehaviour
9495
private bool _readyToContinue = false;
9596
private float _delayTimeInSeconds = 10f;
9697

98+
private bool _listGrammarsTested = false;
99+
private bool _addGrammarTested = false;
100+
private bool _getGrammarTested = false;
101+
private bool _deleteGrammarTested = false;
102+
private string _createdGrammarId;
103+
private string _grammarFileContentType = "application/srgs";
104+
private string _grammarName = "unity-integration-test-grammar";
105+
97106
void Start()
98107
{
99108
LogSystem.InstallDefaultReactors();
100109
_customCorpusFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/speech-to-text/theJabberwocky-utf8.txt";
101110
_customWordsFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/speech-to-text/test-stt-words.json";
111+
_grammarFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/speech-to-text/confirm.abnf";
102112
_acousticResourceMimeType = Utility.GetMimeType(Path.GetExtension(_acousticResourceUrl));
103113
_oggResourceMimeType = Utility.GetMimeType(Path.GetExtension(_oggResourceUrl));
104114
Runnable.Run(CreateService());
@@ -285,12 +295,6 @@ private IEnumerator Examples()
285295
while (!_isCustomizationReady)
286296
yield return null;
287297

288-
// Upgrade customization - not currently implemented in service
289-
//Log.Debug("ExampleSpeechToText.Examples()", "Attempting to upgrade customization {0}", _createdCustomizationID);
290-
//_speechToText.UpgradeCustomization(HandleUpgradeCustomization, _createdCustomizationID);
291-
//while (!_upgradeCustomizationTested)
292-
// yield return null;
293-
294298
// Delete custom word
295299
Log.Debug("ExampleSpeechToText.Examples()", "Attempting to delete custom word {1} in customization {0}", _createdCustomizationID, words.words[2].word);
296300
_service.DeleteCustomWord(HandleDeleteCustomWord, OnFail, _createdCustomizationID, words.words[2].word);
@@ -329,6 +333,37 @@ private IEnumerator Examples()
329333
while (!_readyToContinue)
330334
yield return null;
331335

336+
// List Grammars
337+
Log.Debug("TestSpeechToText.Examples()", "Attempting to list grammars {0}", _createdCustomizationID);
338+
_service.ListGrammars(OnListGrammars, OnFail, _createdCustomizationID);
339+
while (!_listGrammarsTested)
340+
yield return null;
341+
342+
// Add Grammar
343+
Log.Debug("TestSpeechToText.Examples()", "Attempting to add grammar {0}", _createdCustomizationID);
344+
string grammarFile = File.ReadAllText(_grammarFilePath);
345+
_service.AddGrammar(OnAddGrammar, OnFail, _createdCustomizationID, _grammarName, grammarFile, _grammarFileContentType);
346+
while (!_addGrammarTested)
347+
yield return null;
348+
349+
// Get Grammar
350+
Log.Debug("TestSpeechToText.Examples()", "Attempting to get grammar {0}", _createdCustomizationID);
351+
_service.GetGrammar(OnGetGrammar, OnFail, _createdCustomizationID, _grammarName);
352+
while (!_getGrammarTested)
353+
yield return null;
354+
355+
// Wait for customization
356+
_isCustomizationReady = false;
357+
Runnable.Run(CheckCustomizationStatus(_createdCustomizationID));
358+
while (!_isCustomizationReady)
359+
yield return null;
360+
361+
// Delete Grammar
362+
Log.Debug("TestSpeechToText.Examples()", "Attempting to delete grammar {0}", _createdCustomizationID);
363+
_service.DeleteGrammar(OnDeleteGrammar, OnFail, _createdCustomizationID, _grammarName);
364+
while (!_deleteGrammarTested)
365+
yield return null;
366+
332367
_readyToContinue = false;
333368
// Delete customization
334369
Log.Debug("ExampleSpeechToText.Examples()", "Attempting to delete customization {0}", _createdCustomizationID);
@@ -639,6 +674,30 @@ private void HandleDeleteAcousticCustomization(bool success, Dictionary<string,
639674
DeleteAcousticCustomization();
640675
}
641676

677+
private void OnListGrammars(Grammars response, Dictionary<string, object> customData)
678+
{
679+
Log.Debug("ExampleSpeechToText.OnListGrammars()", "{0}", customData["json"].ToString());
680+
_listGrammarsTested = true;
681+
}
682+
683+
private void OnAddGrammar(object response, Dictionary<string, object> customData)
684+
{
685+
Log.Debug("ExampleSpeechToText.OnAddGrammar()", "Success!");
686+
_addGrammarTested = true;
687+
}
688+
689+
private void OnGetGrammar(Grammar response, Dictionary<string, object> customData)
690+
{
691+
Log.Debug("ExampleSpeechToText.OnGetGrammar()", "{0}", customData["json"].ToString());
692+
_getGrammarTested = true;
693+
}
694+
695+
private void OnDeleteGrammar(object response, Dictionary<string, object> customData)
696+
{
697+
Log.Debug("ExampleSpeechToText.OnDeleteGrammar()", "Success!");
698+
_deleteGrammarTested = true;
699+
}
700+
642701
private IEnumerator CheckCustomizationStatus(string customizationID, float delay = 0.1f)
643702
{
644703
Log.Debug("ExampleSpeechToText.CheckCustomizationStatus()", "Checking customization status in {0} seconds...", delay.ToString());

0 commit comments

Comments
 (0)