Try to limit the random artworks returned by the AppleTV provider
This commit is contained in:
3 files changed
+40
-39
No files matched your search
@@ -238,7 +238,7 @@ public struct AppleTV: MetadataService {
|
|||||||
|
|
||||||
if let result = items?.filter({ $0.title == normalizedTerm }).first {
|
if let result = items?.filter({ $0.title == normalizedTerm }).first {
|
||||||
return result
|
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
|
return result
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -8,44 +8,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import MP42Foundation
|
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
|
// MARK: - Data Types
|
||||||
|
|
||||||
extension KeyedDecodingContainer {
|
extension KeyedDecodingContainer {
|
||||||
|
|||||||
@@ -79,3 +79,42 @@ func sendToFileExternalApp(fileURL: URL) -> Bool {
|
|||||||
|
|
||||||
return false
|
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]
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user