Make position an internal property of titleCandidates

This commit is contained in:
mikiher 2023-11-05 14:56:20 +00:00
parent 3a9d09ea63
commit 047e7a72f2

View File

@ -154,9 +154,10 @@ class BookFinder {
this.cleanAuthor = cleanAuthor this.cleanAuthor = cleanAuthor
this.priorities = {} this.priorities = {}
this.positions = {} this.positions = {}
this.currentPosition = 0
} }
add(title, position = 0) { add(title) {
// if title contains the author, remove it // if title contains the author, remove it
title = this.#removeAuthorFromTitle(title) title = this.#removeAuthorFromTitle(title)
@ -174,7 +175,7 @@ class BookFinder {
if (!cleanTitle) return if (!cleanTitle) return
this.candidates.add(cleanTitle) this.candidates.add(cleanTitle)
this.priorities[cleanTitle] = 0 this.priorities[cleanTitle] = 0
this.positions[cleanTitle] = position this.positions[cleanTitle] = this.currentPosition
let candidate = cleanTitle let candidate = cleanTitle
@ -185,10 +186,11 @@ class BookFinder {
if (candidate) { if (candidate) {
this.candidates.add(candidate) this.candidates.add(candidate)
this.priorities[candidate] = 0 this.priorities[candidate] = 0
this.positions[candidate] = position this.positions[candidate] = this.currentPosition
} }
this.priorities[cleanTitle] = 1 this.priorities[cleanTitle] = 1
} }
this.currentPosition++
} }
get size() { get size() {
@ -210,11 +212,7 @@ class BookFinder {
if (priorityDiff) return priorityDiff if (priorityDiff) return priorityDiff
// if same priorirty, prefer candidates that are closer to the beginning (e.g. titles before subtitles) // if same priorirty, prefer candidates that are closer to the beginning (e.g. titles before subtitles)
const positionDiff = this.positions[a] - this.positions[b] const positionDiff = this.positions[a] - this.positions[b]
if (positionDiff) return positionDiff return positionDiff // candidates with same priority always have different positions
// Start with longer candidaets, as they are likely more specific
const lengthDiff = b.length - a.length
if (lengthDiff) return lengthDiff
return b.localeCompare(a)
}) })
Logger.debug(`[${this.constructor.name}] Found ${candidates.length} fuzzy title candidates`) Logger.debug(`[${this.constructor.name}] Found ${candidates.length} fuzzy title candidates`)
Logger.debug(candidates) Logger.debug(candidates)
@ -342,8 +340,8 @@ class BookFinder {
authorCandidates = await authorCandidates.getCandidates() authorCandidates = await authorCandidates.getCandidates()
for (const authorCandidate of authorCandidates) { for (const authorCandidate of authorCandidates) {
let titleCandidates = new BookFinder.TitleCandidates(authorCandidate) let titleCandidates = new BookFinder.TitleCandidates(authorCandidate)
for (const [position, titlePart] of titleParts.entries()) for (const titlePart of titleParts)
titleCandidates.add(titlePart, position) titleCandidates.add(titlePart)
titleCandidates = titleCandidates.getCandidates() titleCandidates = titleCandidates.getCandidates()
for (const titleCandidate of titleCandidates) { for (const titleCandidate of titleCandidates) {
if (titleCandidate == title && authorCandidate == author) continue // We already tried this if (titleCandidate == title && authorCandidate == author) continue // We already tried this