CACS410 Artificial Intelligence

Artificial IntelligenceUnit 911 min read

NLP: Syntax, Semantics, Pragmatics & Applications

Unit 9 of Artificial Intelligence explores Natural Language Processing (NLP), covering its core components—syntax, semantics, and pragmatics—along with real-world applications, challenges, and techniques like tokenization, parsing, and sentiment analysis. This note includes visual breakdowns of NLP pipelines, real-worl

Natural Language Processing (NLP): Core Concepts and Applications

1. What is NLP?

Natural Language Processing (NLP) is a subfield of AI that enables computers to understand, interpret, and generate human language. It bridges linguistics, computer science, and AI to process text or speech in a way that is meaningful to humans.

Why is NLP Important?

  • Human-Computer Interaction: Powers chatbots, voice assistants (e.g., Google Assistant, Siri), and translation tools.
  • Automation: Extracts insights from unstructured data (e.g., customer reviews, legal documents).
  • Accessibility: Helps people with disabilities interact with technology via speech.

2. Key Components of NLP

NLP breaks down language processing into three layers:

A. Syntax (Structure of Language)

Syntax focuses on grammar and sentence structure. It answers:

  • "How are words arranged?"
  • "Is the sentence grammatically correct?"

Example:

  • Input: "The cat sat on the mat."
  • Syntax checks: Subject (The cat) + Verb (sat) + Prepositional phrase (on the mat).

Visual: Syntax Tree

graph TD
    S["Sentence"] --> NP1["NP: The cat"]
    S --> VP["VP: sat on the mat"]
    NP1 --> DT["Det: The"]
    NP1 --> N["Noun: cat"]
    VP --> V["Verb: sat"]
    VP --> PP["PP: on the mat"]
    PP --> P["Prep: on"]
    PP --> NP2["NP: the mat"]
    NP2 --> DT2["Det: the"]
    NP2 --> N2["Noun: mat"]

B. Semantics (Meaning of Language)

Semantics deals with meaning. It answers:

  • "What does the sentence mean?"
  • "Is the meaning logical?"

Example:

  • Input: "The cat is on the mat."
  • Semantics extracts: A feline (cat) is physically located on a piece of furniture (mat).

Visual: Semantic Graph

graph TD
    A["cat"] -->|"is_a"| B["animal"]
    A -->|"location"| C["mat"]
    C -->|"is_a"| D["furniture"]

C. Pragmatics (Context and Intent)

Pragmatics considers context, speaker intent, and real-world knowledge. It answers:

  • "What does the speaker really mean?"
  • "Is there an implied meaning?"

Example:

  • Input: "It’s cold in here."
    • Literal meaning: Temperature is low.
    • Pragmatic meaning: "Close the window."

Why Pragmatics Matters in NLP:

  • Helps AI understand sarcasm, humor, or indirect requests.
  • Critical for customer service bots (e.g., Daraz’s chatbot resolving complaints).

3. Real-World Applications of NLP

A. In Nepal

Application NLP Technique Used Example
eSewa Chatbot Intent recognition, sentiment analysis Users ask: "How to pay traffic fine?" → Bot guides them.
Khalti Customer Support Named Entity Recognition (NER) Extracts user account details from: "My transaction ID is KH12345."
Nepali Newspaper Summarization Text summarization, keyword extraction Converts long news articles into bullet points.

B. Global Examples

Product NLP Technique How It Works
Google Translate Machine Translation (MT), Neural MT Converts "मेरो नाम रमेश हो" → "My name is Ramesh."
WhatsApp Business Chatbot (Dialogue Management) "Order status: 12345" → Bot replies with tracking link.
YouTube Comments Sentiment Analysis Flags toxic comments (e.g., "This video is trash!").

Worked Example: Kathmandu Traffic Route Suggestion

  • Input: "How to reach Thamel from my location?"
  • NLP Pipeline:
    1. Tokenization: Split into words: "How", "to", "reach", "Thamel", "from", "my", "location?"
    2. Intent Classification: Detects "route_query" intent.
    3. Entity Extraction: "Thamel" (destination), "my location" (source).
    4. Pragmatic Response: Uses Google Maps API to suggest the fastest path (avoiding traffic jams).

4. NLP Techniques and Tools

A. Tokenization

Splitting text into words, phrases, or tokens. Example: Input: "I love NLP!" Output: ["I", "love", "NLP", "!"]

B. Stemming & Lemmatization

  • Stemming: Reduces words to root form (crude). "running" → "run"
  • Lemmatization: Uses vocabulary (more accurate). "better" → "good"

Visual: Stemming vs. Lemmatization

graph LR
    A["running"] -->|"Stemmer"| B["run"]
    A -->|"Lemmatizer"| C["run"]
    D["better"] -->|"Stemmer"| E["good"]
    D -->|"Lemmatizer"| F["good"]

C. Part-of-Speech (POS) Tagging

Labels words by grammatical role. Example: "The cat sat on the mat." → ["Det", "Noun", "Verb", "Prep", "Det", "Noun"]

D. Named Entity Recognition (NER)

Identifies people, places, organizations. Example: "Ramesh works at Ncell in Kathmandu." → ["Person: Ramesh", "Organization: Ncell", "Location: Kathmandu"]

E. Sentiment Analysis

Classifies text as positive, negative, or neutral. Example:

  • "This phone is amazing!" → Positive
  • "The service was terrible." → Negative

Visual: Sentiment Analysis Pipeline

flowchart LR
    A["Raw Text"] --> B["Preprocessing"]
    B --> C["Tokenization"]
    C --> D["Feature Extraction"]
    D --> E["Machine Learning Model"]
    E --> F["Sentiment Score"]
    F --> G["Positive/Negative/Neutral"]

5. Challenges in NLP

Challenge Example Solution
Ambiguity "I saw the man on the hill with a telescope." Use contextual clues (e.g., who has the telescope?).
Slang/Informal Language "She’s lit!" (means "excellent") Train models on social media data.
Multilingual Support Nepali → English translation errors Use language-specific models (e.g., Hugging Face’s Nepali BERT).
Sarcasm/Humor "Great, another power cut." Combine semantics + pragmatics.

6. NLP in Machine Translation

A. Rule-Based vs. Statistical vs. Neural MT

Type How It Works Example Limitations
Rule-Based Hard-coded grammar rules. Google Translate (early versions). Poor for idioms, inflexible.
Statistical MT Uses probability from bilingual text. Moses (open-source tool). Slow, requires large datasets.
Neural MT Uses deep learning (e.g., Transformers). Google Translate (2016–present). High accuracy, but computationally expensive.

Worked Example: Nepali → English Translation

  • Input: "मेरो नाम रमेश हो। म काठमाडौँमा बस्छु।"
  • Neural MT Steps:
    1. Tokenize: ["मेरो", "नाम", "रमेश", "हो", "म", "काठमाडौँमा", "बस्छु", "."]
    2. Embed words into vectors.
    3. Transformer model predicts: "My name is Ramesh. I live in Kathmandu."

7. NLP in Chatbots and Virtual Assistants

A. How Chatbots Work

flowchart LR
    A["User Input"] --> B["Preprocess"]
    B --> C["Intent Recognition"]
    C --> D["Entity Extraction"]
    D --> E["Dialogue Manager"]
    E --> F["Response Generation"]
    F --> G["User"]

Example: Daraz Customer Support Bot

  • User: "My order #12345 is delayed."
  • Bot Steps:
    1. Intent: "order_status_query"
    2. Entity: "order_id: 12345"
    3. Response: "Your order is out for delivery. ETA: 2 hours."

B. Limitations

  • Lack of Context: Struggles with multi-turn conversations.
  • Cultural Nuances: Nepali humor/slang may confuse global models.

8. Future of NLP

  • Multimodal NLP: Combines text + images + speech (e.g., describing a photo).
  • Zero-Shot Learning: Models that understand new languages/tasks without training.
  • Ethical NLP: Reducing bias in chatbots (e.g., gender-neutral responses).

In the Real World

  1. eSewa’s Automated Responses

    • Idea Used: Intent classification + NER
    • How: When you type "How to pay traffic fine?", eSewa’s bot extracts:
      • Intent: "payment_inquiry"
      • Entity: "traffic fine"
      • Response: Guides you to the payment portal.
  2. Khalti’s Fraud Detection

    • Idea Used: Sentiment analysis + anomaly detection
    • How: Flags suspicious transactions by analyzing:
      • Unusual phrasing in chat messages (e.g., "Send money to this number").
      • Sudden large transfers from a user’s account.
  3. Nepali Newspaper Summarization (e.g., Kantipur, Republica)

    • Idea Used: Text summarization (extractive/abstractive)
    • How: Converts a 500-word article into 5 bullet points using:
      • Keyword extraction (e.g., "lockdown", "supply chain").
      • Sentence importance scoring.

Exam Tip

How This Unit is Tested

  1. Definitions (10%)

    • Expect: "Define NLP. Explain syntax vs. semantics vs. pragmatics."
    • Key Points to Remember:
      • Syntax = grammar.
      • Semantics = meaning.
      • Pragmatics = context + intent.
  2. Applications (30%)

    • Common Questions:
      • "How does WhatsApp Business use NLP?" → Intent recognition + NER.
      • "Explain how Google Translate works." → Neural MT + attention mechanisms.
    • Tip: Always relate to Nepali examples (eSewa, Khalti, Daraz).
  3. Techniques (40%)

    • Must-Know Steps:
      • Tokenization → POS Tagging → NER → Sentiment Analysis.
    • Worked Example: Given a sentence, show tokenization + POS tagging.
  4. Challenges (20%)

    • Expected Answer:
      • Ambiguity → Use context.
      • Slang → Train on social media data.
      • Multilingual → Use language-specific models.

Model Answer for Past Exam Question

Question: "Describe about natural language processing. Why is pragmatic analysis important in language processing?"

Answer: Natural Language Processing (NLP) is an AI subfield that enables computers to understand, interpret, and generate human language. It involves three key layers:

  1. Syntax: Analyzes sentence structure (e.g., "The cat sat on the mat" → Subject-Verb-Object).
  2. Semantics: Extracts meaning (e.g., "cat" refers to a feline).
  3. Pragmatics: Interprets context and intent (e.g., "It’s cold!" may mean "Close the window").

Why Pragmatic Analysis is Important:

  • Real-World Context: Humans rarely speak literally. Pragmatics helps AI understand:
    • Implied meanings (e.g., "You’re late" → "Hurry up").
    • Politeness (e.g., "Could you help?" vs. "Help me!").
  • Chatbot Effectiveness: Without pragmatics, a bot might fail to:
    • Handle sarcasm (e.g., "Oh great, another power cut.").
    • Resolve ambiguity (e.g., "I saw the man with binoculars" → Who has them?).
  • Nepali Applications: In eSewa chatbots, pragmatic analysis helps:
    • Detect urgent requests (e.g., "My fine is overdue!").
    • Provide empathetic responses (e.g., "I’m sorry for the delay.").

Example:

  • Input: "The meeting is postponed."
    • Syntax: Subject (meeting) + Verb (is postponed).
    • Semantics: The meeting will happen later.
    • Pragmatics: "Please reschedule your plans."

Final Checklist for Full Marks

✅ Define NLP (1 mark). ✅ Explain syntax, semantics, pragmatics (3 marks). ✅ Give 2 real-world examples (eSewa/Khalti) (4 marks). ✅ Discuss challenges (ambiguity, slang) (3 marks). ✅ Relate to Nepali context (1 mark).


transformer model architecture**A diagram of a Transformer’s encoder-decoder layers with attention mechanisms. (Image: Yuening Jia, CC BY-SA 3.0, via Wikimedia Commons)

Based on the TU BCA syllabus for Artificial Intelligence (CACS410), unit 9.

Discussion

Loading…