Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active December 26, 2015 22:49
Show Gist options
  • Save CliffordAnderson/7226156 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/7226156 to your computer and use it in GitHub Desktop.
Serializes disambiguated entities to CSV
(: Extracts entities from web-accessible texts using the Alchemy api :)
(: Serializes Disambiguated Entities to CSV :)
xquery version "3.0";
declare namespace csv = "http://basex.org/modules/csv";
let $apiKey := [Your Key Here]
let $url := "https://raw.github.com/iulibdcs/tei_text/master/vwwp_text/VAB7013.txt"
let $csv :=
<csv>{
let $entities :=
http:send-request(
<http:request method='post' href='http://access.alchemyapi.com/calls/url/URLGetRankedNamedEntities'>
<http:body media-type='application/x-www-form-urlencoded' method='text'>apikey={$apiKey}&amp;url={$url}</http:body>
</http:request>
)/results/entities
for $entity in $entities/entity
where $entity//geo
return
<record>
{$entity/text}
<lon>{fn:substring-before($entity/disambiguated/geo, " ")}</lon>
<lat>{fn:substring-after($entity/disambiguated/geo, " ")}</lat>
</record>
}
</csv>
return csv:serialize($csv)
@CliffordAnderson
Copy link
Author

Sample CSV output:

London,51.50805555555556,-0.12472222222222222
Indiana University,39.775555555555556,-86.17666666666666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment