diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b85365 --- /dev/null +++ b/README.md @@ -0,0 +1,86 @@ + +# fcs-serializer-xqm + +This software package provides an XQuery module developed at the Digital Academy of the Academy of Sciences and Literature | Mainz that may be used to build elements of an [CLARIN FCS endpoint](https://office.clarin.eu/v/CE-2017-1046-FCS-Specification-v20230426.pdf). + +# Requirements +The module was developed and tested to be used with the versions 3.1 of XQuery. + +# How to Use +1. Import the module into your own XQuery script or module in the usual way: + +```xquery +import module namespace fcs-serializer="http://mwb.adwmainz.net/exist/fcs/fcs-serializer" at "PATH/TO/fcs-serializer.xqm"; +``` + +2. Use the following functions: + * `fcs-serializer:get-advanced-data-view` + * `fcs-serializer:get-generic-hit#3` + * `fcs-serializer:get-generic-hit#4` + * `fcs-serializer:get-resource` + +## fcs-serializer:get-advanced-data-view + +```xquery +fcs-serializer:get-advanced-data-view($items as element(item)*) as element(fcs:DataView) +``` + +transforms a sequence of more compact custom item elements [matching the provided schema](schema/annotatedItem.rng) into the standOff `fcs:DataView` element + +### Parameters: + +**$items*** sequence of `item` elements that have all layers as child elements and highlighting information as attribute. Please note that `@highlight` is used to mark the hit itself and the first layer will be used to build the segments - e.g. + +```xquery +( + + Lorem + lorem + , + + ipsum + ipsum + +) +``` + +### Returns: + +**element(fcs:DataView)** the proper FCS equivalent - e.g. + +```xml + + + + + + + + + Lorem + ipsum + + + lorem + ipsum + + + + +``` + +--- + +# License +The software is published under the terms of the MIT license. + + +# Research Software Engineering and Development + +Copyright 2023 Patrick Daniel Brookshire + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/fcs-serializer.xqm b/fcs-serializer.xqm new file mode 100644 index 0000000..0f323ac --- /dev/null +++ b/fcs-serializer.xqm @@ -0,0 +1,117 @@ +xquery version "3.1"; + +(: + : MWB | FCS serializer + : + : Edited and developed by Patrick D. Brookshire and Ute Recker-Hamm + : Academy of Sciences and Literature | Mainz + : + : xquery module containing various functions used to build CLARIN FCS related elements + : + : @author Patrick D. Brookshire + : @licence MIT +:) + +module namespace fcs-serializer="http://mwb.adwmainz.net/exist/fcs/fcs-serializer"; + +declare namespace fcs = "http://clarin.eu/fcs/resource"; + +(:~ builds a FCS Resource element with the given params + : @param $resource-pid the PID of the resource the given hit was found in + : @param $resource-ref the URL of the resource the given hit was found in + : @param $fragment-ref the URL of the resource fragment the given hit represents + : @param $dataViews a sequence of fcs:DataView elements to be included +:) +declare function fcs-serializer:get-resource($resource-pid as xs:string, $resource-ref as xs:anyURI, $fragment-ref as xs:anyURI, $data-views as element(fcs:DataView)*) { + + { + if ($resource-pid eq "") then + () + else + attribute pid { $resource-pid } + } + { + if ($resource-ref eq "") then + () + else + attribute ref { $resource-ref } + } + + { $data-views } + + +}; + +(:~ builds a FCS DataView element with the given params + : @param $preceding-context the text content preceding the hit + : @param $hit the hit itself + : @param $following-context the text content following the hit +:) +declare function fcs-serializer:get-generic-hit($preceding-context as xs:string?, $hit as xs:string, $following-context as xs:string?) as element(fcs:DataView) { + fcs-serializer:get-generic-hit($preceding-context, $hit, $following-context, ()) +}; + +(:~ builds a FCS DataView element with the given params + : @param $preceding-context the text content preceding the hit + : @param $hit the hit itself + : @param $following-context the text content following the hit + : @param $hit-attrs attributes that should be added to the hit element +:) +declare function fcs-serializer:get-generic-hit($preceding-context as xs:string?, $hit as xs:string, $following-context as xs:string?, $hit-attrs as attribute()*) as element(fcs:DataView) { + + + { $preceding-context || " " } + + { $hit-attrs } + { $hit } + + { " " || $following-context } + + +}; + +(:~ builds a FCS DataView element with the given params + : @param $items a sequence of item elements that have all layers as child elements and highlighting information as attribute (e.g. ` test `) Please note that `@highlight` is used to mark the hit itself and the first layer will be used to build the segments +:) +declare function fcs-serializer:get-advanced-data-view($items as element(item)*) as element(fcs:DataView) { + + + { + let $token-lengths := (: pre-calculate token lengths to boost performance :) + for $token in $items/layer[1] + return + string-length($token) + return + + { + for $token-length at $i in $token-lengths + let $start := + if ($i eq 1) then + 1 + else + sum($token-lengths[position() < $i]) + $i + let $end := $start + $token-length - 1 + return + + } + + } + + { + for $layer-id in distinct-values($items/layer/@id) + return + + { + for $item at $i in $items + return + + { $item/@highlight } + { data($item/layer[@id eq $layer-id]) } + + } + + } + + + +}; diff --git a/schema/annotatedItem.rng b/schema/annotatedItem.rng new file mode 100644 index 0000000..f103116 --- /dev/null +++ b/schema/annotatedItem.rng @@ -0,0 +1,33 @@ + + + + + + + + + + An item with annotation layers + + + + + + + + + + + + + An annotation layer + + + + + + + \ No newline at end of file