Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load the text data for sentiment analysis.
NLP
texts = ["I love this movie!", "This is not good.", "Absolutely fantastic!"] labels = [1, 0, 1] from sklearn.feature_extraction.text import CountVectorizer vectorizer = CountVectorizer() X = vectorizer.[1](texts)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform before fitting the vectorizer.
Using fit without transforming the texts.
✗ Incorrect
We use fit_transform to learn the vocabulary and transform the texts into feature vectors in one step.
2fill in blank
mediumComplete the code to train a logistic regression model for sentiment classification.
NLP
from sklearn.linear_model import LogisticRegression model = LogisticRegression() model.[1](X, labels)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using predict before training the model.
Using transform which is not a model method.
✗ Incorrect
We use fit to train the model on the feature vectors and labels.
3fill in blank
hardFix the error in the code to correctly predict sentiment for new texts.
NLP
new_texts = ["I don't like this.", "What a great day!"] X_new = vectorizer.[1](new_texts) predictions = model.predict(X_new)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fit_transform on new data which changes the vocabulary.
Using fit which does not return transformed data.
✗ Incorrect
For new data, we only transform using the existing vocabulary, not fit again.
4fill in blank
hardFill both blanks to create a function that detects negation words and adjusts sentiment accordingly.
NLP
def adjust_for_negation(text, sentiment): negations = ['not', 'no', 'never', 'none'] words = text.lower().split() if any(word [1] negations for word in words): return 1 [2] sentiment return sentiment
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' which checks the opposite condition.
Adding 1 instead of subtracting to flip sentiment.
✗ Incorrect
We check if any word is in negations and then flip the sentiment by subtracting it from 1.
5fill in blank
hardFill all three blanks to build a simple sarcasm detector that flags sarcastic sentences containing 'yeah right' or 'as if'.
NLP
def detect_sarcasm(text): sarcasm_phrases = [[1], [2]] text_lower = text.lower() for phrase in sarcasm_phrases: if phrase [3] text_lower: return True return False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' which checks the opposite condition.
Not quoting the sarcasm phrases as strings.
✗ Incorrect
We list sarcasm phrases as strings and check if each phrase is in the lowercased text.