Created
October 28, 2012 20:36
-
-
Save fjavieralba/3969825 to your computer and use it in GitHub Desktop.
Basic measure of sentiment score of a tagged text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def value_of(sentiment): | |
if sentiment == 'positive': return 1 | |
if sentiment == 'negative': return -1 | |
return 0 | |
def sentiment_score(review): | |
return sum ([value_of(tag) for sentence in dict_tagged_sentences for token in sentence for tag in token[2]]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sentiment_score(dict_tagged_sentences) | |
-4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sentiment_score(dict_tagged_sentences) | |
-7.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sentiment_score(dict_tagged_sentences) | |
-5.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sentence_score(sentence_tokens, previous_token, acum_score): | |
if not sentence_tokens: | |
return acum_score | |
else: | |
current_token = sentence_tokens[0] | |
tags = current_token[2] | |
token_score = sum([value_of(tag) for tag in tags]) | |
if previous_token is not None: | |
previous_tags = previous_token[2] | |
if 'inc' in previous_tags: | |
token_score *= 2.0 | |
elif 'dec' in previous_tags: | |
token_score /= 2.0 | |
elif 'inv' in previous_tags: | |
token_score *= -1.0 | |
return sentence_score(sentence_tokens[1:], current_token, acum_score + token_score) | |
def sentiment_score(review): | |
return sum([sentence_score(sentence, None, 0.0) for sentence in review]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sentence_score(sentence_tokens, previous_token, acum_score): | |
if not sentence_tokens: | |
return acum_score | |
else: | |
current_token = sentence_tokens[0] | |
tags = current_token[2] | |
token_score = sum([value_of(tag) for tag in tags]) | |
if previous_token is not None: | |
previous_tags = previous_token[2] | |
if 'inc' in previous_tags: | |
token_score *= 2.0 | |
elif 'dec' in previous_tags: | |
token_score /= 2.0 | |
return sentence_score(sentence_tokens[1:], current_token, acum_score + token_score) | |
def sentiment_score(review): | |
return sum([sentence_score(sentence, None, 0.0) for sentence in review]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment