Beispiel #1
0
        /// <summary>
        /// I may move this to a utility that strictly maps movies versus this generic "fuzzy" logic.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            bool result    = false;
            bool contains  = false;
            var  compareTo = obj as IMovie;

            if (compareTo != null)
            {
                if (Id != 0 && Id == compareTo.Id)
                {
                    result = true;
                }
                else if (Identifier != null && compareTo.Identifier != null)
                {
                    // The Identifier will trump all other equalities if they are set.
                    result = Identifier == compareTo.Identifier;

                    if (result && Day.HasValue && compareTo.Day.HasValue)
                    {
                        // If both days have values then they HAVE TO MATCH.

                        result = Day.Value == compareTo.Day.Value;
                    }
                }
                else
                {
                    // Make all the tests case insensitive.

                    var movieName     = MovieName.ToLower().Replace(" ", string.Empty);
                    var testMovieName = compareTo.MovieName.ToLower().Replace(" ", string.Empty);

                    result = movieName.Equals(testMovieName);

                    if (!result)
                    {
                        // Not an exact match so try starts with (limited contains)

                        result   = movieName.StartsWith(testMovieName) || testMovieName.StartsWith(movieName);
                        contains = result;
                    }

                    if (!result)
                    {
                        // Not an exact match so try ends with (limited contains)

                        result   = movieName.EndsWith(testMovieName) || testMovieName.EndsWith(movieName);
                        contains = result;
                    }

                    if (!result)
                    {
                        // Not an exact match so try "contains"

                        result   = movieName.IndexOf(testMovieName) > 1 || testMovieName.IndexOf(movieName) > 1;
                        contains = result;
                    }

                    if (result)
                    {
                        // Fail if there is a lot of noise.

                        if (movieName.Length > testMovieName.Length * 2 || testMovieName.Length > movieName.Length * 2)
                        {
                            // Try  to remove some of the noise.

                            var thisYear = $" {DateTime.Now.Year}";

                            movieName     = movieName.Replace(thisYear, string.Empty);
                            testMovieName = testMovieName.Replace(thisYear, string.Empty);

                            if (movieName.Length > testMovieName.Length * 2 || testMovieName.Length > movieName.Length * 2)
                            {
                                result = false;
                            }

                            if (contains)
                            {
                                var tokens1 = MovieName.Split();
                                var tokens2 = compareTo.MovieName.Split();
                                var index   = 0;

                                if (tokens1.Length == 1 || tokens2.Length == 1)
                                {
                                    // If either are a single word then the first words need to match exactly.  This may need to progress to other words.

                                    result = tokens1[0].ToLower().Equals(tokens2[0].ToLower());
                                }

                                // Basically just "starts with".

                                else if (tokens1.Length >= tokens2.Length)
                                {
                                    result = true;

                                    foreach (var token in tokens2)
                                    {
                                        if (token.ToLower() != tokens1[index++].ToLower())
                                        {
                                            result = false;
                                            break;
                                        }
                                    }
                                }
                                else if (tokens2.Length > tokens1.Length)
                                {
                                    result = true;

                                    foreach (var token in tokens1)
                                    {
                                        if (token.ToLower() != tokens2[index++].ToLower())
                                        {
                                            result = false;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (!result)
                    {
                        // Try to compare the names without the word "the"

                        result = movieName.Replace("the ", string.Empty).Replace(" the", string.Empty).Equals(testMovieName.Replace("the ", string.Empty).Replace(" the", string.Empty));
                    }

                    if (result && Day.HasValue && compareTo.Day.HasValue)
                    {
                        // If both days have values then they HAVE TO MATCH.

                        result = Day.Value == compareTo.Day.Value;
                    }
                }
            }

            return(result);
        }