Last active
December 26, 2015 23:59
-
-
Save CliffordAnderson/7234215 to your computer and use it in GitHub Desktop.
Converts geographic entities from the Alchemy API to GeoJSON
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
(: Extracts entities from web-accessible texts using the Alchemy api :) | |
(: Serializes Disambiguated Entities to GeoJSON :) | |
xquery version "3.0"; | |
declare namespace csv = "http://basex.org/modules/json"; | |
let $api-key := [Your API Key] | |
let $url:= "https%3A%2F%2Fraw.github.com%2Fiulibdcs%2Ftei_text%2Fmaster%2Fvwwp_text%2FVAB7013.txt" | |
let $csv := | |
<json type="object"> | |
<type>FeatureCollection</type> | |
<features type="array"> | |
{ | |
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={$api-key}&url={$url}</http:body> | |
</http:request> | |
)/results/entities | |
for $entity in $entities/entity | |
where $entity//geo | |
return | |
<feature type="object"> | |
<type>Feature</type> | |
<properties type="object"> | |
{($entity/text)} | |
</properties> | |
<geometry type="object"> | |
<type>Point</type> | |
<coordinates type="array"> | |
<coordinates>{fn:substring-after($entity/disambiguated/geo, " ")}</coordinates> | |
<coordinates>{fn:substring-before($entity/disambiguated/geo, " ")}</coordinates> | |
</coordinates> | |
</geometry> | |
</feature> | |
}</features></json> | |
return json:serialize($csv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample JSON Output