Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Created February 10, 2017 15:28
Show Gist options
  • Save CliffordAnderson/28d58649309ac7921988271c41386a5e to your computer and use it in GitHub Desktop.
Save CliffordAnderson/28d58649309ac7921988271c41386a5e to your computer and use it in GitHub Desktop.
A little playful XQuery
xquery version "3.1";
declare function local:lookup-word($word as xs:string, $request as xs:string, $id as xs:string, $key as xs:string) {
let $request :=
<http:request href="https://od-api.oxforddictionaries.com/api/v1/entries/en/{$word}/{$request}" method="get">
<http:header name="app_key" value="{$key}"/>
<http:header name="app_id" value="{$id}"/>
</http:request>
return http:send-request($request)
};
let $phrase := "I feel excited!"
for $word in fn:tokenize($phrase, "\W")
let $request := "antonyms"
let $id := "###"
let $key := "###"
let $lookup-word := local:lookup-word(?, ?, $id, $key)
let $entry := $lookup-word($word, $request)
let $senses := $entry//senses/_/antonyms/_/text/text()
let $count := fn:count($senses)
return
if ($count > 0) then
let $rnd := random:integer(fn:count($senses)) + 1
return $senses[$rnd]
else
$word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment