Skip to main content

Geograpy

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.

geograpy-nltk

Basic Usage

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

import geograpy
url = 'http://www.bbc.com/news/world-europe-26919928'
places = geograpy.get_place_context(url=url)

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

  • 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='http://www.bbc.com/news/world-europe-26919928')
e.find_entities()

# 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'])

pc.set_countries()
print pc.countries #['United States']

pc.set_regions()
print pc.regions #['Ohio']

pc.set_cities()
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:

Geograpy uses the following data sources:

Hat tip to Chris Albon for the name.

Release files for geograpy 0.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geograpy 0.3.7
File Size Uploaded
geograpy-0.3.7.tar.gz 1.3 MB Details

Release files / geograpy-0.3.7.tar.gz

Download URL geograpy-0.3.7.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
be5bac6e73a1b00e354e261ecd60d09a4c6301de8ad3866414e7fe721ae67ac4
BLAKE2b-256 checksum
How to use checksums
92aa2cb1d246b61555537aad248cb871bd819823330dee3d92315e98499314a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.7 This release

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.5

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page