Skip to content

Instantly share code, notes, and snippets.

@vai2fc
Created February 10, 2017 22:07
Show Gist options
  • Save vai2fc/734110643820e1a109974139b8ca123d to your computer and use it in GitHub Desktop.
Save vai2fc/734110643820e1a109974139b8ca123d to your computer and use it in GitHub Desktop.
xquery version "3.1";
declare function local:lookup-sentences($word 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}/sentences" method="get">
<http:header name="app_key" value="{$key}"/>
<http:header name="app_id" value="{$id}"/>
</http:request>
return http:send-request($request)
};
declare function local:lookup-synonyms($word 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}/synonyms" method="get">
<http:header name="app_key" value="{$key}"/>
<http:header name="app_id" value="{$id}"/>
</http:request>
return http:send-request($request)
};
let $word := "" (:your starting word:)
let $id := "###" (:your ID number, from the OED API:)
let $key := "###"(:your key, from the OED API:)
let $sentences := local:lookup-sentences($word, $id, $key)
let $phrase-1 := ($sentences//text/text())[1]
let $words :=
for $word in fn:tokenize($phrase-1, '\W+')
let $syn-words := local:lookup-synonyms($word, $id, $key)
let $synonym := ($syn-words//synonyms/_/text/text())
let $rnd := random:integer(fn:count($synonym)+1)
return $synonym[$rnd]
return fn:string-join($words, " ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment