From 64f7ba85a76c27a00e4e607642ad29e6624c1fbd Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sat, 20 Jul 2024 07:58:51 +0200 Subject: [PATCH] Try to limit the random artworks returned by the AppleTV provider --- Classes/MetadataImporters/AppleTV.swift | 2 +- Classes/MetadataImporters/iTunesStore.swift | 38 -------------------- Classes/Utilities.swift | 39 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/Classes/MetadataImporters/AppleTV.swift b/Classes/MetadataImporters/AppleTV.swift index d7ac1e4..8853383 100644 --- a/Classes/MetadataImporters/AppleTV.swift +++ b/Classes/MetadataImporters/AppleTV.swift @@ -238,7 +238,7 @@ public struct AppleTV: MetadataService { if let result = items?.filter({ $0.title == normalizedTerm }).first { return result - } else if let result = items?.first { + } else if let result = items?.filter({ $0.title?.minimumEditDistance(other: normalizedTerm) ?? Int.max < 8 }).first { return result } else { return nil diff --git a/Classes/MetadataImporters/iTunesStore.swift b/Classes/MetadataImporters/iTunesStore.swift index 6f68ee7..a1f08a9 100644 --- a/Classes/MetadataImporters/iTunesStore.swift +++ b/Classes/MetadataImporters/iTunesStore.swift @@ -8,44 +8,6 @@ import Foundation import MP42Foundation -private extension String { - - // All content is licensed under the terms of the MIT open source license. - // Copyright (c) 2016 Matthijs Hollemans and contributors - - func minimumEditDistance(other: String) -> Int { - let m = self.count - let n = other.count - var matrix = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1) - - // initialize matrix - for index in 1...m { - // the distance of any first string to an empty second string - matrix[index][0] = index - } - - for index in 1...n { - // the distance of any second string to an empty first string - matrix[0][index] = index - } - - // compute Levenshtein distance - for (i, selfChar) in self.enumerated() { - for (j, otherChar) in other.enumerated() { - if otherChar == selfChar { - // substitution of equal symbols with cost 0 - matrix[i + 1][j + 1] = matrix[i][j] - } else { - // minimum of the cost of insertion, deletion, or substitution - // added to the already computed costs in the corresponding cells - matrix[i + 1][j + 1] = Swift.min(matrix[i][j] + 1, matrix[i + 1][j] + 1, matrix[i][j + 1] + 1) - } - } - } - return matrix[m][n] - } -} - // MARK: - Data Types extension KeyedDecodingContainer { diff --git a/Classes/Utilities.swift b/Classes/Utilities.swift index 2196a62..c1db3c1 100644 --- a/Classes/Utilities.swift +++ b/Classes/Utilities.swift @@ -79,3 +79,42 @@ func sendToFileExternalApp(fileURL: URL) -> Bool { return false } + + +extension String { + + // All content is licensed under the terms of the MIT open source license. + // Copyright (c) 2016 Matthijs Hollemans and contributors + + func minimumEditDistance(other: String) -> Int { + let m = self.count + let n = other.count + var matrix = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1) + + // initialize matrix + for index in 1...m { + // the distance of any first string to an empty second string + matrix[index][0] = index + } + + for index in 1...n { + // the distance of any second string to an empty first string + matrix[0][index] = index + } + + // compute Levenshtein distance + for (i, selfChar) in self.enumerated() { + for (j, otherChar) in other.enumerated() { + if otherChar == selfChar { + // substitution of equal symbols with cost 0 + matrix[i + 1][j + 1] = matrix[i][j] + } else { + // minimum of the cost of insertion, deletion, or substitution + // added to the already computed costs in the corresponding cells + matrix[i + 1][j + 1] = Swift.min(matrix[i][j] + 1, matrix[i + 1][j] + 1, matrix[i][j + 1] + 1) + } + } + } + return matrix[m][n] + } +}