AI Hints
Home›NLP Tutorial›Mining Documents
NLP · Lesson 46 of 106

Knowledge Graphs

Turning text into connected facts.

⏱ 2 min read▶ Video at 1:22:09🎓 Free
Watch this lesson in the NLP courseWatch on YouTube ↗Full NLP course

Who owns the company that published this book? No single document says so. One says who founded Bright Books. Another says it bought a publisher. To answer, you have to connect the facts. That's what a knowledge graph is for.

Knowledge Graphs
From the video: Knowledge Graphs
  • No single document has the answer
  • You have to connect the facts
  • A map of how things relate

Extracting facts

We use the dependency parsing that we learned earlier. For each sentence, find the verb, its subject, and its object. Each one becomes an edge in a graph, between two entities. Run it. Plain sentences have become a set of connected facts. For messier text, you'd use a language model with a schema to extract these, which works far better on real documents.

graph.pypython
import spacy
from collections import defaultdict

nlp = spacy.load("en_core_web_sm")
text = ("Bright Books acquired Northern Press. Northern Press published Dune. "
        "Sam Lee founded Bright Books. Anna Chen leads Northern Press.")

graph = defaultdict(list)
for sent in nlp(text).sents:
    root = sent.root
    subj = [t for t in root.children if t.dep_ == "nsubj"]
    obj = [t for t in root.children if t.dep_ == "dobj"]
    if subj and obj:
        a = " ".join(w.text for w in subj[0].subtree)
        b = " ".join(w.text for w in obj[0].subtree)
        graph[a].append((root.lemma_, b))

for head, facts in graph.items():
    print(head, "->", facts)
Output
Bright Books -> [('acquire', 'Northern Press')]
Northern Press -> [('publish', 'Dune')]
Sam Lee -> [('found', 'Bright Books')]
Anna Chen -> [('lead', 'Northern Press')]

Answering by hopping

Now we can answer questions that need several steps. How is Sam Lee connected to Dune? We walk through the graph, from one fact to the next. Run it. Three hops, and a clear answer. Sam founded Bright Books, which bought the press, which published Dune. Ordinary RAG would struggle with this, since no single chunk contains the link. This idea, called graph RAG, is used when questions connect many documents.

hops.pypython
graph = {
    "Sam Lee": [("found", "Bright Books")],
    "Bright Books": [("acquire", "Northern Press")],
    "Northern Press": [("publish", "Dune")],
}

def path(start, goal, seen=()):
    for rel, next_node in graph.get(start, []):
        if next_node == goal:
            return [(start, rel, next_node)]
        if next_node not in seen:
            rest = path(next_node, goal, seen + (start,))
            if rest:
                return [(start, rel, next_node)] + rest
    return []

for step in path("Sam Lee", "Dune"):
    print(step)
Output
('Sam Lee', 'found', 'Bright Books')
('Bright Books', 'acquire', 'Northern Press')
('Northern Press', 'publish', 'Dune')

Summary

Knowledge graphs connect facts across documents, and they answer questions that search alone can't. Next, what about trends? What are customers saying this month, compared to last?

Key takeaways
  • Subject, verb, object become edges
  • Hop across facts
  • Graph RAG links documents
Next up: Text Analytics — what are customers saying?