Last active
October 12, 2018 12:37
-
-
Save akeller/4c45ab3fd4438667010c47f7f604d556 to your computer and use it in GitHub Desktop.
Watson Speech-to-Text create custom language model when Watson just doesn't hear you right. Designed specifically for Unity when using STT, TTS, and Assistant (STT and TTS have some naming conflicts)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//I recommend running this only once so you only create one customizationID. | |
//It takes some time for each step to get to a ready state, so I recommend doing this outside of your game if possible and just referencing the customizationID after training. | |
//Be sure to use your customizationID when calling Active (SDK example) when you set your other STT params. | |
private IEnumerator CreateGameBoardModel(){ | |
Debug.Log("Inside CreateGameBoardModel"); | |
//Create customization | |
_speechToText.CreateCustomization(HandleCreateCustomization, OnFail, "unity-game-board", "en-US_BroadbandModel", "Adding game board domain items"); | |
while (!_createCustomizationsAdded) | |
yield return null; | |
_createdCustomizationID = "<YOUR CUSTOMIZATION ID GOES HERE>"; | |
// Wait for customization | |
_isCustomizationReady = false; | |
Runnable.Run(CheckCustomizationStatus(_createdCustomizationID)); | |
while (!_isCustomizationReady) | |
yield return null; | |
// Add custom words from object | |
IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.Words words = new IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.Words(); | |
IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.Word w0 = new IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.Word(); | |
List<IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.Word> wordList = new List<IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.Word>(); | |
w0.word = "B4"; | |
w0.sounds_like = new string[1]; | |
w0.sounds_like[0] = "be for"; | |
w0.display_as = "B4"; | |
wordList.Add(w0); | |
IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.Word w1 = new IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.Word(); | |
w1.word = "C4"; | |
w1.sounds_like = new string[1]; | |
w1.sounds_like[0] = "see for"; | |
w1.display_as = "C4"; | |
wordList.Add(w1); | |
words.words = wordList.ToArray(); | |
Debug.Log("Added words? " + wordList.ToArray().ToString()); | |
Log.Debug("ExampleSpeechToText.Examples()", "Attempting to add custom words in customization {0} using Words object", _createdCustomizationID); | |
_speechToText.AddCustomWords(HandleAddCustomWordsFromObject, OnFail, _createdCustomizationID, words); | |
while (!_addCustomWordsFromObject) | |
yield return null; | |
Debug.Log("Words added"); | |
//// Wait for customization | |
_isCustomizationReady = false; | |
Runnable.Run(CheckCustomizationStatus(_createdCustomizationID)); | |
while (!_isCustomizationReady) | |
yield return null; | |
// Train customization | |
Log.Debug("ExampleSpeechToText.Examples()", "Attempting to train customization {0}", _createdCustomizationID); | |
_speechToText.TrainCustomization(HandleTrainCustomization, OnFail, _createdCustomizationID); | |
while (!_trainCustomization) | |
yield return null; | |
Debug.Log("Training complete"); | |
//// Wait for customization | |
_isCustomizationReady = false; | |
Runnable.Run(CheckCustomizationStatus(_createdCustomizationID)); | |
while (!_isCustomizationReady) | |
yield return null; | |
} | |
private IEnumerator CheckCustomizationStatus(string customizationID, float delay = 0.1f) | |
{ | |
Log.Debug("ExampleSpeechToText.CheckCustomizationStatus()", "Checking customization status in {0} seconds...", delay.ToString()); | |
yield return new WaitForSeconds(delay); | |
// passing customizationID in custom data | |
Dictionary<string, object> customData = new Dictionary<string, object>(); | |
customData["customizationID"] = customizationID; | |
_speechToText.GetCustomization(OnCheckCustomizationStatus, OnFail, customizationID, customData); | |
} | |
private void OnCheckCustomizationStatus(Customization customization, Dictionary<string, object> customData) | |
{ | |
if (customization != null) | |
{ | |
Log.Debug("ExampleSpeechToText.OnCheckCustomizationStatus()", "Customization status: {0}", customization.status); | |
if (customization.status != "ready" && customization.status != "available") | |
Runnable.Run(CheckCustomizationStatus(customData["customizationID"].ToString(), 5f)); | |
else | |
_isCustomizationReady = true; | |
} | |
else | |
{ | |
Log.Debug("ExampleSpeechToText.OnCheckCustomizationStatus()", "Check customization status failed!"); | |
} | |
} | |
//Make sure to reference your custom model by calling it in the Active method (based on the example in the Watson SDK For Unity) | |
// public bool Active | |
// { | |
// get { return _speechToText.IsListening; } | |
// set | |
// { | |
// if (value && !_speechToText.IsListening) | |
// { | |
// _speechToText.CustomizationId = <YOUR_CUSTOMIZATION_ID_HERE>; | |
// _speechToText.DetectSilence = true; | |
// _speechToText.EnableWordConfidence = false; | |
// _speechToText.EnableTimestamps = false; | |
// _speechToText.SilenceThreshold = 0.03f; | |
// _speechToText.MaxAlternatives = 1; | |
// _speechToText.EnableInterimResults = true; | |
// _speechToText.OnError = OnError; | |
// _speechToText.StartListening(OnRecognize); | |
// } | |
// else if (!value && _speechToText.IsListening) | |
// { | |
// _speechToText.StopListening(); | |
// } | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment