Ejemplo n.º 1
0
        public void TestSimilarity()
        {
            var instance = new SorensenDice(2);

            // AB BC CD DE DF FG
            // 1  1  1  1  0  0
            // 1  1  1  0  1  1
            // => 2 x 3 / (4 + 5) = 6/9 = 0.6666
            var result = instance.Similarity("ABCDE", "ABCDFG");

            Assert.Equal(
                expected: 0.6667, // last digit should be 7 because of rounding
                actual: result,
                precision: 4      //0.0001
                );
        }
Ejemplo n.º 2
0
        public ActionResult <IEnumerable <SeriesSearchResult> > Search(string query, int limit = int.MaxValue)
        {
            SorensenDice search = new SorensenDice();

            query = query.ToLowerInvariant();
            query = query.Replace("+", " ");

            List <SeriesSearchResult>       seriesList = new List <SeriesSearchResult>();
            ParallelQuery <SVR_AnimeSeries> allSeries  = RepoFactory.AnimeSeries.GetAll()
                                                         .Where(a => a?.Contract?.AniDBAnime?.AniDBAnime != null &&
                                                                !a.Contract.AniDBAnime.Tags.Select(b => b.TagName)
                                                                .FindInEnumerable(User.GetHideCategories()))
                                                         .AsParallel();

            HashSet <string> languages = new HashSet <string> {
                "en", "x-jat"
            };

            languages.UnionWith(ServerSettings.Instance.LanguagePreference);
            var distLevenshtein = new ConcurrentDictionary <SVR_AnimeSeries, Tuple <double, string> >();

            allSeries.ForAll(a => CheckTitlesFuzzy(search, languages, a, query, ref distLevenshtein, limit));

            var tempListToSort = distLevenshtein.Keys.GroupBy(a => a.AnimeGroupID).Select(a =>
            {
                var tempSeries = a.ToList();
                tempSeries.Sort((j, k) =>
                {
                    var result1    = distLevenshtein[j];
                    var result2    = distLevenshtein[k];
                    var exactMatch = result1.Item1.CompareTo(result2.Item1);
                    if (exactMatch != 0)
                    {
                        return(exactMatch);
                    }

                    string title1 = j.GetSeriesName();
                    string title2 = k.GetSeriesName();
                    if (title1 == null && title2 == null)
                    {
                        return(0);
                    }
                    if (title1 == null)
                    {
                        return(1);
                    }
                    if (title2 == null)
                    {
                        return(-1);
                    }
                    return(string.Compare(title1, title2, StringComparison.InvariantCultureIgnoreCase));
                });
                var result = new SearchGrouping
                {
                    Series   = a.OrderBy(b => b.AirDate).ToList(),
                    Distance = distLevenshtein[tempSeries[0]].Item1,
                    Match    = distLevenshtein[tempSeries[0]].Item2
                };
                return(result);
            });

            Dictionary <SVR_AnimeSeries, Tuple <double, string> > series = tempListToSort.OrderBy(a => a.Distance)
                                                                           .ThenBy(a => a.Match.Length).SelectMany(a => a.Series).ToDictionary(a => a, a => distLevenshtein[a]);

            foreach (KeyValuePair <SVR_AnimeSeries, Tuple <double, string> > ser in series)
            {
                seriesList.Add(new SeriesSearchResult(HttpContext, ser.Key, ser.Value.Item2, ser.Value.Item1));
                if (seriesList.Count >= limit)
                {
                    break;
                }
            }

            return(seriesList);
        }
        public void TestDistance()
        {
            var instance = new SorensenDice();

            NullEmptyTests.TestDistance(instance);
        }