0
0
NLPml~10 mins

N-grams 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
words = ['I', 'love', 'machine', 'learning']
bigrams = [(words[i], words[i+[1]]) for i in range(len(words)-1)]
print(bigrams)
Drag options to blanks, or click blank then click option'
A1
B2
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 instead of 1 causes index errors or skips words.
Using 0 pairs the same word with itself.
2fill in blank
medium

Complete the code to generate trigrams from a list of words.

NLP
words = ['deep', 'learning', 'is', 'fun']
trigrams = [(words[i], words[i+1], words[i+[1]]) for i in range(len(words)-2)]
print(trigrams)
Drag options to blanks, or click blank then click option'
A3
B1
C0
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or 3 causes incorrect grouping or index errors.
Using 0 pairs the same word multiple times.
3fill in blank
hard

Fix the error in the code to correctly generate bigrams.

NLP
words = ['AI', 'is', 'awesome']
bigrams = [(words[i], words[i+[1]]) for i in range(len(words)-1)]
print(bigrams)
Drag options to blanks, or click blank then click option'
A0
B2
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using range(len(words)) causes index error when accessing words[i+1].
Using index difference other than 1 breaks bigram logic.
4fill in blank
hard

Fill both blanks to create a dictionary of word counts from a list.

NLP
words = ['data', 'science', 'data', 'AI']
word_counts = {word: words.count([1]) for word in [2]
print(word_counts)
Drag options to blanks, or click blank then click option'
Aword
Bwords
Cset(words)
Dlist(words)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' instead of 'word' counts the whole list repeatedly.
Using 'words' instead of 'set(words)' causes duplicate keys.
5fill in blank
hard

Fill all three blanks to filter bigrams that start with 'machine'.

NLP
words = ['machine', 'learning', 'is', 'fun']
bigrams = [(words[i], words[i+[1]]) for i in range(len(words)-1)]
filtered = [bigram for bigram in bigrams if bigram[[2]] == [3]]
print(filtered)
Drag options to blanks, or click blank then click option'
A1
B0
C'machine'
D'learning'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 checks the second word instead of the first.
Using wrong index difference breaks bigram creation.
Filtering for 'learning' instead of 'machine' changes the result.