Natural language processing lets computers read, understand and generate human language.
Why Language Is Hard
Language is ambiguous ("I saw the man with the telescope"), context-dependent, full of idioms, and varies across languages, dialects and scripts. NLP combines linguistics, computer science and machine learning. Its two broad parts are natural language understanding (NLU) and natural language generation (NLG).
Definition: Natural language processing. The branch of AI concerned with enabling computers to analyse, understand and generate human language in written or spoken form.
Levels of Analysis
Lexical analysis: splitting text into tokens, finding word forms (stemming and lemmatisation), stop-word removal. Syntactic analysis (parsing): grammatical structure; part-of-speech (POS) tagging and parse trees. Semantic analysis: meaning of words and sentences, word sense disambiguation, named entity recognition (NER) (people, places, organisations). Discourse and pragmatic analysis: connected text, references (coreference) and intention. Python's NLTK and spaCy provide tools for these steps.
Representing Text
A bag of words counts the occurrences of each word, ignoring order; TF-IDF weights words by frequency in a document relative to rarity in the collection. Word embeddings (Word2Vec, GloVe) represent each word as a vector so that similar words are close together. Transformer models (BERT, GPT) produce context-dependent representations and have transformed the field; large language models are trained on huge text collections to predict the next word and can summarise, translate, answer questions and write code.
Tasks and Applications
Machine translation (Google Translate), sentiment analysis of reviews, chatbots and virtual assistants (Alexa, Siri), speech recognition and synthesis, text summarisation, question answering, information extraction, spam detection, autocomplete and spell checking, and search engines. Challenges: low-resource languages (including Indian languages), bias, hallucination and sarcasm.
Table 4.4: Levels of NLP
| Level | Task | Example |
|---|---|---|
| Lexical | Tokens, stems, stop words | "running" → "run" |
| Syntactic | POS tags, parse trees | "dog" is a noun |
| Semantic | Meaning, NER | "Delhi" is a place |
| Pragmatic | Intent in context | "Can you pass the salt?" is a request |
Example 4.4: Preparing text for analysis
Problem: Text: "AI is changing how we work and AI is changing how we live". Tokenise, remove the stop words {is, how, we, and} and count the word frequencies.
Solution:
- Tokens (lower case): ai, is, changing, how, we, work, and, ai, is, changing, how, we, live (13 tokens).
- Remove the stop words is, how, we, and: ai, changing, work, ai, changing, live (6 tokens).
- Counts: ai 2, changing 2, work 1, live 1.
Result: After removing stop words the key terms are ai and changing, each appearing twice.
text = "AI is changing how we work and AI is changing how we live"
tokens = text.lower().split()
stop = {"is", "how", "we", "and"}
kept = [t for t in tokens if t not in stop]
freq = {}
for t in kept:
freq[t] = freq.get(t, 0) + 1
print(len(tokens), "tokens ->", len(kept), "after stop words")
print(freq)
Output
13 tokens -> 6 after stop words
{'ai': 2, 'changing': 2, 'work': 1, 'live': 1}
Common mistakes
- Removing stop words when they carry meaning (negations such as "not").
- Assuming words have one meaning; most are ambiguous.
- Ignoring order with bag-of-words when order matters.
- Trusting generated text without checking facts.
Key takeaways
- NLP = understanding (NLU) + generation (NLG).
- Steps: tokenising, stop-word removal, stemming, POS tagging, parsing, NER.
- Representations: bag of words, TF-IDF, embeddings, transformers.
- Applications: translation, chatbots, sentiment analysis, summarisation.
Practice questions
- What is tokenisation?
- What is named entity recognition?
- Name two applications of NLP.
Answers to the practice questions
- Splitting text into words or sub-word units called tokens.
- Identifying names of people, places, organisations and similar entities in text.
- Machine translation and chatbots (also sentiment analysis, summarisation).
Sources
- Official BCA syllabus Artificial Intelligence, course code 26BCA404DS04, Unit IV
