====== XQueries for aligned texts ======
**See more**: [[n:add-morph-align|add morphological information to aligned text]].
**Objective**: turn an [[http://repos1.alpheios.net/exist/rest/db/app/align-entersentence.xhtml|Alpheios aligned sentence]] into a [[http://docs.moodle.org/24/en/Matching_question_type|Moodle matching question]] XML.
===== Example =====
An example of the exercise imported in Moodle (showing a Latin sentence to be aligned with Croatian):
{{:n:moodlematching.png? |Moodle matching window}}
===== Alpheios XML format for aligned texts =====
Here is how an aligned sentence (L1 = Latin, L2 = Croatian) looks like after exporting the XML from Alpheios Text Alignment tool.
The simplest case is a single L1 word which has a single L2 equivalent:
**Challenges** are:
* L1 words which have //no// L2 equivalents:
* L1 words which have //several// L2 equivalents:
* several L1 words which have //the same// L2 equivalent:
===== Moodle matching question XML format =====
Moodle XML for matching questions looks like this:
Veni igitur et age de pace.
Stoga dođi i pregovaraj o miru.
]]>
(: transform Alpheios aligned text into Moodle quiz XML :)
(: simplest case - one-to-one correspondence :)
let $lt := "![CDATA[" (: less than :)
let $gt := "]]" (: greater than :)
for $v in //*:sentence/*:wds[@lnum="L1"]
return
Textus correlati
<{$lt}Povežite svaku latinsku riječ sljedeće rečenice s njezinim prijevodom.
ex iis quae mihi scribis
iz ovog što mi pišeš
{$gt}>
1.0000000
0.3333333
0
true
<{$lt}Odgovor je točan.
{$gt}>
<{$lt}Odgovor je djelomično točan.
{$gt}>
<{$lt}Odgovor nije točan.
{$gt}>
{for $a in //*:wds[@lnum='L2']/*:w[@n = $v/*:w/*:refs/@nrefs]
return
<{$lt}{data($v/*:w[@n = $a/@n]/*:text)}
{$gt}>
{data($a/*:text)}
}
===== Challenge 1: L1 words with no L2 equivalents =====
The problem:
de
Solution: define a variable (''$refs''); test if there is no ''refs/@nrefs'' combination present in a given ''w'' element (''if... then... else if...''); if the combination does not exist, replace it by zero (0).
The XQuery (caveat --- the produced format is not exactly Moodle XML):
(: test if there are no nrefs in L1 :)
for $nref in //*:wds[@lnum='L1']/*:w
return
{ data($nref/*:text) }
{
let $refs :=
if (string($nref/*:refs/@nrefs)) then
{data(//*:wds[@lnum='L2']/*:w[@n = string($nref/*:refs/@nrefs)]/*:text)}
else if (empty($nref/*:refs/@nrefs)) then
0
else ()
return $refs
}
Result (note the 0's):
bonam
veliko
spem
ufanje
de
0
te
0
concipio;
cèrpim.
non
neskitaš,
discurris
neskitaš,
===== Challenge 2: L1 words which have several L2 equivalents =====
The problem:
inquietaris.
Solution:
let $v := //*:sentence/*:wds[@lnum="L2"]
for $a in //*:wds[@lnum='L1']/*:w[@n = $v/*:w/*:refs/@nrefs]
return
{data($a/*:text)}
{
let $multi :=
(: test if any L1 word points to more L2 ones :)
if (contains(string($a/*:refs/@nrefs), ' ')) then
concat(data($v/*:w[@n = substring-before($a/*:refs/@nrefs, ' ')]/*:text),' ',data($v/*:w[@n = substring-after($a/*:refs/@nrefs,' ')]/*:text))
else if (string($a/*:refs/@nrefs)) then data($v/*:w[@n = string($a/*:refs/@nrefs)])
else ()
return $multi
}
The code produces this:
inquietaris.
te uznemiruje
===== Challenge 3: several L1 words with the same L2 equivalent =====
The problem:
non
discurris
(...)
neskitaš,
The XQuery:
let $v := //*:sentence/*:wds[@lnum="L1"]
for $a in //*:wds[@lnum='L2']/*:w[@n = $v/*:w/*:refs/@nrefs]
return
{
let $multi :=
(: test if any L2 word points to more L1 ones :)
if (contains(string($a/*:refs/@nrefs), ' ')) then
concat(data($v/*:w[@n = substring-before($a/*:refs/@nrefs, ' ')]/*:text),' ',data($v/*:w[@n = substring-after($a/*:refs/@nrefs,' ')]/*:text))
else if (string($a/*:refs/@nrefs)) then data($v/*:w[@n = $a/*:refs/@nrefs])
else ()
return $multi
}
{data($a/*:text)}
We test for the space ('' '') contained in the ''@nrefs'' attribute value. If there is at least one, we take the string apart (currently works for only two nrefs), then get the L2 words and put them together (concat them).
Result:
locorum mutationibus
skitanje.
====== Putting it all together... ======
TBA
====== Generate permutations with XQuery ======
Goal: we want to generate all permutations of a short sentence (cf. [[n:greek-latin-game?#baskets|Learning games for classical languages]]), afterwards creating with selected permutations a Moodle matching questions quiz -- to make the students decide whether a given word order is possible, impossible, or inbetween.
===== Example =====
{{ :n:segesta.png? |Segesta - matching possible word orders}}
===== XQuery =====
We want to generate permutations for the sequence //Cum in Africam venissem//:
Cum
in
Africam
venissem
We'll use the permutation generator described by Pekka Kilpeläinen in 2011.((Kilpeläinen, P. (2012), Using XQuery for problem solving. Softw: Pract. Exper., 42: 1433–1465. doi: [[http://onlinelibrary.wiley.com/doi/10.1002/spe.1140/full|10.1002/spe.1140]].))
The XQuery is here (we use it on first four words of a larger sentence):
(: permutation generator function by Pekka Kilpeläinen, 2011 :)
declare function local:perm1($items as item()*) as element(perm)+ {
if (count($items) le 1) then {$items}
else for $i in 1 to count($items),
$perm in local:perm1(remove($items, $i))
return {insert-before($perm/node(), 1, $items[$i])} };
(: permute first four Latin words :)
let $it := subsequence(//*:wds[1]/*:w, 1, 4)
return local:perm1($it)
This is the result:
Cum
in
Africam
venissem
Cum
in
venissem
Africam
Cum
Africam
in
venissem
Cum
Africam
venissem
in
Cum
venissem
in
Africam
Cum
venissem
Africam
in
in
Cum
Africam
venissem
in
Cum
venissem
Africam
in
Africam
Cum
venissem
in
Africam
venissem
Cum
in
venissem
Cum
Africam
in
venissem
Africam
Cum
Africam
Cum
in
venissem
Africam
Cum
venissem
in
Africam
in
Cum
venissem
Africam
in
venissem
Cum
Africam
venissem
Cum
in
Africam
venissem
in
Cum
venissem
Cum
in
Africam
venissem
Cum
Africam
in
venissem
in
Cum
Africam
venissem
in
Africam
Cum
venissem
Africam
Cum
in
venissem
Africam
in
Cum