aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/FileOrganization/NameUtils.cs
blob: d50696d81a4cf6e3f356f9db801b0d87a10353ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using MediaBrowser.Common.Extensions;
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<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)
        {
            // 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 + " ";

            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();
        }

        /// <summary>
        /// Removes the diacritics.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns>System.String.</returns>
        private static string RemoveDiacritics(string text)
        {
            return String.Concat(
                text.Normalize(NormalizationForm.FormD)
                .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
                                              UnicodeCategory.NonSpacingMark)
              ).Normalize(NormalizationForm.FormC);
        }
    }
}