Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a pre-trained sentiment analysis model.
NLP
from transformers import pipeline sentiment_analyzer = pipeline([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text-classification' which is more general and may not handle nuance well.
Choosing unrelated pipelines like 'ner' or translation.
✗ Incorrect
The 'sentiment-analysis' pipeline loads a model specialized for sentiment tasks.
2fill in blank
mediumComplete the code to analyze sentiment of the sentence.
NLP
result = sentiment_analyzer([1])[0] print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a plain string instead of a list causes errors.
Using parentheses creates a tuple, which is not accepted.
✗ Incorrect
The pipeline expects a list of strings as input to analyze sentiment.
3fill in blank
hardFix the error in the code to get the sentiment label.
NLP
label = result[1]['label'] print(label)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation causes AttributeError.
Using parentheses or curly braces causes syntax errors.
✗ Incorrect
Use square brackets to access dictionary keys in Python.
4fill in blank
hardFill both blanks to create a function that returns sentiment label and score.
NLP
def analyze_sentiment(text): result = sentiment_analyzer([text])[[1]] return result[2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string '0' instead of integer 0 for indexing.
Using dot notation instead of square brackets for dictionary keys.
✗ Incorrect
Index 0 gets the first result; ['label'] accesses the sentiment label.
5fill in blank
hardFill all three blanks to create a function that returns both label and score from sentiment analysis.
NLP
def analyze_sentiment_details(text): result = sentiment_analyzer([text])[[1]] label = result[2] score = result[3] return label, score
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for indexing causes IndexError.
Using dot notation instead of square brackets for dictionary keys.
✗ Incorrect
Index 0 gets the first result; ['label'] and ['score'] access the respective values.