From 3eb4091808735858b01855d298226d239be464af Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 3 Nov 2016 02:37:52 -0400 Subject: move additional classes to new server lib --- .../FileOrganization/NameUtils.cs | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Emby.Server.Implementations/FileOrganization/NameUtils.cs (limited to 'Emby.Server.Implementations/FileOrganization/NameUtils.cs') diff --git a/Emby.Server.Implementations/FileOrganization/NameUtils.cs b/Emby.Server.Implementations/FileOrganization/NameUtils.cs new file mode 100644 index 000000000..eb22ca4ea --- /dev/null +++ b/Emby.Server.Implementations/FileOrganization/NameUtils.cs @@ -0,0 +1,81 @@ +using MediaBrowser.Model.Extensions; +using MediaBrowser.Controller.Entities; +using System; +using System.Globalization; +using MediaBrowser.Controller.Extensions; + +namespace Emby.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) + { + name = name.RemoveDiacritics(); + + name = " " + name + " "; + + name = name.Replace(".", " ") + .Replace("_", " ") + .Replace(" and ", " ") + .Replace(".and.", " ") + .Replace("&", " ") + .Replace("!", " ") + .Replace("(", " ") + .Replace(")", " ") + .Replace(":", " ") + .Replace(",", " ") + .Replace("-", " ") + .Replace("'", " ") + .Replace("[", " ") + .Replace("]", " ") + .Replace(" a ", String.Empty, StringComparison.OrdinalIgnoreCase) + .Replace(" the ", String.Empty, StringComparison.OrdinalIgnoreCase) + .Replace(" ", String.Empty); + + return name.Trim(); + } + } +} -- cgit v1.2.3