Python library for finding country names in English text
Recognising country names in unstructured English text with Python
We have open-sourced a Python library called Country Named Entity Recognition for finding country names in a string. For example, “This trial will include study sites in Namibia, Zimbabwe and South Africa”. This NLP task is called named entity recognition (finding countries in text) and named entity linking (mapping countries to IDs).
Please note Country Named Entity Recognition finds only high confidence countries. A text such as “America” is ambiguous.
Country Named Entity Recognition also only finds the English names of these countries. Names in the local language are not supported.
You can install the Python library by typing in the command line:
pip install country-named-entity-recognition
The source code is on Github and the project is on Pypi.
Usage examples
In your Python console, you can try the following:
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)
Disambiguating Georgia – is it a state or a country?
You can also bring context into the tool. If we encounter the string “Georgia”, by default the library assumes that it refers to the US state and won’t tag it as a country:
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 you can provide a string which contains a clear contextual clue, and the tool will recognise Georgia as the country:
from country_named_entity_recognition import find_countries
find_countries("Salome Zourabichvili is the current president of Georgia.")