N-gram language models help computers guess the next word in a sentence by looking at the last few words. This makes talking to machines feel more natural.
N-gram language models in NLP
Start learning this pattern below
Jump into concepts and practice - no test required
def n_gram_model(text, n): tokens = text.split() n_grams = [tuple(tokens[i:i+n]) for i in range(len(tokens)-n+1)] return n_grams
The function splits text into words and groups them into sequences of length n.
Each group is called an n-gram, like pairs (bigrams) or triples (trigrams).
n_gram_model('I love machine learning', 2)
n_gram_model('Hello world', 1)
n_gram_model('Data science is fun', 3)
This program splits a sentence into bigrams (pairs of words), counts how often each pair appears, and prints the counts.
from collections import Counter def n_gram_model(text, n): tokens = text.lower().split() n_grams = [tuple(tokens[i:i+n]) for i in range(len(tokens)-n+1)] return n_grams # Sample text text = 'I love machine learning and I love coding' # Create bigrams bigrams = n_gram_model(text, 2) # Count frequency of each bigram bigram_counts = Counter(bigrams) print('Bigrams and their counts:') for bigram, count in bigram_counts.items(): print(f'{bigram}: {count}')
N-gram models are simple but can miss meaning because they only look at a few words at a time.
They work best with lots of text to learn common word patterns.
Higher n (like 3 or 4) means more context but needs more data and computing power.
N-gram models group words into sequences to predict or analyze text.
They are easy to build and useful for simple language tasks.
Counting n-grams helps understand common word patterns in text.
Practice
Solution
Step 1: Understand the purpose of n-gram models
N-gram models look at sequences of words to predict what comes next.Step 2: Identify the main function
They use previous words to guess the next word in a sentence.Final Answer:
Predict the next word based on previous words -> Option AQuick Check:
N-gram models predict next word = A [OK]
- Confusing n-gram with translation models
- Thinking n-grams generate images
- Mixing up sentiment analysis with n-grams
'I love AI'?Solution
Step 1: Understand bigrams
Bigrams are pairs of consecutive words in a sentence.Step 2: Extract bigrams from 'I love AI'
The pairs are ('I', 'love') and ('love', 'AI'). ('I', 'love') shows a correct bigram.Final Answer:
('I', 'love') -> Option BQuick Check:
Bigram = consecutive word pairs = C [OK]
- Including three words instead of two
- Mixing word order in pairs
- Selecting non-consecutive words
'the cat sat on the mat', what is the count of the trigram ('the', 'cat', 'sat')?Solution
Step 1: Identify trigrams in the sentence
Trigrams are sequences of three consecutive words. The trigrams are: ('the', 'cat', 'sat'), ('cat', 'sat', 'on'), ('sat', 'on', 'the'), ('on', 'the', 'mat').Step 2: Count the trigram ('the', 'cat', 'sat')
This trigram appears once at the start of the sentence.Final Answer:
1 -> Option CQuick Check:
Trigram count = 1 [OK]
- Counting non-consecutive words
- Confusing bigrams with trigrams
- Overcounting repeated words
words = ['hello', 'world', 'hello'] bigrams = [(words[i], words[i+1]) for i in range(len(words))]
What error will this code produce?
Solution
Step 1: Analyze the loop range
The loop runs from 0 to len(words)-1, which is 0 to 2 for 3 words.Step 2: Check index access inside loop
At i=2, words[i+1] tries to access words[3], which is out of range, causing IndexError.Final Answer:
IndexError: list index out of range -> Option DQuick Check:
Loop index exceeds list length = D [OK]
- Using full length in range causing out-of-bounds
- Assuming no error without testing
- Confusing syntax errors with runtime errors
Solution
Step 1: Understand sparse data in n-gram models
Rare trigrams cause zero or low counts, making predictions unreliable.Step 2: Identify smoothing techniques
Smoothing like Laplace adds small counts to all n-grams, reducing zero probabilities and improving predictions.Final Answer:
Use smoothing methods like Laplace smoothing -> Option AQuick Check:
Smoothing reduces sparse data issues = A [OK]
- Increasing n worsens sparsity
- Removing rare n-grams loses useful info
- Ignoring sparsity leads to poor predictions
