-
-
Save AdamSteffanick/eb958e19aede9e6cd5705aa1d408c219 to your computer and use it in GitHub Desktop.
A little playful XQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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