[Bug 12036] [FT] 3.4.3 Thesaurus Option

http://www.w3.org/Bugs/Public/show_bug.cgi?id=12036

--- Comment #2 from Tim Mills <tim@cbcl.co.uk> 2011-02-24 16:09:38 UTC ---
The following XQuery functions offer a description as to how thesaurus option
levels could work.  They assume the format of thesaurus used in XQFTTS.

Negative levels are normalized to 0.
Ranges from $n to $m where $n > $m is normalized to $n to $n.

declare namespace t = "http://www.w3.org/2007/xqftts/thesaurus";

(: Helper funciton.  Expand each word in words exactly one level :)
declare function local:expand-one-level($words as xs:string*, 
                                        $thesaurus as node(),
                                        $relationship as xs:string)
                 as xs:string*
{
  distinct-values(
    $thesaurus/t:thesaurus/t:entry[t:term = $words]/t:synonym[t:relationship eq
$relationship]/t:term)
};

(: Helper funciton.  Expand each word in words exactly $n levels :)
declare function local:expand-n-levels($words as xs:string*, 
                                       $thesaurus as node(),
                                       $relationship as xs:string,
                                       $n as xs:integer) 
                 as xs:string*
{
  if ($n eq 0)
  then $words
  else let $expansions := local:expand-one-level($words, 
                                                 $thesaurus,
                                                 $relationship)
       return local:expand-n-levels($expansions, 
                                    $thesaurus, 
                                    $relationship, 
                                    $n - 1)
};


(: Helper funciton.  Constrain level values to be non-negative integers :)
declare function local:constrain-level($level as xs:integer)
                 as xs:integer
{
  if ($level lt 0) 
  then 0 
  else $level
};

(: Helper funciton.  Repeatedly expand $words until a fixed point is reached :)
declare function local:expand-to-infinity($words as xs:string*, 
                                          $thesaurus as node(),
                                          $relationship as xs:string)
                 as xs:string*
{
  let $next-level := for $word in $words
                     return local:expand-exactly($word,
                                                 $thesaurus,
                                                 $relationship,
                                                 1)
  let $expansion := distinct-values(($next-level, $words))
  return if (count($words) eq count($expansion))
         then $words
         else local:expand-to-infinity($expansion,
                                       $thesaurus,
                                       $relationship)

};

(: Expand $word exactly $n levels :)
declare function local:expand-exactly($word as xs:string, 
                                      $thesaurus as node(),
                                      $relationship as xs:string,
                                      $n as xs:integer) 
                 as xs:string*
{
  let $level := local:constrain-level($n)
  return local:expand-n-levels($word,
                               $thesaurus,
                               $relationship,
                               $level)

};

(: Expand $word from $from to $to levels :)
declare function local:expand-from-to($word as xs:string, 
                                      $thesaurus as node(),
                                      $relationship as xs:string,
                                      $from as xs:integer,
                                      $to as xs:integer) 
                 as xs:string*
{
  let $lower := local:constrain-level($from)
  let $upper := max(($lower, local:constrain-level($to)))
  return distinct-values(for $level in $lower to $upper
                         return local:expand-exactly($word,
                                                     $thesaurus,
                                                     $relationship,
                                                     $level))
};

(: Expand $word at most $n levels :)
declare function local:expand-at-most($word as xs:string, 
                                      $thesaurus as node(),
                                      $relationship as xs:string,
                                      $n as xs:integer) 
                 as xs:string*
{
  local:expand-from-to($word,
                       $thesaurus,
                       $relationship,
                       0,
                       $n)
};

(: Expand $word at least $n levels :)
declare function local:expand-at-least($word as xs:string, 
                                       $thesaurus as node(),
                                       $relationship as xs:string,
                                       $n as xs:integer) 
                 as xs:string*
{
  let $initial := local:expand-exactly($word,
                                       $thesaurus,
                                       $relationship,
                                       $n)
  return local:expand-to-infinity($initial,
                                  $thesaurus,
                                  $relationship)
};

(: Expand $word when no range expression specified :)
declare function local:expand-default($word as xs:string, 
                                      $thesaurus as node(),
                                      $relationship as xs:string)
                 as xs:string*
{
  local:expand-at-least($word,
                        $thesaurus,
                        $relationship,
                        0)
};

-- 
Configure bugmail: http://www.w3.org/Bugs/Public/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

Received on Thursday, 24 February 2011 16:09:40 UTC