Skip to main content

Extract countries, regions and cities from a URL or text

Project description

geograpy3

Join the chat at https://gitter.im/geograpy3/community Documentation Status pypi Github Actions Build PyPI Status Downloads GitHub issues GitHub closed issues License

geograpy3 is a fork of geograpy2, which is itself a fork of geograpy and inherits most of it, but solves several problems (such as support for utf8, places names with multiple words, confusion over homonyms etc). Also, geograpy3 is compatible with Python 3, unlike geograpy2.

What it is

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

Examples/Tutorial

Install & Setup

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

pip install geograpy3

geograpy3 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

Getting the source code

git clone https://github.com/somnathrakshit/geograpy3
cd geograpy3
scripts/install

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_geoPlace_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 geograpy3 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_geoEntities()

# 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.

Stackoverflow

Credits

geograpy3 uses the following excellent libraries:

  • NLTK for entity recognition
  • newspaper for text extraction from HTML
  • jellyfish for fuzzy text match
  • pylodstorage for storage and retrieval of tabular data from SQL and SPARQL sources

geograpy3 uses the following data sources:

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.

Source Distribution

geograpy3-0.1.18.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

geograpy3-0.1.18-py3-none-any.whl (1.5 MB view details)

Uploaded Python 3

File details

Details for the file geograpy3-0.1.18.tar.gz.

File metadata

  • Download URL: geograpy3-0.1.18.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for geograpy3-0.1.18.tar.gz
Algorithm Hash digest
SHA256 fa120568b9d218573a11dcd0d09d90cb2a2d1f2e9712ada00ddd6ca61e2ecfd1
MD5 2881f6a46ee5450fec742deb4338a3e7
BLAKE2b-256 41ff96232f2f4f064e7757fa638e05a36ca6c5b76f3579b8172a0d2c6d13bce1

See more details on using hashes here.

Provenance

File details

Details for the file geograpy3-0.1.18-py3-none-any.whl.

File metadata

  • Download URL: geograpy3-0.1.18-py3-none-any.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for geograpy3-0.1.18-py3-none-any.whl
Algorithm Hash digest
SHA256 c3694d92c571f5922ac965be83faa5a8de5db14655528dc2a9aadc1d53a1333d
MD5 a84ff9adbe0ba6ed60b273a1a0ef9df8
BLAKE2b-256 55d64a641d28a70a6d03e197df41b4fc72a00cee93b66f164ea75b191bc032d8

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page