Bird
0
0

Find the bug in this advanced sentiment analysis code that handles negations: ```python def sentiment_score(text): negations = ['not', 'never'] score = 0.8 for neg in negations: if neg in text: score = -score return score print(sentiment_score('I am not happy')) ```

medium📝 Debug Q7 of 15
NLP - Sentiment Analysis Advanced
Find the bug in this advanced sentiment analysis code that handles negations: ```python def sentiment_score(text): negations = ['not', 'never'] score = 0.8 for neg in negations: if neg in text: score = -score return score print(sentiment_score('I am not happy')) ```
AFunction missing parameter
BNegation list is empty
CNegation flips score multiple times incorrectly
DScore is never returned
Step-by-Step Solution
Solution:
  1. Step 1: Analyze negation handling loop

    Each negation word found flips the score sign again.
  2. Step 2: Check input text for multiple negations

    Text contains 'not' only, so score flips once correctly here, but if multiple negations appear, flipping multiple times causes errors.
  3. Final Answer:

    Negation flips score multiple times incorrectly -> Option C
  4. Quick Check:

    Negation flipping should happen once per sentence [OK]
Quick Trick: Flip sentiment once for all negations, not per negation word [OK]
Common Mistakes:
MISTAKES
  • Flipping score inside loop multiple times
  • Ignoring multiple negations effect
  • Assuming negation list is empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes