
<a href="https://fastdatascience.com"><span align="left">🌐 fastdatascience.com</span></a>
<a href="https://www.linkedin.com/company/fastdatascience/"><img align="left" src="https://raw.githubusercontent.com//harmonydata/.github/main/profile/linkedin.svg" alt="Fast Data Science | LinkedIn" width="21px"/></a>
<a href="https://twitter.com/fastdatascienc1"><img align="left" src="https://raw.githubusercontent.com//harmonydata/.github/main/profile/x.svg" alt="Fast Data Science | X" width="21px"/></a>
<a href="https://www.instagram.com/fastdatascience/"><img align="left" src="https://raw.githubusercontent.com//harmonydata/.github/main/profile/instagram.svg" alt="Fast Data Science | Instagram" width="21px"/></a>
<a href="https://www.facebook.com/fastdatascienceltd"><img align="left" src="https://raw.githubusercontent.com//harmonydata/.github/main/profile/fb.svg" alt="Fast Data Science | Facebook" width="21px"/></a>
<a href="https://www.youtube.com/channel/UCLPrDH7SoRT55F6i50xMg5g"><img align="left" src="https://raw.githubusercontent.com//harmonydata/.github/main/profile/yt.svg" alt="Fast Data Science | YouTube" width="21px"/></a>
<a href="https://g.page/fast-data-science"><img align="left" src="https://raw.githubusercontent.com//harmonydata/.github/main/profile/google.svg" alt="Fast Data Science | Google" width="21px"/></a>
<a href="https://medium.com/fast-data-science"><img align="left" src="https://raw.githubusercontent.com//harmonydata/.github/main/profile/medium.svg" alt="Fast Data Science | Medium" width="21px"/></a>
<a href="https://mastodon.social/@fastdatascience"><img align="left" src="https://raw.githubusercontent.com//harmonydata/.github/main/profile/mastodon.svg" alt="Fast Data Science | Mastodon" width="21px"/></a>
# Country named entity recognition
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.
# 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")
```
# 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 [Computer software], Version 1.0.0, accessed at https://fastdatascience.com/natural-language-processing/country-named-entity-recognition/, Fast Data Science Ltd (2022)
A BibTeX entry for LaTeX users is
```
@unpublished{countrynamedentityrecognition,
AUTHOR = {Wood, T.A.},
TITLE = {Country Named Entity Recognition (Computer software), Version 1.0.0},
YEAR = {2023},
Note = {To appear},
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.
* Redding, Alisa. [Animals of the Digital Age: Assessing digital media for public interest and engagement in species threatened by wildlife trade.](https://helda.helsinki.fi/items/77960829-145a-4efb-b364-3dbe6ac6bfb4/full) University of Helsinki, Faculty of Science, 2023.
### 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:
* Funk, Christoph and Tönjes, Elena and Teuber, Ramona and Breuer, Lutz, Reading Between the Lines: The Intersection of Research Attention and Sustainable Development Goals (May 31, 2023). Available at SSRN: [https://ssrn.com/abstract=4465055](https://ssrn.com/abstract=4465055) or [http://dx.doi.org/10.2139/ssrn.4465055](http://dx.doi.org/10.2139/ssrn.4465055)
### 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.
* Bosso, Francesco, et al. [Use of Large Language Models for location detection on the example of the terrorism and extremism event database.](https://publications.jrc.ec.europa.eu/repository/bitstream/JRC134961/JRC134961_01.pdf), JRC Technical Report, European Commission (2023).
### Labelling radical content online
Ugochukwu Etudo and Victoria Y. Yoon at Virginia Commonwealth University used the tool in their analysis of radical content online:
* Ugochukwu Etudo, Victoria Y. Yoon (2023) [Ontology-Based Information Extraction for Labeling Radical Online Content Using Distant Supervision](https://pubsonline.informs.org/doi/abs/10.1287/isre.2023.1223). Information Systems Research 0(0). [https://doi.org/10.1287/isre.2023.1223](https://doi.org/10.1287/isre.2023.1223)
## Other named entity recognition tools
* [Medical and clinical named entity recognition Python library](https://fastdatascience.com/ai-in-pharma/medical-named-entity-recognition-python-library/)
Raw data
{
"_id": null,
"home_page": null,
"name": "country-named-entity-recognition",
"maintainer": "Thomas Wood",
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "country, ner, nlp, named entity recognition, natural language processing",
"author": "Thomas Wood",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/59/a3/921496bb44be140db2823f825e7e8e6ca4097a80ffed9bb731ad775608ba/country_named_entity_recognition-1.0.0.tar.gz",
"platform": null,
"description": "\n\n<a href=\"https://fastdatascience.com\"><span align=\"left\">\ud83c\udf10 fastdatascience.com</span></a>\n<a href=\"https://www.linkedin.com/company/fastdatascience/\"><img align=\"left\" src=\"https://raw.githubusercontent.com//harmonydata/.github/main/profile/linkedin.svg\" alt=\"Fast Data Science | LinkedIn\" width=\"21px\"/></a>\n<a href=\"https://twitter.com/fastdatascienc1\"><img align=\"left\" src=\"https://raw.githubusercontent.com//harmonydata/.github/main/profile/x.svg\" alt=\"Fast Data Science | X\" width=\"21px\"/></a>\n<a href=\"https://www.instagram.com/fastdatascience/\"><img align=\"left\" src=\"https://raw.githubusercontent.com//harmonydata/.github/main/profile/instagram.svg\" alt=\"Fast Data Science | Instagram\" width=\"21px\"/></a>\n<a href=\"https://www.facebook.com/fastdatascienceltd\"><img align=\"left\" src=\"https://raw.githubusercontent.com//harmonydata/.github/main/profile/fb.svg\" alt=\"Fast Data Science | Facebook\" width=\"21px\"/></a>\n<a href=\"https://www.youtube.com/channel/UCLPrDH7SoRT55F6i50xMg5g\"><img align=\"left\" src=\"https://raw.githubusercontent.com//harmonydata/.github/main/profile/yt.svg\" alt=\"Fast Data Science | YouTube\" width=\"21px\"/></a>\n<a href=\"https://g.page/fast-data-science\"><img align=\"left\" src=\"https://raw.githubusercontent.com//harmonydata/.github/main/profile/google.svg\" alt=\"Fast Data Science | Google\" width=\"21px\"/></a>\n<a href=\"https://medium.com/fast-data-science\"><img align=\"left\" src=\"https://raw.githubusercontent.com//harmonydata/.github/main/profile/medium.svg\" alt=\"Fast Data Science | Medium\" width=\"21px\"/></a>\n<a href=\"https://mastodon.social/@fastdatascience\"><img align=\"left\" src=\"https://raw.githubusercontent.com//harmonydata/.github/main/profile/mastodon.svg\" alt=\"Fast Data Science | Mastodon\" width=\"21px\"/></a>\n\n# Country named entity recognition\n\nDeveloped by Fast Data Science, https://fastdatascience.com\n\nSource code at https://github.com/fastdatascience/country_named_entity_recognition\n\nPyPI package: https://pypi.org/project/country-named-entity-recognition/\n\nPython library for finding country names in a string.\n\nPlease note this library finds only high confidence countries. A text such as \"America\" is ambiguous.\n\nIt also only finds the English names of these countries. Names in the local language are not supported.\n\n# Requirements\n\nPython 3.9 and above\n\npycountry 22.1.10 and above\n\n# Installation\n\n```\npip install country-named-entity-recognition\n```\n\n# Usage examples\n\n## Example 1\n\n```\nfrom country_named_entity_recognition import find_countries\nfind_countries(\"We are expanding in the UK\")\n```\n\noutputs a list of tuples.\n\n```\n[(Country(alpha_2='GB', alpha_3='GBR', flag='\ud83c\uddec\ud83c\udde7', name='United Kingdom', numeric='826', official_name='United Kingdom of Great Britain and Northern Ireland'),\n <re.Match object; span=(1, 15), match='united kingdom'>)]\n```\n\n## Example 2\n\nThe tool's default behaviour assumes countries are correctly capitalised and punctuated:\n\n```\nfrom country_named_entity_recognition import find_countries\nfind_countries(\"I want to visit france.\")\n```\n\nwill not return anything.\n\nHowever, if your text comes from social media or another non-moderated source, you might want to allow case-insensitive matching:\n\n```\nfrom country_named_entity_recognition import find_countries\nfind_countries(\"I want to visit france.\", is_ignore_case=True)\n```\n\n## Example 3\n\nThis illustrates how you can bring context into the tool. If we encounter the string \"Georgia\", by default it refers to the US state.\n\n```\nfrom country_named_entity_recognition import find_countries\nfind_countries(\"Gladys Knight and the Pips wrote the Midnight Train to Georgia\")\n```\n\nwill return an empty list.\n\nBut what happens if we include a clear contextual clue?\n\n```\nfrom country_named_entity_recognition import find_countries\nfind_countries(\"Salome Zourabichvili is the current president of Georgia.\")\n```\n\nreturns\n\n```\n[(Country(alpha_2='GE', alpha_3='GEO', flag='\ud83c\uddec\ud83c\uddea', name='Georgia', numeric='268'), <re.Match object; span=(34, 41), match='Georgia'>)]\n```\n\nYou can force the latter behaviour:\n\n```\nfrom country_named_entity_recognition import find_countries\nfind_countries(\"I want to visit Georgia.\", is_georgia_probably_the_country=True)\n```\n\n\n# Adding custom variants\n\nIf you find that a variant country name is missing, you can add it using the *add_custom_variants* method.\n\nLet's imagine we want to add Neverneverland as a synonym for the UAE:\n\n```\nfrom country_named_entity_recognition import find_countries, add_custom_variants\nadd_custom_variants([\"Neverneverland\"], \"AE\")\nfind_countries(\"I want to visit Neverneverland\")\n```\n\n# Raising issues\n\nIf 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.\n\n# Who to contact\n\nThomas Wood at https://fastdatascience.com\n\n# How to cite Country Named Entity Recognition?\n\nWe would be grateful for your taking the consideration to cite us. We would suggest something like the following (depending on your style):\n\nWood, T.A., Country Named Entity Recognition [Computer software], Version 1.0.0, accessed at https://fastdatascience.com/natural-language-processing/country-named-entity-recognition/, Fast Data Science Ltd (2022)\n\nA BibTeX entry for LaTeX users is\n\n```\n@unpublished{countrynamedentityrecognition,\n AUTHOR = {Wood, T.A.},\n TITLE = {Country Named Entity Recognition (Computer software), Version 1.0.0},\n YEAR = {2023},\n Note = {To appear},\n url = {https://fastdatascience.com/natural-language-processing/country-named-entity-recognition/}\n}\n\n```\n\n## Case studies of the Country Named Entity Recognition Library\n\nPeople and organisations around the world have been using the library and have cited us.\n\n### The sixth wave of mass species extinction...\n\nAlisa Redding at the University of Helsinki used the tool for her Masters thesis on mass species extinction and biodiversity.\n\n* Redding, Alisa. [Animals of the Digital Age: Assessing digital media for public interest and engagement in species threatened by wildlife trade.](https://helda.helsinki.fi/items/77960829-145a-4efb-b364-3dbe6ac6bfb4/full) University of Helsinki, Faculty of Science, 2023.\n\n### The UN's Sustainable Development Goals (SDGs)\n\nChristoph Funk and his colleagues at Justus-Liebig-Universit\u00e4t Gie\u00dfen (Justus Liebig University Giessen) in Germany used country-named-entity-recognition for their meta-analysis of articles related to Sustainable Development Goals in 2023:\n\n* Funk, Christoph and T\u00f6njes, Elena and Teuber, Ramona and Breuer, Lutz, Reading Between the Lines: The Intersection of Research Attention and Sustainable Development Goals (May 31, 2023). Available at SSRN: [https://ssrn.com/abstract=4465055](https://ssrn.com/abstract=4465055) or [http://dx.doi.org/10.2139/ssrn.4465055](http://dx.doi.org/10.2139/ssrn.4465055)\n\n### The European Commission: detecting terrorism and extremism\n\nFrancesco 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.\n\n* Bosso, Francesco, et al. [Use of Large Language Models for location detection on the example of the terrorism and extremism event database.](https://publications.jrc.ec.europa.eu/repository/bitstream/JRC134961/JRC134961_01.pdf), JRC Technical Report, European Commission (2023).\n\n\n### Labelling radical content online\n\nUgochukwu Etudo and Victoria Y. Yoon at Virginia Commonwealth University used the tool in their analysis of radical content online:\n\n* Ugochukwu Etudo, Victoria Y. Yoon (2023) [Ontology-Based Information Extraction for Labeling Radical Online Content Using Distant Supervision](https://pubsonline.informs.org/doi/abs/10.1287/isre.2023.1223). Information Systems Research 0(0). [https://doi.org/10.1287/isre.2023.1223](https://doi.org/10.1287/isre.2023.1223)\n\n\n## Other named entity recognition tools\n\n* [Medical and clinical named entity recognition Python library](https://fastdatascience.com/ai-in-pharma/medical-named-entity-recognition-python-library/)\n",
"bugtrack_url": null,
"license": null,
"summary": "Finds countries in a string",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://fastdatascience.com/country-named-entity-recognition/"
},
"split_keywords": [
"country",
" ner",
" nlp",
" named entity recognition",
" natural language processing"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b4f8adddf29e6070e7a2a8c7cf066c4a6cfc1fa2f48e98edfd93e384eb2897c5",
"md5": "598dfeabe97ec44674c98690b7590756",
"sha256": "80d57bd89873a2827a9c7ada61a54c2bb4b5ed0d562fc8f5483600b689f4b934"
},
"downloads": -1,
"filename": "country_named_entity_recognition-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "598dfeabe97ec44674c98690b7590756",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 10636,
"upload_time": "2025-09-05T10:52:52",
"upload_time_iso_8601": "2025-09-05T10:52:52.340609Z",
"url": "https://files.pythonhosted.org/packages/b4/f8/adddf29e6070e7a2a8c7cf066c4a6cfc1fa2f48e98edfd93e384eb2897c5/country_named_entity_recognition-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "59a3921496bb44be140db2823f825e7e8e6ca4097a80ffed9bb731ad775608ba",
"md5": "96adf8dec4239086276714b00dc9ccf2",
"sha256": "87dce36cc314f1b8c8b2bf603cb8015f7d740580d1a76b564d979a1bf19d3cb6"
},
"downloads": -1,
"filename": "country_named_entity_recognition-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "96adf8dec4239086276714b00dc9ccf2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 19187,
"upload_time": "2025-09-05T10:52:53",
"upload_time_iso_8601": "2025-09-05T10:52:53.399621Z",
"url": "https://files.pythonhosted.org/packages/59/a3/921496bb44be140db2823f825e7e8e6ca4097a80ffed9bb731ad775608ba/country_named_entity_recognition-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-09-05 10:52:53",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "country-named-entity-recognition"
}