Chunking › Chunking as Preprocessing
Language AI HandbookAll chapters Chunking › Chunking as PreprocessingOne section · Sections in this chapter11 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Chunking is a preprocessing step for several NLP tasks. By identifying phrase boundaries, it provides lightweight structural annotations that simplify later processing.
Information Extraction
The most direct application is extracting structured information from text. A common pattern is identifying subject-verb-object triples by looking for NP-VP-NP sequences:
def extract_svo_triples(sentence, chunker_obj):
"""
Extract subject-verb-object triples using chunk sequences.
Looks for NP-VP-NP patterns in the chunk sequence.
"""
tokens = word_tokenize(sentence)
tagged = pos_tag(tokens)
tree = chunker_obj.parse(tagged)
ordered_chunks = []
for subtree in tree:
if hasattr(subtree, "label"):
text = " ".join(w for w, _ in subtree.leaves())
ordered_chunks.append((subtree.label(), text))
triples = []
for i in range(len(ordered_chunks) - 2):
if (
ordered_chunks[i][0] == "NP"
and ordered_chunks[i + 1][0] == "VP"
and ordered_chunks[i + 2][0] == "NP"
):
triples.append(
{
"subject": ordered_chunks[i][1],
"verb": ordered_chunks[i + 1][1],
"object": ordered_chunks[i + 2][1],
}
)
return triples
# Test on news-style sentences
ie_sentences = [
"The researchers published a landmark study.",
"The company acquired its largest competitor.",
"Scientists identified a new protein in the brain.",
"The president signed the bill into law.",
]
ie_results = [(s, extract_svo_triples(s, chunker)) for s in ie_sentences]Subject-Verb-Object Extraction via Chunking ============================================================ Sentence: The researchers published a landmark study. Subject: The researchers Verb: published Object: a landmark study Sentence: The company acquired its largest competitor. Subject: The company Verb: acquired Object: competitor Sentence: Scientists identified a new protein in the brain. Subject: Scientists Verb: identified Object: a new protein Sentence: The president signed the bill into law. Subject: The president Verb: signed Object: the bill
Noun Phrase Keyword Extraction
Noun phrases often contain the most semantically meaningful content in a text. Extracting and counting them provides a simple keyword extraction method:
from collections import Counter
def extract_keywords_via_np_chunking(text, top_n=10):
"""
Extract noun phrase keywords from text using chunking.
Returns the most frequent multi-word NPs.
"""
sentences = nltk.sent_tokenize(text)
all_nps = []
for sent in sentences:
tokens = word_tokenize(sent)
tagged = pos_tag(tokens)
tree = chunker.parse(tagged)
for subtree in tree:
if hasattr(subtree, "label") and subtree.label() == "NP":
np_text = " ".join(w.lower() for w, _ in subtree.leaves())
# Only keep multi-word NPs (more specific)
if len(subtree.leaves()) > 1:
all_nps.append(np_text)
return Counter(all_nps).most_common(top_n)
# Apply to a sample technology text
tech_text = """
Machine learning has transformed natural language processing research.
Deep learning models achieve remarkable performance on many language tasks.
Large language models like GPT and BERT have redefined the state of the art.
Transformer architectures use attention mechanisms to capture long-range dependencies.
Researchers continue to explore new training methods and model architectures.
Language models learn rich text representations from massive training corpora.
"""
keywords = extract_keywords_via_np_chunking(tech_text)Noun Phrase Keyword Extraction ================================================== Text excerpt: 'Machine learning has transformed natural language processing research. Deep lear...' Top multi-word noun phrase keywords: 1. 'machine learning' (1 occurrence) 2. 'natural language processing research' (1 occurrence) 3. 'deep learning models' (1 occurrence) 4. 'remarkable performance' (1 occurrence) 5. 'many language tasks' (1 occurrence) 6. 'large language models' (1 occurrence) 7. 'the state' (1 occurrence) 8. 'the art' (1 occurrence) 9. 'transformer architectures' (1 occurrence) 10. 'attention mechanisms' (1 occurrence)
This keyword extraction approach is simple but captures meaningful phrases that single-word methods miss. "Large language models" and "natural language processing" are more informative keywords than "models" or "language" alone.
Answer Candidate Extraction for Question Answering
In rule-based question answering, chunking helps identify candidate answer spans. Different question types suggest different chunk types as likely answers:
def find_answer_candidates(question, context_sentence, chunker_obj):
"""
Identify likely answer spans based on question type and chunking.
"""
q_lower = question.lower()
# Map question words to likely answer chunk types
if q_lower.startswith("who"):
target_type = "NP" # Named person = noun phrase
elif q_lower.startswith("where"):
target_type = "PP" # Location = prepositional phrase
elif q_lower.startswith("what"):
target_type = "NP" # Thing = noun phrase
elif q_lower.startswith("when"):
target_type = "NP" # Time expressions are often NPs
else:
target_type = "NP"
tokens = word_tokenize(context_sentence)
tagged = pos_tag(tokens)
tree = chunker_obj.parse(tagged)
candidates = []
for subtree in tree:
if hasattr(subtree, "label") and subtree.label() == target_type:
candidates.append(" ".join(w for w, _ in subtree.leaves()))
return candidates
# Example QA preprocessing
qa_examples = [
(
"Who wrote the novel?",
"The famous author Ernest Hemingway wrote the novel in 1929.",
),
(
"What did scientists discover?",
"Scientists announced a new vaccine for the disease.",
),
(
"Where did the event occur?",
"The summit took place in the heart of Geneva.",
),
]
qa_results = [
(q, c, find_answer_candidates(q, c, chunker)) for q, c in qa_examples
]Answer Candidate Extraction via Chunking ============================================================ Question: Who wrote the novel? Context: The famous author Ernest Hemingway wrote the novel in 1929. Candidates: - 'The famous author Ernest Hemingway' - 'the novel' Question: What did scientists discover? Context: Scientists announced a new vaccine for the disease. Candidates: - 'Scientists' - 'a new vaccine' - 'the disease' Question: Where did the event occur? Context: The summit took place in the heart of Geneva. Candidates: - 'in' - 'of'
Chunking-based candidate extraction predates neural QA systems but remains useful in low-resource settings and as a fast preprocessing step. The candidates narrow the search space before more expensive processing.