All problems
Sentiment Lexicon Score
Problem
Return the sum of lexicon polarities for the tokens present, divided by the total token count.
Tokens missing from the lexicon contribute 0, and an empty token list gives 0.0.
Hint
Polarities can be any real weights, so the score can exceed 1 or drop below 0.
Test cases
case 1
in: [["i","love","this"],{"love":1,"hate":-1}]
expected: 0.3333333333333333
case 2
in: [["i","hate","it"],{"love":1,"hate":-1}]
expected: -0.3333333333333333
case 3
in: [["love","hate"],{"love":1,"hate":-1}]
expected: 0
case 4
in: [[],{"a":1}]
expected: 0
case 5
in: [["ok"],{"ok":2}]
expected: 2
Starter codePython
def sentiment_lexicon_score(tokens, lexicon):
# Your code here
pass5 test cases · run your code against them in the editor