/// <summary> /// Classify a phrase. Returns label information for the input. The status must be `Available` before you can use the classifier to classify text. /// </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.</param> /// <returns><see cref="Classification" />Classification</returns> public Classification Classify(string classifierId, ClassifyInput body) { if (string.IsNullOrEmpty(classifierId)) { throw new ArgumentNullException(nameof(classifierId)); } if (body == null) { throw new ArgumentNullException(nameof(body)); } Classification result = null; try { var request = this.Client.WithAuthentication(this.UserName, this.Password) .PostAsync($"{this.Endpoint}/v1/classifiers/{classifierId}/classify"); request.WithBody <ClassifyInput>(body); result = request.As <Classification>().Result; } catch (AggregateException ae) { throw ae.Flatten(); } return(result); }
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 = _textToClassify }; classifyResult = Classify(classifierId, classifyInput); } 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(getClassifierResult); Assert.IsTrue(createdClassifierId == getClassifierResult.ClassifierId); Assert.IsNotNull(createClassifierResult); Assert.IsNotNull(listClassifiersResult); }
private Classification Classify(string classifierId, ClassifyInput body, Dictionary <string, object> customData = null) { Console.WriteLine("\nAttempting to Classify()"); var result = _service.Classify(classifierId: classifierId, body: body, customData: customData); if (result != null) { Console.WriteLine("Classify() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); } else { Console.WriteLine("Failed to Classify()"); } return(result); }
/// <summary> /// Classify a phrase. /// /// Returns label information for the input. The status must be `Available` before you can use the classifier to /// classify text. /// </summary> /// <param name="classifierId">Classifier ID to use.</param> /// <param name="body">Phrase to classify.</param> /// <param name="customData">Custom data object to pass data including custom request headers.</param> /// <returns><see cref="Classification" />Classification</returns> public Classification Classify(string classifierId, ClassifyInput body, Dictionary <string, object> customData = null) { if (string.IsNullOrEmpty(classifierId)) { throw new ArgumentNullException(nameof(classifierId)); } if (body == null) { throw new ArgumentNullException(nameof(body)); } Classification 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"); restRequest.WithBody <ClassifyInput>(body); if (customData != null) { restRequest.WithCustomData(customData); } restRequest.WithHeader("X-IBMCloud-SDK-Analytics", "service_name=natural_language_classifier;service_version=v1;operation_id=Classify"); result = restRequest.As <Classification>().Result; if (result == null) { result = new Classification(); } result.CustomData = restRequest.CustomData; } catch (AggregateException ae) { throw ae.Flatten(); } return(result); }
/// <summary> /// Classify a phrase. Returns label information for the input. The status must be `Available` before you can use the classifier to classify text. /// </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.</param> /// <param name="customData">Custom data object to pass data including custom request headers.</param> /// <returns><see cref="Classification" />Classification</returns> public Classification Classify(string classifierId, ClassifyInput body, Dictionary <string, object> customData = null) { if (string.IsNullOrEmpty(classifierId)) { throw new ArgumentNullException(nameof(classifierId)); } if (body == null) { throw new ArgumentNullException(nameof(body)); } Classification result = null; try { IClient client; client = this.Client.WithAuthentication(this.UserName, this.Password); var restRequest = client.PostAsync($"{this.Endpoint}/v1/classifiers/{classifierId}/classify"); restRequest.WithBody <ClassifyInput>(body); if (customData != null) { restRequest.WithCustomData(customData); } result = restRequest.As <Classification>().Result; if (result == null) { result = new Classification(); } result.CustomData = restRequest.CustomData; } catch (AggregateException ae) { throw ae.Flatten(); } return(result); }
/** * Classifies email based on weighted sum of subject and body * classification confidences. * * @param moveMail * candidate email to classify * * @returns classification and confidence of given email * * @author Kurtis Kuszmaul **/ private Tuple <string, double> ClassifyMail(Outlook.MailItem moveMail) { string classification; double subConfWeight = .47; double bodyConfWeight = .53; double matchingConfLimit = .85; double differentConfLimit = .45; // Classify subject ClassifyInput classifySubjectInput = new ClassifyInput { Text = moveMail.Subject }; // Get top class and weighted confidence of subject Classification classifySubjectResult = _subClassifier.Classify(subClassifierID, classifySubjectInput); string subClass = classifySubjectResult.TopClass; double subConf = (double)classifySubjectResult.Classes[0].Confidence * subConfWeight; Dictionary <string, List <double> > bodyDict = new Dictionary <string, List <double> >(); List <double> spamList = new List <double>(); List <double> notSpamList = new List <double>(); bodyDict.Add("spam", spamList); bodyDict.Add("not spam", notSpamList); // Break subject into manageable chunks to classify string cleanedBody = moveMail.Body.Replace("\n", " ").Replace("\t", " ").Replace("\r", " "); IList <string> bodyChunks = ChunkBody(cleanedBody, 1000); foreach (string chunk in bodyChunks) { string cleanedChunk = chunk; // Classify chunk of body text ClassifyInput classifyChunkInput = new ClassifyInput { Text = chunk }; // Get top class of body chunk and add it and its confidence to bodyDict Classification classifyChunkResult = _bodyClassifier.Classify(bodyClassifierID, classifyChunkInput); string topChunkClass = classifyChunkResult.TopClass; double chunkConf = (double)classifyChunkResult.Classes[0].Confidence; bodyDict[topChunkClass].Add(chunkConf); } // Determine top classification of body and take average weighted confidence of chunks string bodyClass = bodyDict["spam"].Count > bodyDict["not spam"].Count ? "spam" : "not spam"; List <double> bodyConfList = bodyDict[bodyClass]; double bodyConf = bodyConfList.Average() * bodyConfWeight; // Combine classes and weighted confidences to determine final classification double totalConf; if (subClass == bodyClass) { totalConf = subConf + bodyConf; if (totalConf >= matchingConfLimit) { classification = subClass; } else { classification = "not spam"; totalConf = -1.0; } } else { if (subConf > bodyConf && subConf > differentConfLimit) { classification = subClass; totalConf = subConf / subConfWeight; } else if (bodyConf >= subConf && bodyConf > differentConfLimit) { classification = bodyClass; totalConf = bodyConf / bodyConfWeight; } else { classification = "not spam"; totalConf = -1.0; } } totalConf *= 100; totalConf = Math.Round(totalConf, 2); return(Tuple.Create <string, double>(classification, totalConf)); }