Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
To create bigrams, we pair each word with the next one, so the index difference is 1.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or 3 causes incorrect grouping or index errors.
Using 0 pairs the same word multiple times.
✗ Incorrect
For trigrams, the third word is two positions ahead of the first, so the index difference is 2.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The range should stop before the last index to avoid index error; the index difference is 1 for bigrams.
4fill in blank
hardFill 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'
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.
✗ Incorrect
We count each unique word, so we use 'word' for counting and 'set(words)' to get unique words.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The index difference for bigrams is 1; to check the first word in the tuple, use index 0; filter for 'machine'.