AI Named Entity Extraction Project

Artificial Intelligence project 08

AI Named Entity Extraction Project

Build a complete named entity extraction workflow with reproducible Python code, transparent inputs, reviewable output, and responsible-use limits.

Explore AI training in VizagView all project ideas

AI objective

Extract and locate sensitive or structured entities with transparent patterns, then redact email and phone values.

Data or knowledge source

Plain text containing demonstration email, Indian phone, date, and INR amount formats.

Requirements

  • Python 3.10 or later
  • A terminal or command prompt
  • No third-party packages required
  • About 45-60 minutes to build and review

How the system works

Apply labelled regular expressions, return exact character offsets, sort entities in source order, and redact from the end to preserve earlier offsets.

Validation checklist

  • Entity offsets reproduce the matched text
  • Overwriting runs from right to left
  • Email and phone values are removed from redacted output
  • Supported entity formats are documented

Complete Python code

Save the program as ai_named_entity_extractor.py. The code runs locally and requires no paid API key or model download.

"""A transparent rule-based named-entity extraction baseline."""

from __future__ import annotations

import re
from dataclasses import dataclass


@dataclass(frozen=True)
class Entity:
    text: str
    label: str
    start: int
    end: int


PATTERNS = {
    "EMAIL": r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b",
    "PHONE": r"(?<!\d)(?:\+91[- ]?)?[6-9]\d{9}(?!\d)",
    "DATE": r"\b(?:\d{1,2}[/-]\d{1,2}[/-]\d{4}|\d{4}-\d{2}-\d{2})\b",
    "MONEY": r"(?i)(?:₹|INR\s?)\d[\d,]*(?:\.\d{1,2})?",
}


def extract_entities(text: str) -> list[Entity]:
    entities = []
    for label, pattern in PATTERNS.items():
        entities.extend(Entity(match.group(), label, match.start(), match.end()) for match in re.finditer(pattern, text))
    return sorted(entities, key=lambda entity: (entity.start, entity.end))


def redact(text: str, entities: list[Entity]) -> str:
    result = text
    for entity in sorted(entities, key=lambda item: item.start, reverse=True):
        if entity.label in {"EMAIL", "PHONE"}:
            result = result[:entity.start] + f"[{entity.label}]" + result[entity.end:]
    return result


def main() -> None:
    text = "Contact learner@example.com or +91 9876543210 by 2026-09-15. The fee is INR 12,500."
    entities = extract_entities(text)
    print(entities)
    print(redact(text, entities))


if __name__ == "__main__":
    main()

Run the project

  1. Create and activate a virtual environment.
  2. Install dependencies with No third-party packages required when packages are required.
  3. Run python ai_named_entity_extractor.py.
  4. Review confidence, fallbacks, sources, or error metrics rather than accepting output automatically.
  5. Test additional normal, edge, unsupported, and adversarial inputs.

Expected output

A list of labelled entities with offsets and a redacted text string.

Accuracy, privacy, and responsible-use limits

Rules are an NLP baseline, not a complete privacy system. Formats vary and false negatives can expose data; use expert-reviewed detectors and secure handling for real personal information.

Ways to extend the project

Add organisations and locations, international formats, overlap resolution, test corpora, confidence, and layered machine-learning detection.

Continue learning Artificial Intelligence

Try the next project, return to the Softenant project library, or explore the AI training in Vizag for guided NLP, retrieval, evaluation, automation, and responsible AI practice.