Ejemplo n.º 1
0
        public void TestTriangleInequality()
        {
            var top = Top1000.Take(50).ToArray();

            for (var i = 0; i < top.Length; i++)
            {
                for (var j = 0; j < top.Length; j++)
                {
                    if (j == i)
                    {
                        continue;
                    }

                    for (var k = 0; k < top.Length; k++)
                    {
                        if (k == i || k == j)
                        {
                            continue;
                        }

                        var string1 = top[i];
                        var string2 = top[j];
                        var string3 = top[k];

                        var editDistance12 = EditDistance.GetEditDistance(string1, string2);
                        var editDistance13 = EditDistance.GetEditDistance(string1, string3);
                        var editDistance23 = EditDistance.GetEditDistance(string2, string3);

                        Assert.True(editDistance13 <= editDistance12 + editDistance23);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Top1000Test()
        {
            for (var i = 0; i < Top1000.Length; i++)
            {
                var source = Top1000[i];
                for (var j = 0; j < Top1000.Length; j++)
                {
                    var target        = Top1000[j];
                    var editDistance1 = EditDistance.GetEditDistance(source, target);

                    if (i == j)
                    {
                        Assert.Equal(0, editDistance1);
                    }

                    if (editDistance1 == 0)
                    {
                        Assert.Equal(i, j);
                    }

                    Assert.True(editDistance1 >= 0);

                    var editDistance2 = EditDistance.GetEditDistance(source, target, editDistance1);
                    Assert.Equal(editDistance1, editDistance2);
                }
            }
        }
Ejemplo n.º 3
0
        public void EditDistance11()
        {
            var editDistance = EditDistance.GetEditDistance("book", "moons", 1);

            Assert.Equal(editDistance, EditDistance.BeyondThreshold);
            VerifyEditDistance("book", "moons", 3);
        }
Ejemplo n.º 4
0
 public void EditDistance3()
 {
     Assert.Equal(EditDistance.GetEditDistance("", "aaa"), 3);
     Assert.Equal(EditDistance.GetEditDistance("aaa", ""), 3);
     Assert.Equal(EditDistance.GetEditDistance("aaa", "bbb"), 3);
     Assert.Equal(EditDistance.GetEditDistance("aaab", "a"), 3);
     Assert.Equal(EditDistance.GetEditDistance("a", "aaab"), 3);
     Assert.Equal(EditDistance.GetEditDistance("aababbab", "abababaa"), 3);
 }
Ejemplo n.º 5
0
 public void EditDistance1()
 {
     Assert.Equal(EditDistance.GetEditDistance("", "a"), 1);
     Assert.Equal(EditDistance.GetEditDistance("a", ""), 1);
     Assert.Equal(EditDistance.GetEditDistance("a", "b"), 1);
     Assert.Equal(EditDistance.GetEditDistance("ab", "a"), 1);
     Assert.Equal(EditDistance.GetEditDistance("a", "ab"), 1);
     Assert.Equal(EditDistance.GetEditDistance("aabb", "abab"), 1);
 }
Ejemplo n.º 6
0
        void DoTest(string s1, string s2, string expected, Func <char, char, int> cost)
        {
            var(dist, edits) = EditDistance.GetEditDistance(
                s1.ToList(),
                s2.ToList(),
                cost
                );
            string actual = $"{dist} {string.Join(",", edits.Select(e => e))}";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 7
0
        public void GetEditDistance_Returns0_GivenIdenticalStrings()
        {
            //Arrange
            string first  = "Some string!";
            string second = "Some string!";

            int expected = 0;

            //Act
            int actual = EditDistance.GetEditDistance(first, second);

            //Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public void GetEditDistance_Returns5_GivenLongerStringTop()
        {
            //Arrange
            string first  = "Some string!12345";
            string second = "Some string!";

            int expected = 5;

            //Act
            int actual = EditDistance.GetEditDistance(first, second);

            //Assert
            Assert.AreEqual(expected, actual);
        }
        // private
        private static void Test(string inStr)
        {
            // candidates with ED: 1
            HashSet <string> candSet1 = GetCandidatesByEd1(inStr);

            // print out
            Console.WriteLine("-- inStr: [" + inStr + "]");
            Console.WriteLine("-- candSet1.size(): " + candSet1.Count);
            bool caseFlag = false;             // not case sensitive
            int  dupNo    = 0;

            // check if the candList correct
            foreach (string cand1 in candSet1)
            {
                int ed = EditDistance.GetEditDistance(inStr, cand1, caseFlag);
                // these are errors, should not have any
                if ((ed != 1) && (inStr.Equals(cand1) == false))
                {
                    Console.WriteLine(inStr + "|" + cand1 + "|" + ed);
                }
                // candidate are  same as inStr, produced by replace, not 0
                if (inStr.ToLower().Equals(cand1) == true)
                {
                    dupNo++;
                }
            }
            Console.WriteLine("-- dupNo: " + dupNo);
            //System.out.println(candSet1);
            // candidates with ED: 2
            /// <summary>
            /// HashSet<String> candSet2 = GetCandidatesByEd2(candList1);
            /// int exceedNo = 0;
            /// for(String cand2:candSet2)
            /// {
            ///    int ed = EditDistance.GetEditDistance(inStr, cand2, caseFlag);
            ///    if((ed > 2) && (inStr.equals(cand2) == false))
            ///    {
            ///        System.out.println(inStr + "|" + cand2 + "|" + ed);
            ///        exceedNo++;
            ///    }
            /// }
            /// System.out.println("-- candSet2.size(): " + candSet2.size());
            /// System.out.println("-- exceedNo: " + exceedNo);
            ///
            /// </summary>
        }
Ejemplo n.º 10
0
        // test all split
        private static void TestSplitUtil(string inStr)
        {
            Console.WriteLine("====== test split util (no Dic check) ======");
            Console.WriteLine("----- inStr: [" + inStr + "]");
            // Test possible split combination with 1 split
            HashSet <string> splitSet1 = GetSplitSetBy1Space(inStr);
            bool             caseFlag  = false; // not case sensitive
            int dupNo = 0;

            // print out
            Console.WriteLine("----- Check on candList1 -----");
            Console.WriteLine("-- splitSet1.size(): " + splitSet1.Count);
            Console.WriteLine(splitSet1.ToStringList());
            // check if the candList1 correct by edit distance
            foreach (string split1 in splitSet1)
            {
                int ed = EditDistance.GetEditDistance(inStr, split1, caseFlag);
                // these are errors: because 1 split should have ed = 1
                if ((ed != 1) && (inStr.Equals(split1) == false))
                {
                    Console.WriteLine("**ERR: " + inStr + "|" + split1 + "|" + ed);
                }
                // candidate are same as inStr, produced by replace, not 0
                // check duplicate no.
                else if (inStr.Equals(split1) == true)
                {
                    dupNo++;
                }
            }
            Console.WriteLine("-- dupNo: " + dupNo);
            HashSet <string> candSet1a = GetSplitSetBySpaces(inStr, 1);

            Console.WriteLine("-- candSet1a.size(): " + candSet1a.Count);
            Console.WriteLine(candSet1a.ToStringList());
            HashSet <string> candSet2a = GetSplitSetBySpaces(inStr, 2);

            Console.WriteLine("-- candList2a.size(): " + candSet2a.Count);
            Console.WriteLine(candSet2a.ToStringList());
            // other test 5 for spliting candidates by hyphen
            string testStr = "-123--45-test-123-45.6-";

            Console.WriteLine("- testStr: " + testStr);
            Console.WriteLine("- splitByHyphen: [" + GetSplitByPunc(testStr, '-') + "]");
        }
Ejemplo n.º 11
0
        public void IsCloseTo_ReturnsFalse_GivenInsimilarStringsThreshold0_5()
        {
            //Arrange
            string first     = "Some string!";
            string second    = "Elephant Mongoose!";
            double threshold = 0.50;

            bool expected = false;

            //Act
            bool actual = EditDistance.IsCloseTo(first, second, threshold);

            //Assert
            if (expected != actual)
            {
                int distance = EditDistance.GetEditDistance(first, second);
                throw new Exception($"Edit distance was {distance}");
            }
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 12
0
        private static void VerifyEditDistance(string s, string t, int expectedEditDistance)
        {
            // We want the full edit distance, without bailing out early because we crossed the
            // threshold.
            var editDistance1 = EditDistance.GetEditDistance(s, t);

            Assert.Equal(expectedEditDistance, editDistance1);

            // Edit distances are symmetric.
            var editDistance2 = EditDistance.GetEditDistance(t, s);

            Assert.Equal(editDistance1, editDistance2);

            // If we set hte edit distance as our threshold, we should still find the value.
            var editDistance3 = EditDistance.GetEditDistance(s, t, editDistance1);

            Assert.Equal(editDistance1, editDistance3);

            if (editDistance1 > 0)
            {
                var editDistance4 = EditDistance.GetEditDistance(s, t, editDistance1 - 1);
                Assert.Equal(editDistance4, EditDistance.BeyondThreshold);
            }
        }
Ejemplo n.º 13
0
 private static void AssertEditDistance(int expected, string one, string two)
 {
     Assert.AreEqual(expected, EditDistance.GetEditDistance(one, two));
     Assert.AreEqual(expected, EditDistance.GetEditDistance(two, one));
 }
Ejemplo n.º 14
0
 public void MoreEditDistance()
 {
     Assert.Equal(EditDistance.GetEditDistance("barking", "corkliness"), 6);
 }
 public double GetEditDistance(string target)
 => _underlyingObject.GetEditDistance(target);
Ejemplo n.º 16
0
        public static List <ListEdit> GetListEdits(
            IReadOnlyList <IListItem> list1,
            IReadOnlyList <IListItem> list2
            )
        {
            var result = new List <ListEdit>();

            var edits = EditDistance.GetEditDistance(list1, list2, (c1, c2) =>
            {
                return
                (c1 == null || c2 == null ? 1 : // let deletion/insertion of a node to cost 1
                 c1.Key == c2.Key ? 0 :         // encourage reuse of nodes with same Key by giving it no cost,
                 1);                            // reuse nodes with different Keys
            }).edits;
            int targetIdx = 0;

            foreach (var(i, j, _) in edits)
            {
                if (i.HasValue && j.HasValue)
                {
                    var i1 = list1[i.Value];
                    var i2 = list2[j.Value];
                    if (i1 != i2)
                    {
                        result.Add(new ListEdit
                        {
                            Type    = EditType.Reuse,
                            OldItem = i1,
                            Item    = i2,
                            Index   = targetIdx
                        });
                    }
                    if (i2.IsSelected != i1.IsSelected)
                    {
                        result.Add(new ListEdit
                        {
                            Type  = i2.IsSelected ? EditType.Select : EditType.Deselect,
                            Item  = i2,
                            Index = targetIdx
                        });
                    }
                    ++targetIdx;
                }
                else if (i.HasValue)
                {
                    result.Add(new ListEdit
                    {
                        Type    = EditType.Delete,
                        OldItem = list1[i.Value],
                        Index   = targetIdx
                    });
                }
                else
                {
                    var i2 = list2[j.Value];
                    result.Add(new ListEdit
                    {
                        Type  = EditType.Insert,
                        Item  = i2,
                        Index = targetIdx
                    });
                    if (i2.IsSelected)
                    {
                        result.Add(new ListEdit
                        {
                            Type  = EditType.Select,
                            Item  = i2,
                            Index = targetIdx
                        });
                    }
                    ++targetIdx;
                }
            }

            return(result);
        }
Ejemplo n.º 17
0
 public void EditDistance0()
 {
     Assert.Equal(EditDistance.GetEditDistance("", ""), 0);
     Assert.Equal(EditDistance.GetEditDistance("a", "a"), 0);
 }
Ejemplo n.º 18
0
        public static List <TreeEdit> GetTreeEdits(ITreeNode root1, ITreeNode root2, Options options = null)
        {
            options = options ?? Options.@default;

            var result = new List <TreeEdit>();

            void AddNewNodeEdits(ITreeNode n, ITreeNode parent, int nodeIndex)
            {
                result.Add(new TreeEdit
                {
                    Type       = EditType.Insert,
                    Node       = parent,
                    NewChild   = n,
                    ChildIndex = nodeIndex
                });
                if (n.IsExpanded || options.TemporariltyExpandParentToInitChildren)
                {
                    result.Add(new TreeEdit
                    {
                        Type = EditType.Expand,
                        Node = n
                    });
                }
                if (n.IsSelected)
                {
                    result.Add(new TreeEdit
                    {
                        Type = EditType.Select,
                        Node = n
                    });
                }
                int childIndex = 0;

                foreach (var c in n.Children)
                {
                    AddNewNodeEdits(c, n, childIndex++);
                }
                if (options.TemporariltyExpandParentToInitChildren && !n.IsExpanded)
                {
                    result.Add(new TreeEdit
                    {
                        Type = EditType.Collapse,
                        Node = n
                    });
                }
            }

            void GetEdits(ITreeNode n1, ITreeNode n2)
            {
                if (n1 == n2)
                {
                    return;
                }
                var edits = EditDistance.GetEditDistance(n1.Children, n2.Children, (c1, c2) =>
                {
                    return
                    (c1 == null || c2 == null ? 1 : // let deletion/insertion of a node cost 1
                     c1.Key == c2.Key ? 0 :         // encourage reuse of nodes with same Key by giving it no cost,
                     1);                            // reuse nodes with different Keys
                }).edits;
                int targetIdx = 0;

                foreach (var(i, j, _) in edits)
                {
                    if (i.HasValue && j.HasValue)
                    {
                        var c1 = n1.Children[i.Value];
                        var c2 = n2.Children[j.Value];
                        if (c1 != c2)
                        {
                            result.Add(new TreeEdit
                            {
                                Type       = EditType.Reuse,
                                Node       = n2,
                                OldChild   = c1,
                                NewChild   = c2,
                                ChildIndex = targetIdx
                            });
                        }
                        ++targetIdx;
                        GetEdits(c1, c2);
                        if (c2.IsExpanded != c1.IsExpanded)
                        {
                            result.Add(new TreeEdit
                            {
                                Type = c2.IsExpanded ? EditType.Expand : EditType.Collapse,
                                Node = c2
                            });
                        }
                        if (c2.IsSelected != c1.IsSelected)
                        {
                            result.Add(new TreeEdit
                            {
                                Type = c2.IsSelected ? EditType.Select : EditType.Deselect,
                                Node = c2
                            });
                        }
                    }
                    else if (i.HasValue)
                    {
                        result.Add(new TreeEdit
                        {
                            Type       = EditType.Delete,
                            Node       = n2,
                            OldChild   = n1.Children[i.Value],
                            ChildIndex = targetIdx
                        });
                    }
                    else
                    {
                        AddNewNodeEdits(n2.Children[j.Value], n2, targetIdx);
                        ++targetIdx;
                    }
                }
            }

            GetEdits(root1, root2);
            return(result);
        }