Skip to content

Instantly share code, notes, and snippets.

@fjavieralba
Created October 28, 2012 20:36
Show Gist options
  • Save fjavieralba/3969825 to your computer and use it in GitHub Desktop.
Save fjavieralba/3969825 to your computer and use it in GitHub Desktop.
Basic measure of sentiment score of a tagged text
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]])
sentiment_score(dict_tagged_sentences)
-4
sentiment_score(dict_tagged_sentences)
-7.0
sentiment_score(dict_tagged_sentences)
-5.0
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])
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