Rank metadata search results and honor filename IDs on tagged files
extractSearchTerms dropped a filename's {tmdb-…} ID whenever the movie was
already tagged (name+release-date+media-kind present), returning a bare title
and ignoring the embedded ID — so a search on a tagged or previously-searched
file came back as a fuzzy title list. That branch now also parses the filename,
so the direct ID lookup fires regardless of prior tagging.
Also rank movie title-search results so exact title matches sort to the top of
the fuzzy provider list, and recover the release year from the filename
(ParseFilename strips it from the title without reporting it) to pick the
default entry: exact+year, then exact, then year-only, then the provider's own
order (a stable sort, so relevance survives within each band). Applied in the
search sheet, the queue metadata action, and both AppleScript commands with
identical ranking so scripted indexes stay aligned; the sheet now highlights
the default pick. Bracketed years win over bare dot/space years so
resolutions like .1920.1080p don't win.
Bump the fork version to 1.9.5mkv2.
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
a153763019
commit
13255140e9
8 files changed
+148
-23
No files matched your search
@@ -20,7 +20,9 @@ public enum MetadataFileID: Equatable {
|
||||
public enum MetadataSearchTerms {
|
||||
case none
|
||||
/// `id` is a metadata service ID parsed from the file name, when present.
|
||||
case movie(title: String, id: MetadataFileID? = nil)
|
||||
/// `year` is a release year parsed from the file name, when present; it
|
||||
/// lets search-result ranking prefer the same-year entry.
|
||||
case movie(title: String, id: MetadataFileID? = nil, year: Int? = nil)
|
||||
case tvShow(seriesName: String, season: Int?, episode: Int?)
|
||||
|
||||
public var isMovie: Bool { get {
|
||||
@@ -87,6 +89,26 @@ private func parseFileID(_ lines: [Substring]) -> MetadataFileID? {
|
||||
}
|
||||
}
|
||||
|
||||
// 1950–2099 keeps resolutions (720/1080) and tiny numbers out of the match.
|
||||
// Group 1 is the year in both; the class metacharacters must be escaped or
|
||||
// ICU refuses to compile the pattern at all.
|
||||
private let bracketedYearRegex = try? NSRegularExpression(pattern: "[\\(\\[\\{](19[5-9][0-9]|20[0-9][0-9])[\\)\\]\\}]")
|
||||
private let delimitedYearRegex = try? NSRegularExpression(pattern: "[._ ](19[5-9][0-9]|20[0-9][0-9])(?:[._ ]|$)")
|
||||
|
||||
/// Extracts a release year from the file name. ParseFilename strips the year
|
||||
/// from the title it emits but never reports it, so it is recovered here.
|
||||
/// Bracketed years (`(2026)`, `[2026]`, `{2026}`) are the authoritative
|
||||
/// release-name decoration and win over bare dot/space-delimited ones, which
|
||||
/// can also hit resolution numbers (`Movie.1920.1080p.x264`).
|
||||
private func parseFilenameYear(_ filename: String) -> Int? {
|
||||
for regex in [bracketedYearRegex, delimitedYearRegex].compactMap({ $0 }) {
|
||||
guard let match = regex.firstMatch(in: filename, range: NSRange(filename.startIndex..., in: filename)),
|
||||
let yearRange = Range(match.range(at: 1), in: filename) else { continue }
|
||||
return Int(filename[yearRange])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private func parseFilename(_ filename: String) -> MetadataSearchTerms? {
|
||||
guard let path = Bundle.main.path(forResource: "ParseFilename", ofType: "") else { return nil }
|
||||
|
||||
@@ -119,7 +141,7 @@ private func parseFilename(_ filename: String) -> MetadataSearchTerms? {
|
||||
.replacingOccurrences(of: "[", with: " ")
|
||||
.replacingOccurrences(of: "]", with: " ")
|
||||
.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
||||
return MetadataSearchTerms.movie(title: newTitle, id: parseFileID(lines))
|
||||
return MetadataSearchTerms.movie(title: newTitle, id: parseFileID(lines), year: parseFilenameYear(filename))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,4 +163,53 @@ extension String {
|
||||
func trimmingWhitespacesAndNewlinews() -> String {
|
||||
return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
/// The release year decorated in a file name (`Title (2026)`, `Title.2026`), when present.
|
||||
func parsedAsFilenameYear() -> Int? {
|
||||
return parseFilenameYear(self)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Movie result ranking
|
||||
|
||||
private func normalizedSearchTitle(_ title: String) -> String {
|
||||
let folded = title.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: nil)
|
||||
return String(String.UnicodeScalarView(folded.unicodeScalars.filter { CharacterSet.alphanumerics.contains($0) }))
|
||||
}
|
||||
|
||||
/// The release year of a search result, as far as its (usually ISO) release
|
||||
/// date string exposes it.
|
||||
private func movieResultYear(_ result: MetadataResult) -> Int? {
|
||||
guard let releaseDate = result[.releaseDate] as? String else { return nil }
|
||||
return Int(releaseDate.split(separator: "-").first ?? "")
|
||||
}
|
||||
|
||||
/// Orders movie title-search results so the most likely match for a searched
|
||||
/// title comes first: exact title matches on top (year-matching ones before
|
||||
/// the rest), then year-matching fuzzy hits, then the service's own order.
|
||||
/// The sort is stable, so the provider's relevance ranking survives inside
|
||||
/// each band — it only stops exact matches from being scattered among the
|
||||
/// fuzzy ones. Keep every caller indexing into the same list ranking the same
|
||||
/// way (the AppleScript result list and its index-based fetch must agree).
|
||||
func rankMovieResults(_ results: [MetadataResult], title: String, year: Int?) -> [MetadataResult] {
|
||||
let wanted = normalizedSearchTitle(title)
|
||||
|
||||
func rank(_ result: MetadataResult) -> Int {
|
||||
let name = (result[.name] as? String).map(normalizedSearchTitle)
|
||||
let titleMatch = wanted.isEmpty == false && name == wanted
|
||||
let yearMatch = year != nil && movieResultYear(result) == year
|
||||
switch (titleMatch, yearMatch) {
|
||||
case (true, true): return 3
|
||||
case (true, false): return 2
|
||||
case (false, true): return 1
|
||||
case (false, false): return 0
|
||||
}
|
||||
}
|
||||
|
||||
struct Ranked { let offset: Int; let result: MetadataResult; let rank: Int }
|
||||
let ranked = results.enumerated().map { Ranked(offset: $0.offset, result: $0.element, rank: rank($0.element)) }
|
||||
let ordered: [Ranked] = ranked.sorted { (a: Ranked, b: Ranked) -> Bool in
|
||||
return a.rank != b.rank ? a.rank > b.rank : a.offset < b.offset
|
||||
}
|
||||
return ordered.map { $0.result }
|
||||
}
|
||||
Reference in new issue
Block a user