public void DifrentDistnaceCosts()
        {
            var distanceCalculator = A.Fake <IDistanceCalculator <string> >();
            var correlater         = new LevenshteinCorrelater <string>(distanceCalculator, 2, 2);

            A.CallTo(() => distanceCalculator.Distance(A <string> ._, A <string> ._)).ReturnsLazily(
                (string s1, string s2) =>
            {
                if (s1 == s2)
                {
                    return(0);
                }
                if (s2 == "A" && s1 == "E")
                {
                    return(1);
                }
                return((uint)10);
            });


            var array1 = new[] { "V", "E" };
            var array2 = new[] { "T", "A" };

            var expectedResult = new CorrelaterResult <string>(5,
                                                               new[] { null, "V", "E" }, new[] { "T", null, "A" });

            correlater.AssertComparision(array1, array2, expectedResult);
        }
        public void FirstStringEmptyTest()
        {
            var s = "12345678";
            var expectedResult = new CorrelaterResult <char>(s.Length * insertionCost, "\0\0\0\0\0\0\0\0".ToCharArray(), s.ToCharArray());

            stringCorrelater.AssertComparision(string.Empty, s, expectedResult);
        }
        public void DifrentInsertionCosts2()
        {
            var removalCalculator = A.Fake <IInsertionCalculator <string> >();
            var correlater        = new LevenshteinCorrelater <string>(new BasicDistanceCalculator <string>(10), new BasicRemovalCalculator <string>(10), removalCalculator);

            A.CallTo(() => removalCalculator.InsertionCost(A <string> ._)).ReturnsLazily(
                (string s) =>
            {
                if (s == "A")
                {
                    return(1);
                }
                if (s == "B")
                {
                    return(2);
                }
                return((uint)3);
            });

            var array1 = new string[0];
            var array2 = new[] { "A", "B" };

            var expectedResult = new CorrelaterResult <string>(3, new string[] { null, null }, array2);

            correlater.AssertComparision(array1, array2, expectedResult);
        }
        public void SecondStringEmptyTest()
        {
            var s = "12345678";
            var expectedResult = new CorrelaterResult <char>(s.Length * removalCost, s.ToCharArray(), "\0\0\0\0\0\0\0\0".ToCharArray());

            stringCorrelater.AssertComparision(s, string.Empty, expectedResult);
        }
Ejemplo n.º 5
0
        private CorrelaterResult <T> GetPartualResult(CorrelaterResult <T> retult, int startIndex, int endIndex)
        {
            var array1 = retult.BestMatch1.Skip(startIndex).Take(endIndex - startIndex).ToArray();
            var array2 = retult.BestMatch2.Skip(startIndex).Take(endIndex - startIndex).ToArray();

            return(new CorrelaterResult <T>(retult.Distance, array1, array2));
        }
        public CorrelaterResult <char> StringCorrelaterExsample()
        {
            uint removalCost = 1, insertionCost = 1;

            // You'll need to implement your own IDistanceCalculator<char>.
            // IDistanceCalculator defines the "distance" between any two elements.
            IDistanceCalculator <char> distanceCalculator = new MyDistanceCalculator <char>();

            // The library contains a number of ICorrelaters.
            // LevenshteinCorrelater uses dynamic programing to find the Levenshtein-distance between the two collections.
            // Since a string is actually an IEnumerable<char>, we need to use an ICorrelater<char>.
            ICorrelater <char> correlater =
                new LevenshteinCorrelater <char>(distanceCalculator, removalCost, insertionCost);

            string string1 = "ABC";
            string string2 = "ADC";

            // Correlate the strings.
            CorrelaterResult <char> result = correlater.Correlate(string1, string2);

            // Print some of the result
            Console.WriteLine(result.Distance);
            Console.WriteLine(result.BestMatch1);
            Console.WriteLine(result.BestMatch2);

            return(result);
        }
        [DataRow("1234", "5678", 0)]             // Diff eveywhere
        public void IContinuousCorrelaterTest_InnerCorrelaterIsContinuous(string string1, string string2, int extraTimesToCallOrResultUpdate)
        {
            var levenshteinCorrelater   = new LevenshteinCorrelater <char>(missmatchCost, removalInsertionCost, removalInsertionCost);
            var splitToChunksCorrelater = new SplitToChunksCorrelaterWrapper <char>(levenshteinCorrelater, 2);
            var correlater = new IgnoreIdenticalBeginningAndEndCorrelaterWrapper <char>(splitToChunksCorrelater);

            var match1        = new List <char>();
            var match2        = new List <char>();
            var totalDistance = 0L;
            var totalUpdates  = 0;
            var innerUpdates  = 0;

            correlater.OnResultUpdate += result =>
            {
                match1.AddRange(result.BestMatch1);
                match2.AddRange(result.BestMatch2);
                totalDistance += result.Distance;
                totalUpdates++;
            };
            splitToChunksCorrelater.OnResultUpdate += _ => innerUpdates++;

            var actualResult    = correlater.Correlate(string1, string2);
            var resultFromEvent = new CorrelaterResult <char>(totalDistance, match1.ToArray(), match2.ToArray());

            Assertions.AssertResultIsAsExpected(actualResult, resultFromEvent);
            Assert.AreEqual(extraTimesToCallOrResultUpdate + innerUpdates, totalUpdates, $"Got wrong number of {nameof(totalUpdates)}");
        }
Ejemplo n.º 8
0
        private void AssertWeGetTheRightLcs <T>(T[] collection1, T[] collection2, T[] expectedLcs)
        {
            var result = new CorrelaterResult <T>(0, collection1, collection2);
            var lcs    = result.GetLcsFromResult();

            CollectionAssert.AreEqual(expectedLcs, lcs.ToList());
        }
Ejemplo n.º 9
0
        public void Correlate_SameArrays()
        {
            var array = new[] { "A", "B", "C" };

            var expectedResult = new CorrelaterResult <string>(0, array, array);

            correlater.AssertComparision(array, array, expectedResult);
        }
Ejemplo n.º 10
0
        public void FirstStringEmptyTest()
        {
            var s              = "12345678";
            var correlater     = new HuntSzymanskiCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(s.Length, "\0\0\0\0\0\0\0\0".ToCharArray(), s.ToCharArray());

            correlater.AssertComparision(string.Empty, s, expectedResult);
        }
        public void Correlate_Strings2MuchLongerThanString1()
        {
            innerCorrelater.SetToRetrunInputCollection(0);

            var expectedResult = new CorrelaterResult <char>(-1, AddNullToEnd(new[] { 'a' }, LONG_STRING.Length - 1).ToArray(), LONG_STRING.ToCharArray());

            levenshteinCorrelater.AssertComparision("a", LONG_STRING, expectedResult);
        }
        public void Correlate_NoOffset_DontMissPartOfMatch()
        {
            innerCorrelater.SetToRetrunInputCollection(0);

            var expectedResult = new CorrelaterResult <char>(0, LONG_STRING.ToCharArray(), LONG_STRING.ToCharArray());

            correlater.AssertComparision(LONG_STRING, LONG_STRING, expectedResult);
        }
Ejemplo n.º 13
0
        public void SameStringTest()
        {
            var s              = "abcdefg";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(0, s.ToCharArray(), s.ToCharArray());

            correlater.AssertComparision(s, s, expectedResult);
        }
        public void SameString_ReturnRightMatch()
        {
            var s = "abcdefg";

            var expectedResult = new CorrelaterResult <char>(0, s.ToArray(), s.ToArray());

            levenshteinCorrelater.AssertComparision(s, s, expectedResult);
        }
        public void SecondStringEmptyTest()
        {
            var s              = "12345678";
            var correlater     = new MyersAlgorithmCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(s.Length, s.ToCharArray(), "\0\0\0\0\0\0\0\0".ToCharArray());

            correlater.AssertComparision(s, string.Empty, expectedResult);
        }
Ejemplo n.º 16
0
        public void ComplexString2()
        {
            var s1             = "1ya9az888b2";
            var s2             = "2ya7az6b4";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(10, "1\0ya9\0az888\0b2\0".ToCharArray(), "\02ya\07az\0\0\06b\04".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 17
0
        public void SameStringInTheMiddle()
        {
            var s1             = "123abc456";
            var s2             = "78abc";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(8, "123\0\0abc456".ToCharArray(), "\0\0\078abc\0\0\0".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 18
0
        public void DifferentStart()
        {
            var s1             = "123abc";
            var s2             = "78abc";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(5, "123\0\0abc".ToCharArray(), "\0\0\078abc".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 19
0
        public void OneElementTheSame()
        {
            var s1             = "1a3";
            var s2             = "5a6";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(4, "1\0a3\0".ToCharArray(), "\05a\06".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 20
0
        public void EndTheSame()
        {
            var s1             = "1a";
            var s2             = "5a";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(2, "1\0a".ToCharArray(), "\05a".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 21
0
        public void DifferentCharTest()
        {
            var s1             = "abc1efg";
            var s2             = "abc2efg";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(2, "abc1\0efg".ToCharArray(), "abc\02efg".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 22
0
        public void Correlate_OneElementToRemove()
        {
            var array1 = new[] { "A", "B", "C" };
            var array2 = new[] { "A", "C" };

            var expectedResult = new CorrelaterResult <string>(removalCost, array1, new[] { "A", null, "C" });

            correlater.AssertComparision(array1, array2, expectedResult);
        }
        public void MissingFirstChar_ReturnRightMatch()
        {
            var s1 = "abcdefg";
            var s2 = "bcdefg";

            var expectedResult = new CorrelaterResult <char>(removalInsertionCost, s1.ToArray(), "\0bcdefg".ToArray());

            levenshteinCorrelater.AssertComparision(s1, s2, expectedResult);
        }
        public void DifferentFirstChar_ReturnRightMatch()
        {
            var s1 = "abcdefg";
            var s2 = "zbcdefg";

            var expectedResult = new CorrelaterResult <char>(missmatchCost, s1.ToArray(), s2.ToArray());

            levenshteinCorrelater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 25
0
        public void OneRemovedCharInStringsTest()
        {
            var s1             = "abcdefg";
            var s2             = "abcefg";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(1, s1.ToCharArray(), "abc\0efg".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 26
0
        public void TotallyDifferentStringsTest()
        {
            var s1             = "abcdefghij";
            var s2             = "12345678";
            var correlater     = new HuntSzymanskiCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(s1.Length + s2.Length, "\0\0\0\0\0\0\0\0abcdefghij".ToCharArray(), "12345678\0\0\0\0\0\0\0\0\0\0".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 27
0
        public void TwoAddedCharsInStringsTest()
        {
            var s1             = "abcfg";
            var s2             = "abcdefg";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(2, "abc\0\0fg".ToCharArray(), s2.ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 28
0
        public void Correlate_TotalMismatch()
        {
            var array1 = new[] { "A", "B", "C" };
            var array2 = new[] { "D", "E", "F" };

            var expectedResult = new CorrelaterResult <string>(3 * (removalCost + insertionCost), new[] { null, null, null, "A", "B", "C" }, new[] { "D", "E", "F", null, null, null });

            correlater.AssertComparision(array1, array2, expectedResult);
        }
Ejemplo n.º 29
0
        public void TotallyDifferentStrings()
        {
            var s1             = "abcdefg";
            var s2             = "1234567";
            var correlater     = new PatienceDiffCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(14, "abcdefg\0\0\0\0\0\0\0".ToCharArray(), "\0\0\0\0\0\0\01234567".ToCharArray());

            correlater.AssertComparision(s1, s2, expectedResult);
        }
Ejemplo n.º 30
0
        public void DoubleCharTest2()
        {
            var s = "123421";

            var correlater     = new HuntSzymanskiCorrelater <char>();
            var expectedResult = new CorrelaterResult <char>(0, s.ToCharArray(), s.ToCharArray());

            correlater.AssertComparision(s, s, expectedResult);
        }