Skip to main content

Finds countries in a string

Project description

Fast Data Science logo

🌐 fastdatascience.com Fast Data Science | LinkedIn Fast Data Science | X Fast Data Science | Instagram Fast Data Science | Facebook Fast Data Science | YouTube Fast Data Science | Google Fast Data Science | Medium Fast Data Science | Mastodon

Country named entity recognition

my badge PyPI package version number License pypi Version version number PyPi downloads forks

Developed by Fast Data Science, https://fastdatascience.com

Source code at https://github.com/fastdatascience/country_named_entity_recognition

PyPI package: https://pypi.org/project/country-named-entity-recognition/

Python library for finding country names in a string.

Please note this library finds only high confidence countries. A text such as "America" is ambiguous.

It also only finds the English names of these countries. Names in the local language are not supported.

New! In 2025 we have added support for spaCy

Requirements

Python 3.9 and above

pycountry 22.1.10 and above

Installation

pip install country-named-entity-recognition

Usage examples

Example 1

from country_named_entity_recognition import find_countries
find_countries("We are expanding in the UK")

outputs a list of tuples.

[(Country(alpha_2='GB', alpha_3='GBR', flag='🇬🇧', name='United Kingdom', numeric='826', official_name='United Kingdom of Great Britain and Northern Ireland'),
  <re.Match object; span=(1, 15), match='united kingdom'>)]

Example 2

The tool's default behaviour assumes countries are correctly capitalised and punctuated:

from country_named_entity_recognition import find_countries
find_countries("I want to visit france.")

will not return anything.

However, if your text comes from social media or another non-moderated source, you might want to allow case-insensitive matching:

from country_named_entity_recognition import find_countries
find_countries("I want to visit france.", is_ignore_case=True)

Example 3

This illustrates how you can bring context into the tool. If we encounter the string "Georgia", by default it refers to the US state.

from country_named_entity_recognition import find_countries
find_countries("Gladys Knight and the Pips wrote the Midnight Train to Georgia")

will return an empty list.

But what happens if we include a clear contextual clue?

from country_named_entity_recognition import find_countries
find_countries("Salome Zourabichvili is the current president of Georgia.")

returns

[(Country(alpha_2='GE', alpha_3='GEO', flag='🇬🇪', name='Georgia', numeric='268'), <re.Match object; span=(34, 41), match='Georgia'>)]

You can force the latter behaviour:

from country_named_entity_recognition import find_countries
find_countries("I want to visit Georgia.", is_georgia_probably_the_country=True)

Adding custom variants

If you find that a variant country name is missing, you can add it using the add_custom_variants method.

Let's imagine we want to add Neverneverland as a synonym for the UAE:

from country_named_entity_recognition import find_countries, add_custom_variants
add_custom_variants(["Neverneverland"], "AE")
find_countries("I want to visit Neverneverland")

Using the library with spaCy

If you are already using spaCy in your project, you'll be pleased to see that you can pass a spaCy Doc object into the tool:

import spacy
from country_named_entity_recognition.country_finder_spacy import find_countries_in_spacy_doc
nlp = spacy.blank("en")
doc = nlp("I went to the USA")
country_matches = find_countries_in_spacy_doc(nlp, doc)
print (country_matches)

Raising issues

If you find a problem, you are welcome either to raise an issue at https://github.com/fastdatascience/country_named_entity_recognition/issues or to make a pull request and I will merge it into the project.

Who to contact

Thomas Wood at https://fastdatascience.com

How to cite Country Named Entity Recognition?

We would be grateful for your taking the consideration to cite us. We would suggest something like the following (depending on your style):

Wood, T.A. Country Named Entity Recognition. Zenodo, 5 Sept. 2025, https://doi.org/10.5281/zenodo.17062716.

A BibTeX entry for LaTeX users is

@unpublished{countrynamedentityrecognition,
    AUTHOR = {Wood, T.A.},
    TITLE  = {Country Named Entity Recognition (Computer software), Version 1.0.2},
    YEAR   = {2025},
    doi    = {10.5281/zenodo.17062716},
    url = {https://fastdatascience.com/natural-language-processing/country-named-entity-recognition/}
}

Case studies of the Country Named Entity Recognition Library

People and organisations around the world have been using the library and have cited us.

The sixth wave of mass species extinction...

Alisa Redding at the University of Helsinki used the tool for her Masters thesis on mass species extinction and biodiversity.

The UN's Sustainable Development Goals (SDGs)

Christoph Funk and his colleagues at Justus-Liebig-Universität Gießen (Justus Liebig University Giessen) in Germany used country-named-entity-recognition for their meta-analysis of articles related to Sustainable Development Goals in 2023:

The European Commission: detecting terrorism and extremism

Francesco Bosso and his team at the European Commission wrote a report investigating NLP for location detection with a focus on the JRC Terrorism and Extremism Database.

Labelling radical content online

Ugochukwu Etudo and Victoria Y. Yoon at Virginia Commonwealth University used the tool in their analysis of radical content online:

Analysing text to assess indicators in Sustainable Development Goals

Elena Tönjes used the library to assess sustainability indicators in her PhD thesis:

Other named entity recognition tools

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

country_named_entity_recognition-1.0.2.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file country_named_entity_recognition-1.0.2.tar.gz.

File metadata

File hashes

Hashes for country_named_entity_recognition-1.0.2.tar.gz
Algorithm Hash digest
SHA256 136f8be97ae7519058e4bb06b78d1c405e0d106131c6ded696cf9ff6050be061
MD5 8dfa75ff8c4975026f6bb6c8f491717f
BLAKE2b-256 df4e934faa310196683b64a94ed17160aad1ca69bfe19e64a9e14d033c128dd1

See more details on using hashes here.

File details

Details for the file country_named_entity_recognition-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for country_named_entity_recognition-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cc545e5aea1371509e3b35d2e4e90e47826a88fefd70da01b9d5cf4f1a705058
MD5 99c818312799a302944c2308cfb690d3
BLAKE2b-256 2690281a16e1787d450350ed07125d7e896c9df4e89c6cac3454cb412afdb44e

See more details on using hashes here.

Supported by

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