From News Articles to Knowledge Graphs with spaCy and NetworkX
Building a complete NLP pipeline to extract entities, discover relationships, and visualize knowledge networks from unstructured text
News articles encode relationships in plain language all the time… “Apple acquired Beats Electronics”, “President Obama visited France”, “Google announced a partnership with Samsung”.
Each sentence is a triple in disguise: two entities and a verb tying them together. If we extract enough of them across enough articles, we get a knowledge graph: a structure we can query, visualize, and reason over.
This post walks through that pipeline end-to-end. We start from raw news text, run named entity recognition with spaCy, infer relationships, and analyze the resulting graph.
As always, you can find the companion notebook on the Graphs for Data Science GitHub repository:
We use the AG News corpus, 120,000 articles across four categories (World, Sports, Business, Sci/Tech). For the sake of expediency, we sample 2,000 articles, balanced 500 per category. Loading is a single call to Hugging Face:
from datasets import load_dataset
dataset = load_dataset('ag_news', split='train')
df = dataset.to_pand…



