Skip to content

Instantly share code, notes, and snippets.

@natematias
Created May 22, 2014 19:44
Show Gist options
  • Save natematias/88ad38ad58e9935dc218 to your computer and use it in GitHub Desktop.
Save natematias/88ad38ad58e9935dc218 to your computer and use it in GitHub Desktop.
Accessing a CLIFF server from Python
import nltk
import urllib
import json
import urllib2
from urllib import urlopen
# download From Egypt to Japan by Henry Martyn
url = "http://www.gutenberg.org/cache/epub/39474/pg39474.txt"
raw_text = urlopen(url).read()
host ="http://HOSTNAME/CLIFF/parse/text"
vars = {"q":raw_text}
data = urllib.urlencode(vars)
req = urllib2.Request(host,data)
response = urllib2.urlopen(req)
response_data = response.read()
response_json = json.loads(response_data)
print json.dumps(response_json, sort_keys=True,
indent=4, separators=(',', ': '))
@natematias
Copy link
Author

Here's a truncated version of the resulting keys, which probably doesn't parse because I didn't pay that much attention to how I truncated it:
{
"milliseconds": 10139,
"results": {
"organizations": [
{
"count": 3,
"name": "Suez Canal"
},
{
"count": 1,
"name": "National Assembly"
},
{
"count": 1,
"name": "United States Army"
}
],
"people": [
{
"count": 4,
"name": "Henry M. Field"
},
{
"count": 3,
"name": "Melissa McDaniel"
}
],
"organizations": [
[
{
"confidence": 1.0,
"countryCode": "MM",
"featureClass": "P",
"featureCode": "PPLA",
"id": 1298824,
"lat": 16.80528,
"lon": 96.15611,
"name": "Yangon",
"population": 4477638,
"source": {
"charIndex": 602616,
"string": "Rangoon"
},
"stateCode": "17"
},
{
"confidence": 1.0,
"countryCode": "CA",
"featureClass": "P",
"featureCode": "PPL",
"id": 5884485,
"lat": 45.78682,
"lon": -64.27068,
"name": "Amherst Point",
"population": 0,
"source": {
"charIndex": 603089,
"string": "Amherst Point"
},
"stateCode": "07"
},
{
"confidence": 1.0,
"countryCode": "IN",
"featureClass": "A",
"featureCode": "ADM1",
"id": 1252881,
"lat": 24.0,
"lon": 88.0,
"name": "State of West Bengal",
"population": 84600002,
"source": {
"charIndex": 604039,
"string": "Bengal"
},
"stateCode": "28"
}
]
}
},
"status": "ok",
"version": "1.0.0"
}

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