Exemplo n.º 1
0
        /// <summary>
        /// Classifies using WEKA.
        /// </summary>
        public override string classify(Substroke substroke, FeatureSketch featureSketch)
        {
            // Get the features for this substroke
            double[] featureValues = featureSketch.GetValuesSingle(substroke);

            // classify using Weka
            string classification = _wekaWrapper.Value.classify(featureValues);

            return(classification);
        }
Exemplo n.º 2
0
        private static void SSandGroupFeatures(bool SingleStrokeOn, bool GroupingOn,
                                               List <string> sketches, Dictionary <string, string> mapping, List <string> allClasses,
                                               Dictionary <string, bool> ssFeaturesOn, Features ssFeatures, List <string> ssFeatureNames, List <string> ssClassificationNames,
                                               Dictionary <string, bool> pairFeaturesOn, Features pairFeatures, List <string> pairFeatureNames, List <string> users, string dir,
                                               Dictionary <Guid, string> allSSClassifications)
        {
            List <string> ignore = new List <string>();

            /*ignore.Add("DoubleShaftArrow");
             * ignore.Add("EquationType");
             * ignore.Add("ForceEquilEquation");
             * ignore.Add("MomentEquilEquation");
             * ignore.Add("GeomTrigEquation");
             * ignore.Add("OtherEquation");
             * ignore.Add("OtherText");
             * ignore.Add("Ellipses");
             * ignore.Add("Box");
             * ignore.Add("Triangle");
             * ignore.Add("AngleSquare");
             * ignore.Add("Angle");
             * ignore.Add("OtherGeometry");
             * ignore.Add("Other");*/
            ignore.Add("unlabeled");

            #region Calculate Features
            foreach (string sketchFile in sketches)
            {
                Sketch.Sketch sketch = new ReadXML(sketchFile).Sketch;
                //sketch = Utilities.General.ReOrderParentShapes(sketch);
                //sketch = Utilities.General.LinkShapes(sketch);

                string nameShort = Path.GetFileNameWithoutExtension(sketchFile);
                int    index     = nameShort.IndexOf('_');
                string userName  = nameShort.Substring(0, index);
                //if (sketchFile.Contains("_T"))
                //userName += "_T";
                //else if (sketchFile.Contains("_P"))
                //userName += "_P";

                if (!users.Contains(userName))
                {
                    users.Add(userName);
                }

                FeatureSketch fSketch = new FeatureSketch(sketch, ssFeaturesOn, pairFeaturesOn, new Dictionary <string, double[]>());
                foreach (Substroke stroke in sketch.SubstrokesL)
                {
                    stroke.Classification = mapping[stroke.FirstLabel];
                }

                Dictionary <Substroke, double[]> valuesSingle;
                Dictionary <string, Dictionary <FeatureStrokePair, double[]> > class2pairValues;

                if (SingleStrokeOn)
                {
                    fSketch.GetValuesSingle(out valuesSingle, ValuePreparationStage.Normalized);

                    foreach (Substroke s in sketch.Substrokes)
                    {
                        string label = s.FirstLabel;
                        string cls   = mapping[label];

                        if (!ignore.Contains(label))
                        {
                            ssFeatures.Add(userName, "SingleStroke", valuesSingle[s], cls);
                        }
                    }
                }

                if (GroupingOn)
                {
                    Dictionary <Substroke, string> classifications = new Dictionary <Substroke, string>();
                    // Use actual classifications from Weka classifier
                    //foreach (Substroke s in sketch.Substrokes)
                    //classifications.Add(s, allSSClassifications[s.Id]);
                    foreach (Substroke s in sketch.SubstrokesL)
                    {
                        classifications.Add(s, s.Classification);
                    }

                    fSketch.GetValuesPairwise(out class2pairValues, classifications, ValuePreparationStage.Normalized);

                    foreach (KeyValuePair <string, Dictionary <FeatureStrokePair, double[]> > kvp in class2pairValues)
                    {
                        string cls = kvp.Key;
                        Dictionary <FeatureStrokePair, double[]> features = kvp.Value;

                        foreach (FeatureStrokePair pair in features.Keys)
                        {
                            if (pair.StrokeA.ParentShapes[0] == pair.StrokeB.ParentShapes[0])
                            {
                                string parentName = mapping[pair.StrokeA.ParentShapes[0].Label];
                                string join       = "Join";// +parentName;

                                //System.Drawing.Rectangle bbox = Utilities.Compute.BoundingBox(pair.StrokeA.ParentShapes[0].Substrokes);
                                //double diag = Utilities.Compute.DiagonalLength(bbox);
                                double[] minInShape    = GetMinDistanceInShape(pair, pair.StrokeA.ParentShapes[0], features);
                                double   factor        = 1.15;
                                double   minDistThresh = 200.0;
                                if (pair.SubstrokeDistance.Min <= minDistThresh ||
                                    pair.SubstrokeDistance.Min <= minInShape[0] * factor ||
                                    pair.SubstrokeDistance.Min <= minInShape[1] * factor)
                                {
                                    pairFeatures.Add(userName, cls, features[pair], join);
                                }
                                else
                                {
                                    pairFeatures.Add(userName, cls, features[pair], "Ignore");
                                }
                            }
                            else
                            {
                                //if (features[pair][2] < 0.15)
                                pairFeatures.Add(userName, cls, features[pair], "NoJoin");
                            }
                        }
                    }
                }

                /*foreach (Shape shape in sketch.ShapesL)
                 * {
                 *  if (shape.ParentShape.SubstrokesL.Count != 0)
                 *      continue;
                 *
                 *  Shape chained = ChainSubstrokesInShape(shape, fSketch);
                 *  if (!EquivalentShapes(shape, chained))
                 *  {
                 *      Console.WriteLine("ERROR - Shape wasn't chained!! {0}, {1}", shape.Label, Path.GetFileNameWithoutExtension(sketchFile));
                 *  }
                 * }*/
            }
            #endregion


            #region Printing

            if (SingleStrokeOn)
            {
                foreach (string user in users)
                {
                    string filename = dir + "\\SingleStrokeTraining_" + user + ".arff";
                    ssFeatures.PrintARFF(filename, user, "SingleStroke", true);
                    string filename2 = dir + "\\" + user + "_features.txt";
                    ssFeatures.Print(filename2, user, "SingleStroke");
                }
                ssFeatures.PrintARFF(dir + "\\SingleStrokeTraining_all.arff", "all", "SingleStroke", true);
            }

            if (GroupingOn)
            {
                foreach (string user in users)
                {
                    foreach (string cls in ssClassificationNames)
                    {
                        string filename = dir + "\\Grouping_" + user + "_" + cls + ".arff";
                        pairFeatures.PrintARFF(filename, user, cls, true);
                        string testFilename = dir + "\\testing\\Grouping_" + user + "_" + cls + ".arff";
                        pairFeatures.PrintTestingARFF(testFilename, user, cls, true);
                    }

                    /*string filename = dir + "Grouping_" + user + ".arff";
                     * pairFeatures.PrintARFF(filename, user, "All", true);
                     * string testFilename = dir + "\\testing\\Grouping_" + user + ".arff";
                     * pairFeatures.PrintTestingARFF(testFilename, user, "All", true);*/
                }

                foreach (string cls in ssClassificationNames)
                {
                    string filename = dir + "\\Grouping_all_" + cls + ".arff";
                    pairFeatures.PrintARFF(filename, "all", cls, true);
                }

                /*string filename2 = dir + "Grouping_All.arff";
                 * pairFeatures.PrintARFF(filename2, "all", "All", true);*/
            }

            #endregion
        }