Exercise: Customizing spaCy Models

Let's test the concepts we've learned through a programming exercise.

We'll cover the following

Problem:

You are tasked with building a named entity recognition (NER) model that can recognize fruit names in text. Using the spaCy library, write a Python script that trains a new NER component to recognize fruit entities and applies it to a sample input text.

Your script should perform the following steps:

  1. Define a list of training examples with at least five sentences with named entities of the FRUIT type.

  2. Initialize a new spaCy model and add a new NER component with the FRUIT label.

  3. Train the model on the training examples for a fixed number of epochs (for example, 25).

  4. Apply the trained model to a sample input text that includes at least two fruit entities and print the labels of any recognized entities.

Your script should handle any errors or exceptions that may occur during model training or application and provide informative error messages if necessary.

Get hands-on with 1200+ tech skills courses.