Beispiel #1
0
        public static void DistanceInterfaceTest(IDistance distance, string first, string second, int expected)
        {
            var resultString = distance.GetDistance(first, second);

            Assert.AreEqual(expected, resultString);

            var resultNorm = distance.GetDistance(new NormalizedString(first), new NormalizedString(second));

            Assert.AreEqual(expected, resultNorm);

            var resultToken = distance.GetDistance(new Token(first), new Token(second));

            Assert.AreEqual(expected, resultToken);
        }
        public string GetClassification(Record newRecord)
        {
            //Getting all the distances
            Dictionary <Record, double> distancesToNeighbours = new Dictionary <Record, double>();

            foreach (var alreadyClassifiedRecord in _trainingSet)
            {
                var distanceToThisClassifiedRecord = distanceMeasure.GetDistance(newRecord, alreadyClassifiedRecord);

                distancesToNeighbours.Add(alreadyClassifiedRecord, distanceToThisClassifiedRecord);
            }

            //Getting the K amount of closest neighbours
            var KNeighbours = distancesToNeighbours.OrderBy(n => n.Value).Take(K);

            //Counting the classifications of the neighbours
            var classificationsCounts = new Dictionary <string, int>();

            foreach (var neighbour in KNeighbours)
            {
                var neighboursClassification = neighbour.Key.Classification;

                if (classificationsCounts.ContainsKey(neighboursClassification) == false)
                {
                    classificationsCounts.Add(neighboursClassification, 1);
                }
                else
                {
                    classificationsCounts[neighboursClassification] += 1;
                }
            }

            //Returning the classification which happens most among the neighbours
            return(classificationsCounts.ToList().OrderByDescending(c => c.Value).First().Key);
        }
Beispiel #3
0
 public double GetDistance(Realization fst, Realization snd)
 {
     if (fst.Syllables.Length != snd.Syllables.Length)
     {
         return(double.MaxValue);
     }
     else if (fst.Syllables.Length == 0)
     {
         return(0);
     }
     else
     {
         return(fst.Syllables.Zip(snd.Syllables, (s1, s2) => _syllableDistance.GetDistance(s1, s2)).Sum());
     }
 }
Beispiel #4
0
        public double GetDistance(WordForm fst, WordForm snd)
        {
            int length = Math.Max(fst.Realizations.Length, snd.Realizations.Length);

            if (length == 0)
            {
                return(0);
            }

            var distances =
                from s1 in fst.Realizations
                from s2 in snd.Realizations
                select _realizationDistance.GetDistance(s1, s2);

            return(distances.Min(d => d));
        }
Beispiel #5
0
        public double GetDistance(Realization fst, Realization snd)
        {
            int length = Math.Max(fst.Syllables.Length, snd.Syllables.Length);

            if (length == 0)
            {
                return(0);
            }

            var candidates =
                from ss1 in fst.Syllables.Pad(Syllable.Null, length)
                from ss2 in snd.Syllables.Pad(Syllable.Null, length)
                select ss1.Zip(ss2, (s1, s2) => _syllableDistance.GetDistance(s1, s2)).Sum(d => d);

            return(candidates.Min(d => d) / length);
        }
Beispiel #6
0
        public Tuple <double, double, WordForm>[] GetNeighbors(string form, int take = 10)
        {
            var wordForm = _nounsByForm[form].First();

            var scoredNeighbors = _nouns
                                  .Select(wf =>
            {
                double distance   = _distance.GetDistance(wordForm, wf);
                double similarity = 1 / (1 + distance);
                return(Tuple.Create(similarity, distance, wf));
            })
                                  .OrderByDescending(t => t.Item1)
                                  .Take(take);

            return(scoredNeighbors.ToArray());
        }
Beispiel #7
0
        public Vector2D[] ComputeLayout(LayoutSettings settings, Vector2D[] initLayout)
        {
            if (settings == null)
            {
                settings = new LayoutSettings();
            }
            if (mNumPoints == 1)
            {
                return(settings.AdjustLayout(new Vector2D[] { new Vector2D() }));
            }                                                                                         // trivial case
            const double eps = 0.00001;

            Vector2D[] layout = new Vector2D[mNumPoints];
            // initialize layout
            if (initLayout != null)
            {
                int initLen = Math.Min(mNumPoints, initLayout.Length);
                Array.Copy(initLayout, layout, initLen);
                for (int i = initLayout.Length; i < mNumPoints; i++)
                {
                    layout[i] = new Vector2D(mRnd.NextDouble(), mRnd.NextDouble());
                }
            }
            else
            {
                for (int i = 0; i < mNumPoints; i++)
                {
                    layout[i] = new Vector2D(mRnd.NextDouble(), mRnd.NextDouble());
                }
            }
            // main optimization loop
            double globalStress = 0, stressDiff = 0;
            double oldGlobalStress = double.MaxValue;

            for (int step = 0; step < mMaxSteps; step++)
            {
                globalStress = 0;
                for (int i = 0; i < mNumPoints; i++)
                {
                    double   div    = 0;
                    Vector2D newPos = new Vector2D(0, 0);
                    for (int j = 0; j < mNumPoints; j++)
                    {
                        if (i != j)
                        {
                            double dIj = mDistFunc.GetDistance(i, j);
                            if (dIj < eps)
                            {
                                dIj = eps;
                            }
                            double wIj       = 1.0 / Math.Pow(dIj, 2);
                            double xIMinusXJ = layout[i].X - layout[j].X;
                            double yIMinusYJ = layout[i].Y - layout[j].Y;
                            double denom     = Math.Sqrt(Math.Pow(xIMinusXJ, 2) + Math.Pow(yIMinusYJ, 2));
                            if (denom < eps)
                            {
                                denom = eps;
                            }                                 // avoid dividing by zero
                            div      += wIj;
                            newPos.X += wIj * (layout[j].X + dIj * (xIMinusXJ / denom));
                            newPos.Y += wIj * (layout[j].Y + dIj * (yIMinusYJ / denom));
                            if (i < j)
                            {
                                Vector2D diff = layout[i] - layout[j];
                                globalStress += wIj * Math.Pow(diff.GetLength() - dIj, 2);
                            }
                        }
                    }
                    layout[i].X = newPos.X / div;
                    layout[i].Y = newPos.Y / div;
                }
                stressDiff = oldGlobalStress - globalStress;
                if ((step - 1) % 100 == 0)
                {
                    mLogger.Info("ComputeLayout", "Global stress: {0:0.00} Diff: {1:0.0000}", globalStress, stressDiff);
                }
                oldGlobalStress = globalStress;
                if (stressDiff <= mMinDiff)
                {
                    break;
                }
            }
            mLogger.Info("ComputeLayout", "Final global stress: {0:0.00} Diff: {1:0.0000}", globalStress, stressDiff);
            return(settings.AdjustLayout(layout));
        }