Extracting locations from text using Python - GeeksforGeeks

30 Jun, 2021
In this article, we are going to see how to extract location from text using Python.
While working with texts, the requirement can be the detection of cities, regions, states, and countries and relationships between them in the received text. This can be very useful for geographical studies. In this article, we will use the locationtagger library.
Text mining that requires some grammar-based rules and statistical modelling approaches is usually carried using NER (Named Enitity Recognition) Algorithms. An entity extracted from NER can be the name of a person, place, organization, or product. The locationtagger library is a byproduct of further tagging and filtering places from all the other entities present.
To install this module type the below command in the terminal.
pip install locationtagger
After the installation, a few nltk modules are required to download using code.
Python3
import nltkimport spacy  # essential entity models downloadsnltk.downloader.download('maxent_ne_chunker')nltk.downloader.download('words')nltk.downloader.download('treebank')nltk.downloader.download('maxent_treebank_pos_tagger')nltk.downloader.download('punkt')nltk.download('averaged_perceptron_tagger')
Also from the command line:
python -m spacy download en_core_web_sm
Example 1: Printing countries, cities and regions from Text.
Various functions can be used to get cities, countries, regions etc from the text. 
Functions Used:
locationtagger.find_location(text) : Return the entity with location information. The “text” parameter takes text as input.
entity.countries : Extracts all the countries in text.
entity.regions : Extracts all the states in text.
entity.cities : Extracts all the cities in text.
Code: 
Python3
import locationtagger  # initializing sample textsample_text = "India has very rich and vivid culture\       widely spread from Kerala to Nagaland to Haryana to Maharashtra. " \       "Delhi being capital with Mumbai financial capital.\       Can be said better than some western cities such as " \       " Munich, London etc. Pakistan and Bangladesh share its borders"  # extracting entities.place_entity = locationtagger.find_locations(text = sample_text)  # getting all countriesprint("The countries in text : ")print(place_entity.countries)  # getting all statesprint("The states in text : ")print(place_entity.regions)  # getting all citiesprint("The cities in text : ")print(place_entity.cities)
Output : 
Example 2: Extracting Relations of locations
In this example, various functions are discussed which perform the task of getting relations of cities, regions, and states with each other. 
Functions Used:
entity.country_regions : Extracts the country where regions are found in text.
entity.country_cities : Extracts the country where cities are found in text.
entity.other_countries : Extracts all countries list whose regions or cities are present in text.
entity.region_cities : Extracts the regions with whose cities are found in text.
entity.other_regions : Extracts all regions list whose cities are present in text.
entity.other : All entities not recognized as place names, are extracted to this.
Python3
import locationtagger  # initializing sample textsample_text = "India has very rich and vivid culture widely\        spread from Kerala to Nagaland to Haryana to Maharashtra. " \       "Mumbai being financial capital can be said better\       than some western cities such as " \       " Lahore, Canberra etc. Pakistan and Nepal share its borders"  # extracting entities.place_entity = locationtagger.find_locations(text = sample_text)  # getting all country regionsprint("The countries regions in text : ")print(place_entity.country_regions)  # getting all country citiesprint("The countries cities in text : ")print(place_entity.country_cities)  # getting all other countriesprint("All other countries in text : ")print(place_entity.other_countries)  # getting all region citiesprint("The region cities in text : ")print(place_entity.region_cities)  # getting all other regionsprint("All other regions in text : ")print(place_entity.other_regions)  # getting all other entitiesprint("All other entities in text : ")print(place_entity.other)
Output: 
Python Framework
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()
Defaultdict in Python