Natural Language Processing
Computational Linguistics Pipeline
Learn how computers process human language through lexical, syntactic, semantic, discourse and pragmatic analysis. Progress from foundational concepts to Python implementation using spaCy.
Learning objectives
By the end of this lesson, students should be able to:
- Explain the five major levels of language analysis.
- Differentiate lexical, morphological, syntactic and semantic analysis.
- Understand discourse integration and pragmatic interpretation.
- Explain tokenization, lemmatization and POS tagging.
- Interpret dependency labels such as
nsubj,objandamod. - Write basic NLP programs using Python and spaCy.
Learning journey
🧠 REMEMBER
What should I know? Learn definitions, terminology and foundational concepts.
LEVEL 2🔍 UNDERSTAND
How does it work? Explore the language pipeline and its processing stages.
LEVEL 3💻 APPLY
How can I implement it? Practise Python programs and dependency parsing.
What should I know?
Build a strong foundation by understanding the important terms and definitions used in computational linguistics.
1. Introduction to NLP
Natural Language Processing (NLP) is a field of Artificial Intelligence that enables computers to process, analyse, interpret and generate human language.
Humans communicate using languages such as English, Telugu and Hindi. Computers process information through numerical representations and computational operations. NLP provides techniques that help computers work with human language data.
2. Five levels of language analysis
1. Lexical and Morphological Analysis
Lexical analysis identifies and processes individual words or tokens in a text.
Morphological analysis studies the internal structure of words, including roots, prefixes, suffixes and grammatical forms.
Example: students → student + plural suffix -s.
Example: developing → base form develop.
2. Syntactic Analysis
Syntactic analysis examines how words are arranged and related according to grammatical rules. It identifies sentence structure and grammatical relationships.
Example: Students develop applications.
- Students = subject
- develop = verb
- applications = object
3. Semantic Analysis
Semantic analysis deals with the meaning of words, phrases and sentences. It examines relationships between words and whether an interpretation is meaningful.
Example:
- Students develop applications. – A meaningful sentence in a typical context.
- Students drink applications. – Grammatically possible, but semantically unusual in an ordinary context.
4. Discourse Integration
Discourse integration analyses how multiple sentences are connected. It uses context from earlier sentences to interpret later expressions.
Example:
Ravi purchased a laptop. He uses it for programming.
- He → Ravi
- it → laptop
Identifying these references is known as coreference resolution.
Key terms: Context · Coreference · Pronoun resolution5. Pragmatic Analysis
Pragmatic analysis interprets the intended meaning of an expression using the situation, speaker intention and context.
Example: "Can you open the window?"
Literal meaning: A question about someone's ability. Intended meaning in an ordinary conversation: A polite request to open the window.
Key terms: Intention · Context · Implied meaning3. Comparison of the five levels
| Level | Main focus | Question answered |
|---|---|---|
| Lexical / Morphological | Words and their internal structure | What are the words and their forms? |
| Syntactic | Grammatical structure | How are the words related? |
| Semantic | Meaning | What does the sentence mean? |
| Discourse | Connections across sentences | How are sentences connected? |
| Pragmatic | Intention and situation | What does the speaker intend? |
4. Important NLP terminology
| Term | Meaning | Example |
|---|---|---|
| Tokenization | Splitting text into tokens | "I study NLP." → I, study, NLP, . |
| Stemming | Reducing words using rule-based truncation | studies → studi (possible stem) |
| Lemmatization | Finding the dictionary base form | students → student |
| POS tagging | Assigning grammatical word categories | student → NOUN |
| Parsing | Identifying grammatical structure | Subject–verb–object |
| Coreference resolution | Finding expressions referring to the same entity | Ravi → He |
token.lemma_ provides the lemma; it does not perform stemming.
5. Level 1 – Knowledge check
Attempt all five questions. Select one option and check your answers to receive feedback.
Complete the five questions to finish Level 1.
How does it work?
Explore how human language moves through different stages of computational analysis.
1. NLP processing workflow
Consider the sentence:
Tokens, roots, word forms
Grammatical structure and parsing
Meaning and word relationships
Context across sentences
Intention and situation
2. Stage-by-stage example
Stage 1: Lexical and morphological analysis
Input:
Students are developing intelligent applications.
Possible token sequence:
Students
are
developing
intelligent
applications
.
Possible lemma forms:
Students → student
are → be
developing → develop
intelligent → intelligent
applications → application
Tokenization identifies individual tokens. Lemmatization maps inflected words to their base forms.
Stage 2: Syntactic analysis
The system analyses the grammatical relationships among tokens.
Example: Students develop intelligent applications.
- Students is the subject of develop.
- develop is the main verb.
- applications is the direct object of develop.
- intelligent describes applications.
These relationships can be represented using a dependency parse.
Stage 3: Semantic analysis
The system examines what the sentence expresses, including word meanings and semantic relationships.
For example, in "Students develop applications", the verb develop describes an action associated with the object applications.
Semantic interpretation is more complex than simply checking whether a sentence sounds natural. Meaning can depend on domain knowledge, word sense and context.
Stage 4: Discourse integration
Consider two sentences:
Ravi purchased a laptop.
He uses it for programming.
A discourse system may identify:
Herefers toRavi.itrefers tolaptop.
This requires information from more than one sentence.
Stage 5: Pragmatic analysis
Consider the sentence:
Can you open the window?
Depending on the situation, it may be interpreted as a polite request rather than a question about physical ability.
Pragmatics considers speaker intention, social context and conversational circumstances.
3. Understanding POS tagging
Part-of-Speech (POS) tagging assigns a grammatical category to each token.
| POS tag | Meaning | Example |
|---|---|---|
| NOUN | Noun: person, place, object, animal, concept or idea | Students |
| VERB | Action or state | develop |
| ADJ | Adjective: describes a noun | intelligent |
| ADV | Adverb: modifies a verb, adjective or another adverb | quickly |
| PRON | Pronoun | He |
| DET | Determiner | the |
4. Understanding dependency parsing
Dependency parsing identifies grammatical relationships between words. In spaCy, token.dep_ returns the dependency label associated with a token.
Three important dependency labels
nsubj
Nominal subject
Identifies the nominal subject associated with a verb.
Example: Students in "Students develop applications."
dobj / obj
Direct object
Identifies the direct object of a verb.
Example: applications in "Students develop applications."
amod
Adjectival modifier
Identifies an adjective modifying a noun.
Example: intelligent in "intelligent applications."
ROOT
Root of the dependency tree
Identifies the central syntactic head of the sentence.
Example: develop in "Students develop applications."
Dependency relationship – conceptual view
Students develop intelligent applications.
nsubj
obj / dobj
amod
Conceptual relationships; the exact printed labels depend on the model and spaCy version.
dobj for a direct object. Newer Universal Dependencies-style spaCy models commonly use obj. The grammatical purpose is the same. Always inspect the output of the installed model rather than assuming a particular label.
5. Python and spaCy introduction
spaCy is a Python library for practical NLP tasks such as tokenization, lemmatization, POS tagging and dependency parsing.
Install spaCy and its English model in your Python environment:
pip install spacy
python -m spacy download en_core_web_sm
Example 1: Tokenization
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students are learning Natural Language Processing."
doc = nlp(text)
for token in doc:
print(token.text)
Explanation:
import spacyimports the library.spacy.load(...)loads the English language model.nlp(text)processes the text and creates a Doc object.for token in dociterates through tokens.token.textprints the original token text.
Illustrative output:
Students
are
learning
Natural
Language
Processing
.
Example 2: Lemmatization
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students are developing applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.lemma_)
Explanation: token.lemma_ returns the lemma or dictionary base form assigned by the language model.
Typical output:
Students -> student
are -> be
developing -> develop
applications -> application
. -> .
Example 3: POS tagging
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop intelligent applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.pos_)
Illustrative output:
Students -> NOUN
develop -> VERB
intelligent -> ADJ
applications -> NOUN
. -> PUNCT
Example 4: Dependency parsing
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop intelligent applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.dep_)
Explanation:
token.textgives the word.token.dep_gives its grammatical dependency label.token.head.textgives the syntactic head of the token.
Typical relationships include a nominal subject for Students, a root for develop, an adjectival modifier for intelligent and a direct-object relation for applications. Exact labels may vary by model.
en_core_web_sm does not automatically perform full discourse integration, general semantic understanding or pragmatic reasoning. Those tasks require additional models, methods and context.
6. Real-world applications
| Application | How NLP helps |
|---|---|
| Chatbots | Interpreting user questions and conversational context. |
| Machine translation | Analysing and generating language across languages. |
| Sentiment analysis | Classifying opinions expressed in text. |
| Information extraction | Identifying names, dates, entities and relationships. |
| Question answering | Finding information relevant to a question. |
| Document search | Representing and matching text with search queries. |
7. Level 2 – Understanding check
Complete the five questions to finish Level 2.
How can I implement it?
Practise the concepts through Python programs. Try each exercise before opening its solution.
Tokenization using spaCy
Objective: Split a sentence into tokens and display each token.
Sample input:
Students are learning Natural Language Processing.
Expected output:
Students
are
learning
Natural
Language
Processing
.
Task: Complete the program using spaCy.
Show starter code
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students are learning Natural Language Processing."
doc = nlp(text)
for token in doc:
# Print the original token
print(__________)
Hint
Use the token attribute that returns the original text of each token.
View solution
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students are learning Natural Language Processing."
doc = nlp(text)
for token in doc:
print(token.text)
Lemmatization
Objective: Display each token and its lemma.
Sample input:
Students are developing applications.
Expected illustrative output:
Students -> student
are -> be
developing -> develop
applications -> application
Starter code
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students are developing applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", __________)
Hint
Use the spaCy attribute that returns the dictionary base form of a word.
View solution
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students are developing applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.lemma_)
POS tagging
Objective: Identify the grammatical category of each token.
Sample input:
Students develop intelligent applications.
Expected illustrative output:
Students -> NOUN
develop -> VERB
intelligent -> ADJ
applications -> NOUN
Starter code
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop intelligent applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", __________)
Hint
Use the attribute that returns tags such as NOUN, VERB and ADJ.
View solution
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop intelligent applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.pos_)
Dependency parsing
Objective: Identify the grammatical dependency relationship of each token.
Sample input:
Students develop intelligent applications.
Task: Print the token and its dependency label.
Starter code
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop intelligent applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", __________)
Hint
Use the dependency attribute of a token. It ends with an underscore.
View solution
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop intelligent applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.dep_)
To inspect the head of each token as well:
for token in doc:
print(token.text, "->", token.dep_, "->", token.head.text)
Interpret dependency relationships
Objective: Understand how dependency labels describe the relationships among words.
Consider:
Students develop intelligent applications.
Answer these questions:
- Which word is the nominal subject of the verb?
- Which word is the direct object of the verb?
- Which word describes the noun applications?
- Which word is the main syntactic root?
Hint
Think about WHO performs the action, WHAT receives the action, WHICH word describes the noun and WHICH word is the main verb.
View solution
- Students →
nsubj - applications →
objordobj, depending on the model - intelligent →
amod - develop →
ROOT
6. Debugging challenge
A student writes the following code:
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop applications."
doc = nlp(text)
for token in doc:
print(token.lemma)
Question: What is wrong with the last line?
Show correction
For spaCy's string representation of the lemma, use token.lemma_, not token.lemma.
for token in doc:
print(token.lemma_)
token.lemma is an internal numeric lexical attribute, whereas token.lemma_ returns the readable string.
7. Practical case study
Case: Understanding a student feedback message
Input message:
Identify the relevant analysis:
- Tokenization: identify individual tokens.
- Morphology: identify the base form of
likes. - Syntax: identify grammatical categories and relationships.
- Discourse: determine who
Herefers to and whatitrefers to. - Semantics and pragmatics: interpret the positive evaluation in the context of the feedback.
View suggested analysis
likes→like(lemma)Raviis the subject of the first sentence.Herefers to Ravi.itrefers to the NLP course.very usefulexpresses a positive evaluation.
These are contextual interpretations; a complete automated system may require coreference and semantic models in addition to basic spaCy parsing.
Answer Key and Solutions
Attempt each question before opening its solution. Quiz explanations are displayed after checking the relevant quiz.
Level 1 – Remember: MCQ answers
Reveal Level 1 answer key
| Question | Answer | Explanation |
|---|---|---|
| Q1 | B | Morphological analysis studies roots, prefixes, suffixes and word forms. |
| Q2 | C | Syntactic analysis studies grammatical relationships such as subject, verb and object. |
| Q3 | A | Semantic analysis examines sentence and word meaning. |
| Q4 | D | Discourse integration connects information across sentences. |
| Q5 | B | Pragmatic analysis interprets intention using context. |
Level 2 – Understand: MCQ answers
Reveal Level 2 answer key
| Question | Answer | Explanation |
|---|---|---|
| Q1 | A | Tokenization splits text into tokens. |
| Q2 | C | token.lemma_ returns the lemma string. |
| Q3 | B | POS tagging assigns grammatical categories. |
| Q4 | D | nsubj identifies the nominal subject. |
| Q5 | A | amod identifies an adjectival modifier. |
Level 3 – Programming solutions
Exercise 1: Tokenization
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students are learning Natural Language Processing."
doc = nlp(text)
for token in doc:
print(token.text)
Expected result: Each word and punctuation mark is printed as an individual token.
Exercise 2: Lemmatization
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students are developing applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.lemma_)
Expected result: Students → student; are → be; developing → develop; applications → application.
Exercise 3: POS tagging
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop intelligent applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.pos_)
Expected result: Students → NOUN; develop → VERB; intelligent → ADJ; applications → NOUN.
Exercise 4: Dependency parsing
import spacy
nlp = spacy.load("en_core_web_sm")
text = "Students develop intelligent applications."
doc = nlp(text)
for token in doc:
print(token.text, "->", token.dep_)
Expected relationships: Students → nsubj; develop → ROOT; intelligent → amod; applications → obj or dobj, depending on the model.
Exercise 5: Interpret dependency relationships
- Students – nominal subject (
nsubj). - applications – direct object (
objordobj). - intelligent – adjectival modifier (
amod). - develop – root of the dependency tree (
ROOT).
Debugging challenge solution
Incorrect: token.lemma
Correct: token.lemma_
The underscore version returns the readable lemma string.
Case study solution
In "Ravi likes the NLP course. He says it is very useful":
likeshas lemmalike.Herefers to Ravi.itrefers to the NLP course.very usefulexpresses a positive evaluation.
Important examination questions
- Define Natural Language Processing and computational linguistics.
- Explain the five levels of language analysis with examples.
- Differentiate stemming and lemmatization.
- Explain syntactic and semantic analysis with examples.
- What is discourse integration? Explain coreference resolution.
- Explain pragmatic analysis using a conversational example.
- What is tokenization? Write a Python program using spaCy.
- Explain POS tagging and important POS categories.
- What is dependency parsing? Explain nsubj, obj/dobj and amod.
- Write a Python program to perform tokenization, lemmatization, POS tagging and dependency parsing.
Additional practice questions
- How does a computer represent text for machine-learning algorithms?
- Why is semantic analysis different from syntactic analysis?
- Can a sentence be grammatically correct but semantically unusual? Give an example.
- Why does discourse analysis require information from earlier sentences?
- How can pragmatic analysis improve chatbot responses?
Final learning summary
🧠 What should I know?
- Five levels of language analysis.
- Key NLP terminology.
- Definitions and examples.
🔍 What should I understand?
- How language analysis works.
- POS and dependency relationships.
- How context influences interpretation.
💻 What should I implement?
- Tokenization.
- Lemmatization.
- POS tagging.
- Dependency parsing.
🎯 What should I practise?
- Python programming.
- Output interpretation.
- Debugging and case studies.
Lexical = Words
Syntactic = Structure
Semantic = Meaning
Discourse = Connections
Pragmatic = Intention
0 Comments