public static void TestDistance(IStringDistance instance)
        {
            Assert.Equal(0.0, instance.Distance("", ""), 1);
            Assert.Equal(3.0, instance.Distance("", "foo"), 1);
            Assert.Equal(3.0, instance.Distance("foo", ""), 1);

            AssertArgumentNullExceptions(instance);
        }
Ejemplo n.º 2
0
        private static void CheckTitlesFuzzy(IStringDistance search, HashSet <string> languages, SVR_AnimeSeries a, string query,
                                             ref ConcurrentDictionary <SVR_AnimeSeries, Tuple <double, string> > distLevenshtein, int limit)
        {
            if (distLevenshtein.Count >= limit)
            {
                return;
            }
            if (a?.Contract?.AniDBAnime?.AnimeTitles == null)
            {
                return;
            }
            var    dist  = double.MaxValue;
            string match = string.Empty;

            var seriesTitles = a.Contract.AniDBAnime.AnimeTitles
                               .Where(b => languages.Contains(b.Language.ToLower()) &&
                                      b.TitleType != Shoko.Models.Constants.AnimeTitleType.ShortName).Select(b => b.Title)
                               .ToList();

            foreach (string title in seriesTitles)
            {
                if (string.IsNullOrWhiteSpace(title))
                {
                    continue;
                }
                var result = 0.0;
                // Check for exact match
                if (!title.Equals(query, StringComparison.Ordinal))
                {
                    result = search.Distance(title, query);
                }
                // For Dice, 1 is no reasonable match
                if (result >= 1)
                {
                    continue;
                }
                // Don't count an error as liberally when the title is short
                if (title.Length < 5 && result > 0.8)
                {
                    continue;
                }
                if (result < dist)
                {
                    match = title;
                    dist  = result;
                }
                else if (Math.Abs(result - dist) < 0.00001)
                {
                    if (title.Length < match.Length)
                    {
                        match = title;
                    }
                }
            }
            // Keep the lowest distance, then by shortest title
            if (dist < double.MaxValue)
            {
                distLevenshtein.AddOrUpdate(a, new Tuple <double, string>(dist, match),
                                            (key, oldValue) =>
                {
                    if (oldValue.Item1 < dist)
                    {
                        return(oldValue);
                    }
                    if (Math.Abs(oldValue.Item1 - dist) < 0.00001)
                    {
                        return(oldValue.Item2.Length < match.Length
                                ? oldValue
                                : new Tuple <double, string>(dist, match));
                    }

                    return(new Tuple <double, string>(dist, match));
                });
            }
        }
 public static void AssertArgumentNullExceptions(IStringDistance instance)
 {
     Assert.Throws(typeof(ArgumentNullException), () => instance.Distance(null, null));
     Assert.Throws(typeof(ArgumentNullException), () => instance.Distance(null, ""));
     Assert.Throws(typeof(ArgumentNullException), () => instance.Distance("", null));
 }