improve name matching

This commit is contained in:
Luke Pulverenti 2015-04-09 17:11:57 -04:00
parent 1b549adf0f
commit 1b600aee37
26 changed files with 81 additions and 43 deletions

View File

@ -259,7 +259,7 @@ namespace MediaBrowser.Api
.GetRecursiveChildren(i => i is IHasArtist)
.Cast<IHasArtist>()
.SelectMany(i => i.AllArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.FirstOrDefault(i =>
{
i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar));
@ -281,7 +281,7 @@ namespace MediaBrowser.Api
return libraryManager.RootFolder.GetRecursiveChildren()
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.FirstOrDefault(i =>
{
i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar));
@ -301,7 +301,7 @@ namespace MediaBrowser.Api
return libraryManager.RootFolder
.GetRecursiveChildren(i => i is Game)
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.FirstOrDefault(i =>
{
i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar));
@ -324,7 +324,7 @@ namespace MediaBrowser.Api
return libraryManager.RootFolder
.GetRecursiveChildren()
.SelectMany(i => i.Studios)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.FirstOrDefault(i =>
{
i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar));
@ -348,7 +348,7 @@ namespace MediaBrowser.Api
.GetRecursiveChildren()
.SelectMany(i => i.People)
.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.FirstOrDefault(i =>
{
i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar));

View File

@ -76,7 +76,7 @@ namespace MediaBrowser.Api
.ToArray();
result.Genres = items.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.OrderBy(i => i)
.ToArray();

View File

@ -410,7 +410,7 @@ namespace MediaBrowser.Api.Movies
return items
.SelectMany(i => i.People.Where(p => !string.Equals(PersonType.Director, p.Type, StringComparison.OrdinalIgnoreCase)).Take(2))
.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase);
.DistinctNames();
}
private IEnumerable<string> GetDirectors(IEnumerable<BaseItem> items)
@ -419,7 +419,7 @@ namespace MediaBrowser.Api.Movies
.Select(i => i.People.FirstOrDefault(p => string.Equals(PersonType.Director, p.Type, StringComparison.OrdinalIgnoreCase)))
.Where(i => i != null)
.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase);
.DistinctNames();
}
}
}

View File

@ -79,12 +79,12 @@ namespace MediaBrowser.Api.Music
var artists1 = album1
.AllArtists
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToList();
var artists2 = album2
.AllArtists
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
return points + artists1.Where(artists2.ContainsKey).Sum(i => 5);

View File

@ -170,7 +170,7 @@ namespace MediaBrowser.Api
points += item1.Studios.Where(i => item2.Studios.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 3);
var item2PeopleNames = item2.People.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
points += item1.People.Where(i => item2PeopleNames.ContainsKey(i.Name)).Sum(i =>

View File

@ -132,7 +132,7 @@ namespace MediaBrowser.Api.UserLibrary
.Where(i => !i.IsFolder)
.OfType<IHasAlbumArtist>()
.SelectMany(i => i.AlbumArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(name =>
{
try
@ -152,7 +152,7 @@ namespace MediaBrowser.Api.UserLibrary
.Where(i => !i.IsFolder)
.OfType<IHasArtist>()
.SelectMany(i => i.AllArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(name =>
{
try

View File

@ -105,7 +105,7 @@ namespace MediaBrowser.Api.UserLibrary
return itemsList
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(name => LibraryManager.GetGameGenre(name));
}
}

View File

@ -108,7 +108,7 @@ namespace MediaBrowser.Api.UserLibrary
{
return items
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(name =>
{
try

View File

@ -105,7 +105,7 @@ namespace MediaBrowser.Api.UserLibrary
return itemsList
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(name => LibraryManager.GetMusicGenre(name));
}
}

View File

@ -127,7 +127,7 @@ namespace MediaBrowser.Api.UserLibrary
return allPeople
.Select(i => i.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(name =>
{

View File

@ -109,7 +109,7 @@ namespace MediaBrowser.Api.UserLibrary
return itemsList
.SelectMany(i => i.Studios)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(name => LibraryManager.GetStudio(name));
}
}

View File

@ -1,6 +1,5 @@
using System;
using MediaBrowser.Controller.Library;
using System.Collections.Generic;
using System.Linq;
namespace MediaBrowser.Controller.Entities.Audio
{
@ -20,11 +19,11 @@ namespace MediaBrowser.Controller.Entities.Audio
{
public static bool HasArtist(this IHasArtist hasArtist, string artist)
{
return hasArtist.Artists.Contains(artist, StringComparer.OrdinalIgnoreCase);
return NameExtensions.EqualsAny(hasArtist.Artists, artist);
}
public static bool HasAnyArtist(this IHasArtist hasArtist, string artist)
{
return hasArtist.AllArtists.Contains(artist, StringComparer.OrdinalIgnoreCase);
return NameExtensions.EqualsAny(hasArtist.AllArtists, artist);
}
}
}

View File

@ -2,7 +2,6 @@
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Users;
using System;
using System.Collections.Generic;

View File

@ -283,7 +283,7 @@ namespace MediaBrowser.Controller.Entities
var tasks = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos })
.Where(i => !i.IsFolder)
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i =>
{
try
@ -313,7 +313,7 @@ namespace MediaBrowser.Controller.Entities
.Where(i => i.Genres.Contains(displayParent.Name, StringComparer.OrdinalIgnoreCase))
.OfType<IHasAlbumArtist>()
.SelectMany(i => i.AlbumArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i =>
{
try
@ -337,7 +337,7 @@ namespace MediaBrowser.Controller.Entities
.Where(i => !i.IsFolder)
.OfType<IHasAlbumArtist>()
.SelectMany(i => i.AlbumArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i =>
{
try
@ -361,7 +361,7 @@ namespace MediaBrowser.Controller.Entities
.Where(i => !i.IsFolder)
.OfType<IHasArtist>()
.SelectMany(i => i.Artists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i =>
{
try
@ -385,7 +385,7 @@ namespace MediaBrowser.Controller.Entities
.Where(i => !i.IsFolder)
.OfType<IHasAlbumArtist>()
.SelectMany(i => i.AlbumArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i =>
{
try
@ -552,7 +552,7 @@ namespace MediaBrowser.Controller.Entities
var tasks = GetRecursiveChildren(parent, user, new[] { CollectionType.Movies, CollectionType.BoxSets, string.Empty })
.Where(i => i is Movie)
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i =>
{
try
@ -724,7 +724,7 @@ namespace MediaBrowser.Controller.Entities
var tasks = GetRecursiveChildren(parent, user, new[] { CollectionType.TvShows, string.Empty })
.OfType<Series>()
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i =>
{
try
@ -776,7 +776,7 @@ namespace MediaBrowser.Controller.Entities
var tasks = GetRecursiveChildren(parent, user, new[] { CollectionType.Games })
.OfType<Game>()
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i =>
{
try

View File

@ -0,0 +1,41 @@
using MediaBrowser.Common.Extensions;
using MoreLinq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MediaBrowser.Controller.Library
{
public static class NameExtensions
{
public static bool AreEqual(string name1, string name2)
{
name1 = NormalizeForComparison(name1);
name2 = NormalizeForComparison(name2);
return string.Equals(name1, name2, StringComparison.OrdinalIgnoreCase);
}
public static bool EqualsAny(IEnumerable<string> names, string name)
{
name = NormalizeForComparison(name);
return names.Any(i => string.Equals(NormalizeForComparison(i), name, StringComparison.OrdinalIgnoreCase));
}
private static string NormalizeForComparison(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return string.Empty;
}
return name.RemoveDiacritics();
}
public static IEnumerable<string> DistinctNames(this IEnumerable<string> names)
{
return names.DistinctBy(NormalizeForComparison, StringComparer.OrdinalIgnoreCase);
}
}
}

View File

@ -186,6 +186,7 @@
<Compile Include="Library\IUserViewManager.cs" />
<Compile Include="Library\LibraryManagerExtensions.cs" />
<Compile Include="Library\MetadataConfigurationStore.cs" />
<Compile Include="Library\NameExtensions.cs" />
<Compile Include="Library\PlaybackStopEventArgs.cs" />
<Compile Include="Library\UserDataSaveEventArgs.cs" />
<Compile Include="LiveTv\ILiveTvItem.cs" />

View File

@ -54,7 +54,7 @@ namespace MediaBrowser.Providers.Music
var currentList = item.Genres.ToList();
item.Genres = taggedItems.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToList();
if (currentList.Count != item.Genres.Count || !currentList.OrderBy(i => i).SequenceEqual(item.Genres.OrderBy(i => i), StringComparer.OrdinalIgnoreCase))

View File

@ -926,10 +926,8 @@ namespace MediaBrowser.Server.Implementations.Library
if (isArtist)
{
var validFilename = _fileSystem.GetValidFilename(name).Trim();
var existing = RootFolder
.GetRecursiveChildren(i => i is T && string.Equals(_fileSystem.GetValidFilename(i.Name).Trim(), validFilename, StringComparison.OrdinalIgnoreCase))
.GetRecursiveChildren(i => i is T && NameExtensions.AreEqual(i.Name, name))
.Cast<T>()
.FirstOrDefault();

View File

@ -122,7 +122,7 @@ namespace MediaBrowser.Server.Implementations.Library
var artists = items.OfType<Audio>()
.SelectMany(i => i.AllArtists)
.Where(i => !string.IsNullOrWhiteSpace(i))
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToList();
foreach (var item in artists)

View File

@ -49,7 +49,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
.ToList();
var allArtists = allSongs.SelectMany(i => i.AllArtists)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToList();
var numComplete = 0;

View File

@ -36,7 +36,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
{
var items = _libraryManager.RootFolder.GetRecursiveChildren(i => (i is Game))
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToList();
var numComplete = 0;

View File

@ -37,7 +37,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
{
var items = _libraryManager.RootFolder.GetRecursiveChildren(i => !(i is IHasMusicGenres) && !(i is Game))
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToList();
var numComplete = 0;

View File

@ -36,7 +36,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
{
var items = _libraryManager.RootFolder.GetRecursiveChildren(i => (i is IHasMusicGenres))
.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToList();
var numComplete = 0;

View File

@ -35,7 +35,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
{
var items = _libraryManager.RootFolder.GetRecursiveChildren()
.SelectMany(i => i.Studios)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.ToList();
var numComplete = 0;

View File

@ -839,7 +839,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv
var programList = programs.ToList();
var genres = programList.SelectMany(i => i.Genres)
.Distinct(StringComparer.OrdinalIgnoreCase)
.DistinctNames()
.Select(i => _libraryManager.GetGenre(i))
.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);

View File

@ -1419,5 +1419,5 @@
"LabelConversionCpuCoreLimit": "CPU core limit:",
"LabelConversionCpuCoreLimitHelp": "Limit the number of CPU cores that will be used during sync conversion.",
"OptionEnableFullSpeedConversion": "Enable full speed conversion",
"OptionEnableFullSpeedConversionHelp": "By default, sync conversion is performed at a low speed to reduce resource consumption."
"OptionEnableFullSpeedConversionHelp": "By default, sync conversion is performed at a low speed to minimize resource consumption."
}