/// <summary> /// Classify multiple phrases. Returns label information for multiple phrases. The status must be `Available` before you can use the classifier to classify text. Note that classifying Japanese texts is a beta feature. /// </summary> /// <param name="classifierId">Classifier ID to use.</param> /// <param name="body">Phrase to classify. The maximum length of the text phrase is 1024 characters. You can submit up to 30 text phrases in a request.</param> /// <param name="customData">Custom data object to pass data including custom request headers.</param> /// <returns><see cref="ClassificationCollection" />ClassificationCollection</returns> public ClassificationCollection ClassifyCollection(string classifierId, ClassifyCollectionInput body, Dictionary <string, object> customData = null) { if (string.IsNullOrEmpty(classifierId)) { throw new ArgumentNullException(nameof(classifierId)); } if (body == null) { throw new ArgumentNullException(nameof(body)); } ClassificationCollection result = null; try { var request = this.Client.WithAuthentication(this.UserName, this.Password) .PostAsync($"{this.Endpoint}/v1/classifiers/{classifierId}/classify_collection"); request.WithBody <ClassifyCollectionInput>(body); if (customData != null) { request.WithCustomData(customData); } result = request.As <ClassificationCollection>().Result; if (result == null) { result = new ClassificationCollection(); } result.CustomData = request.CustomData; } catch (AggregateException ae) { throw ae.Flatten(); } return(result); }
private ClassificationCollection ClassifyCollection(string classifierId, ClassifyCollectionInput body, Dictionary <string, object> customData = null) { Console.WriteLine("\nAttempting to ClassifyCollection()"); var result = _service.ClassifyCollection(classifierId: classifierId, body: body, customData: customData); if (result != null) { Console.WriteLine("ClassifyCollection() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); } else { Console.WriteLine("Failed to ClassifyCollection()"); } return(result); }
/// <summary> /// Classify multiple phrases. /// /// Returns label information for multiple phrases. The status must be `Available` before you can use the /// classifier to classify text. /// /// Note that classifying Japanese texts is a beta feature. /// </summary> /// <param name="classifierId">Classifier ID to use.</param> /// <param name="body">Phrase to classify. You can submit up to 30 text phrases in a request.</param> /// <param name="customData">Custom data object to pass data including custom request headers.</param> /// <returns><see cref="ClassificationCollection" />ClassificationCollection</returns> public ClassificationCollection ClassifyCollection(string classifierId, ClassifyCollectionInput body, Dictionary <string, object> customData = null) { if (string.IsNullOrEmpty(classifierId)) { throw new ArgumentNullException(nameof(classifierId)); } if (body == null) { throw new ArgumentNullException(nameof(body)); } ClassificationCollection result = null; try { IClient client = this.Client; if (_tokenManager != null) { client = this.Client.WithAuthentication(_tokenManager.GetToken()); } if (_tokenManager == null) { client = this.Client.WithAuthentication(this.UserName, this.Password); } var restRequest = client.PostAsync($"{this.Endpoint}/v1/classifiers/{classifierId}/classify_collection"); restRequest.WithBody <ClassifyCollectionInput>(body); if (customData != null) { restRequest.WithCustomData(customData); } restRequest.WithHeader("X-IBMCloud-SDK-Analytics", "service_name=natural_language_classifier;service_version=v1;operation_id=ClassifyCollection"); result = restRequest.As <ClassificationCollection>().Result; if (result == null) { result = new ClassificationCollection(); } result.CustomData = restRequest.CustomData; } catch (AggregateException ae) { throw ae.Flatten(); } return(result); }
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; }
public void TestClassifiers_Success() { var listClassifiersResult = ListClassifiers(); string classifierId = null; if (listClassifiersResult.Classifiers.Count > 0) { classifierId = listClassifiersResult.Classifiers[0].ClassifierId; } Classification classifyResult = null; if (!string.IsNullOrEmpty(classifierId)) { ClassifyInput classifyInput = new ClassifyInput { Text = _textToClassify1 }; classifyResult = Classify(classifierId, classifyInput); } ClassificationCollection classifyCollectionResult = null; if (!string.IsNullOrEmpty(classifierId)) { ClassifyCollectionInput classifyCollectionInput = new ClassifyCollectionInput { Collection = new List <ClassifyInput>() { new ClassifyInput() { Text = _textToClassify0 }, new ClassifyInput() { Text = _textToClassify1 } } }; classifyCollectionResult = ClassifyCollection(classifierId, classifyCollectionInput); } Classifier createClassifierResult = null; using (FileStream classifierData = File.OpenRead(_classifierDataFilePath), metadata = File.OpenRead(_metadataDataFilePath)) { createClassifierResult = _service.CreateClassifier(metadata, classifierData); } var createdClassifierId = createClassifierResult.ClassifierId; var getClassifierResult = GetClassifier(createdClassifierId); if (!string.IsNullOrEmpty(classifierId) && !string.IsNullOrEmpty(createdClassifierId)) { DeleteClassifier(createdClassifierId); } if (!string.IsNullOrEmpty(classifierId)) { Assert.IsNotNull(classifyResult); Assert.IsNotNull(classifyCollectionResult); } Assert.IsNotNull(getClassifierResult); Assert.IsTrue(createdClassifierId == getClassifierResult.ClassifierId); Assert.IsNotNull(createClassifierResult); Assert.IsNotNull(listClassifiersResult); }
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; }