0
0
NLPml~10 mins

Python NLP ecosystem (NLTK, spaCy, Hugging Face) - Interactive Code Practice

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

Complete the code to tokenize a sentence using NLTK.

NLP
import nltk
nltk.download('punkt')
sentence = "Hello world!"
tokens = nltk.word_tokenize([1])
print(tokens)
Drag options to blanks, or click blank then click option'
A"Hello world!"
Btokens
Csentence
Dnltk
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the token list instead of the sentence string.
Passing the module name instead of the text.
2fill in blank
medium

Complete the code to load the English model in spaCy.

NLP
import spacy
nlp = spacy.load([1])
doc = nlp("This is a test.")
print([token.text for token in doc])
Drag options to blanks, or click blank then click option'
A"small_model"
B"english"
C"en"
D"en_core_web_sm"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect model names like "english" or "en" which are not valid spaCy model names.
Forgetting the quotes around the model name.
3fill in blank
hard

Fix the error in the Hugging Face pipeline code to perform sentiment analysis.

NLP
from transformers import pipeline
sentiment = pipeline([1])
result = sentiment("I love learning NLP!")
print(result)
Drag options to blanks, or click blank then click option'
A"sentiment-analysis"
Bsentiment-analysis
C"sentiment_analysis"
Dsentiment_analysis
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the task name without quotes causing a NameError.
Using underscores instead of hyphens in the string.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths for words longer than 3 characters.

NLP
words = ["apple", "is", "good", "for", "you"]
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length as the dictionary value.
Using less than symbol instead of greater than in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths for words longer than 3 characters.

NLP
words = ["apple", "is", "good", "for", "you"]
result = [1]: [2] for word in words if len(word) [3] 3
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Using less than symbol instead of greater than in the condition.
Swapping keys and values.