Example #1
0
        /// <summary>Get the sentiment of a sentence.</summary>
        /// <param name="sentence">
        /// The sentence as a core map.
        /// POS tags and Lemmas are a prerequisite.
        /// See
        /// <see cref="Edu.Stanford.Nlp.Ling.CoreAnnotations.PartOfSpeechAnnotation"/>
        /// and
        /// <see cref="Edu.Stanford.Nlp.Ling.CoreAnnotations.LemmaAnnotation"/>
        /// .
        /// </param>
        /// <returns>The sentiment class of this sentence.</returns>
        public virtual SentimentClass Classify(ICoreMap sentence)
        {
            ICounter <string> features = Featurize(sentence);
            RVFDatum <SentimentClass, string> datum = new RVFDatum <SentimentClass, string>(features);

            return(impl.ClassOf(datum));
        }
Example #2
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.TypeLoadException"/>
        private static void DemonstrateSerialization()
        {
            System.Console.Out.WriteLine();
            System.Console.Out.WriteLine("Demonstrating working with a serialized classifier");
            ColumnDataClassifier         cdc = new ColumnDataClassifier(where + "examples/cheese2007.prop");
            IClassifier <string, string> cl  = cdc.MakeClassifier(cdc.ReadTrainingExamples(where + "examples/cheeseDisease.train"));

            // Exhibit serialization and deserialization working. Serialized to bytes in memory for simplicity
            System.Console.Out.WriteLine();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream    oos  = new ObjectOutputStream(baos);

            oos.WriteObject(cl);
            oos.Close();
            byte[] @object                       = baos.ToByteArray();
            ByteArrayInputStream bais            = new ByteArrayInputStream(@object);
            ObjectInputStream    ois             = new ObjectInputStream(bais);
            LinearClassifier <string, string> lc = ErasureUtils.UncheckedCast(ois.ReadObject());

            ois.Close();
            ColumnDataClassifier cdc2 = new ColumnDataClassifier(where + "examples/cheese2007.prop");

            // We compare the output of the deserialized classifier lc versus the original one cl
            // For both we use a ColumnDataClassifier to convert text lines to examples
            System.Console.Out.WriteLine();
            System.Console.Out.WriteLine("Making predictions with both classifiers");
            foreach (string line in ObjectBank.GetLineIterator(where + "examples/cheeseDisease.test", "utf-8"))
            {
                IDatum <string, string> d  = cdc.MakeDatumFromLine(line);
                IDatum <string, string> d2 = cdc2.MakeDatumFromLine(line);
                System.Console.Out.Printf("%s  =origi=>  %s (%.4f)%n", line, cl.ClassOf(d), cl.ScoresOf(d).GetCount(cl.ClassOf(d)));
                System.Console.Out.Printf("%s  =deser=>  %s (%.4f)%n", line, lc.ClassOf(d2), lc.ScoresOf(d).GetCount(lc.ClassOf(d)));
            }
        }
        public virtual double Score <F>(IClassifier <L, F> classifier, GeneralDataset <L, F> data)
        {
            IList <L> guesses = new List <L>();
            IList <L> labels  = new List <L>();

            for (int i = 0; i < data.Size(); i++)
            {
                IDatum <L, F> d     = data.GetRVFDatum(i);
                L             guess = classifier.ClassOf(d);
                guesses.Add(guess);
            }
            int[] labelsArr = data.GetLabelsArray();
            labelIndex = data.labelIndex;
            for (int i_1 = 0; i_1 < data.Size(); i_1++)
            {
                labels.Add(labelIndex.Get(labelsArr[i_1]));
            }
            labelIndex = new HashIndex <L>();
            labelIndex.AddAll(data.LabelIndex().ObjectsList());
            labelIndex.AddAll(classifier.Labels());
            int numClasses = labelIndex.Size();

            tpCount  = new int[numClasses];
            fpCount  = new int[numClasses];
            fnCount  = new int[numClasses];
            negIndex = labelIndex.IndexOf(negLabel);
            for (int i_2 = 0; i_2 < guesses.Count; ++i_2)
            {
                L   guess      = guesses[i_2];
                int guessIndex = labelIndex.IndexOf(guess);
                L   label      = labels[i_2];
                int trueIndex  = labelIndex.IndexOf(label);
                if (guessIndex == trueIndex)
                {
                    if (guessIndex != negIndex)
                    {
                        tpCount[guessIndex]++;
                    }
                }
                else
                {
                    if (guessIndex != negIndex)
                    {
                        fpCount[guessIndex]++;
                    }
                    if (trueIndex != negIndex)
                    {
                        fnCount[trueIndex]++;
                    }
                }
            }
            return(GetFMeasure());
        }
Example #4
0
 public override double Score <F>(IClassifier <L, F> classifier, GeneralDataset <L, F> data)
 {
     labelIndex = new HashIndex <L>();
     labelIndex.AddAll(classifier.Labels());
     labelIndex.AddAll(data.labelIndex.ObjectsList());
     ClearCounts();
     int[] labelsArr = data.GetLabelsArray();
     for (int i = 0; i < data.Size(); i++)
     {
         IDatum <L, F> d     = data.GetRVFDatum(i);
         L             guess = classifier.ClassOf(d);
         AddGuess(guess, labelIndex.Get(labelsArr[i]));
     }
     FinalizeCounts();
     return(GetFMeasure());
 }
Example #5
0
 public PrecisionRecallStats(IClassifier <L, F> classifier, Dataset <L, F> data, L positiveClass)
 {
     for (int i = 0; i < data.Size(); ++i)
     {
         IDatum <L, F> d             = data.GetDatum(i);
         L             guess         = classifier.ClassOf(d);
         L             label         = d.Label();
         bool          guessPositive = guess.Equals(positiveClass);
         bool          isPositive    = label.Equals(positiveClass);
         if (isPositive && guessPositive)
         {
             tpCount++;
         }
         if (isPositive && !guessPositive)
         {
             fnCount++;
         }
         if (!isPositive && guessPositive)
         {
             fpCount++;
         }
     }
 }