예제 #1
0
        public IEnumerator TestClassify()
        {
            Log.Debug("NaturalLanguageClassifierServiceV1IntegrationTests", "Attempting to Classify...");
            Classification classifyResponse = null;

            service.Classify(
                callback: (DetailedResponse <Classification> response, IBMError error) =>
            {
                Log.Debug("NaturalLanguageClassifierServiceV1IntegrationTests", "Classify result: {0}", response.Response);
                classifyResponse = response.Result;
                Assert.IsNotNull(classifyResponse);
                Assert.IsNotNull(classifyResponse.Classes);
                Assert.IsTrue(classifyResponse.Classes.Count > 0);
                Assert.IsNotNull(classifyResponse.TopClass);
                Assert.IsTrue(classifyResponse.Text == textToClassify0);
                Assert.IsTrue(classifyResponse.ClassifierId == classifierId);
                Assert.IsNull(error);
            },
                classifierId: classifierId,
                text: textToClassify0
                );

            while (classifyResponse == null)
            {
                yield return(null);
            }
        }
        public void Classify_Success()
        {
            IClient  client  = Substitute.For <IClient>();
            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(client);

            var classifierId = "classifierId";
            var text         = "text";

            var result = service.Classify(classifierId: classifierId, text: text);

            JObject bodyObject = new JObject();

            if (!string.IsNullOrEmpty(text))
            {
                bodyObject["text"] = JToken.FromObject(text);
            }
            var json = JsonConvert.SerializeObject(bodyObject);

            request.Received().WithBodyContent(Arg.Is <StringContent>(x => x.ReadAsStringAsync().Result.Equals(json)));
            client.Received().PostAsync($"{service.ServiceUrl}/v1/classifiers/{classifierId}/classify");
        }
예제 #3
0
        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);
        }
        public void Classify()
        {
            IamAuthenticator authenticator = new IamAuthenticator(
                apikey: "{apikey}");

            NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(authenticator);

            service.SetServiceUrl("{serviceUrl}");

            var result = service.Classify(
                classifierId: "10D41B-nlc-1",
                text: "How hot will it be today?"
                );

            Console.WriteLine(result.Response);
        }
예제 #5
0
        public void Classify()
        {
            TokenOptions tokenOptions = new TokenOptions()
            {
                IamApiKey  = apikey,
                ServiceUrl = url
            };

            NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(tokenOptions);

            var result = service.Classify(
                classifierId: classifierId,
                text: "Will it be hot today?"
                );

            Console.WriteLine(result.Response);
        }
예제 #6
0
        public void Classify()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(config);

            service.SetEndpoint(url);

            var result = service.Classify(
                classifierId: classifierId,
                text: "Will it be hot today?"
                );

            Console.WriteLine(result.Response);
        }
예제 #7
0
        public void TestClassifiers_Success()
        {
            service.WithHeader("X-Watson-Test", "1");
            var listClassifiersResult = service.ListClassifiers();

            string classifierId = null;

            if (listClassifiersResult.Result.Classifiers.Count > 0)
            {
                classifierId = listClassifiersResult.Result.Classifiers[0].ClassifierId;
            }

            DetailedResponse <Classification> classifyResult = null;

            if (!string.IsNullOrEmpty(classifierId))
            {
                service.WithHeader("X-Watson-Test", "1");
                classifyResult = service.Classify(
                    classifierId: classifierId,
                    text: textToClassify1
                    );
            }

            DetailedResponse <ClassificationCollection> classifyCollectionResult = null;

            if (!string.IsNullOrEmpty(classifierId))
            {
                var collection = new List <ClassifyInput>()
                {
                    new ClassifyInput()
                    {
                        Text = textToClassify0
                    },
                    new ClassifyInput()
                    {
                        Text = textToClassify1
                    }
                };

                service.WithHeader("X-Watson-Test", "1");
                classifyCollectionResult = service.ClassifyCollection(
                    classifierId: classifierId,
                    collection: collection
                    );
            }

            DetailedResponse <Classifier> createClassifierResult = null;

            using (FileStream trainingDataFile = File.OpenRead(classifierDataFilePath), metadataFile = File.OpenRead(metadataDataFilePath))
            {
                using (MemoryStream trainingData = new MemoryStream(), metadata = new MemoryStream())
                {
                    trainingDataFile.CopyTo(trainingData);
                    metadataFile.CopyTo(metadata);
                    service.WithHeader("X-Watson-Test", "1");
                    createClassifierResult = service.CreateClassifier(
                        trainingMetadata: metadata,
                        trainingData: trainingData
                        );
                }
            }

            var createdClassifierId = createClassifierResult.Result.ClassifierId;

            service.WithHeader("X-Watson-Test", "1");
            var getClassifierResult = service.GetClassifier(
                classifierId: createdClassifierId
                );

            if (!string.IsNullOrEmpty(classifierId) && !string.IsNullOrEmpty(createdClassifierId))
            {
                service.WithHeader("X-Watson-Test", "1");
                service.DeleteClassifier(
                    classifierId: createdClassifierId
                    );
            }

            if (!string.IsNullOrEmpty(classifierId))
            {
                Assert.IsNotNull(classifyResult);
                Assert.IsNotNull(classifyCollectionResult);
            }
            Assert.IsNotNull(getClassifierResult);
            Assert.IsTrue(createdClassifierId == getClassifierResult.Result.ClassifierId);
            Assert.IsNotNull(createClassifierResult);
            Assert.IsNotNull(listClassifiersResult);
        }
예제 #8
0
        /**
         * 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));
        }