AI Hints
Home›NLP Tutorial›Evaluation and Extraction
NLP · Lesson 15 of 106

Parsing Grammar

Who did what to whom.

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

Man bites dog is news. Dog bites man isn't. The very same words, but a different meaning, and the only difference is who did what to whom. To get that, a computer has to understand grammar.

Parsing Grammar
From the video: Parsing Grammar
  • Man bites dog
  • Same words, new meaning
  • Who did what to whom?

Parts of speech and dependencies

SpaCy tags each word with a part of speech, such as noun or verb. It also draws a dependency tree. Each word points to the word that it depends on. Run it. Returned is the root, the main action. Customer is its subject, and book is its object. Angry describes the customer, and torn describes the book. The whole sentence has become a structure that a program can walk through.

parse.pypython
import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("The angry customer returned the torn book yesterday.")

for t in doc:
    print(f"{t.text:<10} {t.pos_:<6} {t.dep_:<8} -> {t.head.text}")
Output
The        DET    det      -> customer
angry      ADJ    amod     -> customer
customer   NOUN   nsubj    -> returned
returned   VERB   ROOT     -> returned
the        DET    det      -> book
torn       ADJ    amod     -> book
book       NOUN   dobj     -> returned
yesterday  NOUN   npadvmod -> returned
.          PUNCT  punct    -> returned

Extracting who did what

Let's use this. For each sentence, find the root verb, then its subject, and its object. Run it. Clean little facts, from plain sentences. Sam returned the book. The courier lost a parcel. This is called relation extraction, and it's how people build knowledge graphs from documents. No training needed, for simple sentences, and it runs in a fraction of a second.

triples.pypython
import spacy

nlp = spacy.load("en_core_web_sm")
texts = ["Sam returned the book.", "The courier lost my parcel.",
         "Bright Books refunded the customer."]

for text in texts:
    doc = nlp(text)
    root = [t for t in doc if t.dep_ == "ROOT"][0]
    who = [c.text for c in root.children if c.dep_ == "nsubj"]
    what = [c.text for c in root.children if c.dep_ == "dobj"]
    print(f"{who} -> {root.lemma_} -> {what}")
Output
['Sam'] -> return -> ['book']
['courier'] -> lose -> ['parcel']
['Books'] -> refund -> ['customer']

Summary

Tags and dependency trees turn sentences into structure. All of the classic tools are now in your hands, and they're fast, cheap, and readable. But they've got one big weakness. They don't know what words mean. Parcel and package are as different as parcel and banana. Next, the idea that changed everything.

Key takeaways
  • Parts of speech and dependencies
  • Find subject, verb, object
  • Classic NLP has no sense of meaning
Next up: Word Embeddings — meaning as numbers