Skip to main content

Extract place names and meta data from text or URLs

Project description


Extract place names from a URL or text, and add context to those names -- for
example distinguishing between a country, region or city.

## Install & Setup

Grab the package using `pip` (this will take a few minutes)

pip install geograpy

Geograpy uses [NLTK]( for entity recognition, so you'll also need
to download the models we're using. Fortunately there's a command that'll take
care of this for you.


## Basic Usage

Import the module, give some text or a URL, and presto.

import geograpy
url = ''
places = geograpy.get_place_context(url=url)

Now you have access to information about all the places mentioned in the linked

* `places.countries` _contains a list of country names_
* `places.regions` _contains a list of region names_
* `places.cities` _contains a list of city names_
* `places.other` _lists everything that wasn't clearly a country, region or city_

Note that the `other` list might be useful for shorter texts, to pull out
information like street names, points of interest, etc, but at the moment is
a bit messy when scanning longer texts that contain possessive forms of proper
nouns (like "Russian" instead of "Russia").

## But Wait, There's More

In addition to listing the names of discovered places, you'll also get some
information about the relationships between places.

* `places.country_regions` _regions broken down by country_
* `places.country_cities` _cities broken down by country_
* `places.address_strings` _city, region, country strings useful for geocoding_

## Last But Not Least

While a text might mention many places, it's probably focused on one or two, so
Geograpy also breaks down countries, regions and cities by number of mentions.

* `places.country_mentions`
* `places.region_mentions`
* `places.city_mentions`

Each of these returns a list of tuples. The first item in the tuple is the place
name and the second item is the number of mentions. For example:

[('Russian Federation', 14), (u'Ukraine', 11), (u'Lithuania', 1)]

## If You're Really Serious

You can of course use each of Geograpy's modules on their own. For example:

from geograpy import extraction

e = extraction.Extractor(url='')

# You can now access all of the places found by the Extractor
print e.places

Place context is handled in the `places` module. For example:

from geograpy import places

pc = places.PlaceContext(['Cleveland', 'Ohio', 'United States'])

print pc.countries #['United States']

print pc.regions #['Ohio']

print pc.cities #['Cleveland']

print pc.address_strings #['Cleveland, Ohio, United States']

And of course all of the other information shown above (`country_regions` etc)
is available after the corresponding `set_` method is called.

## Credits

Geograpy uses the following excellent libraries:

* [NLTK]( for entity recognition
* [newspaper]( for text extraction from HTML
* [jellyfish]( for fuzzy text match
* [pycountry]( for country/region lookups

Geograpy uses the following data sources:

* [GeoLite2]( for city lookups
* [ISO3166ErrorDictionary]( for common country mispellings _via [Sara-Jayne Terp](

Hat tip to [Chris Albon]( for the name.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for geograpy, version 0.2.2
Filename, size File type Python version Upload date Hashes
Filename, size geograpy-0.2.2.tar.gz (1.3 MB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page