Created
February 10, 2017 15:28
-
-
Save CliffordAnderson/28d58649309ac7921988271c41386a5e 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