diff options
| author | Luke <luke.pulverenti@gmail.com> | 2016-12-18 00:44:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-12-18 00:44:33 -0500 |
| commit | e7cebb91a73354dc3e0d0b6340c9fbd6511f4406 (patch) | |
| tree | 6f1c368c766c17b7514fe749c0e92e69cd89194a /Emby.Server.Implementations/FileOrganization/NameUtils.cs | |
| parent | 025905a3e4d50b9a2e07fbf4ff0a203af6604ced (diff) | |
| parent | aaa027f3229073e9a40756c3157d41af2a442922 (diff) | |
Merge pull request #2350 from MediaBrowser/beta
Beta
Diffstat (limited to 'Emby.Server.Implementations/FileOrganization/NameUtils.cs')
| -rw-r--r-- | Emby.Server.Implementations/FileOrganization/NameUtils.cs | 81 |
1 files changed, 81 insertions, 0 deletions
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<T, int> GetMatchScore<T>(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<T, int>(series, 0); + } + } + } + + return new Tuple<T, int>(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(); + } + } +} |
