Skip to content

Commit

Permalink
Guarantee meaningful closeMatch links by matching primary contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
kwahlin committed Nov 16, 2023
1 parent f45a276 commit faee3a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 12 additions & 8 deletions librisworks/scripts/merge-works.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ new File(System.getProperty('clusters')).splitEachLine(~/[\t ]+/) { cluster ->
}
}

List<String> linkableWorkIris = uniqueWorksAndTheirInstances.findResults { it.getV1().workIri() }
List<Doc> linkableWorks = uniqueWorksAndTheirInstances.findResults { workDoc, _ -> workDoc.workIri() ? workDoc : null }

uniqueWorksAndTheirInstances.each { Doc workDoc, List<Doc> instanceDocs ->
// Link more instances to existing linked work
if (workDoc.existsInStorage && !workDoc.instanceData && instanceDocs) {
replaceWorkData(workDoc, c.merge([workDoc] + instanceDocs))
// TODO: Update adminMetadata? To say that additional instances may have contributed to the linked work.
addCloseMatch(workDoc, linkableWorkIris)
addCloseMatch(workDoc, linkableWorks)
saveAndLink(workDoc, instanceDocs, workDoc.existsInStorage)
writeWorkReport(docs, workDoc, instanceDocs, WorkStatus.UPDATED)
return
}
// New merged work
if (!workDoc.existsInStorage && !workDoc.instanceData) {
addAdminMetadata(workDoc, instanceDocs.collect { ['@id': it.recordIri()] })
addCloseMatch(workDoc, linkableWorkIris)
addCloseMatch(workDoc, linkableWorks)
saveAndLink(workDoc, instanceDocs, workDoc.existsInStorage)
writeWorkReport(docs, workDoc, instanceDocs, WorkStatus.NEW)
return
}
// Local work, save if new closeMatch links created
if (workDoc.instanceData && addCloseMatch(workDoc, linkableWorkIris)) {
if (workDoc.instanceData && addCloseMatch(workDoc, linkableWorks)) {
saveAndLink(workDoc)
}
}
Expand Down Expand Up @@ -171,12 +171,16 @@ static void replaceWorkData(Doc workDoc, Map replacement) {
workDoc.workData.putAll(replacement)
}

boolean addCloseMatch(Doc workDoc, List<String> workIris) {
def linkable = (workIris - workDoc.thingIri()).collect { ['@id': it] }
boolean addCloseMatch(Doc workDoc, List<Doc> linkableWorks) {
def linkTo = linkableWorks.findAll { d ->
d.workIri() != workDoc.thingIri()
&& d.primaryContributor() == workDoc.primaryContributor()
}.collect { ['@id': it.workIri()] }

def closeMatch = asList(workDoc.workData['closeMatch'])

if (linkable && !closeMatch.containsAll(linkable)) {
workDoc.workData['closeMatch'] = (closeMatch + linkable).unique()
if (linkTo && !closeMatch.containsAll(linkTo)) {
workDoc.workData['closeMatch'] = (closeMatch + linkTo).unique()
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ class Doc {
asList(instanceData?.reproductionOf)
}

Map primaryContributor() {
contribution().findResult { it['@type'] == 'PrimaryContribution' ? asList(it.agent).find() : null }
}

String editionStatement() {
instanceData?.editionStatement
}
Expand Down

0 comments on commit faee3a8

Please sign in to comment.