From 9d40b684bf6af4e987e226c78c11d6daf6f5cd9b Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 22 Jan 2014 12:05:06 -0500 Subject: #680 - episode organization --- .../FileOrganization/NameUtils.cs | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 MediaBrowser.Server.Implementations/FileOrganization/NameUtils.cs (limited to 'MediaBrowser.Server.Implementations/FileOrganization/NameUtils.cs') diff --git a/MediaBrowser.Server.Implementations/FileOrganization/NameUtils.cs b/MediaBrowser.Server.Implementations/FileOrganization/NameUtils.cs new file mode 100644 index 000000000..75e5d92c3 --- /dev/null +++ b/MediaBrowser.Server.Implementations/FileOrganization/NameUtils.cs @@ -0,0 +1,92 @@ +using MediaBrowser.Controller.Entities; +using System; +using System.Globalization; +using System.Linq; +using System.Text; + +namespace MediaBrowser.Server.Implementations.FileOrganization +{ + public static class NameUtils + { + private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + internal static Tuple GetMatchScore(string sortedName, int? year, T series) + where T : BaseItem + { + var score = 0; + + var seriesNameWithoutYear = series.Name; + if (series.ProductionYear.HasValue) + { + seriesNameWithoutYear = seriesNameWithoutYear.Replace(series.ProductionYear.Value.ToString(UsCulture), String.Empty); + } + + if (IsNameMatch(sortedName, seriesNameWithoutYear)) + { + score++; + + if (year.HasValue && series.ProductionYear.HasValue) + { + if (year.Value == series.ProductionYear.Value) + { + score++; + } + else + { + // Regardless of name, return a 0 score if the years don't match + return new Tuple(series, 0); + } + } + } + + return new Tuple(series, score); + } + + + private static bool IsNameMatch(string name1, string name2) + { + name1 = GetComparableName(name1); + name2 = GetComparableName(name2); + + return String.Equals(name1, name2, StringComparison.OrdinalIgnoreCase); + } + + private static string GetComparableName(string name) + { + // TODO: Improve this - should ignore spaces, periods, underscores, most likely all symbols and + // possibly remove sorting words like "the", "and", etc. + + name = RemoveDiacritics(name); + + name = " " + name.ToLower() + " "; + + name = name.Replace(".", " ") + .Replace("_", " ") + .Replace("&", " ") + .Replace("!", " ") + .Replace("(", " ") + .Replace(")", " ") + .Replace(",", " ") + .Replace("-", " ") + .Replace(" a ", String.Empty) + .Replace(" the ", String.Empty) + .Replace(" ", String.Empty); + + return name.Trim(); + } + + /// + /// Removes the diacritics. + /// + /// The text. + /// System.String. + private static string RemoveDiacritics(string text) + { + return String.Concat( + text.Normalize(NormalizationForm.FormD) + .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != + UnicodeCategory.NonSpacingMark) + ).Normalize(NormalizationForm.FormC); + } + } +} -- cgit v1.2.3