From bdcee8c7c17610da0be3d2963db86b0678923bb1 Mon Sep 17 00:00:00 2001 From: ta264 Date: Thu, 2 Sep 2021 21:12:14 +0100 Subject: [PATCH] New: Try matching with "Unabridged" removed from title --- .../Identification/DistanceFixture.cs | 16 ++++++++++++++++ .../BookImport/Identification/Distance.cs | 16 ++++++++++++++++ .../Identification/DistanceCalculator.cs | 8 ++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Identification/DistanceFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Identification/DistanceFixture.cs index f1e98bf8e..8c7f0c841 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Identification/DistanceFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Identification/DistanceFixture.cs @@ -163,6 +163,22 @@ namespace NzbDrone.Core.Test.MediaFiles.BookImport.Identification dist.Penalties.Should().BeEquivalentTo(new Dictionary> { { "string", new List { 0.0 } } }); } + [Test] + public void test_add_string_multiple_options_multiple_values_match() + { + var dist = new Distance(); + dist.AddString("string", new List { "cat", "dog" }, new List { "dog", "mouse" }); + dist.Penalties.Should().BeEquivalentTo(new Dictionary> { { "string", new List { 0.0 } } }); + } + + [Test] + public void test_add_string_multiple_options_multiple_values_no_match() + { + var dist = new Distance(); + dist.AddString("string", new List { "cat", "dog" }, new List { "y", "z" }); + dist.Penalties.Should().BeEquivalentTo(new Dictionary> { { "string", new List { 1.0 } } }); + } + [Test] public void test_distance() { diff --git a/src/NzbDrone.Core/MediaFiles/BookImport/Identification/Distance.cs b/src/NzbDrone.Core/MediaFiles/BookImport/Identification/Distance.cs index e40d84b90..d662bb208 100644 --- a/src/NzbDrone.Core/MediaFiles/BookImport/Identification/Distance.cs +++ b/src/NzbDrone.Core/MediaFiles/BookImport/Identification/Distance.cs @@ -173,6 +173,22 @@ namespace NzbDrone.Core.MediaFiles.BookImport.Identification } } + public void AddString(string key, List values, List options) + { + if (!values.Any() && !options.Any()) + { + Add(key, 0.0); + } + else if (!values.Any() || !options.Any()) + { + Add(key, 1.0); + } + else + { + Add(key, values.Min(v => options.Min(o => StringScore(v, o)))); + } + } + public void AddBool(string key, bool expr) { Add(key, expr ? 1.0 : 0.0); diff --git a/src/NzbDrone.Core/MediaFiles/BookImport/Identification/DistanceCalculator.cs b/src/NzbDrone.Core/MediaFiles/BookImport/Identification/DistanceCalculator.cs index 509028155..3585e4e80 100644 --- a/src/NzbDrone.Core/MediaFiles/BookImport/Identification/DistanceCalculator.cs +++ b/src/NzbDrone.Core/MediaFiles/BookImport/Identification/DistanceCalculator.cs @@ -19,6 +19,8 @@ namespace NzbDrone.Core.MediaFiles.BookImport.Identification private static readonly RegexReplace StripSeriesRegex = new RegexReplace(@"\([^\)].+?\)$", string.Empty, RegexOptions.Compiled); + private static readonly RegexReplace CleanTitleCruft = new RegexReplace(@"\((?:unabridged)\)", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Compiled); + public static Distance BookDistance(List localTracks, Edition edition) { var dist = new Distance(); @@ -68,8 +70,10 @@ namespace NzbDrone.Core.MediaFiles.BookImport.Identification } } - dist.AddString("book", title, titleOptions); - Logger.Trace("book: '{0}' vs '{1}'; {2}", title, titleOptions.ConcatToString("' or '"), dist.NormalizedDistance()); + var fileTitles = new[] { title, CleanTitleCruft.Replace(title) }.Distinct().ToList(); + + dist.AddString("book", fileTitles, titleOptions); + Logger.Trace("book: '{0}' vs '{1}'; {2}", fileTitles.ConcatToString("' or '"), titleOptions.ConcatToString("' or '"), dist.NormalizedDistance()); var isbn = localTracks.MostCommon(x => x.FileTrackInfo.Isbn); if (isbn.IsNotNullOrWhiteSpace() && edition.Isbn13.IsNotNullOrWhiteSpace())