public void CompareTest(string first, string second, double expectedResult)
        {
            //Act
            var result = StringsComparison.CompareStrings(first, second);

            //Assert
            Assert.AreEqual(expectedResult, result);
        }
        public void CompareStrings_MultipleMatch()
        {
            double result = StringsComparison.CompareStrings("testtest", "test");

            Assert.AreEqual(.5, result);

            result = StringsComparison.CompareStrings("test", "testtest ");
            Assert.AreEqual(.5, result);
        }
예제 #3
0
        /// <summary>
        /// Searching for duplicates in string. Indicate duplicates according to given threshold.
        /// </summary>
        /// <param name="str">String of values separated by \n or \r or \r\n</param>
        /// <param name="threshold">Threshold</param>
        /// <returns> String of duplicates in format of Dup1~Dup2 separated by NewLine</returns>
        private static async Task <string> FindDuplicates([NotNull] string str, double threshold)
        {
            return(await Task.Run(() =>
            {
                if (string.IsNullOrEmpty(str))
                {
                    return "";
                }

                var result = new Dictionary <string, List <string> >();
                var lines = str.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

                foreach (var line1 in lines)
                {
                    foreach (var line2 in lines)
                    {
                        if (line1 == line2)
                        {
                            continue;
                        }

                        if (StringsComparison.CompareStrings(line1, line2) < threshold)
                        {
                            continue;
                        }

                        if (result.TryGetValue(line2, out var list))
                        {
                            list.Add(line1);
                        }
                        else
                        {
                            result.Add(line2, new List <string> {
                                line1
                            });
                        }
                    }
                }

                return result.Aggregate(new StringBuilder(),
                                        (rslt, item) => rslt.AppendFormat("{0}{1}~{2}", rslt.Length > 0 ? "\n" : "", item.Key, string.Join(";", item.Value)),
                                        rslt => rslt.ToString());
            }));
        }
예제 #4
0
 /// <summary>
 /// Compare two strings
 /// </summary>
 /// <param name="str1">First string</param>
 /// <param name="str2">Second string</param>
 /// <returns> Similarity in range from 0 to 1 </returns>
 private static async Task <double> Compare(string str1, string str2)
 {
     return(await Task.Run(() => StringsComparison.CompareStrings(str1, str2)));
 }
        public void CompareStrings_MatchingStrings()
        {
            double result = StringsComparison.CompareStrings("test", "test");

            Assert.AreEqual(1, result);
        }
        public void CompareStrings_EmptyStrings()
        {
            double result = StringsComparison.CompareStrings("", "");

            Assert.AreEqual(0, result);
        }
        public void CompareStrings_NoMatchStrings()
        {
            double result = StringsComparison.CompareStrings("test", "world");

            Assert.AreEqual(0, result);
        }
        public void CompareStrings_PartialMatchingStrings()
        {
            double result = StringsComparison.CompareStrings("test", "test1");

            Assert.AreEqual(.8, result);
        }
 public void StringComparisonExceptionsTest(string first, string second)
 {
     Assert.Throws <ArgumentNullException>(() => StringsComparison.CompareStrings(first, second));
 }