NLP Tutorials — Part 24: Named Entity Recognition

Welcome back to another article in the NLP Tutorials series! In this article we won’t be looking at high-end architectures but instead explore a key concept in the NLP domain — Named Entity Recognition (NER). We might have heard of this term as one of the important concepts that has a lot of applications in real-world scenarios. Let’s understand what it means and how we can create a NER model using a few popular NLP libraries.

NER

NER is popularly known as entity recognition/extraction in text. It identifies a single token (word) and a small group of words and tags within a pre-defined category. There are categories like Organizations, Quantities, Names, Geographic locations, Currency, Events, Product names, Person names, Facilities, Date and Time etc. For example, in the sentence According to the New York Times, mayor Bill approved a yearlong cap on Uber vehicles in New York”, the entities and their categories are — New York Times (ORG), Bill (PER), Uber (ORG) and New York (LOC).

Using NER the intuition of understanding is better due to the extraction of entities and bucketing them into well-defined categories, which can be even better supported with more metadata. And more importantly, language models are sometimes pretrained on the NER tasks, which makes it quite an important concept to understand. Given below are a few applications of NER:

  1. Content Classification
  2. Efficient Search Space for Algorithms
  3. Recommender Engines
  4. Customer Support
  5. Robust and Enhanced Repository of Research Papers

Now that we have a basic intuition of things, let’s attempt to build a basic NER model ourselves.

Training a NER model

We will be utilizing the spaCy library for creating an NLP pipeline for NER training. spaCy has certain requirements regarding the format of the training data, so let’s get that done. Once the data is ready in the specified format, it’s a breeze to train and evaluate the NER model.

train_data = [
    ("The pizza in Naples it the best", {'entities' : [(4, 8, 'FOOD'), (13, 18, 'LOC')]}),
    ("India is famous for Taj Mahal and Infosys", {'entities' : [(0, 4, 'LOC'), (20, 28, 'LOC'), (34, 40, 'ORG')]}),
    ("Germany is one of the best countries in Europe", {'entities' : [(0, 6, 'LOC'), (40, 45, 'LOC')]})
]

Now, it’s time to define the spaCy pipeline.

nlp = spacy.blank('en')
ner = nlp.create_pipe('ner')

nlp.add_pipe(ner, last = True)

Let’s enter the annotations/entities we defined earlier to the nlp pipe as labels for the model to associate while training.

for _, annot in train_data:
    for e in annot.get('entities'):
        ner.add_label(ent[2])

other_p = [pipe for pipe in nlp.pipe_names if pipe!= 'ner']

Training time now!

from spacy.training.example import Example

loss = {}

for batch in spacy.util.minibatch(train_data):
    for data, annot in batch:
        d = nlp.make_doc(data)
        ex = Example.from_dict(d, annot)
        nlp.update([example], losses = loss, drop = 0.5)
        print(loss)

Once the training is complete, we shall now have a look at the entities in the NER pipeline.

for t,_ in train_data:

    d = nlp(t)
    print('Entities', [(ent.text, ent.label) for ent in doc.ents])

This format can also be used as a test string for getting real-time predictions from the trained model.

Given here is the complete code in a single python script.

Conclusion

It was quite easy to train an NER model thanks to the spaCy library. I hope you were able to understand the importance of NER and how it can be implemented. Try it yourself with a fairly big dataset and you will be amazed at the results.

Author

Pranav Raikote

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s