Try to limit the random artworks returned by the AppleTV provider

This commit is contained in:
Damiano Galassi committed 2024-07-20 07:58:51 +02:00
1 parent 59a715f6d6
commit 64f7ba85a7
3 files changed
+40 -39

No files matched your search

+1 -1
View File
@@ -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
@@ -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 {
+39
View File
@@ -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]
}
}