Syntactic and Semantic Analysis
Syntactic Analysis (Parsing)
Syntax concerns how words combine to form grammatically correct sentences. Syntactic analysis checks a sentence against the grammar rules of a language and produces a structural representation, usually a parse tree.
S
/ \
NP VP
| / \
"Riya" V NP
| |
"reads" "books"
A grammar defines rules such as:
S -> NP VP
NP -> Det N | N
VP -> V NP
import nltk
from nltk import CFG, ChartParser
grammar = CFG.fromstring("""
S -> NP VP
NP -> 'Riya' | 'books'
VP -> V NP
V -> 'reads'
""")
parser = ChartParser(grammar)
for tree in parser.parse(['Riya', 'reads', 'books']):
print(tree)
# (S (NP Riya) (VP (V reads) (NP books)))
Syntactic ambiguity arises when a sentence has more than one valid parse tree:
# "I saw the man with a telescope" has two valid parses:
# 1. I used a telescope to see the man (PP attaches to VP)
# 2. The man had a telescope (PP attaches to NP "the man")
We study grammars and parsing algorithms (CFG, top-down, bottom-up, CYK) in detail in Unit 2.
Semantic Analysis
Semantics is the study of meaning. While syntax checks whether a sentence is grammatically well-formed, semantics checks whether it is meaningful.
Classic example (Noam Chomsky): "Colorless green ideas sleep furiously." This sentence is syntactically correct (NP + VP structure) but semantically anomalous — "colorless" and "green" contradict, and "ideas" cannot literally "sleep".
Semantic analysis involves:
| Task | Description |
|---|---|
| Word sense assignment | Choosing the correct meaning of an ambiguous word in context |
| Semantic role labeling | Identifying who did what to whom ("Riya" = agent, "books" = theme) |
| Compositional semantics | Combining word meanings into sentence meaning |
| Named entity semantics | Recognizing that "Paris" can be a city or a person's name |
# Semantic role labeling (conceptual)
sentence = "Riya gave Zoya a book."
# Agent: Riya
# Recipient: Zoya
# Theme: a book
# Action: gave
Semantic analysis is what lets a system understand that "The cat chased the mouse" and "The mouse was chased by the cat" describe the same event, despite different syntax (active vs passive voice).