Skip to content
Permalink
Browse files

Improve Search Functions

  • Loading branch information...
da3dsoul committed Nov 1, 2019
1 parent 4bc74c7 commit 14eded1d3a5c74b7a6d6ff53d9ee699211d0e23e
Showing with 17 additions and 20 deletions.
  1. +1 −1 Shoko.Commons
  2. +16 −19 Shoko.Server/Utilities/SeriesSearch.cs
Submodule Shoko.Commons updated 1 files
+4 −4 Utils/Misc.cs
@@ -61,7 +61,7 @@ public static string SanitizeFuzzy(string value, bool replaceInvalid)
if (a?.Contract?.AniDBAnime?.AniDBAnime.AllTitles == null) return;
var dist = new Misc.SearchInfo<string> {Index = -1, Distance = int.MaxValue};
string match = string.Empty;
foreach (string title in a.Contract.AniDBAnime.AnimeTitles.Select(b => b.Title).ToList())
foreach (string title in a.GetAllTitles())
{
if (string.IsNullOrEmpty(title)) continue;
int k = Math.Max(Math.Min((int) (title.Length / 6D), (int) (query.Length / 6D)), 1);
@@ -72,7 +72,7 @@ public static string SanitizeFuzzy(string value, bool replaceInvalid)
{
dist = result;
}
else if (result.Distance == dist.Distance)
else if (Math.Abs(result.Distance - dist.Distance) < 0.0001D)
{
if (title.Length < match.Length) match = title;
}
@@ -84,7 +84,7 @@ public static string SanitizeFuzzy(string value, bool replaceInvalid)
(key, oldValue) =>
{
if (oldValue.Distance < dist.Distance) return oldValue;
if (oldValue.Distance == dist.Distance)
if (Math.Abs(oldValue.Distance - dist.Distance) < 0.0001D)
return oldValue.Result.Length < dist.Result.Length
? oldValue
: dist;
@@ -123,7 +123,7 @@ public static string SanitizeFuzzy(string value, bool replaceInvalid)
if (dist.Distance < int.MaxValue)
distLevenshtein.AddOrUpdate(a, dist,
(key, oldValue) =>
Math.Min(oldValue.Distance, dist.Distance) == dist.Distance ? dist : oldValue);
Math.Abs(Math.Min(oldValue.Distance, dist.Distance) - dist.Distance) < 0.0001D ? dist : oldValue);
}

if (distLevenshtein.Count >= limit || a?.Contract?.AniDBAnime?.CustomTags == null ||
@@ -153,7 +153,8 @@ public static string SanitizeFuzzy(string value, bool replaceInvalid)

if (dist.Distance < int.MaxValue)
distLevenshtein.AddOrUpdate(a, dist,
(key, oldValue) => Math.Min(oldValue.Distance, dist.Distance) == dist.Distance ? dist : oldValue);
(key, oldValue) => Math.Abs(Math.Min(oldValue.Distance, dist.Distance) - dist.Distance) < 0.0001D
? dist : oldValue);
}

/// <summary>
@@ -247,29 +248,25 @@ private static List<SearchResult> SearchTagsEquals(string query, int limit, Para

private static List<SearchResult> SearchTitlesIndexOf(string query, int limit, ParallelQuery<SVR_AnimeSeries> allSeries)
{
List<SearchResult> seriesList = new List<SearchResult>();
string sanitizedQuery = SanitizeFuzzy(query, false);
allSeries.ForAll(series1 =>
return allSeries.Select(series1 =>
{
foreach (string title in series1.GetAllTitles())
{
int index = title.IndexOf(sanitizedQuery, StringComparison.InvariantCultureIgnoreCase);
if (index == -1) continue;
lock(seriesList)
return new SearchResult
{
seriesList.Add(new SearchResult
{
Result = series1,
Distance = 0,
ExactMatch = true,
Index = index,
Match = sanitizedQuery
});
}
Result = series1,
Distance = 0,
ExactMatch = true,
Index = index,
Match = sanitizedQuery
};
}
});

return seriesList.OrderBy(a => a.Index).ThenBy(a => a.Result.GetSeriesName()).Take(limit).ToList();
return null;
}).Where(a => a != null).OrderBy(a => a.Index).ThenBy(a => a.Result.GetSeriesName()).Take(limit).ToList();
}

private static List<SearchResult> SearchTitlesFuzzy(string query, int limit, ParallelQuery<SVR_AnimeSeries> allSeries)

0 comments on commit 14eded1

Please sign in to comment.
You can’t perform that action at this time.