-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal-rank.py
282 lines (223 loc) · 7.62 KB
/
final-rank.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# coding: utf-8
# In[1]:
import string
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import re
import csv
# ### Tag Tweets as HI,EN,CME,CMH or CMEQ
# In[2]:
tweet_types = {}
meta = {}
with open('Datasheet.csv','r') as f:
for x in f:
tuples=x.split(',')
en_count=0
hi_count=0
total_word_count=0
other_count=0
ne_count=0
meta_data=[]
for i in range(1, len(tuples)) :
r = tuples[i].split(':')
s_index = int(r[0])
e_index = int(r[1])
w_type = str(r[2])
count = 1
meta_data.append((s_index,e_index,w_type))
if(w_type == 'EN'):
en_count+=count
elif w_type == 'HI' :
hi_count+= count
elif w_type == 'OTHER' :
other_count+=count
elif w_type == 'NE' :
ne_count+=count
meta[tuples[0]]=meta_data
total_word_count=en_count+hi_count
en_ratio = float(en_count)/float(total_word_count)
hi_ratio = float(hi_count)/float(total_word_count)
t='None'
if(en_ratio>.9):
t='EN'
elif hi_ratio > .9:
t='HI'
elif hi_ratio > .5:
t='CMH'
elif en_ratio > .5:
t='CME'
elif en_ratio == .5:
t='CMEQ'
tweet_types[tuples[0]]= t
# In[3]:
filtered_tweets={}
with open('data.csv','rU') as f:
reader = csv.reader(f, delimiter=',')
for tweet_id,user_id,tweet in reader:
filtered_words = []
for s,e,t in meta[tweet_id]:
word = tweet[s-1:e]
if t=='HI' and word.lower().startswith('main'):
continue
filtered_words.append(word)
filtered_tweets[tweet_id]=' '.join(filtered_words)
# ### Tokenize, filter stopwords and stem
# In[4]:
PUNCTUATION = set(string.punctuation)
STOPWORDS = set(stopwords.words('english'))
STEMMER = PorterStemmer()
# Function to break text into "tokens", lowercase them, remove punctuation and stopwords, and stem them
def tokenize(text):
tokens = word_tokenize(re.sub('[^A-Za-z0-9]+', ' ', text))
lowercased = [t.lower() for t in tokens]
no_punctuation = []
for word in lowercased:
punct_removed = ''.join([letter for letter in word if not letter in PUNCTUATION])
no_punctuation.append(punct_removed)
no_stopwords = [w for w in no_punctuation if not w in STOPWORDS]
stemmed = [STEMMER.stem(w) for w in no_stopwords]
return [w for w in stemmed if w]
# ### Words to be ranked
#
# Read the words to be ranked from input.txt
# In[5]:
print 'Reading the words to be ranked from input.txt'
stemmed_key_words = []
key_words = []
with open('input.txt','rU') as f:
reader = csv.reader(f)
for key_word, in reader:
key_words.append(key_word)
stemmed_key_words.append(tokenize(key_word)[0])
# # Identify required HashTags
# In[6]:
req_hash_tags=set()
with open('data.csv','rU') as f:
reader = csv.reader(f, delimiter=',')
for tweet_id,user_id,tweet in reader:
words = tweet.split(" ")
hash_tags = []
for word in words:
word = word.lower()
if word.startswith('#'):
hash_tags.append(word)
if len(hash_tags)>0:
words = tokenize(tweet)
for word in words:
if word in stemmed_key_words:
for hash_tag in hash_tags:
req_hash_tags.add(hash_tag)
break
# # User Metric based on HI and CMH HashTags only
# In[7]:
word_users = {}
with open('data.csv','rU') as f:
reader = csv.reader(f, delimiter=',')
for tweet_id,user_id,tweet in reader:
words = tweet.split(" ")
hash_tag_present=False
for word in words:
if(word.startswith('#')):
if word in req_hash_tags:
hash_tag_present=True
break
if (tweet_types[tweet_id]!='HI' and tweet_types[tweet_id]!= 'CMH') and hash_tag_present==False:
continue
words = tokenize(filtered_tweets[tweet_id])
for word in words:
word = word.lower()
if word in stemmed_key_words :
tweet_type = tweet_types[tweet_id]
if word not in word_users:
word_users[word] = {}
if tweet_type not in word_users[word]:
word_users[word][tweet_type] = set([user_id])
else:
word_users[word][tweet_type].add(user_id)
# In[8]:
user_metric_dict = {}
u_en_dict = {}
u_hi_dict = {}
u_cmh_dict = {}
for word,type_user_counts in word_users.items():
u_en = 0.0
if 'EN' in type_user_counts:
u_en = float(len(type_user_counts['EN']))
u_hi = 0.0
if 'HI' in type_user_counts:
u_hi = float(len(type_user_counts['HI']))
u_cmh = 0.0
if 'CMH' in type_user_counts:
u_cmh = float(len(type_user_counts['CMH']))
if u_en == 0:
print word
u_en = 1
user_metric = ((u_hi+u_cmh)/u_en)
u_en_dict[word] = u_en
u_hi_dict[word] = u_hi
u_cmh_dict[word] = u_cmh
user_metric_dict[word] = user_metric
# # Tweet Metric based on Hindi and CMH Hash Tags
# In[9]:
word_tweets = {}
with open('data.csv','rU') as f:
reader = csv.reader(f, delimiter=',')
for tweet_id,user_id,tweet in reader:
words = tweet.split(" ")
hash_tag_present=False
for word in words:
if(word.startswith('#')):
if word in req_hash_tags:
hash_tag_present=True
break
if (tweet_types[tweet_id]!='HI' and tweet_types[tweet_id]!= 'CMH') and hash_tag_present==False:
continue
words = tokenize(filtered_tweets[tweet_id])
for word in words:
word = word.lower()
if word in stemmed_key_words :
tweet_type = tweet_types[tweet_id]
if word not in word_tweets:
word_tweets[word] = {}
if tweet_type not in word_tweets[word]:
word_tweets[word][tweet_type] = set([tweet_id])
else:
word_tweets[word][tweet_type].add(tweet_id)
# In[10]:
tweet_metric_dict = {}
t_en_dict = {}
t_hi_dict = {}
t_cmh_dict = {}
for word,type_tweet_counts in word_tweets.items():
t_en = 0.0
if 'EN' in type_tweet_counts:
t_en = float(len(type_tweet_counts['EN']))
t_hi = 0.0
if 'HI' in type_tweet_counts:
t_hi = float(len(type_tweet_counts['HI']))
t_cmh = 0.0
if 'CMH' in type_tweet_counts:
t_cmh = float(len(type_tweet_counts['CMH']))
if t_en == 0:
print word
t_en = 1
tweet_metric = ((t_hi+t_cmh)/t_en)
t_en_dict[word] = t_en
t_hi_dict[word] = t_hi
t_cmh_dict[word] = t_cmh
tweet_metric_dict[word] = tweet_metric
# ### Final metric calculation
# Final metric is calculated as mean of tweet metric and user metric
# In[19]:
final_ranks = []
for key_word,stemmed_key_word in zip(key_words,stemmed_key_words):
final_metric = (user_metric_dict[stemmed_key_word]+tweet_metric_dict[stemmed_key_word])/2
final_ranks.append((key_word,final_metric))
# In[20]:
word_rank_list = [s[0] for s in sorted(final_ranks,key=lambda l:l[1],reverse=True)]
# ### Write output to the file
# In[43]:
with open('output.csv','w') as f:
writer = csv.writer(f)
writer.writerows([(e[1],e[0]+1) for e in enumerate(word_rank_list)])