All problems
Aspect Sentiment Pairs
Problem
For each aspect word occurrence, sum the polarities of all lexicon-scored tokens within window positions on either side, inclusive of the aspect itself.
Return [aspect, score] pairs in order of occurrence. Aspects not present in the lexicon still produce a pair with score 0.0.
Hint
Clamp the window range to the sequence boundaries before summing.
Test cases
case 1
in: [["food","was","great"],["food"],{"great":1},2]
expected: [["food",1]]
case 2
in: [["bad","food","bad"],["food"],{"bad":-1},1]
expected: [["food",-2]]
case 3
in: [["food"],["food"],{},3]
expected: [["food",0]]
case 4
in: [["a","food","b","food"],["food"],{"a":1,"b":2},1]
expected: [["food",3],["food",2]]
case 5
in: [["nope"],["food"],{"nope":1},1]
expected: []
Starter codePython
def aspect_sentiment_pairs(tokens, aspects, lexicon, window):
# Your code here
pass5 test cases · run your code against them in the editor