Natural Language Processing · Intermediate level

Intermediate level · Module 8: Information Retrieval and Information Extraction · Lesson 2 of 3

Named Entity Recognition and Rule-Based Extraction

Learners extract people, organisations, places, dates and amounts with spaCy's NER, add local place names with an EntityRuler, and use regex for structured fields.

In this lesson you will

  • Extract entities with spaCy NER
  • Add custom rules for local entities and evaluate extraction

Key concepts

named entity recognitionentity labels (PERSON, ORG, GPE, DATE, MONEY)EntityRulergazetteerprecision and recall for extraction

The lesson

Named entity recognition (NER) finds spans that name things: people, organisations, places (GPE), dates and money. With en_core_web_sm, doc.ents gives entities and ent.label_ their type.

Statistical models trained mostly on non-African text often miss or mislabel local names such as payams, counties and organisations. An EntityRuler adds patterns from a gazetteer (a list of known names): ruler = nlp.add_pipe('entity_ruler', before='ner'); ruler.add_patterns([{'label': 'GPE', 'pattern': 'Yambio'}]). Regular expressions handle fields like amounts ('25,000 SSP') and reference numbers.

Evaluate extraction against hand-marked entities in 30 documents: precision (were the found entities correct?) and recall (were all entities found?), reported by entity type.

Step by step

  1. Run spaCy NER on 30 project documents and inspect the entities.
  2. Build a gazetteer of the state capitals and county names used in the documents, checked against an official public list.
  3. Add an EntityRuler and a regex for amounts in SSP.
  4. Compare precision and recall with and without the rules.

Practice activity

Build and evaluate an entity extractor for the project documents with local rules.

Expected result: An extractor with measured precision and recall by type, improved by local rules.

Check your understanding

  1. What is a gazetteer?

    Show answer

    A list of known names, such as places, used to find entities.

  2. Why add an EntityRuler before the statistical NER?

    Show answer

    So that trusted local patterns are applied first and are not overridden.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.