Medical Information Extraction
Python Programming
Healthcare Data Analysis
Natural Language Processing
Medical Informatics

Medical information extraction using Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Medical information extraction turns unstructured clinical text into structured data such as diagnoses, medications, dosages, dates, and symptoms. In Python, the work usually starts with a simple pipeline: clean the text, detect target patterns, normalize values, and emit a structured record for downstream analysis. The hard part is not writing one regex, but choosing an extraction approach that is accurate enough for noisy real-world notes.

Start With a Clear Extraction Goal

Medical text can contain many entity types, but each project should start with a narrow target. For example:

  • medication name and dosage
  • diagnosis mentions
  • admission and discharge dates
  • vital signs

If you try to extract everything at once, the code quickly becomes hard to validate. A focused extractor is easier to test and much easier to improve later.

Consider this small note:

text
Patient reports chest pain. Started aspirin 81 mg daily on 2026-02-10.
BP 140/90. Follow-up in 2 weeks.

A useful first goal might be extracting medications and blood pressure values.

A Lightweight Rule-Based Extractor in Python

For many internal workflows, a rule-based pipeline is a reasonable starting point. It is transparent, easy to debug, and fully runnable with the Python standard library.

python
1import re
2
3note = (
4    "Patient reports chest pain. "
5    "Started aspirin 81 mg daily on 2026-02-10. "
6    "BP 140/90. Follow-up in 2 weeks."
7)
8
9med_pattern = re.compile(
10    r"(?P<drug>[A-Za-z]+)\\s+(?P<dose>\\d+)\\s*(?P<unit>mg|mcg|g)\\s+(?P<freq>daily|bid|tid)",
11    re.IGNORECASE,
12)
13
14bp_pattern = re.compile(r"BP\\s+(?P<systolic>\\d{2,3})/(?P<diastolic>\\d{2,3})")
15
16med_match = med_pattern.search(note)
17bp_match = bp_pattern.search(note)
18
19result = {
20    "medication": med_match.groupdict() if med_match else None,
21    "blood_pressure": bp_match.groupdict() if bp_match else None,
22}
23
24print(result)

This code is intentionally narrow. That is a strength, not a weakness. In healthcare text, explicit scope is safer than pretending one pattern covers every case.

Normalize What You Extract

Raw matches are rarely ready for analysis. Even simple fields need normalization:

  • convert dosage strings to numeric values
  • standardize drug names
  • parse dates into a machine-friendly format
  • map synonyms to one canonical label

Here is a small normalization step:

python
1from datetime import datetime
2
3raw_date = "2026-02-10"
4parsed = datetime.strptime(raw_date, "%Y-%m-%d").date()
5print(parsed.isoformat())

Without normalization, reporting and downstream joins become inconsistent very quickly.

Move to NLP When Rules Stop Scaling

Rule-based extraction works well when:

  • document format is fairly stable
  • target entities are specific
  • explainability is important

Once phrasing becomes more varied, named entity recognition and custom NLP models become useful. In Python, teams often evaluate spaCy, domain-adapted models, or transformer-based pipelines. Even then, rule-based post-processing still matters because medical notes often include shorthand, abbreviations, and template fragments.

A realistic architecture often combines both:

  • NLP model to propose entity spans
  • rules to validate and normalize those spans
  • dictionaries to map terms to preferred labels

That hybrid approach is easier to trust than an opaque end-to-end pipeline with no validation layer.

Privacy and Evaluation Cannot Be Afterthoughts

Medical text is sensitive. Even if the code only extracts structured fields, logs and test fixtures can still expose protected data. Keep these constraints in mind:

  • never log raw notes unless the environment is explicitly approved for that purpose
  • redact or synthesize examples for development
  • validate extraction quality on representative, permissioned data

Accuracy evaluation also needs to be concrete. Measure precision and recall against a reviewed sample, not against intuition. A pipeline that extracts the right medication name but misses dosage or frequency may still be unusable for the intended workflow.

Build Small, Reviewable Output Records

Downstream systems prefer simple records over free-form strings. A compact dictionary is a good default:

python
1record = {
2    "patient_id": "example-001",
3    "medication_name": "aspirin",
4    "dose_value": 81,
5    "dose_unit": "mg",
6    "frequency": "daily",
7    "systolic_bp": 140,
8    "diastolic_bp": 90,
9}
10
11print(record)

That structure is much easier to audit, load into analytics pipelines, or compare in unit tests.

Common Pitfalls

  • Trying to solve all entity types with one large regex or one large model pass.
  • Ignoring normalization and leaving values as inconsistent free text.
  • Testing on toy notes that do not resemble real clinical writing.
  • Logging raw medical text in development or debugging output.
  • Measuring success by anecdotal examples instead of reviewed extraction metrics.

Summary

  • Start with one clearly defined extraction target such as medications or vitals.
  • Rule-based Python extractors are a practical first step when requirements are narrow.
  • Normalize extracted values before storing or analyzing them.
  • Use NLP models when language variation grows, but keep rules for validation.
  • Treat privacy, auditability, and evaluation as core design requirements.

Course illustration
Course illustration

All Rights Reserved.