-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DD-296 : DDM, convert title to report (#11)
* attempt to find archeo report numbers * replace titles in dcmiMetadata * log changes made to DDM * add package for transformation * abr rule agnostic of report rule * ignore plain briefrapport, report label without title trailer * add report from profile to dcmiMetadata * pattern matches to ifs, log missed titles
- Loading branch information
Showing
18 changed files
with
49,604 additions
and
161 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/scala/nl/knaw/dans/easy/bag2deposit/ddm/DdmTransformer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Copyright (C) 2020 DANS - Data Archiving and Networked Services ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package nl.knaw.dans.easy.bag2deposit.ddm | ||
|
||
import better.files.File | ||
import nl.knaw.dans.easy.bag2deposit.InvalidBagException | ||
import nl.knaw.dans.lib.logging.DebugEnhancedLogging | ||
|
||
import scala.util.{ Failure, Success, Try } | ||
import scala.xml.transform.{ RewriteRule, RuleTransformer } | ||
import scala.xml.{ Node, NodeSeq } | ||
|
||
case class DdmTransformer(cfgDir: File) extends DebugEnhancedLogging { | ||
|
||
private val reportRewriteRule = ReportRewriteRule(cfgDir) | ||
private val profileRuleTransformer = new RuleTransformer(reportRewriteRule) | ||
private val dcmiMetadataRuleTransformer = new RuleTransformer( | ||
reportRewriteRule, | ||
AbrRewriteRule(cfgDir / "ABR-period.csv", "temporal", "ddm:temporal"), | ||
AbrRewriteRule(cfgDir / "ABR-complex.csv", "subject", "ddm:subject"), | ||
) | ||
|
||
private case class DdmRewriteRule(reportNumberFromFirstTitle: NodeSeq) extends RewriteRule { | ||
override def transform(n: Node): Seq[Node] = { | ||
if (n.label != "dcmiMetadata") n | ||
else <dcmiMetadata> | ||
{ dcmiMetadataRuleTransformer(n).nonEmptyChildren } | ||
{ reportNumberFromFirstTitle } | ||
</dcmiMetadata>.copy(prefix = n.prefix, attributes = n.attributes, scope = n.scope) | ||
} | ||
} | ||
|
||
def transform(ddmIn: Node): Try[Node] = { | ||
|
||
// the single title may become a title and/or reportNumber | ||
val transformedFirstTitle = (ddmIn \ "profile" \ "title").flatMap(profileRuleTransformer) | ||
val reportNumberFromFirstTitle = transformedFirstTitle.filter(_.label == "reportNumber") | ||
val notConvertedFirstTitle = transformedFirstTitle.filter(_ => reportNumberFromFirstTitle.isEmpty) | ||
|
||
// the transformation | ||
val ddmRuleTransformer = new RuleTransformer(DdmRewriteRule(reportNumberFromFirstTitle)) | ||
val ddmOut = ddmRuleTransformer(ddmIn) | ||
|
||
// logging and error handling | ||
val notConvertedTitles = (ddmOut \ "dcmiMetadata" \ "title") ++ notConvertedFirstTitle | ||
logBriefRapportTitles(notConvertedTitles, ddmOut) | ||
val problems = ddmOut \\ "notImplemented" // fail slow trick | ||
if (problems.nonEmpty) | ||
Failure(InvalidBagException(problems.map(_.text).mkString("; "))) | ||
else ddmOut.headOption.map(Success(_)) | ||
.getOrElse(Failure(InvalidBagException("DDM transformation returned empty sequence"))) | ||
} | ||
|
||
private def logBriefRapportTitles(notConvertedTitles: NodeSeq, ddmOut: Node): Unit = { | ||
// these titles need a more complex transformation or manual fix before the final export | ||
notConvertedTitles.foreach { node => | ||
val title = node.text | ||
if (title.toLowerCase.matches(s"brief[^a-z]*rapport${ reportRewriteRule.nrTailRegexp } }")) | ||
logger.info(s"briefrapport rightsHolder=[${ ddmOut \ "rightsHolder" }] publisher=[${ ddmOut \ "publisher" }] titles=[$title]") | ||
} | ||
} | ||
} | ||
|
73 changes: 73 additions & 0 deletions
73
src/main/scala/nl/knaw/dans/easy/bag2deposit/ddm/ReportRewriteRule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright (C) 2020 DANS - Data Archiving and Networked Services ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package nl.knaw.dans.easy.bag2deposit.ddm | ||
|
||
import better.files.File | ||
import nl.knaw.dans.easy.bag2deposit.parseCsv | ||
import nl.knaw.dans.lib.logging.DebugEnhancedLogging | ||
|
||
import scala.xml.transform.RewriteRule | ||
import scala.xml.{ Elem, Node } | ||
|
||
case class ReportRewriteRule(cfgDir: File) extends RewriteRule with DebugEnhancedLogging { | ||
|
||
case class ReportCfg(uuid: String, label: String, regexp: String) | ||
|
||
private val digit = "[0-9]" | ||
|
||
/** alpha numeric (and a little more) */ | ||
private val an = "[-_/.a-z0-9]" | ||
|
||
/** just one that does not match easy-dataset:99840 "Arcadis Archeologische Rapporten [2017 - 116]" */ | ||
val nrRegexp = s"\\W+$an*$digit$an*" | ||
|
||
private val trailer = "([.]|:.*)?" | ||
val nrTailRegexp = s"$nrRegexp$trailer" | ||
private val missedRegExp = s".*(notitie|rapport|bericht|publicat).*$nrRegexp$trailer" | ||
|
||
val reportMap: Seq[ReportCfg] = parseCsv(cfgDir / "ABR-reports.csv", 0) | ||
.map(r => ReportCfg( | ||
uuid = r.get(0), | ||
label = r.get(1), | ||
regexp = r.get(2).trim + nrTailRegexp, | ||
)).toSeq | ||
|
||
override def transform(n: Node): Seq[Node] = { | ||
if (n.label != "title") n | ||
else { | ||
val titleValue = n.text | ||
val lowerCaseTitle = titleValue.trim.toLowerCase | ||
val reports = reportMap | ||
.filter(cfg => lowerCaseTitle.matches(cfg.regexp)) | ||
.map(cfg => toReportNr(titleValue.replaceAll(":.*", ""), cfg.uuid)) | ||
.theSeq | ||
if (reports.isEmpty && lowerCaseTitle.matches(missedRegExp)) | ||
logger.info(s"potential report number: $titleValue") | ||
if (titleValue == reports.text) | ||
reports | ||
else reports :+ n | ||
} | ||
} | ||
|
||
private def toReportNr(titleValue: String, uuid: String): Elem = { | ||
<ddm:reportNumber | ||
schemeURI="https://data.cultureelerfgoed.nl/term/id/abr/7a99aaba-c1e7-49a4-9dd8-d295dbcc870e" | ||
valueURI={ s"https://data.cultureelerfgoed.nl/term/id/abr/$uuid" } | ||
subjectScheme="ABR Rapporten" | ||
reportNo={ titleValue.replaceAll(s".*($nrRegexp)$trailer", "$1").trim } | ||
>{ titleValue }</ddm:reportNumber> | ||
} | ||
} |
Oops, something went wrong.