forked from neuralinfo/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3_2_4.py
More file actions
executable file
·42 lines (33 loc) · 1.04 KB
/
Assignment3_2_4.py
File metadata and controls
executable file
·42 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
#
# David Paculdo
# W205
# Assignment 3
import sys
import nltk
import re
import string
# positive and negative words from: http://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html
pos_words=[word.strip() for word in open("positive-words.txt").readlines()]
neg_words=[word.strip() for word in open("negative-words.txt").readlines()]
my_file=open("most_retweeted.txt")
sentiment_file=open("sentiment_analysis.txt","w")
from_string=""
# reading re-tweet file to clean up, tokenize and classify each tweet
for tweet in my_file:
tweet=tweet[3:]
tweet=tweet.translate(None,"/:#.,-'?")
sentence=""
pos_count=0
neg_count=0
for word in tweet.strip().split(" "):
if not word.startswith("http") and not word.startswith("@"):
sentence=sentence+word.lower()+" "
tokens=nltk.word_tokenize(sentence)
for token in tokens:
if token in pos_words:
pos_count+=1
if token in neg_words:
neg_count+=1
sentiment_total=pos_count-neg_count
sentiment_file.write("Sentiment Analysis: "+str(sentiment_total)+"\tOriginal Tweet: "+tweet)