Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link SAB classification #1454

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions whelktool/scripts/2024/sab/main.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
SAB = "https://id.kb.se/term/kssb"

SAB_MAP = [:]

missing = [:]

boolean interpretClassification(Map thing) {
var modified = false
var isInstance = 'instanceOf' in thing

for (Map cls : asList(thing.classification)) {
if (asList(cls.inScheme).any {
it[ID] == SAB || it.code?.toLowerCase()?.startsWith("kssb")
}) {
List<Map> sabRefs = null

var clsCode = cls.code

if (isInstance) {
var mediaSubdiv = clsCode.find(/(\/[A-Z]+)/)
if (mediaSubdiv in SAB_MAP) {
var basecode = clsCode.replace(mediaSubdiv, '')
if (basecode in SAB_MAP) {
sabRefs = [ [(ID): SAB_MAP[basecode]], [(ID): SAB_MAP[mediaSubdiv]] ]
}
}
}

if (!sabRefs) {
if (clsCode in SAB_MAP) {
sabRefs = [ [(ID): SAB_MAP[clsCode]] ]
} else if (clsCode) {
sabRefs = splitSabCode(clsCode)
}
}

if (sabRefs) {
cls.clear()
if (sabRefs.size() == 1) {
cls.putAll(sabRefs[0])
} else {
cls[TYPE] = 'Classification'
cls.code = clsCode
cls.inScheme = [(ID): SAB]
cls.broader = sabRefs

var missed = sabRefs.findAll { ID !in it && it[TYPE] != 'ShelfLocal' }
if (missed) {
missed.each {
missing.get(it.code, []) << clsCode
}
}
}
modified = true
} else if (clsCode && !clsCode.contains('z ')) {
missing.get(clsCode, []) << ''
}
}
}

if (isInstance) {
if (ID !in thing.instanceOf) {
modified |= interpretClassification(thing.instanceOf)
}
}

return modified
}

List<Map> splitSabCode(String code) {
var chunks = parseSabCode(code)
return chunks.findResults { chunk ->
if (chunk.size() == 0) {
return null
}

if (chunk.indexOf(' ') > -1) {
if (chunk.startsWith('z ')) {
return [(TYPE): 'ShelfLocal', label: chunk.substring(2)]
}
return null
}

// TODO: Improve? Other parts may be subcomponents of chunks[0]...
if (chunk =~ /^[.:(]/) {
var prefixedCode = chunks[0][0] + chunk
if (prefixedCode in SAB_MAP) {
chunk = prefixedCode
}
}

if (chunk in SAB_MAP) {
return [(ID): SAB_MAP[chunk]]
}

//var slug = URLEncoder.encode(chunk)
//return [(ID): "${SAB}/${slug}"]
return [code: chunk]
}
}

List<String> parseSabCode(String code) {
// TODO: starts with any /[a-z]/?
if (code.startsWith('u')) {
code = code.substring(1) + ',u'
}

var spaceIdx = code.indexOf('z ')
var rest = []
if (spaceIdx > -1) {
rest << code.substring(spaceIdx)
code = code.substring(0, spaceIdx)
}

return code.split(/(?=z\s+.+|\(\w+\)|[,\/=:.-])/) + rest
}


selectBySqlWhere("""
data#>>'{@graph,0,inDataset,0,@id}' IN ('https://id.kb.se/dataset/sab', 'https://id.kb.se/dataset/sab/precoordinated') AND
data#>>'{@graph,1,inScheme,@id}' = 'https://id.kb.se/term/kssb' AND
data#>>'{@graph,1,@type}' != 'Collection'
""") {
def cls = it.graph[1]

// FIXME: Make *all* sab.ttl codes unique! (Use altLabel for "shortcode"?)
def code = cls.code
if (cls[TYPE].indexOf('Subdivision') > -1) {
def firstIdChar = URLDecoder.decode(cls[ID].substring(SAB.size() + 1))[0]
if (!cls.code.startsWith(firstIdChar)) {
code = firstIdChar + code
}
}
//if (code != cls.code) println code + ' => ' + cls.code

SAB_MAP[code] = cls[ID]
}
println "Loaded ${SAB_MAP.size()} SAB references"


selectBySqlWhere("""
collection <> 'hold' AND
data#>>'{@graph,1}' LIKE '%kssb%'
""") { data ->
/*
selectByIds(['8rkj0wql14q40gb']) { data ->
*/
def (record, instance) = data.graph

if (interpretClassification(instance)) {
data.scheduleSave()
}
}

missingLog = getReportWriter("sab-missing.txt")
missing.keySet().sort().each {
missingLog.println "${it} ${missing[it].size()} ${missing[it].unique().join(' | ')}"
}
Loading