Unit 3 - Notes
INT344
Unit 3: Natural Language Processing with Probabilistic Models
This unit covers the fundamental probabilistic techniques used in Natural Language Processing (NLP) to handle ambiguity, spelling errors, sequence prediction, and semantic representation.
1. Autocorrect
Autocorrect is an application that changes words in a text to their nearest correct spelling equivalents. It relies on finding the word in a vocabulary that is most "similar" to the misspelled word based on specific metrics.
1.1 Minimum Edit Distance
Minimum Edit Distance is a string metric used to quantify how dissimilar two strings (e.g., words) are to one another by counting the minimum number of operations required to transform one string into the other.
The Three Basic Operations:
- Insertion: Adding a character.
- Deletion: Removing a character.
- Substitution: Replacing one character with another.
Levenshtein Distance:
This is the most common algorithm for calculating edit distance. Costs are usually assigned as follows:
- Insertion/Deletion cost = 1
- Substitution cost = 2 (often viewed as 1 deletion + 1 insertion, though some variations assign it 1).
Dynamic Programming Algorithm:
To calculate the distance between a source string of length and a target string of length , we construct a matrix of size .
The recurrence relation for cell is:
- Base Case: . The first row represents transforming an empty string to (all insertions). The first column represents transforming to an empty string (all deletions).
- Result: The value at is the minimum edit distance.
1.2 Spellchecker to Correct Misspelled Words
A probabilistic spellchecker typically follows a four-step process:
- Identify the Misspelled Word: Check if the word exists in the dictionary.
- Generate Candidates: Create a list of potential correct words. This is done by applying edit operations (edit distance 1 or 2) to the misspelled word.
- Filter Candidates: Keep only the candidates that are actual real words in the language's vocabulary.
- Score Candidates: Find the most likely correction using probabilities.
The Noisy Channel Model:
To select the best candidate, we use Bayes' Theorem. Let be the correct word and be the misspelled candidate (the observation). We want to find the that maximizes :
Since is constant for all candidates, we maximize:
- (Language Model): The probability that the word appears in the language (frequency in a corpus). This favors common words.
- (Error Model): The probability that the user types when they intended to type . This relies on edit distance statistics (e.g., how often is 'a' typed as 's'?).
2. Part of Speech (POS) Tagging and Hidden Markov Models
POS tagging is the process of assigning a grammatical category (Noun, Verb, Adjective, etc.) to each word in a text corpus. This is difficult because many words are ambiguous (e.g., "book" can be a noun or a verb).
2.1 About Markov Chains
A Markov Chain is a stochastic model describing a sequence of possible events.
Key Properties:
- Markov Assumption: The probability of a future state depends only on the current state, not on the sequence of events that preceded it.
- States (): A finite set of states (e.g., sunny, rainy).
- Transition Matrix (): Contains probabilities of moving from state to state .
2.2 Hidden Markov Models (HMMs)
In a Markov Chain, states are visible. In an HMM, the states are hidden (we cannot see them directly), but they produce observations (which we can see).
Components of an HMM in POS Tagging:
- Hidden States (): The Part of Speech tags (Noun, Verb, Det, etc.).
- Observations (): The actual words in the sentence.
- Transition Probabilities (): The probability of one tag following another (e.g., likelihood of a Noun following a Determiner).
- Emission Probabilities (): The probability of a specific word being generated given a specific tag (e.g., likelihood that the tag "Verb" emits the word "run").
- Initial Probabilities (): The probability of a sentence starting with a specific tag.
2.3 Part-Of-Speech Tags using a Text Corpus
To build an HMM for POS tagging, we need to calculate the Transition () and Emission () matrices using a labeled text corpus (a large dataset where linguists have already tagged the words).
Calculating Transition Probabilities:
- : Count of times tag is immediately followed by tag .
- : Total count of tag in the corpus.
Calculating Emission Probabilities:
- : Count of times tag is associated with word .
- : Total count of tag .
Decoding (Viterbi Algorithm):
Once the HMM is trained (matrices calculated), we use the Viterbi Algorithm to find the most likely sequence of hidden tags for a new sentence. This algorithm uses dynamic programming to avoid computing all possible tag sequences, maximizing the path probability product of transitions and emissions.
3. Autocomplete and Language Models
Autocomplete systems suggest the completion of a word or the next word in a sentence. This relies on Language Modeling, which assigns probabilities to sequences of words.
3.1 N-gram Language Models
An N-gram is a contiguous sequence of items from a given sample of text.
- Unigram (N=1): Individual words ("I", "love", "coding"). Assumes word independence.
- Bigram (N=2): Pairs of words ("I love", "love coding").
- Trigram (N=3): Triplets ("I love coding").
Calculating Sequence Probabilities:
The goal is to calculate . Using the Chain Rule of Probability:
Because calculating probabilities with long histories is computationally expensive and suffers from data sparsity, we apply the Markov Assumption to N-grams. For a Bigram model, the probability of a word depends only on the previous word:
3.2 Autocomplete Language Model using a Text Corpus
To build an autocomplete system, we calculate probabilities based on counting frequencies in a corpus.
Maximum Likelihood Estimation (MLE):
For a Bigram model, the probability of word following is:
The Problem of Sparsity (Zero Probability):
If a specific sequence (e.g., "purple elephant") never appears in the training corpus, the probability becomes 0. If this is part of a longer sentence, the probability of the entire sentence becomes 0.
Smoothing (Laplace Smoothing / Add-k):
To fix sparsity, we add a small number (usually 1, denoted as ) to the numerator and adjust the denominator by the vocabulary size ().
Autocomplete Implementation:
Given a history (the words typed so far), the system looks for the word that maximizes .
4. Word Embeddings with Neural Networks
Traditional NLP represented words as discrete atomic symbols (e.g., "hotel" = ID 452). This approach lacks the notion of similarity. Word embeddings solve this by representing words as continuous vectors.
4.1 Word Embeddings
A word embedding is a learned representation where words that have the same meaning have a similar representation. They are dense vectors (typically 50 to 300 dimensions) of real numbers.
One-Hot Encoding vs. Embeddings:
- One-Hot: A vector of length (vocabulary size) with a single '1' and all other zeros.
- Sparse, high-dimensional.
- No relationship between words (dot product of any two different words is 0).
- Embedding: A vector of length (e.g., 300).
- Dense, low-dimensional.
- Captures relationships.
Training with Neural Networks:
Embeddings are typically learned using "self-supervised" learning on large text corpora. The neural network tries to predict a word given its context (or vice versa).
- Word2Vec (Mikolov et al.):
- CBOW (Continuous Bag of Words): Predicts the target word based on context words.
- Skip-gram: Predicts context words given a target word.
4.2 Semantic Meaning of Words
The primary power of embeddings is that the geometric distance between vectors corresponds to semantic similarity.
Properties:
- Similarity: Words like "frog" and "toad" will have vectors that are very close to each other in vector space.
- Analogies: Algebraic operations on vectors produce semantic results.
- The Classic Example:
- This implies the vector direction for "Gender" or "Royalty" is consistent across the space.
- The Classic Example:
Cosine Similarity:
To measure how similar two words (vectors and ) are, we calculate the cosine of the angle between them:
- Value is 1 if vectors point in exactly the same direction (identical meaning).
- Value is 0 if vectors are orthogonal (unrelated).
- Value is -1 if vectors are opposite.