from mediameter.cliff import Cliff
cliff = Cliff('http://localhost',8999)

"""
Explanation: Easy Automated Geocoding of Text with CLIFF-up
By Andy Halterman. 
Modified by Rahul Bhargava
MIT's CLIFF is a piece of software for extracting geolocation data from text, bundled into a server that can be accessed via API calls. I've bundled CLIFF into a Vagrant virtual machine for people (like me) who aren't thrilled about learning how to set up Tomcat servers and get Java configurations right. See the CLIFF-up repo on Github for the code to get CLIFF running easily inside a Vagrant virtual machine. Follow the instructions there to get CLIFF-up and then come back here for a walkthough of how to use CLIFF once it's running.
CLIFF is built on a number of free and open source projects, including Berico Technologies' CLAVIN geoparsing software, Stanford's CoreNLP natural language software, and Geonames.org's free gazetteer of place names and coordinates.
You can use the mediameter-cliff Python module to make queries to your server.  Install it with pip intstall mediameter-cliff and then fire up this python notebook.
End of explanation
"""


sentence = "In Sweden, the episode brought back memories of another incident in 1981, when Sweden discovered that a Soviet submarine had run aground off Swedish shores at Karlskrona in the south of the country."
print sentence

"""
Explanation: Give it a sentence you're interested in geolocating. (From the New York Times):
End of explanation
"""


data = cliff.parseText(sentence)
data

"""
Explanation: You can simple call the parseText method to get some results.
End of explanation
"""


data['results']['places']['focus']

"""
Explanation: You'll see that it returns some data about the query (time elapsed, status, version), a list of organizations mentioned, a list of people, and the places in the story. One of CLIFF's big selling points is that it distinguishes between "focus" places–the location the text is really about, and "mention" places that appear peripherally in the text. You'll also notice that it's fast: 52 milliseconds, which is the return on the long Lucene index building process when you did vagrant up.
We can cut the results down to just the "focus" places, which is presumably what we're interested in.
End of explanation
"""


for i in data['results']['places']['focus']['cities']:
    print i['name']

"""
Explanation: Or we can pare it down even further and just look at the city names:
End of explanation
"""


paragraph = ["The United States continued its assault on Islamic State militants this week, conducting 14 airstrikes in recent days in Syria and Iraq, U.S. Central Command said, three of them near the predominantly Kurdish border town of Kobani.", "Turkish President Tayyip Erdogan has criticized the U.S.-led coalition's focus on Kobani, which has been besieged by Islamic State for more than a month, and warned its attention needed to be turned to other parts of the conflict.", "The Syrian civil war has killed close to 200,000 people and forced more than 3 million refugees to flee the country, according to the United Nations.", "At least 11 children were killed in Damascus when mortars fell on a school in an eastern district of the Syrian capital, the Britain-based Syrian Observatory for Human Rights, which monitors the war, said on Wednesday.","The school was in a rebel-held part of Qaboun, a district in the east of the city which is contested between government and rebel forces, the monitoring group said.","The death toll was expected to rise because a number of those wounded were in critical condition, it said.","Fighters linked to al Qaeda also took ground from moderate Syrian rebels last week in the northern province of Idlib, expanding their control.","A member of the Syrian rebel forces based in southeastern Turkey said on Wednesday the Nusra Front had made further gains in recent days."]

for i in paragraph:
    print i
    print "\n"

for i, sentence in enumerate(paragraph):
    p = cliff.parseText(sentence)
    num = i
    if 'cities' in p['results']['places']['focus']:
        for i in p['results']['places']['focus']['cities']:
            place = i['name']
            lat = i['lat']
            lon = i['lon']
            print "Cities in sentence " + str(num) + ": " + place + " (" + str(lat) + ", " + str(lon) + ")"


"""
Explanation: Multi-sentence example: Syria
The point of automated geocoding is obviously to do it at scale, perhaps as part of an event data project.
Let's take a look at an example a little closer to what we'll be doing with event data, where we'd like to use it to extract the places where events are happening. Our event extraction software, PETRARCH handles the event extraction, but we will rely on a separate program to figure out the places associated with events in each sentence.
Here, I'm giving it a list of sentences from a recent Reuters story about Syria. Normally, I would split the paragraph into sentences automatically using CoreNLP's sentence splitter function, but I've done that step by hand here to keep this example light weight.
End of explanation
"""


