Blog Post #2: Praxis Assignment – Mapping

I started off with a simple enough idea: to programmatically map locations in a text.

I went to a store in London and then went to Lake Erie to go fishing.

The idea was to extract “London” from the text and then place it on a map. This proved to be more complex than I anticipated. Both SpaCy and NLTK offer entity recognition through pre-built models that do a decent job of extracting the location (in this case, London) from the text but aren’t equipped to add context.

After running through several possibilities, I came across Mordecai, a project authored by MSU professor Andy Halterman. It uses a SpaCy model to extract locations, it then runs the text through another model that predicts the context based on the other words in the text. In the case of our example, London would be correctly classified as London, the city in Ontario, Canada because of the contextual clue “Lake Erie.”

{   'doc_text': 'I went to a store in London and then went to Lake Erie to '
                'go fishing.',
    'event_location_raw': '',
    'geolocated_ents': [   {   'adm1_count': 1.0,
                               'admin1_code': '08',
                               'admin1_name': 'Ontario',
                               'admin1_parent_match': 0,
                               'admin2_code': '',
                               'admin2_name': '',
                               'alt_name_length': 3.2188758248682006,
                               'ascii_dist': 0.0,
                               'avg_dist': 0.11450381679389314,
                               'city_id': '6058560',
                               'city_name': 'London',
                               'country_code3': 'CAN',
                               'country_code_parent_match': 0,
                               'country_count': 1.0,
                               'end_char': 29,
                               'feature_class': 'P',
                               'feature_code': 'PPL',
                               'geonameid': '6058560',
                               'lat': 42.98339,
                               'lon': -81.23304,
                               'max_dist': 0.2692307692307692,
                               'min_dist': 0.0,
                               'name': 'London',
                               'score': 0.9999490976333618,
                               'search_name': 'London',
                               'start_char': 23},

I also tried with the following variations (there’s a Thames River close to both Londons):

A. I went to a store in London and then went to Thames River to go fishing.

B. I went to a store in London and then went to River Thames to go fishing.

The first example, returned two results London, England and London, Ontario with London England scoring slightly higher. The second example only returned London, England.

Given more time, I would have liked to try mapping the Black Mountain corpus. For this assignment, I chose to work with just one poem: “I, Maximus of Gloucester, to You” by Charles Olson.

After extracting the location data with Mordecai, I saved it as a CSV file, and I used Folium (an open-source mapping library) to map the datapoints

import pandas as pd
import folium

# read in the poem
df = pd.read_csv('poem.csv')

# map data to folium
m = folium.Map(location=[-6.1753924, 106.8271528], zoom_start=2)

# add markers to map
for i in range(0, len(df)):
    folium.Marker(
        location=[df.iloc[i]['lat'], df.iloc[i]['lon']],
        popup=df.iloc[i]['text'],
        icon=folium.Icon(color='green')
    ).add_to(m)
folium map

Mordecai extracted:

  • Oregon, Ohio
  • New England, Jamaica
  • Gloucester, England

While context was added to each of the locations found in the poem, only one of them was correct. I attempted a similar process with four other poems with similar results.

While this tool is useful when the cues in the text are obvious (or more likely common, e.g. Paris and the Eiffel Tower) the Mordecai model would need to be tuned differently for this type of text.

Some interesting questions that were raised in the process of completing this assignment were:

  • Could it be possible to extract location based on dialectical differences?
  • Mordecai was funded by the CIA as part of the Political Instability Task Force. What are the ethical implications of using a tool like this for alternative purposes?
  • Creative processes like poetry seem to be at odds with the statistical methods used in Natural Language Processing or machine learning. What kind of patterns are worth looking for?
  • In poetry, geographic references serve a variety of purposes. A map, while appearing multi-dimensional, can flatten literary analysis to just coordinates. Could a map reveal something new?