Skip to content

Instantly share code, notes, and snippets.

@honnibal
Created February 14, 2016 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honnibal/9d6316e6a929d73eb78a to your computer and use it in GitHub Desktop.
Save honnibal/9d6316e6a929d73eb78a to your computer and use it in GitHub Desktop.
# Simple sentiment analysis with lots and lots of problems. For answer to Quora thread:
# https://www.quora.com/Would-it-be-possible-for-an-undergraduate-like-me-to-create-a-sentiment-analysis-program
import sys
from collections import counter
with open(sys.argv[1]) as file_:
positive_text = file_.read()
with open(sys.argv[2]) as file_:
negative_text = file_.read()
with open(sys.argv[3]) as file_:
problem_text = file_.read()
positive_freqs = Counter(positive_text.split())
negative_freqs = Counter(negative_text.split())
problem_freqs = Counter(problem_text.split())
positive_score = 0.0
negative_score = 0.0
for word, freq in problem_freqs.items():
pos_freq = positive_freqs.get(word, 0.01)
neg_freq = negative_freqs.get(word, 0.01)
positive_score += freq * (pos_freq / (pos_freq + neg_freq))
negative_score += freq * (neg_freq / (pos_freq + neg_freq))
if positive_score >= negative_score:
print("It's positive!", positive_score, negative_score)
else:
print("It's negative!", positive_score, negative_score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment