Created
March 1, 2011 22:12
-
-
Save lrvick/849998 to your computer and use it in GitHub Desktop.
This file contains 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
import urllib2, json, base64, sys, termios, tty, os | |
USER = "your_twitter_user" | |
PASS = "your_twitter_pass" | |
neg_filename = "negative.txt" | |
pos_filename = "postive.txt" | |
queries = ['awesome','beautiful','shit','fuck','android','iphone','blackberry','windows','linux','apple','google'] | |
query_post = str("track="+",".join([q for q in queries])) | |
def read_key(): | |
oldattr = termios.tcgetattr(sys.stdin) | |
try: | |
tty.setraw(sys.stdin) | |
return sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(sys.stdin, termios.TCSANOW, oldattr) | |
pos_file = open(pos_filename,"w") | |
try: | |
for line in pos_file: | |
pos_count += 1 | |
except IOError: | |
pos_count = 0 | |
neg_file = open(neg_filename,"w") | |
try: | |
for line in neg_file: | |
neg_count += 1 | |
except IOError: | |
neg_count = 0 | |
httprequest = urllib2.Request('http://stream.twitter.com/1/statuses/filter.json',query_post) | |
auth = base64.b64encode('%s:%s' % (USER, PASS)) | |
httprequest.add_header('Authorization', "basic %s" % auth) | |
stream = urllib2.urlopen(httprequest) | |
for item in stream: | |
data = json.loads(item) | |
if data.get('user',None): | |
os.system("clear") | |
tweet_text = data['text'].encode('utf8') | |
print('Synt sentiment trainer | Totals: Postive (%s) Negative (%s)' % (pos_count,neg_count)) | |
print('\n') | |
print tweet_text | |
print('\n') | |
print("Please choose sentiment for this tweet:") | |
print(" 1 = Positive, 2 = Neutral, 3 = Negative, q = Quit ) > ") | |
key = read_key() | |
if key == 'q': | |
print('\n') | |
print("You excaped the dungeon!") | |
break | |
if key == '1': | |
pos_file.write("%s \n" % tweet_text) | |
pos_count += 1 | |
print('\n') | |
print("Comment saved as: Positive") | |
if key == '2': | |
print('\n') | |
print("Comment Ignored as: Neutral") | |
if key == '3': | |
neg_file.write("%s \n" % tweet_text) | |
neg_count += 1 | |
print('\n') | |
print("Comment saved as: Negative") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment