Skip to content

Commit

Permalink
dumped files
Browse files Browse the repository at this point in the history
  • Loading branch information
pdbro2k authored Oct 30, 2023
1 parent 8b15441 commit 52682ad
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 0 deletions.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
(
<item>
<layer id="http://www.example.com/ns/fcs/layer/text">Lorem</layer>
<layer id="http://www.example.com/ns/fcs/layer/lemma">lorem</layer>
</item>,
<item highlight="h1">
<layer id="http://www.example.com/ns/fcs/layer/text">ipsum</layer>
<layer id="http://www.example.com/ns/fcs/layer/lemma">ipsum</layer>
</item>
)
```

### Returns:

**element(fcs:DataView)** the proper FCS equivalent - e.g.

```xml
<fcs:DataView xmlns:fcs="http://clarin.eu/fcs/resource" type="application/x-clarin-fcs-adv+xml">
<adv:Advanced xmlns:adv="http://clarin.eu/fcs/dataview/advanced" unit="item">
<adv:Segments>
<adv:Segment id="s0" start="1" end="5"/>
<adv:Segment id="s1" start="7" end="11"/>
</adv:Segments>
<adv:Layers>
<adv:Layer id="http://www.example.com/ns/fcs/layer/text">
<adv:Span ref="s0">Lorem</adv:Span>
<adv:Span ref="s1" highlight="h1">ipsum</adv:Span>
</adv:Layer>
<adv:Layer id="http://www.example.com/ns/fcs/layer/lemma">
<adv:Span ref="s0">lorem</adv:Span>
<adv:Span ref="s1" highlight="h1">ipsum</adv:Span>
</adv:Layer>
</adv:Layers>
</adv:Advanced>
</fcs:DataView>
```

---

# License
The software is published under the terms of the MIT license.


# Research Software Engineering and Development

Copyright 2023 <a href="https://orcid.org/0000-0002-5843-7577">Patrick Daniel Brookshire</a>

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.
117 changes: 117 additions & 0 deletions fcs-serializer.xqm
Original file line number Diff line number Diff line change
@@ -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)*) {
<fcs:Resource xmlns:fcs="http://clarin.eu/fcs/resource">
{
if ($resource-pid eq "") then
()
else
attribute pid { $resource-pid }
}
{
if ($resource-ref eq "") then
()
else
attribute ref { $resource-ref }
}
<fcs:ResourceFragment ref="{ $fragment-ref }">
{ $data-views }
</fcs:ResourceFragment>
</fcs:Resource>
};

(:~ 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) {
<fcs:DataView type="application/x-clarin-fcs-hits+xml">
<hits:Result xmlns:hits="http://clarin.eu/fcs/dataview/hits">
{ $preceding-context || " " }
<hits:Hit>
{ $hit-attrs }
{ $hit }
</hits:Hit>
{ " " || $following-context }
</hits:Result>
</fcs:DataView>
};

(:~ 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. `<item highlight="h1"> <layer id="http://www.example.com/ns/fcs/layer/text">test</layer> </item>`) 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) {
<fcs:DataView type="application/x-clarin-fcs-adv+xml">
<adv:Advanced unit="item" xmlns:adv="http://clarin.eu/fcs/dataview/advanced">
{
let $token-lengths := (: pre-calculate token lengths to boost performance :)
for $token in $items/layer[1]
return
string-length($token)
return
<adv:Segments>
{
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
<adv:Segment id="s{ $i - 1 }" start="{ $start }" end="{ $end }"/>
}
</adv:Segments>
}
<adv:Layers>
{
for $layer-id in distinct-values($items/layer/@id)
return
<adv:Layer id="{ $layer-id }">
{
for $item at $i in $items
return
<adv:Span ref="s{ $i - 1 }">
{ $item/@highlight }
{ data($item/layer[@id eq $layer-id]) }
</adv:Span>
}
</adv:Layer>
}
</adv:Layers>
</adv:Advanced>
</fcs:DataView>
};
33 changes: 33 additions & 0 deletions schema/annotatedItem.rng
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<grammar
xmlns="http://relaxng.org/ns/structure/1.0"
xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<ref name="item"/>
</start>

<!-- ROOT ITEM -->
<define name="item">
<element name="item">
<a:documentation>An item with annotation layers</a:documentation>
<optional>
<attribute name="highlight"/>
</optional>
<oneOrMore>
<ref name="layer"/>
</oneOrMore>
</element>
</define>

<!-- LAYER -->
<define name="layer">
<element name="layer">
<a:documentation>An annotation layer</a:documentation>
<attribute name="id">
<data type="anyURI"/>
</attribute>
<text/>
</element>
</define>
</grammar>

0 comments on commit 52682ad

Please sign in to comment.