0
0
NLPml~10 mins

Sentiment with context (sarcasm, negation) 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 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'
Atransform
Bfit_transform
Cfit
Dtoarray
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform before fitting the vectorizer.
Using fit without transforming the texts.
2fill in blank
medium

Complete 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'
Afit
Bscore
Cpredict
Dtransform
Attempts:
3 left
💡 Hint
Common Mistakes
Using predict before training the model.
Using transform which is not a model method.
3fill in blank
hard

Fix 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'
Afit_transform
Bpredict
Ctransform
Dfit
Attempts:
3 left
💡 Hint
Common Mistakes
Using fit_transform on new data which changes the vocabulary.
Using fit which does not return transformed data.
4fill in blank
hard

Fill 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'
Ain
Bnot in
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' which checks the opposite condition.
Adding 1 instead of subtracting to flip sentiment.
5fill in blank
hard

Fill 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'
A"yeah right"
B"as if"
Cin
Dnot in
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' which checks the opposite condition.
Not quoting the sarcasm phrases as strings.