private void OnRecognize(Data data) { SpeechResultList result = ((SpeechToTextData)data).Results; if (result.HasFinalResult()) { string text = result.Results[0].Alternatives[0].Transcript; double textConfidence = result.Results[0].Alternatives[0].Confidence; Log.Debug("NlcWidget", "OnRecognize: {0} ({1:0.00})", text, textConfidence); EventManager.Instance.SendEvent(Constants.Event.ON_DEBUG_MESSAGE, string.Format("{0} ({1:0.00})", text, textConfidence)); if (textConfidence > m_MinWordConfidence) { if (!string.IsNullOrEmpty(m_ClassifierId)) { if (!m_NLC.Classify(m_ClassifierId, text, OnClassified)) { Log.Error("NlcWidget", "Failed to send {0} to NLC.", text); } } else { Log.Equals("NlcWidget", "No valid classifier set."); } } else { if (textConfidence > m_IgnoreWordConfidence) { EventManager.Instance.SendEvent(Constants.Event.ON_CLASSIFY_FAILURE, result); } } } }
private void OnRecognize(Data data) { SpeechResultList result = ((SpeechToTextData)data).Results; if (result.HasFinalResult()) { string text = result.Results[0].Alternatives[0].Transcript; double textConfidence = result.Results[0].Alternatives[0].Confidence; Log.Debug("NaturalLanguageClassifierWidget", "OnRecognize: {0} ({1:0.00})", text, textConfidence); EventManager.Instance.SendEvent("OnDebugMessage", string.Format("{0} ({1:0.00})", text, textConfidence)); if (textConfidence > MinWordConfidence) { if (!string.IsNullOrEmpty(m_ClassifierId)) { if (!m_NaturalLanguageClassifier.Classify(m_ClassifierId, text, OnClassified)) { Log.Error("NaturalLanguageClassifierWidget", "Failed to send {0} to Natural Language Classifier.", text); } } else { Log.Equals("NaturalLanguageClassifierWidget", "No valid classifier set."); } } else { Log.Debug("NaturalLanguagClassifierWidget", "Text confidence {0} < {1} (Min word confidence)", textConfidence, MinWordConfidence); if (textConfidence > IgnoreWordConfidence) { Log.Debug("NaturalLanguageClassifierWidget", "Text confidence {0} > {1} (Ignore word confidence)", textConfidence, IgnoreWordConfidence); EventManager.Instance.SendEvent("OnClassifyFailure", result); } } } }
public override IEnumerator RunTest() { if (Config.Instance.FindCredentials(m_NLC.GetServiceID()) == null) { yield break; } m_NLC.FindClassifier("TestNLC/", OnFindClassifier); while (!m_FindClassifierTested) { yield return(null); } if (m_TrainClassifier) { string trainingData = File.ReadAllText(Application.dataPath + "/Watson/Editor/TestData/weather_data_train.csv"); Test(m_NLC.TrainClassifier("TestNLC/" + DateTime.Now.ToString(), "en", trainingData, OnTrainClassifier)); while (!m_TrainClasifierTested) { yield return(null); } } else if (!string.IsNullOrEmpty(m_ClassifierId)) { Test(m_NLC.Classify(m_ClassifierId, "Is it hot outside", OnClassify)); while (!m_ClassifyTested) { yield return(null); } } #if TEST_DELETE if (!string.IsNullOrEmpty(m_ClassifierId)) { Test(m_NLC.DeleteClassifer(m_ClassifierId, OnDeleteClassifier)); while (!m_DeleteTested) { yield return(null); } } #endif yield break; }
private IEnumerator Examples() { // Get classifiers if (!_service.GetClassifiers(OnGetClassifiers, OnFail)) { Log.Debug("ExampleNaturalLanguageClassifier.GetClassifiers()", "Failed to get classifiers!"); } while (!_getClassifiersTested) { yield return(null); } if (_classifierIds.Count == 0) { Log.Debug("ExampleNaturalLanguageClassifier.Examples()", "There are no trained classifiers. Please train a classifier..."); } if (_classifierIds.Count > 0) { // Get each classifier foreach (string classifierId in _classifierIds) { if (!_service.GetClassifier(OnGetClassifier, OnFail, classifierId)) { Log.Debug("ExampleNaturalLanguageClassifier.GetClassifier()", "Failed to get classifier {0}!", classifierId); } } while (!_getClassifierTested) { yield return(null); } } if (!_areAnyClassifiersAvailable && _classifierIds.Count > 0) { Log.Debug("ExampleNaturalLanguageClassifier.Examples()", "All classifiers are training..."); } // Train classifier #if TRAIN_CLASSIFIER string dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/weather_data_train.csv"; var trainingContent = File.ReadAllText(dataPath); if (!_service.TrainClassifier(OnTrainClassifier, OnFail, _classifierName + "/" + DateTime.Now.ToString(), "en", trainingContent)) { Log.Debug("ExampleNaturalLanguageClassifier.TrainClassifier()", "Failed to train clasifier!"); } while (!_trainClassifierTested) { yield return(null); } #endif #if DELETE_TRAINED_CLASSIFIER if (!string.IsNullOrEmpty(_classifierToDelete)) { if (!_service.DeleteClassifer(OnDeleteTrainedClassifier, OnFail, _classifierToDelete)) { Log.Debug("ExampleNaturalLanguageClassifier.DeleteClassifer()", "Failed to delete clasifier {0}!", _classifierToDelete); } } #endif // Classify if (_areAnyClassifiersAvailable) { if (!_service.Classify(OnClassify, OnFail, _classifierId, _inputString)) { Log.Debug("ExampleNaturalLanguageClassifier.Classify()", "Failed to classify!"); } while (!_classifyTested) { yield return(null); } } // Classify Collection ClassifyCollectionInput classifyCollectionInput = new ClassifyCollectionInput() { collection = new List <ClassifyInput>() { new ClassifyInput() { text = "Is it hot outside?" }, new ClassifyInput() { text = "Is it going to rain?" } } }; if (_areAnyClassifiersAvailable) { if (!_service.ClassifyCollection(OnClassifyCollection, OnFail, _classifierId, classifyCollectionInput)) { Log.Debug("ExampleNaturalLanguageClassifier.ClassifyCollection()", "Failed to classify!"); } while (!_classifyCollectionTested) { yield return(null); } } Log.Debug("ExampleNaturalLanguageClassifier.Examples()", "Natural language classifier examples complete."); }
public override IEnumerator RunTest() { LogSystem.InstallDefaultReactors(); // Test NaturalLanguageClassifier using loaded credentials NaturalLanguageClassifier autoNaturalLanguageClassifier = new NaturalLanguageClassifier(); while (!autoNaturalLanguageClassifier.Credentials.HasIamTokenData()) { yield return(null); } autoNaturalLanguageClassifier.GetClassifiers(OnAutoGetClassifiers, OnFail); while (!_autoGetClassifiersTested) { yield return(null); } VcapCredentials vcapCredentials = new VcapCredentials(); fsData data = null; string result = null; string credentialsFilepath = "../sdk-credentials/credentials.json"; // Load credentials file if it exists. If it doesn't exist, don't run the tests. if (File.Exists(credentialsFilepath)) { result = File.ReadAllText(credentialsFilepath); } else { yield break; } // Add in a parent object because Unity does not like to deserialize root level collection types. result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES"); // Convert json to fsResult fsResult r = fsJsonParser.Parse(result, out data); if (!r.Succeeded) { throw new WatsonException(r.FormattedMessages); } // Convert fsResult to VcapCredentials object obj = vcapCredentials; r = _serializer.TryDeserialize(data, obj.GetType(), ref obj); if (!r.Succeeded) { throw new WatsonException(r.FormattedMessages); } // Set credentials from imported credntials Credential credential = vcapCredentials.GetCredentialByname("natural-language-classifier-sdk")[0].Credentials; // Create credential and instantiate service TokenOptions tokenOptions = new TokenOptions() { IamApiKey = credential.IamApikey, }; // Create credential and instantiate service Credentials credentials = new Credentials(tokenOptions, credential.Url); // Wait for tokendata while (!credentials.HasIamTokenData()) { yield return(null); } naturalLanguageClassifier = new NaturalLanguageClassifier(credentials); // Get classifiers if (!naturalLanguageClassifier.GetClassifiers(OnGetClassifiers, OnFail)) { Log.Debug("TestNaturalLanguageClassifier.GetClassifiers()", "Failed to get classifiers!"); } while (!_getClassifiersTested) { yield return(null); } if (_classifierIds.Count == 0) { Log.Debug("TestNaturalLanguageClassifier.Examples()", "There are no trained classifiers. Please train a classifier..."); } if (_classifierIds.Count > 0) { // Get each classifier foreach (string classifierId in _classifierIds) { if (!naturalLanguageClassifier.GetClassifier(OnGetClassifier, OnFail, classifierId)) { Log.Debug("TestNaturalLanguageClassifier.GetClassifier()", "Failed to get classifier {0}!", classifierId); } } while (!_getClassifierTested) { yield return(null); } } if (!_areAnyClassifiersAvailable && _classifierIds.Count > 0) { Log.Debug("TestNaturalLanguageClassifier.Examples()", "All classifiers are training..."); } // Train classifier #if TRAIN_CLASSIFIER string dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/weather_data_train.csv"; var trainingContent = File.ReadAllText(dataPath); if (!naturalLanguageClassifier.TrainClassifier(OnTrainClassifier, OnFail, _classifierName + "/" + DateTime.Now.ToString(), "en", trainingContent)) { Log.Debug("TestNaturalLanguageClassifier.TrainClassifier()", "Failed to train clasifier!"); } while (!_trainClassifierTested) { yield return(null); } #endif #if DELETE_TRAINED_CLASSIFIER if (!string.IsNullOrEmpty(_classifierToDelete)) { if (!naturalLanguageClassifier.DeleteClassifer(OnDeleteTrainedClassifier, OnFail, _classifierToDelete)) { Log.Debug("TestNaturalLanguageClassifier.DeleteClassifer()", "Failed to delete clasifier {0}!", _classifierToDelete); } } #endif // Classify if (_areAnyClassifiersAvailable) { if (!naturalLanguageClassifier.Classify(OnClassify, OnFail, _classifierId, _inputString)) { Log.Debug("TestNaturalLanguageClassifier.Classify()", "Failed to classify!"); } while (!_classifyTested) { yield return(null); } } // Classify Collection ClassifyCollectionInput classifyCollectionInput = new ClassifyCollectionInput() { collection = new List <ClassifyInput>() { new ClassifyInput() { text = "Is it hot outside?" }, new ClassifyInput() { text = "Is it going to rain?" } } }; if (_areAnyClassifiersAvailable) { if (!naturalLanguageClassifier.ClassifyCollection(OnClassifyCollection, OnFail, _classifierId, classifyCollectionInput)) { Log.Debug("TestNaturalLanguageClassifier.ClassifyCollection()", "Failed to classify!"); } while (!_classifyCollectionTested) { yield return(null); } } Log.Debug("TestNaturalLanguageClassifier.RunTest()", "Natural language classifier examples complete."); yield break; }
void Start() { Debug.Log("Input String: " + m_InputString); m_NaturalLanguageClassifier.Classify(m_ClassifierId, m_InputString, OnClassify); }
public override IEnumerator RunTest() { LogSystem.InstallDefaultReactors(); VcapCredentials vcapCredentials = new VcapCredentials(); fsData data = null; string result = null; var vcapUrl = Environment.GetEnvironmentVariable("VCAP_URL"); var vcapUsername = Environment.GetEnvironmentVariable("VCAP_USERNAME"); var vcapPassword = Environment.GetEnvironmentVariable("VCAP_PASSWORD"); using (SimpleGet simpleGet = new SimpleGet(vcapUrl, vcapUsername, vcapPassword)) { while (!simpleGet.IsComplete) { yield return(null); } result = simpleGet.Result; } // Add in a parent object because Unity does not like to deserialize root level collection types. result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES"); // Convert json to fsResult fsResult r = fsJsonParser.Parse(result, out data); if (!r.Succeeded) { throw new WatsonException(r.FormattedMessages); } // Convert fsResult to VcapCredentials object obj = vcapCredentials; r = _serializer.TryDeserialize(data, obj.GetType(), ref obj); if (!r.Succeeded) { throw new WatsonException(r.FormattedMessages); } // Set credentials from imported credntials Credential credential = vcapCredentials.VCAP_SERVICES["natural_language_classifier"]; _username = credential.Username.ToString(); _password = credential.Password.ToString(); _url = credential.Url.ToString(); // Create credential and instantiate service Credentials credentials = new Credentials(_username, _password, _url); // Or authenticate using token //Credentials credentials = new Credentials(_url) //{ // AuthenticationToken = _token //}; naturalLanguageClassifier = new NaturalLanguageClassifier(credentials); // Get classifiers if (!naturalLanguageClassifier.GetClassifiers(OnGetClassifiers, OnFail)) { Log.Debug("TestNaturalLanguageClassifier.GetClassifiers()", "Failed to get classifiers!"); } while (!_getClassifiersTested) { yield return(null); } if (_classifierIds.Count == 0) { Log.Debug("TestNaturalLanguageClassifier.Examples()", "There are no trained classifiers. Please train a classifier..."); } if (_classifierIds.Count > 0) { // Get each classifier foreach (string classifierId in _classifierIds) { if (!naturalLanguageClassifier.GetClassifier(OnGetClassifier, OnFail, classifierId)) { Log.Debug("TestNaturalLanguageClassifier.GetClassifier()", "Failed to get classifier {0}!", classifierId); } } while (!_getClassifierTested) { yield return(null); } } if (!_areAnyClassifiersAvailable && _classifierIds.Count > 0) { Log.Debug("TestNaturalLanguageClassifier.Examples()", "All classifiers are training..."); } // Train classifier #if TRAIN_CLASSIFIER string dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/weather_data_train.csv"; var trainingContent = File.ReadAllText(dataPath); if (!naturalLanguageClassifier.TrainClassifier(OnTrainClassifier, OnFail, _classifierName + "/" + DateTime.Now.ToString(), "en", trainingContent)) { Log.Debug("TestNaturalLanguageClassifier.TrainClassifier()", "Failed to train clasifier!"); } while (!_trainClassifierTested) { yield return(null); } #endif #if DELETE_TRAINED_CLASSIFIER if (!string.IsNullOrEmpty(_classifierToDelete)) { if (!naturalLanguageClassifier.DeleteClassifer(OnDeleteTrainedClassifier, OnFail, _classifierToDelete)) { Log.Debug("TestNaturalLanguageClassifier.DeleteClassifer()", "Failed to delete clasifier {0}!", _classifierToDelete); } } #endif // Classify if (_areAnyClassifiersAvailable) { if (!naturalLanguageClassifier.Classify(OnClassify, OnFail, _classifierId, _inputString)) { Log.Debug("TestNaturalLanguageClassifier.Classify()", "Failed to classify!"); } while (!_classifyTested) { yield return(null); } } // Classify Collection ClassifyCollectionInput classifyCollectionInput = new ClassifyCollectionInput() { collection = new List <ClassifyInput>() { new ClassifyInput() { text = "Is it hot outside?" }, new ClassifyInput() { text = "Is it going to rain?" } } }; if (_areAnyClassifiersAvailable) { if (!naturalLanguageClassifier.ClassifyCollection(OnClassifyCollection, OnFail, _classifierId, classifyCollectionInput)) { Log.Debug("TestNaturalLanguageClassifier.ClassifyCollection()", "Failed to classify!"); } while (!_classifyCollectionTested) { yield return(null); } } Log.Debug("TestNaturalLanguageClassifier.RunTest()", "Natural language classifier examples complete."); yield break; }
public override IEnumerator RunTest() { LogSystem.InstallDefaultReactors(); try { VcapCredentials vcapCredentials = new VcapCredentials(); fsData data = null; // Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format. // See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html. var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES"); var fileContent = File.ReadAllText(environmentalVariable); // Add in a parent object because Unity does not like to deserialize root level collection types. fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES"); // Convert json to fsResult fsResult r = fsJsonParser.Parse(fileContent, out data); if (!r.Succeeded) { throw new WatsonException(r.FormattedMessages); } // Convert fsResult to VcapCredentials object obj = vcapCredentials; r = _serializer.TryDeserialize(data, obj.GetType(), ref obj); if (!r.Succeeded) { throw new WatsonException(r.FormattedMessages); } // Set credentials from imported credntials Credential credential = vcapCredentials.VCAP_SERVICES["natural_language_classifier"][TestCredentialIndex].Credentials; _username = credential.Username.ToString(); _password = credential.Password.ToString(); _url = credential.Url.ToString(); } catch { Log.Debug("TestNaturalLanguageClassifier", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication"); } // Create credential and instantiate service Credentials credentials = new Credentials(_username, _password, _url); // Or authenticate using token //Credentials credentials = new Credentials(_url) //{ // AuthenticationToken = _token //}; naturalLanguageClassifier = new NaturalLanguageClassifier(credentials); // Get classifiers if (!naturalLanguageClassifier.GetClassifiers(OnGetClassifiers)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to get classifiers!"); } while (!_getClassifiersTested) { yield return(null); } if (_classifierIds.Count == 0) { Log.Debug("ExampleNaturalLanguageClassifier", "There are no trained classifiers. Please train a classifier..."); } if (_classifierIds.Count > 0) { // Get each classifier foreach (string classifierId in _classifierIds) { if (!naturalLanguageClassifier.GetClassifier(classifierId, OnGetClassifier)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to get classifier {0}!", classifierId); } } while (!_getClassifierTested) { yield return(null); } } if (!_areAnyClassifiersAvailable && _classifierIds.Count > 0) { Log.Debug("ExampleNaturalLanguageClassifier", "All classifiers are training..."); } // Train classifier #if TRAIN_CLASSIFIER string dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/weather_data_train.csv"; var trainingContent = File.ReadAllText(dataPath); if (!naturalLanguageClassifier.TrainClassifier(_classifierName + "/" + DateTime.Now.ToString(), "en", trainingContent, OnTrainClassifier)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to train clasifier!"); } while (!_trainClassifierTested) { yield return(null); } #endif #if DELETE_TRAINED_CLASSIFIER if (!string.IsNullOrEmpty(_classifierToDelete)) { if (!naturalLanguageClassifier.DeleteClassifer(_classifierToDelete, OnDeleteTrainedClassifier)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to delete clasifier {0}!", _classifierToDelete); } } #endif // Classify if (_areAnyClassifiersAvailable) { if (!naturalLanguageClassifier.Classify(_classifierId, _inputString, OnClassify)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to classify!"); } while (!_classifyTested) { yield return(null); } } Log.Debug("ExampleNaturalLanguageClassifier", "Natural language classifier examples complete."); yield break; }
private IEnumerator Examples() { // Get classifiers if (!naturalLanguageClassifier.GetClassifiers(OnGetClassifiers)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to get classifiers!"); } while (!_getClassifiersTested) { yield return(null); } if (_classifierIds.Count == 0) { Log.Debug("ExampleNaturalLanguageClassifier", "There are no trained classifiers. Please train a classifier..."); } if (_classifierIds.Count > 0) { // Get each classifier foreach (string classifierId in _classifierIds) { if (!naturalLanguageClassifier.GetClassifier(classifierId, OnGetClassifier)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to get classifier {0}!", classifierId); } } while (!_getClassifierTested) { yield return(null); } } if (!_areAnyClassifiersAvailable && _classifierIds.Count > 0) { Log.Debug("ExampleNaturalLanguageClassifier", "All classifiers are training..."); } // Train classifier #if TRAIN_CLASSIFIER string dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/weather_data_train.csv"; var trainingContent = File.ReadAllText(dataPath); if (!naturalLanguageClassifier.TrainClassifier(_classifierName + "/" + DateTime.Now.ToString(), "en", trainingContent, OnTrainClassifier)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to train clasifier!"); } while (!_trainClassifierTested) { yield return(null); } #endif #if DELETE_TRAINED_CLASSIFIER if (!string.IsNullOrEmpty(_classifierToDelete)) { if (!naturalLanguageClassifier.DeleteClassifer(_classifierToDelete, OnDeleteTrainedClassifier)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to delete clasifier {0}!", _classifierToDelete); } } #endif // Classify if (_areAnyClassifiersAvailable) { if (!naturalLanguageClassifier.Classify(_classifierId, _inputString, OnClassify)) { Log.Debug("ExampleNaturalLanguageClassifier", "Failed to classify!"); } while (!_classifyTested) { yield return(null); } } Log.Debug("ExampleNaturalLanguageClassifier", "Natural language classifier examples complete."); }