0
0
NLPml~10 mins

N-gram language models in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create bigrams from a list of words.

NLP
bigrams = [(words[i], words[i+[1]]) for i in range(len(words)-1)]
Drag options to blanks, or click blank then click option'
A0
B2
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 2 as the offset causes index errors or wrong pairs.
2fill in blank
medium

Complete the code to count the frequency of each trigram in the text.

NLP
from collections import Counter
trigrams = [(words[i], words[i+1], words[i+[1]]) for i in range(len(words)-2)]
trigram_counts = Counter(trigrams)
Drag options to blanks, or click blank then click option'
A1
B0
C3
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or 3 causes incorrect trigram formation or index errors.
3fill in blank
hard

Fix the error in the code that calculates the probability of a bigram using counts.

NLP
bigram_prob = bigram_counts[bigram] / [1]
Drag options to blanks, or click blank then click option'
Asum(bigram_counts.values())
Blen(bigram_counts)
Clen(bigram_counts[bigram])
Dbigram_counts[bigram[0]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using length of bigram_counts or counts of a single word causes wrong probabilities.
4fill in blank
hard

Fill both blanks to create a dictionary of bigram probabilities from counts.

NLP
bigram_probs = {bg: bigram_counts[bg] / [1] for bg in [2]
Drag options to blanks, or click blank then click option'
Asum(bigram_counts.values())
Bbigram_counts.keys()
Cbigram_counts.items()
Dlen(bigram_counts)
Attempts:
3 left
💡 Hint
Common Mistakes
Using items() instead of keys() causes errors in dictionary comprehension.
5fill in blank
hard

Fill the two blanks to build a conditional probability dictionary for bigrams.

NLP
cond_probs = {}
for (w1, w2), count in bigram_counts.items():
    w1_total = sum(c for (word1, _), c in bigram_counts.items() if word1 == [1])
    cond_probs.setdefault(w1, {})[[2]] = count / w1_total
Drag options to blanks, or click blank then click option'
Aw2
Bw1
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up w1 and w2 keys causes wrong dictionary structure or division errors.