Package: geograpy3
geograpy3geograpy3 is a fork ofgeograpy2, which is itself a fork ofgeograpyand 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.since geograpy3 0.0.2 cities,countries and regions are matched against a database derived from the corresponding wikidata entriesWhat it isgeograpy extracts place names from a URL or text, and adds context to those names -- for example distinguishing between a country, region or city.The extraction is a two step process. The first process is a Natural Language Processing task which analyzes a text for potential mentions of geographic locations. In the next step the words which represent such locations are looked up using the Locator.If you already know that your content has geographic information you might want to use the Locator interface directly.Examples/Tutorialsee Examples/Tutorial WikiInstall & SetupGrab the package usingpip(this will take a few minutes)pipinstallgeograpy3geograpy3 usesNLTKfor 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-nltkGetting the source codegitclonehttps://github.com/somnathrakshit/geograpy3cdgeograpy3
scripts/installBasic UsageImport the module, give some text or a URL, and presto.importgeograpyurl='https://en.wikipedia.org/wiki/2012_Summer_Olympics_torch_relay'places=geograpy.get_geoPlace_context(url=url)Now you have access to information about all the places mentioned in the linked
article.places.countriescontains a list of country namesplaces.regionscontains a list of region namesplaces.citiescontains a list of city namesplaces.otherlists everything that wasn't clearly a country, region or cityNote that theotherlist 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 MoreIn addition to listing the names of discovered places, you'll also get some
information about the relationships between places.places.country_regionsregions broken down by countryplaces.country_citiescities broken down by countryplaces.address_stringscity, region, country strings useful for geocodingLast But Not LeastWhile 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_mentionsplaces.region_mentionsplaces.city_mentionsEach 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 SeriousYou can of course use each of Geograpy's modules on their own. For example:fromgeograpyimportextractione=extraction.Extractor(url='https://en.wikipedia.org/wiki/2012_Summer_Olympics_torch_relay')e.find_geoEntities()# You can now access all of the places found by the Extractorprint(e.places)Place context is handled in theplacesmodule. For example:fromgeograpyimportplacespc=places.PlaceContext(['Cleveland','Ohio','United States'])pc.set_countries()printpc.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_regionsetc)
is available after the correspondingset_method is called.StackoverflowQuestions tagged with 'geograpy'Creditsgeograpy3 uses the following excellent libraries:NLTKfor entity recognitionnewspaperfor text extraction from HTMLjellyfishfor fuzzy text matchpylodstoragefor storage and retrieval of tabular data from SQL and SPARQL sourcesgeograpy3 uses the following data sources:ISO3166ErrorDictionaryfor common country mispellingsviaSara-Jayne TerpWikidatafor country/region/city information with disambiguation via populationHat tip toChris Albonfor the name.