comparison software/eXist/webapp/mpdl/interface/lt/wordInfo.xql @ 7:5589d865af7a

Erstellung XQL/XSL Applikation
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 08 Feb 2011 15:16:46 +0100
parents
children d6f528ad5d96
comparison
equal deleted inserted replaced
6:2396a569e446 7:5589d865af7a
1 xquery version "1.0";
2
3 import module namespace mpdl-time = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/util/time" at "../../util/time.xql";
4
5 declare namespace request="http://exist-db.org/xquery/request";
6 declare namespace xlink="http://www.w3.org/1999/xlink";
7
8 (: TODO: Verlinkung von lemma, form, weiteren Einheiten in den Infos auf mpdl-proto (HTML und XML) :)
9
10 let $type := request:get-parameter("type", "all")
11 let $language := request:get-parameter("language", "")
12 let $word := request:get-parameter("word", "")
13 let $placeHref := request:get-parameter("placeHref", "")
14 let $output := request:get-parameter("output", "xml")
15
16 let $currentTimeBegin := util:system-time()
17
18 let $lemmas := mpdltext:get-lemmas-by-form-name($language, $word)
19 let $htmlOrderedLemmas :=
20 if (empty($lemmas))
21 then ("no lemmas found for your query")
22 else
23 for $lemma in $lemmas/lemmas/lemma
24 let $lemmaText := concat($lemma/lemma-name, " (", $lemma/provider, ")")
25 let $orderedFormsStrTmp :=
26 for $form in $lemma/forms/form
27 let $formStr := concat($form/form-name, " (", $form/provider, ")")
28 order by $form/form-name
29 return $formStr
30 let $orderedFormsStr := string-join($orderedFormsStrTmp, ", ")
31 let $remotePerseusLink :=
32 if ($language = "ar" or $language = "la")
33 then concat("http://www.perseus.tufts.edu/hopper/morph?l=", $lemma/lemma-name, "&amp;la=", $language)
34 else if ($language = "el")
35 then concat("http://www.perseus.tufts.edu/hopper/morph?l=", $lemma/lemma-name, "&amp;la=greek")
36 else ""
37 let $lemmaLi :=
38 if ($language = "ar" or $language = "el" or $language = "la")
39 then
40 <li><b>Lemma: </b>{$lemmaText} (see this entry in <a href="{$remotePerseusLink}">www.perseus.tufts.edu</a>)
41 <ul>{$orderedFormsStr}</ul>
42 </li>
43 else
44 <li><b>Lemma: </b>{$lemmaText}
45 <ul>{$orderedFormsStr}</ul>
46 </li>
47 order by $lemma/lemma-name
48 return $lemmaLi
49 let $xmlOrderedLemmas :=
50 for $lemma in $lemmas/lemmas/lemma
51 let $orderedForms :=
52 for $form in $lemma/forms/form
53 order by $form/form-name
54 return $form
55 let $retLemma :=
56 <lemma>
57 {$lemma/provider}
58 {$lemma/language}
59 {$lemma/lemma-name}
60 <forms size="{$lemma/forms-size}">{$orderedForms}</forms>
61 </lemma>
62 order by $lemma/lemma-name
63 return $retLemma
64 let $lemmasStrTmpTmp :=
65 if (empty($lemmas))
66 then ""
67 else
68 for $lemma in $lemmas/lemmas/lemma
69 let $lemmaStr := $lemma/lemma-name
70 order by $lemma/lemma-name
71 return $lemmaStr
72 let $lemmasStrWithoutWord := string-join($lemmasStrTmpTmp, " ")
73 let $lemmasStrTmp :=
74 if (not(contains($lemmasStrWithoutWord, $word)))
75 then concat($word, " ", $lemmasStrWithoutWord) (: also the form itself is added :)
76 else $lemmasStrWithoutWord
77 let $lemmasStr :=
78 if ($language = "de" and (contains($lemmasStrTmp, "ae") or contains($lemmasStrTmp, "oe") or contains($lemmasStrTmp, "ue") or contains($lemmasStrTmp, "ss")))
79 then replace(replace(replace(replace($lemmasStrTmp, "ae", "ä"), "oe", "ö"), "ue", "ü"), "ss", "ß")
80 else $lemmasStrTmp
81 let $lemmasStrTokenized := tokenize($lemmasStr, " ")
82
83 let $dictionariesLocal :=
84 if ($type = "all" or $type = "dict")
85 then mpdltext:get-lex-entries-by-lucene-query($language, $lemmasStr)
86 else ()
87 let $dictionariesRemoteTmp :=
88 if ($language = "de")
89 then
90 <lexica>
91 <lexicon>
92 <name>dwds</name>
93 <description>Deutsches Wörterbuch der deutschen Sprache</description>
94 </lexicon>
95 </lexica>
96 else if ($language = "el")
97 then
98 <lexica>
99 <lexicon>
100 <name>slater</name>
101 <description>William J. Slater, Lexicon to Pindar</description>
102 </lexicon>
103 </lexica>
104 else if ($language = "fr")
105 then
106 <lexica>
107 <lexicon>
108 <name>artfl-fr</name>
109 <description>The ARTFL project: Dictionnaires d'autrefois: French dictionaries of the 17th, 18th, 19th and 20th centuries</description>
110 </lexicon>
111 <lexicon>
112 <name>artfl-fr-en</name>
113 <description>The ARTFL project: French - English dictionary</description>
114 </lexicon>
115 </lexica>
116 else if ($language = "la")
117 then
118 <lexica>
119 <lexicon>
120 <name>lewis</name>
121 <description>Charlton T. Lewis, an Elementary Latin Dictionary</description>
122 </lexicon>
123 </lexica>
124 else if ($language = "nl")
125 then
126 <lexica>
127 <lexicon>
128 <name>wikiwoordenboek</name>
129 <description>Wiktionary: WikiWoordenboek</description>
130 </lexicon>
131 </lexica>
132 else if ($language = "zh")
133 then
134 <lexica>
135 <lexicon>
136 <name>linyutan</name>
137 <description>Lin Yutang</description>
138 </lexicon>
139 </lexica>
140 else ()
141
142 let $dictionariesRemote :=
143 for $lex in $dictionariesRemoteTmp//lexicon
144 let $lexName := $lex/name
145 let $lexEntries :=
146 for $l in $lemmasStrTokenized
147 let $lLink :=
148 if ($language = "el")
149 then mpdltext:transcode("unicode", "betacode", $l)
150 else if ($language = "zh")
151 then mpdltext:encode-big5($l)
152 else $l
153 let $repairedEntryContentLink :=
154 if ($lexName = "dwds")
155 then concat("http://beta.dwds.de/?qu=", $l)
156 else if ($lexName = "slater")
157 then concat("http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.04.0072:entry=", $lLink)
158 else if ($lexName = "artfl-fr-en")
159 then concat("http://machaut.uchicago.edu/?resource=frengdict&amp;action=search&amp;french=", $l)
160 else if ($lexName = "artfl-fr")
161 then concat("http://artflx.uchicago.edu/cgi-bin/dicos/pubdico1look.pl?strippedhw=", $l)
162 else if ($lexName = "lewis")
163 then concat("http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.04.0060:entry=", $l)
164 else if ($lexName = "wikiwoordenboek")
165 then concat("http://nl.wiktionary.org/wiki/", $l)
166 else if ($lexName = "linyutan")
167 then concat("http://humanum.arts.cuhk.edu.hk/cgi-bin/agrep-lindict?query=", $lLink, "&amp;category=wholerecord")
168 else ""
169 let $lexiconEntry :=
170 <entry>
171 <form>{$l}</form>
172 <content>
173 <xml-valid>true</xml-valid>
174 <original-entry>&lt;original-entry&gt;&lt;/original-entry&gt;</original-entry>
175 <repaired-entry><directLink xlink:type="simple" xlink:href="{$repairedEntryContentLink}">{$l}</directLink></repaired-entry>
176 </content>
177 </entry>
178 return $lexiconEntry
179 return
180 <lexica>
181 <lexicon>
182 <name>{$lex/name}</name>
183 <description>{$lex/description}</description>
184 <entries>{$lexEntries}</entries>
185 </lexicon>
186 </lexica>
187
188 let $dictionaries :=
189 <result>
190 <lexica>
191 {$dictionariesLocal//lexicon}
192 {$dictionariesRemote//lexicon}
193 </lexica>
194 </result>
195 let $retDictionaries :=
196 if (empty($dictionaries))
197 then ()
198 else
199 for $dictionary in $dictionaries/lexica/lexicon
200 let $dictName := $dictionary/name
201 let $dictDescription := $dictionary/description
202 let $dictEntries := $dictionary/entries
203 let $entryDictEntryContent :=
204 for $dictEntry in $dictionary/entries/entry
205 let $dictEntryContent := $dictEntry/content
206 let $dictEntryXmlValid := $dictEntryContent/xml-valid
207 let $dictEntryOriginalContent := $dictEntryContent/original-entry
208 let $dictEntryRepairedContent := $dictEntryContent/repaired-entry
209 let $dictEntryRepairedContentLink := $dictEntryRepairedContent/directLink
210 let $dictEntryForm :=
211 if ($language = "el")
212 then mpdltext:transcode("unicode", "betacode", string($dictEntry/form))
213 else if ($language = "ar")
214 then mpdltext:transcode("unicode", "buckwalter", string($dictEntry/form))
215 else $dictEntry/form
216 let $dictEntryContentParsedTmp :=
217 if ($dictEntryXmlValid = "true" and empty($dictEntryRepairedContentLink))
218 then util:parse($dictEntryRepairedContent)
219 else if ($dictEntryXmlValid = "true" and not(empty($dictEntryRepairedContentLink)) and $output = "html")
220 then <div>External link: <a href="{$dictEntryRepairedContentLink/@xlink:href}">{$dictEntryRepairedContentLink}</a></div>
221 else if ($dictEntryXmlValid = "true" and not(empty($dictEntryRepairedContentLink)) and $output = "xml")
222 then <div>{$dictEntryRepairedContentLink}</div>
223 else <bla><text>[<i>Remark: entry in dictionary has no valid XML, so only the Betacode text version of the entry could be displayed</i>]</text><p></p>{$dictEntryOriginalContent}</bla>
224 let $dictEntryContentParsed :=
225 if ($dictionary/name = "ls" and $output = "html")
226 then <div>{$dictEntryContentParsedTmp}<br/>(external link to <a href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.04.0059:entry={$dictEntry/form}">www.perseus.tufts.edu</a>)</div>
227 else if ($dictionary/name = "ls" and $output = "xml")
228 then <div>{$dictEntryContentParsedTmp}<directLink type="simple" xlink:href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.04.0059:entry={$dictEntry/form}">www.perseus.tufts.edu</directLink></div>
229 else if ($dictionary/name = "lsj" and $output = "html")
230 then <div>{$dictEntryContentParsedTmp}<br/>(external link to <a href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.04.0057:entry={$dictEntryForm}">www.perseus.tufts.edu</a>)</div>
231 else if ($dictionary/name = "lsj" and $output = "xml")
232 then <div>{$dictEntryContentParsedTmp}<directLink type="simple" xlink:href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.04.0057:entry={$dictEntryForm}">www.perseus.tufts.edu</directLink></div>
233 else if ($dictionary/name = "autenrieth" and $output = "html")
234 then <div>{$dictEntryContentParsedTmp}<br/>(external link to <a href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.04.0073:entry={$dictEntryForm}">www.perseus.tufts.edu</a>)</div>
235 else if ($dictionary/name = "autenrieth" and $output = "xml")
236 then <div>{$dictEntryContentParsedTmp}<directLink type="simple" xlink:href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.04.0073:entry={$dictEntryForm}">www.perseus.tufts.edu</directLink></div>
237 else if ($dictionary/name = "buckwalter" and $output = "html")
238 then <div>{$dictEntryContentParsedTmp}<br/>(external link to <a href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:2002.02.0014:entry={$dictEntryForm}">www.perseus.tufts.edu</a>)</div>
239 else if ($dictionary/name = "buckwalter" and $output = "xml")
240 then <div>{$dictEntryContentParsedTmp}<directLink type="simple" xlink:href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:2002.02.0014:entry={$dictEntryForm}">www.perseus.tufts.edu</directLink></div>
241 else if ($dictionary/name = "salmone" and $output = "html")
242 then <div>{$dictEntryContentParsedTmp}<br/>(external link to <a href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:2002.02.0005:entry={$dictEntryForm}">www.perseus.tufts.edu</a>)</div>
243 else if ($dictionary/name = "salmone" and $output = "xml")
244 then <div>{$dictEntryContentParsedTmp}<directLink type="simple" xlink:href="http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:2002.02.0005:entry={$dictEntryForm}">www.perseus.tufts.edu</directLink></div>
245 else $dictEntryContentParsedTmp
246 let $liDictEntryContentParsed :=
247 if ($output = "html")
248 then <li>{($dictEntryContentParsed)}</li>
249 else if ($output = "xml")
250 then <entry>{($dictEntryContentParsed)}</entry>
251 else ()
252 return $liDictEntryContentParsed
253 let $dictLi :=
254 if ($output = "html")
255 then
256 <li><b>{$dictDescription}</b>
257 <ul>{$entryDictEntryContent}</ul>
258 </li>
259 else if ($output = "xml")
260 then
261 <dictionaryEntries>
262 <dictionary>
263 <name>{$dictName}</name>
264 <description>{$dictDescription}</description>
265 <entries>{$entryDictEntryContent}</entries>
266 </dictionary>
267 </dictionaryEntries>
268 else ()
269 return $dictLi
270
271 let $wikiArticles :=
272 if (empty($lemmas))
273 then ()
274 else
275 for $l in $lemmasStrTokenized
276 let $wikiHref1 := concat("http://", $language, ".wikipedia.org/wiki/", $l)
277 let $wikiHref2 := concat("http://", $language, ".wikipedia.org/wiki/index.php?search=", $l)
278 let $wikiArticle :=
279 if ($output = "html")
280 then <li><b>Article: </b>External link: <a href="{$wikiHref1}">{$l}</a> (or search for <a href="{$wikiHref2}">{$l})</a></li>
281 else if ($output = "xml")
282 then
283 <article>
284 <name>{$l}</name>
285 <directLink xlink:type="simple" xlink:href="{$wikiHref1}"/>
286 <searchLink xlink:type="simple" xlink:href="{$wikiHref2}"/>
287 </article>
288 else ()
289 return $wikiArticle
290 let $places :=
291 if ($placeHref != "" and $output = "html")
292 then <li><b>Place: </b>External link: <a href="{$placeHref}">{$word}</a></li>
293 else if ($placeHref != "" and $output = "html")
294 then
295 <place>
296 <name>{$word}</name>
297 <directLink xlink:type="simple" xlink:href="{$placeHref}"/>
298 </place>
299 else ()
300
301 let $dict :=
302 if (not(empty($retDictionaries)) and ($type = "all" or $type = "dict") and $output = "html")
303 then
304 <p>
305 <h3>Dictionary</h3>
306 <ul>{$retDictionaries}</ul>
307 </p>
308 else if (empty($retDictionaries) and ($type = "all" or $type = "dict") and $output = "html")
309 then
310 <p>
311 <h3>Dictionary</h3>
312 <ul>No information available</ul>
313 </p>
314 else if (not(empty($retDictionaries)) and ($type = "all" or $type = "dict") and $output = "xml")
315 then $retDictionaries
316 else ()
317 let $morph :=
318 if (not(empty($htmlOrderedLemmas)) and ($type = "all" or $type = "morph") and $output = "html")
319 then
320 <p>
321 <h3>Morphology</h3>
322 <ul>{$htmlOrderedLemmas}</ul>
323 </p>
324 else if (empty($htmlOrderedLemmas) and ($type = "all" or $type = "morph") and $output = "html")
325 then
326 <p>
327 <h3>Morphology</h3>
328 <ul>No information available</ul>
329 </p>
330 else if (not(empty($htmlOrderedLemmas)) and ($type = "all" or $type = "morph") and $output = "xml")
331 then <morphologyEntries>{$xmlOrderedLemmas}</morphologyEntries>
332 else if ($type = "morph" and $output = "string")
333 then $lemmasStrWithoutWord
334 else ()
335 let $wiki :=
336 if (not(empty($wikiArticles)) and ($type = "all" or $type = "wiki") and $output = "html")
337 then
338 <p>
339 <h3>Wikipedia</h3>
340 <ul>{$wikiArticles}</ul>
341 </p>
342 else if (not(empty($wikiArticles)) and ($type = "all" or $type = "wiki") and $output = "xml")
343 then <wikiEntries>{$wikiArticles}</wikiEntries>
344 else ()
345 let $place :=
346 if (not(empty($places)) and ($type = "all" or $type = "place") and $output = "html")
347 then
348 <p>
349 <h3>Place</h3>
350 <ul>{$places}</ul>
351 </p>
352 else if (not(empty($places)) and ($type = "all" or $type = "place") and $output = "xml")
353 then <placeEntries>{$places}</placeEntries>
354 else ()
355
356 let $currentTimeEnd := util:system-time()
357 let $neededTime := mpdl-time:duration-as-ms($currentTimeEnd - $currentTimeBegin)
358
359 let $declare :=
360 if ($output = "html" or $output = "string")
361 then util:declare-option("exist:serialize", "method=html media-type=text/html omit-xml-declaration=no indent=yes encoding=utf-8")
362 else util:declare-option("exist:serialize", "method=xml media-type=text/xml omit-xml-declaration=no indent=yes encoding=utf-8")
363
364 let $queryResultHeaderStr := <h2>Word information for: {$word}</h2>
365 let $commentExternalLinks :=
366 "[* no guarantee for external links]"
367
368 let $retXmlResult :=
369 <word>
370 <form>{$word}</form>
371 <provider>Max Planck Institute for the History of Science, Berlin</provider>
372 {$morph}
373 {$dict}
374 {$wiki}
375 {$place}
376 </word>
377 let $retHtmlResult :=
378 <html>
379 <head>
380 <title>Mpdl: word information</title>
381 </head>
382 <body>
383 <table align="right" valign="top">
384 <td>[<i>This is a MPDL language technology service</i>] <a href="../../info.xql?info=wordInfo" onclick="window.open(&quot;../../info.xql?info=wordInfo&quot;, &quot;InfoWindow&quot;, &quot;menubar=no,width=800,height=600,toolbar=yes,scrollbars=yes&quot;);return false"><img src="../../images/info.png" valign="bottom" width="15" height="15" border="0" alt="MPDL language technology service"/></a></td>
385 </table>
386 <p/>
387 {$queryResultHeaderStr}
388 {$morph}
389 {$dict}
390 {$wiki}
391 {$place}
392 {$commentExternalLinks}
393 <hr/>
394 <p/>
395 Elapsed time: {$neededTime} ms, see the <a href="/exist/xquery.xml">XQuery documentation</a> and the <a href="wordInfo.xql?_source=yes">XQuery source</a> of this page, if you find a bug <a href="https://itgroup.mpiwg-berlin.mpg.de:8080/tracs/mpdl-project-software/newticket">let us know</a>
396 </body>
397 </html>
398
399 let $retResult :=
400 if ($output = "html")
401 then $retHtmlResult
402 else if ($type = "morph" and $output = "string")
403 then $morph
404 else $retXmlResult
405 return $retResult