Exemple #1
0
        public virtual int classify(int k, int[] unknown, double distanceThreshold)
        {
            FeatureVectorAsObject key = new FeatureVectorAsObject(unknown, FEATURES);

            int?catI = null;

            classifyCache.TryGetValue(key, out catI);
            nClassifyCalls++;
            if (catI != null)
            {
                nClassifyCacheHits++;
                return(catI.Value);
            }
            Neighbor[] kNN = this.kNN(unknown, k, distanceThreshold);
            IDictionary <int, MutableDouble> similarities = getCategoryToSimilarityMap(kNN, k, Y);
            int cat = getCategoryWithMaxValue(similarities);

            if (cat == -1)
            {
                // try with less strict match threshold to get some indication of alignment
                kNN          = this.kNN(unknown, k, org.antlr.codebuff.Trainer.MAX_CONTEXT_DIFF_THRESHOLD2);
                similarities = getCategoryToSimilarityMap(kNN, k, Y);
                cat          = getCategoryWithMaxValue(similarities);
            }

            classifyCache[key] = cat;
            return(cat);
        }
Exemple #2
0
        public virtual string getPredictionAnalysis(InputDocument doc, int k, int[] unknown, IList <int> Y, double distanceThreshold)
        {
            FeatureVectorAsObject key = new FeatureVectorAsObject(unknown, FEATURES);

            Neighbor[] kNN = null;
            neighborCache.TryGetValue(key, out kNN);
            nNNCalls++;
            if (kNN == null)
            {
                kNN = this.kNN(unknown, k, distanceThreshold);
                neighborCache[key] = kNN;
            }
            else
            {
                nNNCacheHits++;
            }
            IDictionary <int, MutableDouble> similarities = getCategoryToSimilarityMap(kNN, k, Y);
            int cat = getCategoryWithMaxValue(similarities);

            if (cat == -1)
            {
                // try with less strict match threshold to get some indication of alignment
                kNN          = this.kNN(unknown, k, org.antlr.codebuff.Trainer.MAX_CONTEXT_DIFF_THRESHOLD2);
                similarities = getCategoryToSimilarityMap(kNN, k, Y);
                cat          = getCategoryWithMaxValue(similarities);
            }

            string displayCat;
            int    c = cat & 0xFF;

            if (c == org.antlr.codebuff.Trainer.CAT_INJECT_NL || c == org.antlr.codebuff.Trainer.CAT_INJECT_WS)
            {
                displayCat = Formatter.getWSCategoryStr(cat);
            }
            else
            {
                displayCat = Formatter.getHPosCategoryStr(cat);
            }
            displayCat = !string.ReferenceEquals(displayCat, null) ? displayCat : "none";

            StringBuilder buf = new StringBuilder();

            buf.Append(Trainer.featureNameHeader(FEATURES));
            buf.Append(Trainer._toString(FEATURES, doc, unknown) + "->" + similarities + " predicts " + displayCat);
            buf.Append("\n");
            if (kNN.Length > 0)
            {
                kNN = kNN.Take(Math.Min(k, kNN.Length)).ToArray();
                foreach (Neighbor n in kNN)
                {
                    buf.Append(n.ToString(FEATURES, Y));
                    buf.Append("\n");
                }
            }
            return(buf.ToString());
        }