Skip to content

Commit

Permalink
Merge pull request #1303 from libris/feature/part-number-on-work-title
Browse files Browse the repository at this point in the history
Add partNumber from instance title to work title
  • Loading branch information
kwahlin authored Sep 11, 2023
2 parents ffd08a6 + 4757417 commit ab1ebc7
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions librisworks/src/main/groovy/se/kb/libris/mergeworks/Util.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,7 @@ class Util {
null
]

static def toWorkTitleForm = { Map title ->
// partName/partNumber is usually in hasPart but not always
def partName = title['partName']
def partNumber = title['partNumber']

def hasPart = title['hasPart']
if (hasPart) {
partName = hasPart[0]['partName']
partNumber = hasPart[0]['partNumber']
}

partName = asList(partName)[0]
partNumber = asList(partNumber)[0]

static Map appendTitlePartsToMainTitle(Map title, String partNumber, String partName = null) {
if (partNumber && partName) {
title['mainTitle'] += ". $partNumber, $partName"
} else if (partNumber) {
Expand All @@ -208,7 +195,16 @@ class Util {
title['mainTitle'] += ". $partName"
}

return title.subMap(['@type', 'mainTitle', 'source'])
title.remove('partNumber')
title.remove('partName')

return title
}

static String findTitlePart(List<Map> title, String prop) {
// partName/partNumber is usually found in hasPart but not always
def partNumber = title.findResult { Map t -> t[prop] ?: t['hasPart'].findResult { it[prop] } }
return asList(partNumber).find()
}

// Return the most common title for the best encodingLevel
Expand All @@ -219,22 +215,27 @@ class Util {
return linkedWorkTitle
}

for (def level : bestEncodingLevel) {
def onLevel = docs.findAll { it.encodingLevel() == level }
def bestWorkTitle = mostCommonWorkTitle(onLevel)
if (bestWorkTitle) {
return bestWorkTitle
}
def bestInstanceTitle = mostCommonHighestEncodingLevel(docs, this.&mostCommonInstanceTitle)
def bestWorkTitle = mostCommonHighestEncodingLevel(docs, this.&mostCommonWorkTitle)

def partNumber = findTitlePart(bestInstanceTitle, 'partNumber')
def partName = findTitlePart(bestInstanceTitle, 'partName')

if (bestWorkTitle) {
return bestWorkTitle.collect { appendTitlePartsToMainTitle(it, partNumber) }
}

return bestInstanceTitle.collect { appendTitlePartsToMainTitle(it, partNumber, partName) }
}

static def mostCommonHighestEncodingLevel(Collection<Doc> docs, Closure<Collection<Doc>> findMostCommon) {
for (def level : bestEncodingLevel) {
def onLevel = docs.findAll { it.encodingLevel() == level }
def bestInstanceTitle = mostCommonInstanceTitle(onLevel)
if (bestInstanceTitle) {
return bestInstanceTitle.collect(toWorkTitleForm)
def mostCommonTitle = findMostCommon(onLevel)
if (mostCommonTitle) {
return mostCommonTitle
}
}

return null
}

Expand Down

0 comments on commit ab1ebc7

Please sign in to comment.