Country named entity recognition Python library

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

(“We are expanding in the UK”)

outputs a list of tuples.

Example 2

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

(“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:

Tricky edge cases for named entity recognition

Gladys Knight. Image source: https://www.flickr.com/people/36277035@N06 . Licence: Attribution-ShareAlike 2.0 Generic (CC BY-SA 2.0)

Gladys Knight. Image source. Licence: Attribution-ShareAlike 2.0 Generic (CC BY-SA 2.0)

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:

(“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:

(“Salome Zourabichvili is the current president of Georgia.")

returns

You can provide some metadata via an optional parameter to force the tool to assume Georgia is the country:

“I want to visit Georgia.”,

Adding custom variants to Country Named Entity Recognition

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

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

(“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.

Was wir für Sie tun können

Verwandeln Sie unstrukturierte Daten in umsetzbare Erkenntnisse

Kontaktiere uns