-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweets.py
More file actions
167 lines (138 loc) · 4.06 KB
/
Tweets.py
File metadata and controls
167 lines (138 loc) · 4.06 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
# open file
import xlrd
import re
import nltk
from nltk.stem import WordNetLemmatizer as wnl
wb=xlrd.open_workbook('Tweets.xls')
Obamash=wb.sheet_by_index(0);
#Obamatweets=Obamash.col_values(4)
Romneysh=wb.sheet_by_index(1);
#Romneytweets=Romneysh.col_values(4)
#f = open ("C:\Users\Rahul\Desktop\q.txt","r")
obamaTuple= []
obamaTweet=[]
for rownum in range(Obamash.nrows):
obamaTuple=(unicode((Obamash.cell(rownum,3).value)).encode('utf-8'),(Obamash.cell(rownum,6).value))
obamaTweet.append(obamaTuple)
RomneyTuple=[]
RomneyTweet=[]
for rownum in range(Romneysh.nrows):
RomneyTuple=((Romneysh.cell(rownum,3).value),(Romneysh.cell(rownum,6).value))
RomneyTweet.append(RomneyTuple)
#Removes Punctutaions
def checkPuntuations(obamaTweet):
count=0
while count < len(obamaTweet):
#tweet=(unicode(obamaTweet[count])).encode('utf-8')
#tweet=str(tweet)
tweet=obamaTweet[count]
obamaTweet[count]=re.sub(r'[%\.\'\"\?:,;!-]',' ',tweet)
count=count+1
#Removes URLs of the form https://charecters
def checkURL(obamaTweet):
count=0
while count < len(obamaTweet):
tweet=(unicode(obamaTweet[count])).encode('utf-8')
tweet=str(tweet)
obamaTweet[count]=re.sub('http.//[\w\d\.\\/]*','',tweet)
count=count+1
#Removes the username of the form @Rahul
def checkUsername(obamaTweet):
count=0
while count < len(obamaTweet):
#tweet=(unicode(obamaTweet[count])).encode('utf-8')
#tweet=str(tweet)
tweet=obamaTweet[count]
obamaTweet[count]=re.sub('@[\w\d_]*','',tweet)
count=count+1
#Removes the hashTags
def checkHashTag(obamaTweet):
count=0
while count < len(obamaTweet):
#tweet=(unicode(obamaTweet[count])).encode('utf-8')
#tweet=str(tweet)
tweet=obamaTweet[count]
obamaTweet[count]=re.sub('#','',tweet)
count=count+1
#Removes HTML tags of the form <*>
def checkHtmlTag(obamaTweet):
count=0
while count < len(obamaTweet):
#tweet=(unicode(obamaTweet[count])).encode('utf-8')
#tweet=str(tweet)
tweet=obamaTweet[count]
obamaTweet[count]=re.sub('<.*?>','',tweet)
count=count+1
#Removes Stop Words
def checkStopWords(obamaTweet):
count=0
while count < len(obamaTweet):
#tweet=(unicode(obamaTweet[count])).encode('utf-8')
#tweet=str(tweet)
tweet=obamaTweet[count]
tweet=tweet.split()
nltkVariable=nltk.corpus.stopwords.words('english')
for word in tweet:
if word in nltkVariable:
tweet.remove(word)
obamaTweet[count]=tweet
count=count+1
def checkRepeatedWord(obamaTweet):
count=0
while count < len(obamaTweet):
#tweet=(unicode(obamaTweet[count])).encode('utf-8')
#tweet=str(tweet)
tweet=obamaTweet[count]
obamaTweet[count]=re.sub(r'([a-z])\1+',r'\1',tweet)
count=count+1
def preProcessData(tuple):
count=0
for tweet,sentiment in tuple:
#Remove HashTags
tweet=re.sub('#','',tweet)
#Remove Username like @Rahul
tweet=re.sub('@[\w\d_]*','',tweet)
#Remove URL's
tweet=re.sub('http.//[\w\d\.\\/]*','',tweet)
#Remove Puntuations
tweet=re.sub(r'[%\.\'\"\?:,;!-]',' ',tweet)
#Remove HTML Tags
tweet=re.sub('<.*?>','',tweet)
#Remove rpeadted Words
tweet=re.sub(r'([a-z])\1+',r'\1',tweet)
#Removing words that start with a number or a special character
tweet = re.sub(r'^[^a-zA-Z]+',' ',tweet)
#Convert camel Casing into space Separated word
tweet=re.sub("([a-z])([A-Z])","\g<1> \g<2>",tweet)
#Remove additional white spaces
tweet = re.sub('[\s]+', ' ', tweet)
#Remove StopWords
tweet=tweet.split()
nltkVariable=nltk.corpus.stopwords.words('english')
for word in tweet:
if word in nltkVariable:
tweet.remove(word)
#Lemmatize Words
tweet=[wnl().lemmatize(word) for word in tweet]
if count==6:
#print tweet
# tweet=re.sub('http.//[\w\d\.\\/]*','',tweet)
print tweet
count=count+1
preProcessData(obamaTweet)
#print (obamaTweet[6])
#checkURL(obamaTweet)
#checkPuntuations(obamaTweet)
#checkUsername(obamaTweet)
#checkHashTag(obamaTweet)
#checkHtmlTag(obamaTweet)
#checkRepeatedWord(obamaTweet)
#checkStopWords(obamaTweet)
#print (obamaTweet[6])
#Read whole file into data
#data = f.read()
# Print it
#print data
# Close the file
#f.close()