More documentation.

More documentation.

xquery version "3.0";

(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : 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.
 :)

(:~
 : This module provides an XQuery API to full-text functions.
 :

 : The ft:thesaurus-lookup function has "levels"
 : and "relationship" parameters.
 : The values for these are implementation-defined.
 : Zorba's default implementation uses the
 : WordNet lexical database,
 : version 3.0.
 :

 : In WordNet,
 : the number of "levels" that two phrases are apart
 : are how many hierarchical meanings apart they are.
 : For example,
 : "canary" is 5 levels away from "vertebrate"
 : (carary > finch > oscine > passerine > bird > vertebrate).
 :

 : When using the WordNet implementation,
 : Zorba supports all of the relationships (and their abbreviations)
 : specified by ISO 2788 and ANSI/NISO Z39.19-2005
 : with the exceptions of "HN" (history note)
 : and "X SN" (see scope note for).
 : Additionally, Zorba also supports all WordNet relationships.
 :

 :

 :     Rel.
 :     Meaning
 :     WordNet Rel.
 :

 :

 :     BT
 :     broader term
 :     hypernym
 :

 :

 :     BTG
 :     broader term generic
 :     hypernym
 :

 :

 :     BTI
 :     broader term instance
 :     instance hypernym
 :

 :

 :     BTP
 :     broader term partitive
 :     part meronym
 :

 :

 :     NT
 :     narrower term
 :     hyponym
 :

 :

 :     NTG
 :     narrower term generic
 :     hyponym
 :

 :

 :     NTI
 :     narrower term instance
 :     instance hyponym
 :

 :

 :     NTP
 :     narrower term partitive
 :     part holonym
 :

 :

 :     RT
 :     related term
 :     also see
 :

 :

 :     SN
 :     scope note
 :     n/a
 :

 :

 :     TT
 :     top term
 :     hypernym
 :

 :

 :     UF
 :     non-preferred term
 :     n/a
 :

 :

 :     USE
 :     preferred term
 :     n/a
 :

 :

 : Relationships are case-insensitive.
 :)
module namespace ft = "http://www.zorba-xquery.com/modules/full-text";

import schema namespace ft-schema =
  "http://www.zorba-xquery.com/modules/full-text";

declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://www.zorba-xquery.com/errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Gets the current language: either the langauge specified by the
 : declare ft-option using language statement or the one returned
 : by ft:host-lang().
 :
 : @return said language.
 :)
declare function ft:current-lang()
  as xs:language external;

(:~
 : Gets the host's current language.  The "host" is the computer on which Zorba
 : is running.  The host's current language is obtained as follows:
 :

 :

 :      For *nix systems:
 :

 :

 :          If setlocale(3) returns non-null,
 :          the language corresponding to that locale is used.
 :

 :

 :          Else, if the LANG environment variable is set,
 :          that language is ued.
 :

 :

 :          Otherwise, there is no default language.
 :

 :

 :

 :

 :      For Windows systems, the language corresponding to the locale returned
 :      by the GetLocaleInfoA() function is used.
 :

 :

 :
 : @return said language.
 :)
declare function ft:host-lang()
  as xs:language external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : @param $lang The language of $word.
 : @return true only if the $word is a stop-word.
 :)
declare function ft:is-stop-word( $word as xs:string, $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.  The word's language is assumed to be the
 : one returned by ft:current-lang().
 : @return true only if the $word is a stop-word.
 :)
declare function ft:is-stop-word( $word as xs:string )
  as xs:boolean
{
  ft:is-stop-word( $word, ft:current-lang() )
};

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : @param $lang The language of $word.
 : @return the stem of $word.
 :)
declare function ft:stem( $word as xs:string, $lang as xs:language )
  as xs:string external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.  The word's language is assumed to be the
 : one returned by ft:current-lang().
 : @return the stem of $word.
 :)
declare function ft:stem( $word as xs:string )
  as xs:string
{
  ft:stem( $word, ft:current-lang() )
};

(:~
 : Strips all diacritical marks from all characters.
 :
 : @param $string The string to strip diacritical marks from.
 : @return $string with diacritical marks stripped.
 :)
declare function ft:strip-diacritics( $string as xs:string )
  as xs:string external;

(:~
 : Looks-up the given phrase in the default thesaurus.
 :
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one returned by ft:current-lang().
 : @return the related phrases.
 : @error err:FTST0009 if ft:current-lang() is unsupported.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data.
 :)
declare function ft:thesaurus-lookup( $phrase as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @return the related phrases.
 : @error err:FTST0009 if $lang is unsupported.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one the one returned by ft:current-lang().
 : @return the related phrases.
 : @error err:FTST0009 if ft:current-lang() is unsupported.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string )
  as xs:string+
{
  ft:thesaurus-lookup( $uri, $phrase, ft:current-lang() )
};

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @return the original and related phrases.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is unsupported.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @param $level-least The minimum number of levels within the thesaurus to be
 : travers$ed.
 : @param $level-most The maximum number of levels within the thesaurus to be
 : traversed.
 : @return the original and related phrases.
 : @error err:FOCA0003 if either $level-least or
 : $level-most is either negative or too large.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is unsupported.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string,
                                      $level-least as xs:integer,
                                      $level-most as xs:integer )
  as xs:string+ external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The document to tokenize.
 : @param $lang The default language of $doc.
 : @return a (possibly empty) sequence of tokens.
 :)
declare function ft:tokenize( $doc as node(), $lang as xs:language )
  as node()* external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The XML document to tokenize.  The document's default language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 :)
declare function ft:tokenize( $doc as node() )
  as node()*
{
  ft:tokenize( $doc, ft:current-lang() )
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : @param $lang The default language of $string.
 : @return a (possibly empty) sequence of tokens.
 :)
declare function ft:tokenize-string( $string as xs:string,
                                     $lang as xs:language )
  as xs:string*
{
  () (: TODO :)
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.  The string's default language is
 : assumed to be the host's current language.
 : @return a (possibly empty) sequence of tokens.
 :)
declare function ft:tokenize-string( $string as xs:string )
  as xs:string*
{
  ft:tokenize-string( $string, ft:current-lang() )
};

(:~
 : Gets properties of the tokenizer for the given language.
 :
 : @param $lang The langauage of the tokenizer to get the properties of.
 : @return said properties.
 :)
declare function ft:tokenizer-properties( $lang as xs:language )
  as node() external;

(:~
 : Gets properties of the tokenizer for the current language.
 :
 : @return said properties.
 :)
declare function ft:tokenizer-properties()
  as node()
{
  ft:tokenizer-properties( ft:current-lang() )
};

(: vim:set et sw=2 ts=2: :)

xquery version "3.0";

(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : 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.
 :)

(:~
 : This module provides an XQuery API to full-text functions.
 :)
module namespace ft = "http://www.zorba-xquery.com/modules/full-text";

import schema namespace ft-schema =
  "http://www.zorba-xquery.com/modules/full-text";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Gets the current language: either the langauge specified by the
 : declare ft-option using language statement or the host's
 : current language if none.
 :
 : @return said language.
 :)
declare function ft:current-lang()
  as xs:language external;

(:~
 : Gets the host's current language.
 :
 : @return said language.
 :)
declare function ft:host-lang()
  as xs:language external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : @param $lang The language of $word.
 : @return true only if the $word is a stop-word.
 :)
declare function ft:is-stop-word( $word as xs:string, $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.  The word's language is assumed to be the
 : one in the static context (if specified via declare ft-option)
 : or the host's current language (if not).
 : @return true only if the $word is a stop-word.
 :)
declare function ft:is-stop-word( $word as xs:string )
  as xs:boolean
{
  ft:is-stop-word( $word, ft:current-lang() )
};

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : @param $lang The language of $word.
 : @return the stem of $word.
 :)
declare function ft:stem( $word as xs:string, $lang as xs:language )
  as xs:string external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.  The word's language is assumed to be the
 : one in the static context (if specified via declare ft-option)
 : or the host's current language (if not).
 : @return the stem of $word.
 :)
declare function ft:stem( $word as xs:string )
  as xs:string
{
  ft:stem( $word, ft:current-lang() )
};

(:~
 : Strips all diacritical marks from all characters.
 :
 : @param $string The string to strip diacritical marks from.
 : @return $string with diacritical marks stripped.
 :)
declare function ft:strip-diacritics( $string as xs:string )
  as xs:string external;

(:~
 : Looks-up the given phrase in the default thesaurus.
 :
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one in the static context (if specified via declare
 : ft-option) or the host's current language (if not).
 : @return the related phrases.
 :)
declare function ft:thesaurus-lookup( $phrase as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @return the related phrases.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one in the static context (if specified via declare
 : ft-option) or the host's current language (if not).
 : @return the related phrases.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string )
  as xs:string+
{
  ft:thesaurus-lookup( $uri, $phrase, ft:current-lang() )
};

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @return the related phrases.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @param $level-least The minimum number of levels within the thesaurus to be
 : travers$ed.
 : @param $level-most The maximum number of levels within the thesaurus to be
 : traversed.
 : @return the related phrases.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string,
                                      $level-least as xs:integer,
                                      $level-most as xs:integer )
  as xs:string+ external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The document to tokenize.
 : @param $lang The default language of $doc.
 : @return a (possibly empty) sequence of tokens.
 :)
declare function ft:tokenize( $doc as node(), $lang as xs:language )
  as node()* external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The XML document to tokenize.  The document's default language
 : is assumed to be the one in the static context (if specified via
 : declare ft-option) or the host's current language (if not).
 : @return a (possibly empty) sequence of tokens.
 :)
declare function ft:tokenize( $doc as node() )
  as node()*
{
  ft:tokenize( $doc, ft:current-lang() )
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : @param $lang The default language of $string.
 : @return a (possibly empty) sequence of tokens.
 :)
declare function ft:tokenize-string( $string as xs:string,
                                     $lang as xs:language )
  as xs:string*
{
  () (: TODO :)
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.  The string's default language is
 : assumed to be the host's current language.
 : @return a (possibly empty) sequence of tokens.
 :)
declare function ft:tokenize-string( $string as xs:string )
  as xs:string*
{
  ft:tokenize-string( $string, ft:current-lang() )
};

(:~
 : Gets properties of the tokenizer for the given language.
 :
 : @param $lang The langauage of the tokenizer to get the properties of.
 : @return said properties.
 :)
declare function ft:tokenizer-properties( $lang as xs:language )
  as node() external;

(:~
 : Gets properties of the tokenizer for the current language.
 :
 : @return said properties.
 :)
declare function ft:tokenizer-properties()
  as node()
{
  ft:tokenizer-properties( ft:current-lang() )
};

(: vim:set et sw=2 ts=2: :)


avoid having twice the same query in history

svn path=/trunk/eXist-1.0/; revision=267

xquery version "1.0";

declare namespace request="http://exist-db.org/xquery/request";
declare namespace util="http://exist-db.org/xquery/util";

(: Namespace for the local functions in this script :)
declare namespace f="http://exist-db.org/xquery/local-functions";

(:
    Display hits for the current page. The number of hits to print per page
    is read from parameter "howmany". Parameter "start" identifies the
    current offset in the result set.
:)
declare function f:display($hits as item()*, $count as xs:int)
as element()+
{
    if (empty($hits)) then

Nothing found! Back to query form.

    else
        let $howmany := request:request-parameter("howmany", "10") cast as xs:int,
            $start := request:request-parameter("start", "1") cast as xs:int,
            $end := if ($start + $howmany le $count) then $start + $howmany - 1 else $count
        return (

                        <New Query>

                { f:navbar($start, $end, $howmany, $count) }
                {
                    for $p in $start to $end
                    let $current := item-at($hits, $p),
                        $style := if($p mod 2 eq 0) then "high" else "low"
                    return

                            {$p}

                                <xml-source>{$current}</xml-source>

                }
                { f:navbar($start, $end, $howmany, $count) }

                        <New Query>

        )
};

(: Display the navigation bar :)
declare function f:navbar($start as xs:int, $end as xs:int,
$hitsPerPage as xs:int, $count as xs:int) as element()
{
    let $uri := request:request-uri()
    return

                {
                    (: Link to previous page :)
                    if ($start gt 1) then
                        <<
                    else ()
                }

                {
                    (: Create page shortcuts :)
                    let $sections := $count idiv $hitsPerPage - 1,
                        $currentSection := $start idiv $hitsPerPage,
                        $lastSection :=
                            if ($sections lt 0) then 0 else
                                (: show 10 max unless current page > 10 :)
                                if ($sections gt 9) then
                                    if ($currentSection gt 8) then $currentSection + 1
                                    else 9
                                else $sections
                    for $i in 0 to $lastSection
                    let $s := xs:int($i * $hitsPerPage + 1)
                    return
                        if($i ne $currentSection) then

                                [{$i + 1}]

                        else
                            (: current page doesn't get a link :)
                            [{$i + 1}]
                }

                {
                    (: Link to next page :)
                    if ($end lt $count) then

                            >>

                    else ()
                }

};

(: Auxiliary function called in add-to-history()
   to avoid having twice the same query in history
:)
declare function f:string-list-union(
   $list as xs:string*,
   $s as xs:string ) as xs:string*
{
  let $contains :=
    for $ss in $list
      return
        if ( $ss = $s ) then true
        else ""

  return
    if ( $contains ) then
      $list
    else
      ( $list, $s )
};

(:  Add the last query to the query-history. The history is
    stored in the session as an XQuery sequence.
:)
declare function f:add-to-history($query as xs:string) as empty()
{
    let $history := request:get-session-attribute("history")
    return
        request:set-session-attribute( "history",
                                       f:string-list-union($history, $query) )
};

(:  The main function. If a query has been passed in parameter
    "query", execute the query and store the results into the
    session. If "query" is empty, try to retrieve the previous
    result set from the session.
:)
declare function f:main() as element()+
{
    let $query := request:request-parameter("query", ()),
        $previous := request:get-session-attribute("results"),
        $collection := request:request-parameter("collection", ())
    return
        if ($query) then
            let $startTime := current-time(),
                $result := util:eval($query, $collection),
                $count := count($result),
                $queryTime := current-time() - $startTime
            return (

Found {$count} hits in
                {get-seconds-from-dayTimeDuration($queryTime)} seconds.
,
                request:set-session-attribute("results", $result),
                f:add-to-history($query),
                f:display($result, $count)
            )
        else if ($previous) then
            f:display($previous, count($previous))
        else

Please specify a query! Back to query
            form.

};

<document xmlns:xi="http://www.w3.org/2001/XInclude">

        <logo src="logo.jpg"/>
        <title>Open Source XML Database</title>
	    <author email="wolfgang@exist-db.org">Wolfgang M. Meier</author>

xquery version "1.0";

declare namespace request="http://exist-db.org/xquery/request";
declare namespace util="http://exist-db.org/xquery/util";

(: Namespace for the local functions in this script :)
declare namespace f="http://exist-db.org/xquery/local-functions";

(:
    Display hits for the current page. The number of hits to print per page
    is read from parameter "howmany". Parameter "start" identifies the
    current offset in the result set.
:)
declare function f:display($hits as item()*, $count as xs:int)
as element()+
{
    if (empty($hits)) then

Nothing found! Back to query form.

    else
        let $howmany := request:request-parameter("howmany", "10") cast as xs:int,
            $start := request:request-parameter("start", "1") cast as xs:int,
            $end := if ($start + $howmany le $count) then $start + $howmany - 1 else $count
        return (

                        <New Query>

                { f:navbar($start, $end, $howmany, $count) }
                {
                    for $p in $start to $end
                    let $current := item-at($hits, $p),
                        $style := if($p mod 2 eq 0) then "high" else "low"
                    return

                            {$p}

                                <xml-source>{$current}</xml-source>

                }
                { f:navbar($start, $end, $howmany, $count) }

                        <New Query>

        )
};

(: Display the navigation bar :)
declare function f:navbar($start as xs:int, $end as xs:int,
$hitsPerPage as xs:int, $count as xs:int) as element()
{
    let $uri := request:request-uri()
    return

                {
                    (: Link to previous page :)
                    if ($start gt 1) then
                        <<
                    else ()
                }

                {
                    (: Create page shortcuts :)
                    let $sections := $count idiv $hitsPerPage - 1,
                        $currentSection := $start idiv $hitsPerPage,
                        $lastSection :=
                            if ($sections lt 0) then 0 else
                                (: show 10 max unless current page > 10 :)
                                if ($sections gt 9) then
                                    if ($currentSection gt 8) then $currentSection + 1
                                    else 9
                                else $sections
                    for $i in 0 to $lastSection
                    let $s := xs:int($i * $hitsPerPage + 1)
                    return
                        if($i ne $currentSection) then

                                [{$i + 1}]

                        else
                            (: current page doesn't get a link :)
                            [{$i + 1}]
                }

                {
                    (: Link to next page :)
                    if ($end lt $count) then

                            >>

                    else ()
                }

};

(:  Add the last query to the query-history. The history is
    stored in the session as an XQuery sequence.
:)
declare function f:add-to-history($query as xs:string) as empty()
{
    let $history := request:get-session-attribute("history")
    return
        request:set-session-attribute("history", ($history, $query))
};

(:  The main function. If a query has been passed in parameter
    "query", execute the query and store the results into the
    session. If "query" is empty, try to retrieve the previous
    result set from the session.
:)
declare function f:main() as element()+
{
    let $query := request:request-parameter("query", ()),
        $previous := request:get-session-attribute("results"),
        $collection := request:request-parameter("collection", ())
    return
        if ($query) then
            let $startTime := current-time(),
                $result := util:eval($query, $collection),
                $count := count($result),
                $queryTime := current-time() - $startTime
            return (

Found {$count} hits in
                {get-seconds-from-dayTimeDuration($queryTime)} seconds.
,
                request:set-session-attribute("results", $result),
                f:add-to-history($query),
                f:display($result, $count)
            )
        else if ($previous) then
            f:display($previous, count($previous))
        else

Please specify a query! Back to query
            form.

};

<document xmlns:xi="http://www.w3.org/2001/XInclude">

        <logo src="logo.jpg"/>
        <title>Open Source XML Database</title>
	    <author email="wolfgang@exist-db.org">Wolfgang M. Meier</author>


s/doc/node/

s/doc/node/

xquery version "3.0";

(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : 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.
 :)

(:~
 : This module provides an XQuery API to full-text functions.
 :
Notes on the thesaurus

 : The ft:thesaurus-lookup function has "levels"
 : and "relationship" parameters.
 : The values for these are implementation-defined.
 : Zorba's default implementation uses the
 : WordNet lexical database,
 : version 3.0.
 :

 : In WordNet,
 : the number of "levels" that two phrases are apart
 : are how many hierarchical meanings apart they are.
 : For example,
 : "canary" is 5 levels away from "vertebrate"
 : (carary > finch > oscine > passerine > bird > vertebrate).
 :

 : When using the WordNet implementation,
 : Zorba supports all of the relationships (and their abbreviations)
 : specified by
 : ISO 2788
 : and
 : ANSI/NISO Z39.19-2005
 : with the exceptions of "HN" (history note)
 : and "X SN" (see scope note for).
 : Additionally, Zorba also supports all WordNet relationships.
 :

 :

 :     Rel.
 :     Meaning
 :     WordNet Rel.
 :

 :

 :     BT
 :     broader term
 :     hypernym
 :

 :

 :     BTG
 :     broader term generic
 :     hypernym
 :

 :

 :     BTI
 :     broader term instance
 :     instance hypernym
 :

 :

 :     BTP
 :     broader term partitive
 :     part meronym
 :

 :

 :     NT
 :     narrower term
 :     hyponym
 :

 :

 :     NTG
 :     narrower term generic
 :     hyponym
 :

 :

 :     NTI
 :     narrower term instance
 :     instance hyponym
 :

 :

 :     NTP
 :     narrower term partitive
 :     part holonym
 :

 :

 :     RT
 :     related term
 :     also see
 :

 :

 :     SN
 :     scope note
 :     n/a
 :

 :

 :     TT
 :     top term
 :     hypernym
 :

 :

 :     UF
 :     non-preferred term
 :     n/a
 :

 :

 :     USE
 :     preferred term
 :     n/a
 :

 :

 : Relationships are case-insensitive.
 :
Notes on tokens

 : The ft:tokenize()
 : and ft:tokenize-string() functions
 : return a sequence of <token> elements
 : containing attributes for each token:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      lang
 :      xs:language
 :      The language of the token.
 :

 :

 :      sentence
 :      xs:nonNegativeInteger
 :      The sentence number.
 :

 :

 :      paragraph
 :      xs:nonNegativeInteger
 :      The paragraph number.
 :

 :

 :      value
 :      xs:string
 :      The token string value.
 :

 :

 :      node-ref
 :      xs:string
 :      A reference to the originating node.
 :

 :

 :
Notes on tokenizer properties

 : The ft:tokenizer-properties() functions
 : return a single <tokenizer-properties> element
 : containing attributes that describe the tokenizer's properties:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      elements-separate-tokens
 :      xs:boolean
 :
 :        If true,
 :        XML elements separate tokens,
 :        for example
 :        <b>B</b>old
 :        would be 2 tokens instead of 1.
 :
 :

 :

 :)
module namespace ft = "http://www.zorba-xquery.com/modules/full-text";

import schema namespace ft-schema =
  "http://www.zorba-xquery.com/modules/full-text";

declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://www.zorba-xquery.com/errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Gets the current
 : language:
 : either the langauge specified by the
 : declare ft-option using language statement (if any)
 : or the one returned by ft:host-lang() (if none).
 :
 : @return said language.
 :)
declare function ft:current-lang()
  as xs:language external;

(:~
 : Gets the host's current
 : language.
 : The "host" is the computer on which Zorba is running.
 : The host's current language is obtained as follows:
 :

 :

 :      For *nix systems:
 :

 :

 :          If setlocale(3) returns non-null,
 :          the language corresponding to that locale is used.
 :

 :

 :          Else, if the LANG environment variable is set,
 :          that language is ued.
 :

 :

 :          Otherwise, there is no default language.
 :

 :

 :

 :

 :      For Windows systems, the language corresponding to the locale returned
 :      by the GetLocaleInfoA() function is used.
 :

 :

 : @return said language.
 :)
declare function ft:host-lang()
  as xs:language external;

(:~
 : Checks whether the given
 : language
 : is supported for stemming.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stem-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : @param $lang The
 : language
 : of $word.
 : @return true only if $word is a stop-word.
 : @error zerr:ZXQP8405 if $lang is not supported for stop-words.
 :)
declare function ft:is-stop-word( $word as xs:string, $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : The word's language
 : is assumed to be the one returned by ft:current-lang().
 : @return true only if $word is a stop-word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : stop-words specifically.
 :)
declare function ft:is-stop-word( $word as xs:string )
  as xs:boolean
{
  ft:is-stop-word( $word, ft:current-lang() )
};

(:~
 : Checks whether the given
 : language
 : is supported for stop words.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stop-word-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given
 : language
 : is supported for look-up using the default thesaurus.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-thesaurus-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given
 : language
 : is supported for look-up using the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $lang The language to check.
 : @return true only if the language is supported.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 :)
declare function ft:is-thesaurus-lang-supported( $uri as xs:string,
                                                 $lang as xs:language )
  as xs:boolean external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : @param $lang The
 : language
 : of $word.
 : @return the stem of $word.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZXQP8404 if $lang is not supported for stemming
 : specifically.
 :)
declare function ft:stem( $word as xs:string, $lang as xs:language )
  as xs:string external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : The word's language
 : is assumed to be the one returned by ft:current-lang().
 : @return the stem of $word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8404 if ft:current-lang() is not supported for
 : stemming specifically.
 :)
declare function ft:stem( $word as xs:string )
  as xs:string
{
  ft:stem( $word, ft:current-lang() )
};

(:~
 : Strips all diacritical marks from all characters.
 :
 : @param $string The string to strip diacritical marks from.
 : @return $string with diacritical marks stripped.
 :)
declare function ft:strip-diacritics( $string as xs:string )
  as xs:string external;

(:~
 : Looks-up the given phrase in the default thesaurus.
 :
 : @param $phrase The phrase to look up.
 : The phrase's
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $phrase as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @return the original and related phrases.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : The phrase's
 : language
 : is assumed to be the one the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is unsupported in
 : general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string )
  as xs:string+
{
  ft:thesaurus-lookup( $uri, $phrase, ft:current-lang() )
};

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @return the original and related phrases.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @param $level-least The minimum number of levels within the thesaurus to be
 : travers$ed.
 : @param $level-most The maximum number of levels within the thesaurus to be
 : traversed.
 : @return the original and related phrases.
 : @error err:FOCA0003 if either $level-least or
 : $level-most is either negative or too large.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string,
                                      $level-least as xs:integer,
                                      $level-most as xs:integer )
  as xs:string+ external;

(:~
 : Tokenizes the given document.
 :
 : @param $node The node to tokenize.
 : @param $lang The default
 : language
 : of $node.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize( $node as node(), $lang as xs:language )
  as node()* external;

(:~
 : Tokenizes the given document.
 :
 : @param $node The node to tokenize.
 : The document's default
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize( $node as node() )
  as node()*
{
  ft:tokenize( $node, ft:current-lang() )
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : @param $lang The default
 : language
 : of $string.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize-string( $string as xs:string,
                                     $lang as xs:language )
  as xs:string* external;

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : The string's default
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize-string( $string as xs:string )
  as xs:string*
{
  ft:tokenize-string( $string, ft:current-lang() )
};

(:~
 : Gets properties of the tokenizer for the given
 : language.
 :
 : @param $lang The langauage of the tokenizer to get the properties of.
 : @return said properties.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenizer-properties( $lang as xs:language )
  as node() external;

(:~
 : Gets properties of the tokenizer for the
 : language
 : returned by ft:current-lang().
 :
 : @return said properties.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenizer-properties()
  as node()
{
  ft:tokenizer-properties( ft:current-lang() )
};

(: vim:set et sw=2 ts=2: :)

xquery version "3.0";

(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : 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.
 :)

(:~
 : This module provides an XQuery API to full-text functions.
 :
Notes on the thesaurus

 : The ft:thesaurus-lookup function has "levels"
 : and "relationship" parameters.
 : The values for these are implementation-defined.
 : Zorba's default implementation uses the
 : WordNet lexical database,
 : version 3.0.
 :

 : In WordNet,
 : the number of "levels" that two phrases are apart
 : are how many hierarchical meanings apart they are.
 : For example,
 : "canary" is 5 levels away from "vertebrate"
 : (carary > finch > oscine > passerine > bird > vertebrate).
 :

 : When using the WordNet implementation,
 : Zorba supports all of the relationships (and their abbreviations)
 : specified by
 : ISO 2788
 : and
 : ANSI/NISO Z39.19-2005
 : with the exceptions of "HN" (history note)
 : and "X SN" (see scope note for).
 : Additionally, Zorba also supports all WordNet relationships.
 :

 :

 :     Rel.
 :     Meaning
 :     WordNet Rel.
 :

 :

 :     BT
 :     broader term
 :     hypernym
 :

 :

 :     BTG
 :     broader term generic
 :     hypernym
 :

 :

 :     BTI
 :     broader term instance
 :     instance hypernym
 :

 :

 :     BTP
 :     broader term partitive
 :     part meronym
 :

 :

 :     NT
 :     narrower term
 :     hyponym
 :

 :

 :     NTG
 :     narrower term generic
 :     hyponym
 :

 :

 :     NTI
 :     narrower term instance
 :     instance hyponym
 :

 :

 :     NTP
 :     narrower term partitive
 :     part holonym
 :

 :

 :     RT
 :     related term
 :     also see
 :

 :

 :     SN
 :     scope note
 :     n/a
 :

 :

 :     TT
 :     top term
 :     hypernym
 :

 :

 :     UF
 :     non-preferred term
 :     n/a
 :

 :

 :     USE
 :     preferred term
 :     n/a
 :

 :

 : Relationships are case-insensitive.
 :
Notes on tokens

 : The ft:tokenize()
 : and ft:tokenize-string() functions
 : return a sequence of <token> elements
 : containing attributes for each token:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      lang
 :      xs:language
 :      The language of the token.
 :

 :

 :      sentence
 :      xs:nonNegativeInteger
 :      The sentence number.
 :

 :

 :      paragraph
 :      xs:nonNegativeInteger
 :      The paragraph number.
 :

 :

 :      value
 :      xs:string
 :      The token string value.
 :

 :

 :      node-ref
 :      xs:string
 :      A reference to the originating node.
 :

 :

 :
Notes on tokenizer properties

 : The ft:tokenizer-properties() functions
 : return a single <tokenizer-properties> element
 : containing attributes that describe the tokenizer's properties:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      elements-separate-tokens
 :      xs:boolean
 :
 :        If true,
 :        XML elements separate tokens,
 :        for example
 :        <b>B</b>old
 :        would be 2 tokens instead of 1.
 :
 :

 :

 :)
module namespace ft = "http://www.zorba-xquery.com/modules/full-text";

import schema namespace ft-schema =
  "http://www.zorba-xquery.com/modules/full-text";

declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://www.zorba-xquery.com/errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Gets the current
 : language:
 : either the langauge specified by the
 : declare ft-option using language statement (if any)
 : or the one returned by ft:host-lang() (if none).
 :
 : @return said language.
 :)
declare function ft:current-lang()
  as xs:language external;

(:~
 : Gets the host's current
 : language.
 : The "host" is the computer on which Zorba is running.
 : The host's current language is obtained as follows:
 :

 :

 :      For *nix systems:
 :

 :

 :          If setlocale(3) returns non-null,
 :          the language corresponding to that locale is used.
 :

 :

 :          Else, if the LANG environment variable is set,
 :          that language is ued.
 :

 :

 :          Otherwise, there is no default language.
 :

 :

 :

 :

 :      For Windows systems, the language corresponding to the locale returned
 :      by the GetLocaleInfoA() function is used.
 :

 :

 : @return said language.
 :)
declare function ft:host-lang()
  as xs:language external;

(:~
 : Checks whether the given
 : language
 : is supported for stemming.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stem-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : @param $lang The
 : language
 : of $word.
 : @return true only if $word is a stop-word.
 : @error zerr:ZXQP8405 if $lang is not supported for stop-words.
 :)
declare function ft:is-stop-word( $word as xs:string, $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : The word's language
 : is assumed to be the one returned by ft:current-lang().
 : @return true only if $word is a stop-word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : stop-words specifically.
 :)
declare function ft:is-stop-word( $word as xs:string )
  as xs:boolean
{
  ft:is-stop-word( $word, ft:current-lang() )
};

(:~
 : Checks whether the given
 : language
 : is supported for stop words.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stop-word-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given
 : language
 : is supported for look-up using the default thesaurus.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-thesaurus-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given
 : language
 : is supported for look-up using the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $lang The language to check.
 : @return true only if the language is supported.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 :)
declare function ft:is-thesaurus-lang-supported( $uri as xs:string,
                                                 $lang as xs:language )
  as xs:boolean external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : @param $lang The
 : language
 : of $word.
 : @return the stem of $word.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZXQP8404 if $lang is not supported for stemming
 : specifically.
 :)
declare function ft:stem( $word as xs:string, $lang as xs:language )
  as xs:string external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : The word's language
 : is assumed to be the one returned by ft:current-lang().
 : @return the stem of $word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8404 if ft:current-lang() is not supported for
 : stemming specifically.
 :)
declare function ft:stem( $word as xs:string )
  as xs:string
{
  ft:stem( $word, ft:current-lang() )
};

(:~
 : Strips all diacritical marks from all characters.
 :
 : @param $string The string to strip diacritical marks from.
 : @return $string with diacritical marks stripped.
 :)
declare function ft:strip-diacritics( $string as xs:string )
  as xs:string external;

(:~
 : Looks-up the given phrase in the default thesaurus.
 :
 : @param $phrase The phrase to look up.
 : The phrase's
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $phrase as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @return the original and related phrases.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : The phrase's
 : language
 : is assumed to be the one the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is unsupported in
 : general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string )
  as xs:string+
{
  ft:thesaurus-lookup( $uri, $phrase, ft:current-lang() )
};

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @return the original and related phrases.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @param $level-least The minimum number of levels within the thesaurus to be
 : travers$ed.
 : @param $level-most The maximum number of levels within the thesaurus to be
 : traversed.
 : @return the original and related phrases.
 : @error err:FOCA0003 if either $level-least or
 : $level-most is either negative or too large.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string,
                                      $level-least as xs:integer,
                                      $level-most as xs:integer )
  as xs:string+ external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The document to tokenize.
 : @param $lang The default
 : language
 : of $doc.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize( $doc as node(), $lang as xs:language )
  as node()* external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The XML document to tokenize.
 : The document's default
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize( $doc as node() )
  as node()*
{
  ft:tokenize( $doc, ft:current-lang() )
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : @param $lang The default
 : language
 : of $string.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize-string( $string as xs:string,
                                     $lang as xs:language )
  as xs:string* external;

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : The string's default
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize-string( $string as xs:string )
  as xs:string*
{
  ft:tokenize-string( $string, ft:current-lang() )
};

(:~
 : Gets properties of the tokenizer for the given
 : language.
 :
 : @param $lang The langauage of the tokenizer to get the properties of.
 : @return said properties.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenizer-properties( $lang as xs:language )
  as node() external;

(:~
 : Gets properties of the tokenizer for the
 : language
 : returned by ft:current-lang().
 :
 : @return said properties.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenizer-properties()
  as node()
{
  ft:tokenizer-properties( ft:current-lang() )
};

(: vim:set et sw=2 ts=2: :)


Added "Notes on tokens".

Added "Notes on tokens".

xquery version "3.0";

(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : 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.
 :)

(:~
 : This module provides an XQuery API to full-text functions.
 :
Notes on the thesaurus

 : The ft:thesaurus-lookup function has "levels"
 : and "relationship" parameters.
 : The values for these are implementation-defined.
 : Zorba's default implementation uses the
 : WordNet lexical database,
 : version 3.0.
 :

 : In WordNet,
 : the number of "levels" that two phrases are apart
 : are how many hierarchical meanings apart they are.
 : For example,
 : "canary" is 5 levels away from "vertebrate"
 : (carary > finch > oscine > passerine > bird > vertebrate).
 :

 : When using the WordNet implementation,
 : Zorba supports all of the relationships (and their abbreviations)
 : specified by ISO 2788 and ANSI/NISO Z39.19-2005
 : with the exceptions of "HN" (history note)
 : and "X SN" (see scope note for).
 : Additionally, Zorba also supports all WordNet relationships.
 :

 :

 :     Rel.
 :     Meaning
 :     WordNet Rel.
 :

 :

 :     BT
 :     broader term
 :     hypernym
 :

 :

 :     BTG
 :     broader term generic
 :     hypernym
 :

 :

 :     BTI
 :     broader term instance
 :     instance hypernym
 :

 :

 :     BTP
 :     broader term partitive
 :     part meronym
 :

 :

 :     NT
 :     narrower term
 :     hyponym
 :

 :

 :     NTG
 :     narrower term generic
 :     hyponym
 :

 :

 :     NTI
 :     narrower term instance
 :     instance hyponym
 :

 :

 :     NTP
 :     narrower term partitive
 :     part holonym
 :

 :

 :     RT
 :     related term
 :     also see
 :

 :

 :     SN
 :     scope note
 :     n/a
 :

 :

 :     TT
 :     top term
 :     hypernym
 :

 :

 :     UF
 :     non-preferred term
 :     n/a
 :

 :

 :     USE
 :     preferred term
 :     n/a
 :

 :

 : Relationships are case-insensitive.
 :
Notes on tokens

 : The ft:tokenize()
 : and ft:tokenize-string() functions
 : return a sequence of <token> elements
 : containing attributes for each token:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      lang
 :      xs:language
 :      The language of the token.
 :

 :

 :      sentence
 :      xs:nonNegativeInteger
 :      The sentence number.
 :

 :

 :      paragraph
 :      xs:nonNegativeInteger
 :      The paragraph number.
 :

 :

 :      value
 :      xs:string
 :      The token string value.
 :

 :

 :      node-ref
 :      xs:string
 :      A reference to the originating node.
 :

 :

 :
Notes on tokenizer properties

 : The ft:tokenizer-properties() functions
 : return a single <tokenizer-properties> element
 : containing attributes that describe the tokenizer's properties:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      elements-separate-tokens
 :      xs:boolean
 :
 :        If true,
 :        XML elements separate tokens,
 :        for example
 :        <b>B</b>old
 :        would be 2 tokens instead of 1.
 :
 :

 :

 :)
module namespace ft = "http://www.zorba-xquery.com/modules/full-text";

import schema namespace ft-schema =
  "http://www.zorba-xquery.com/modules/full-text";

declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://www.zorba-xquery.com/errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Gets the current language: either the langauge specified by the
 : declare ft-option using language statement (if any)
 : or the one returned by ft:host-lang() (if none).
 :
 : @return said language.
 :)
declare function ft:current-lang()
  as xs:language external;

(:~
 : Gets the host's current language.  The "host" is the computer on which Zorba
 : is running.  The host's current language is obtained as follows:
 :

 :

 :      For *nix systems:
 :

 :

 :          If setlocale(3) returns non-null,
 :          the language corresponding to that locale is used.
 :

 :

 :          Else, if the LANG environment variable is set,
 :          that language is ued.
 :

 :

 :          Otherwise, there is no default language.
 :

 :

 :

 :

 :      For Windows systems, the language corresponding to the locale returned
 :      by the GetLocaleInfoA() function is used.
 :

 :

 : @return said language.
 :)
declare function ft:host-lang()
  as xs:language external;

(:~
 : Checks whether the given language is supported for stemming.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stem-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : @param $lang The language of $word.
 : @return true only if $word is a stop-word.
 : @error zerr:ZXQP8405 if $lang is not supported for stop-words.
 :)
declare function ft:is-stop-word( $word as xs:string, $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.  The word's language is assumed to be the
 : one returned by ft:current-lang().
 : @return true only if $word is a stop-word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : stop-words specifically.
 :)
declare function ft:is-stop-word( $word as xs:string )
  as xs:boolean
{
  ft:is-stop-word( $word, ft:current-lang() )
};

(:~
 : Checks whether the given language is supported for stop words.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stop-word-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given language is supported for look-up using the default
 : thesaurus.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-thesaurus-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given language is supported for look-up using the
 : thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $lang The language to check.
 : @return true only if the language is supported.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 :)
declare function ft:is-thesaurus-lang-supported( $uri as xs:string,
                                                 $lang as xs:language )
  as xs:boolean external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : @param $lang The language of $word.
 : @return the stem of $word.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZXQP8404 if $lang is not supported for stemming
 : specifically.
 :)
declare function ft:stem( $word as xs:string, $lang as xs:language )
  as xs:string external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.  The word's language is assumed to be the
 : one returned by ft:current-lang().
 : @return the stem of $word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8404 if ft:current-lang() is not supported for
 : stemming specifically.
 :)
declare function ft:stem( $word as xs:string )
  as xs:string
{
  ft:stem( $word, ft:current-lang() )
};

(:~
 : Strips all diacritical marks from all characters.
 :
 : @param $string The string to strip diacritical marks from.
 : @return $string with diacritical marks stripped.
 :)
declare function ft:strip-diacritics( $string as xs:string )
  as xs:string external;

(:~
 : Looks-up the given phrase in the default thesaurus.
 :
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $phrase as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @return the original and related phrases.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is unsupported in
 : general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string )
  as xs:string+
{
  ft:thesaurus-lookup( $uri, $phrase, ft:current-lang() )
};

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @return the original and related phrases.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @param $level-least The minimum number of levels within the thesaurus to be
 : travers$ed.
 : @param $level-most The maximum number of levels within the thesaurus to be
 : traversed.
 : @return the original and related phrases.
 : @error err:FOCA0003 if either $level-least or
 : $level-most is either negative or too large.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string,
                                      $level-least as xs:integer,
                                      $level-most as xs:integer )
  as xs:string+ external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The document to tokenize.
 : @param $lang The default language of $doc.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize( $doc as node(), $lang as xs:language )
  as node()* external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The XML document to tokenize.  The document's default language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize( $doc as node() )
  as node()*
{
  ft:tokenize( $doc, ft:current-lang() )
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : @param $lang The default language of $string.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize-string( $string as xs:string,
                                     $lang as xs:language )
  as xs:string*
{
  () (: TODO :)
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.  The string's default language is
 : assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize-string( $string as xs:string )
  as xs:string*
{
  ft:tokenize-string( $string, ft:current-lang() )
};

(:~
 : Gets properties of the tokenizer for the given language.
 :
 : @param $lang The langauage of the tokenizer to get the properties of.
 : @return said properties.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenizer-properties( $lang as xs:language )
  as node() external;

(:~
 : Gets properties of the tokenizer for the language returned by
 : ft:current-lang().
 :
 : @return said properties.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenizer-properties()
  as node()
{
  ft:tokenizer-properties( ft:current-lang() )
};

(: vim:set et sw=2 ts=2: :)

xquery version "3.0";

(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : 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.
 :)

(:~
 : This module provides an XQuery API to full-text functions.
 :
Notes on the thesaurus

 : The ft:thesaurus-lookup function has "levels"
 : and "relationship" parameters.
 : The values for these are implementation-defined.
 : Zorba's default implementation uses the
 : WordNet lexical database,
 : version 3.0.
 :

 : In WordNet,
 : the number of "levels" that two phrases are apart
 : are how many hierarchical meanings apart they are.
 : For example,
 : "canary" is 5 levels away from "vertebrate"
 : (carary > finch > oscine > passerine > bird > vertebrate).
 :

 : When using the WordNet implementation,
 : Zorba supports all of the relationships (and their abbreviations)
 : specified by ISO 2788 and ANSI/NISO Z39.19-2005
 : with the exceptions of "HN" (history note)
 : and "X SN" (see scope note for).
 : Additionally, Zorba also supports all WordNet relationships.
 :

 :

 :     Rel.
 :     Meaning
 :     WordNet Rel.
 :

 :

 :     BT
 :     broader term
 :     hypernym
 :

 :

 :     BTG
 :     broader term generic
 :     hypernym
 :

 :

 :     BTI
 :     broader term instance
 :     instance hypernym
 :

 :

 :     BTP
 :     broader term partitive
 :     part meronym
 :

 :

 :     NT
 :     narrower term
 :     hyponym
 :

 :

 :     NTG
 :     narrower term generic
 :     hyponym
 :

 :

 :     NTI
 :     narrower term instance
 :     instance hyponym
 :

 :

 :     NTP
 :     narrower term partitive
 :     part holonym
 :

 :

 :     RT
 :     related term
 :     also see
 :

 :

 :     SN
 :     scope note
 :     n/a
 :

 :

 :     TT
 :     top term
 :     hypernym
 :

 :

 :     UF
 :     non-preferred term
 :     n/a
 :

 :

 :     USE
 :     preferred term
 :     n/a
 :

 :

 : Relationships are case-insensitive.
 :
Notes on tokenizer properties

 : The ft:tokenizer-properties() functions
 : return a single <tokenizer-properties> element
 : containing attributes that describe the tokenizer's properties:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      elements-separate-tokens
 :      xs:boolean
 :
 :        If true,
 :        XML elements separate tokens,
 :        for example
 :        <b>B</b>old
 :        would be 2 tokens instead of 1.
 :
 :

 :

 :)
module namespace ft = "http://www.zorba-xquery.com/modules/full-text";

import schema namespace ft-schema =
  "http://www.zorba-xquery.com/modules/full-text";

declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://www.zorba-xquery.com/errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Gets the current language: either the langauge specified by the
 : declare ft-option using language statement (if any)
 : or the one returned by ft:host-lang() (if none).
 :
 : @return said language.
 :)
declare function ft:current-lang()
  as xs:language external;

(:~
 : Gets the host's current language.  The "host" is the computer on which Zorba
 : is running.  The host's current language is obtained as follows:
 :

 :

 :      For *nix systems:
 :

 :

 :          If setlocale(3) returns non-null,
 :          the language corresponding to that locale is used.
 :

 :

 :          Else, if the LANG environment variable is set,
 :          that language is ued.
 :

 :

 :          Otherwise, there is no default language.
 :

 :

 :

 :

 :      For Windows systems, the language corresponding to the locale returned
 :      by the GetLocaleInfoA() function is used.
 :

 :

 : @return said language.
 :)
declare function ft:host-lang()
  as xs:language external;

(:~
 : Checks whether the given language is supported for stemming.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stem-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : @param $lang The language of $word.
 : @return true only if $word is a stop-word.
 : @error zerr:ZXQP8405 if $lang is not supported for stop-words.
 :)
declare function ft:is-stop-word( $word as xs:string, $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.  The word's language is assumed to be the
 : one returned by ft:current-lang().
 : @return true only if $word is a stop-word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : stop-words specifically.
 :)
declare function ft:is-stop-word( $word as xs:string )
  as xs:boolean
{
  ft:is-stop-word( $word, ft:current-lang() )
};

(:~
 : Checks whether the given language is supported for stop words.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stop-word-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given language is supported for look-up using the default
 : thesaurus.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-thesaurus-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given language is supported for look-up using the
 : thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $lang The language to check.
 : @return true only if the language is supported.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 :)
declare function ft:is-thesaurus-lang-supported( $uri as xs:string,
                                                 $lang as xs:language )
  as xs:boolean external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : @param $lang The language of $word.
 : @return the stem of $word.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZXQP8404 if $lang is not supported for stemming
 : specifically.
 :)
declare function ft:stem( $word as xs:string, $lang as xs:language )
  as xs:string external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.  The word's language is assumed to be the
 : one returned by ft:current-lang().
 : @return the stem of $word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8404 if ft:current-lang() is not supported for
 : stemming specifically.
 :)
declare function ft:stem( $word as xs:string )
  as xs:string
{
  ft:stem( $word, ft:current-lang() )
};

(:~
 : Strips all diacritical marks from all characters.
 :
 : @param $string The string to strip diacritical marks from.
 : @return $string with diacritical marks stripped.
 :)
declare function ft:strip-diacritics( $string as xs:string )
  as xs:string external;

(:~
 : Looks-up the given phrase in the default thesaurus.
 :
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $phrase as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @return the original and related phrases.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is unsupported in
 : general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string )
  as xs:string+
{
  ft:thesaurus-lookup( $uri, $phrase, ft:current-lang() )
};

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @return the original and related phrases.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @param $level-least The minimum number of levels within the thesaurus to be
 : travers$ed.
 : @param $level-most The maximum number of levels within the thesaurus to be
 : traversed.
 : @return the original and related phrases.
 : @error err:FOCA0003 if either $level-least or
 : $level-most is either negative or too large.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string,
                                      $level-least as xs:integer,
                                      $level-most as xs:integer )
  as xs:string+ external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The document to tokenize.
 : @param $lang The default language of $doc.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize( $doc as node(), $lang as xs:language )
  as node()* external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The XML document to tokenize.  The document's default language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize( $doc as node() )
  as node()*
{
  ft:tokenize( $doc, ft:current-lang() )
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : @param $lang The default language of $string.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize-string( $string as xs:string,
                                     $lang as xs:language )
  as xs:string*
{
  () (: TODO :)
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.  The string's default language is
 : assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize-string( $string as xs:string )
  as xs:string*
{
  ft:tokenize-string( $string, ft:current-lang() )
};

(:~
 : Gets properties of the tokenizer for the given language.
 :
 : @param $lang The langauage of the tokenizer to get the properties of.
 : @return said properties.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenizer-properties( $lang as xs:language )
  as node() external;

(:~
 : Gets properties of the tokenizer for the language returned by
 : ft:current-lang().
 :
 : @return said properties.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenizer-properties()
  as node()
{
  ft:tokenizer-properties( ft:current-lang() )
};

(: vim:set et sw=2 ts=2: :)


fixup read otherids

fixup read otherids

import module namespace csd_bl = "https://github.com/openhie/openinfoman/csd_bl";
declare default element  namespace   "urn:ihe:iti:csd:2013";
declare variable $careServicesRequest as item() external;

(:
   The query will be executed against the root element of the CSD document.

   The dynamic context of this query has $careServicesRequest set to contain any of the search
   and limit paramaters as sent by the Service Finder
:)

let $provs0 := if (exists($careServicesRequest/otherID/@position)) then /CSD/providerDirectory/*  else ()
let $provs1 := if (exists($careServicesRequest/id/@entityID)) then csd_bl:filter_by_primary_id($provs0,$careServicesRequest/id) else ()

let $provs2 :=
  if (count($provs1) = 1)
    then
    let $provider :=  $provs1[1]
    let $other_ids :=
      if ($careServicesRequest/code/text())
      then $provider/otherID[@code = $careServicesRequest/code/text()]
      else $provider/otherID

    return
    <provider entityID="{$provider/@entityID}">
      {(
	$other_ids
        ,
	$provider/record
	)}
    </provider>
  else ()

return csd_bl:wrap_providers($provs2)

import module namespace csd_bl = "https://github.com/openhie/openinfoman/csd_bl";
declare default element  namespace   "urn:ihe:iti:csd:2013";
declare variable $careServicesRequest as item() external;

(:
   The query will be executed against the root element of the CSD document.

   The dynamic context of this query has $careServicesRequest set to contain any of the search
   and limit paramaters as sent by the Service Finder
:)

let $provs0 := if (exists($careServicesRequest/otherID/@position)) then /CSD/providerDirectory/*  else ()
let $provs1 := if (exists($careServicesRequest/id/@entityID)) then csd_bl:filter_by_primary_id($provs0,$careServicesRequest/id) else ()
let $other_ids :=
  if ($careServicesRequest/code/text())
  then $provider/otherID[@code = $careServicesRequest/code/text()]
  else $provider/otherID

let $provs2 :=
  if (count($provs1) = 1)
    then
    let $provider :=  $provs1[1]
    return
    <provider entityID="{$provider/@entityID}">
      {(
	$other_ids
        ,
	$provider/record
	)}
    </provider>
  else ()

return csd_bl:wrap_providers($provs2)