-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFairSumm.py
More file actions
353 lines (292 loc) · 10.9 KB
/
FairSumm.py
File metadata and controls
353 lines (292 loc) · 10.9 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
'''
Code of FairSumm fair text summarization algorithm
By: adash
'''
from nltk.stem.porter import *
import sys
reload(sys)
sys.setdefaultencoding("latin-1")
import re,os
import math
import string
from collections import defaultdict
from copy import deepcopy
import subprocess
import pandas as pd
import argparse
import zipfile
def parse_args():
'''
Parses the FairSumm arguments.
'''
parser = argparse.ArgumentParser(description="Run FairSumm.")
parser.add_argument('--file', nargs='?', default='Claritin.txt', help= 'Enter the file name containing the input file name, fairness notion and the length of the summary. a) Claritin.txt or b) US-Election.txt or c) METOO.txt. Default is Claritin.txt')
parser.add_argument('--evaluation', nargs = '?', type=int, default = 0, help='If 1, the code evaluates ROUGE scores for the generated summary. If 0, it does not evaluate the ROUGE scores. Default value is 0.')
return parser.parse_args()
args = parse_args()
Threshold = {}
print(args)
if args.evaluation not in {0, 1}:
print('Please enter either 0 or 1 for evaluation')
exit(0)
print(type(args.evaluation))
#exit(0)
inputfile = open('./'+args.file, 'r').readlines()
inpt = inputfile[0].split('<||>')[1].strip()
length = int(inputfile[1].split('<||>')[1].strip())
num_class = int(inputfile[2].split('<||>')[1].strip())
for i in range(3, 3+num_class):
content = inputfile[i].split('<||>')
Threshold[content[0]] = int(content[1].strip())
if length != sum(list(Threshold.values())):
print('Set the thresholds such that sum of tweets of each classes will add up to the length of the desired summary.')
exit(0)
st = PorterStemmer()
if not os.path.exists("./Dataset/"+inpt):
print('Enter the dataset name properly. You can check it by executing python FairSumm.py --help on your terminal')
exit(0)
zf = zipfile.ZipFile('./Dataset/'+inpt+'/cosinescores.zip')
A = pd.read_csv(zf.open('cosinescores.csv'))
#A = pd.read_csv('./Dataset/'+inpt+'/cosinescores.csv')
corpus_sim = list(A.iloc[:, 1:].sum(axis = 1))
print(sum(corpus_sim))
pattern=re.compile(r'[\d+\.]*[\d]+|[^\w]+') #pattern to detect numbers (real/integer) non alphanumeric (no underscore)
Summary = []
lamda = 0.5
inf = 21
#stopword dictionary from "stopwords.txt" file
stopWordDict = defaultdict(int)
stopWordFile = open("./Dataset/stopwords.txt","r")
for line in stopWordFile:
stopWordDict[line.strip()]=1
sensitive_info = {}
Tweets = []
Tweetids = {}
with open('./Dataset/'+inpt+'/input.txt', 'r') as file:
for line in file.readlines():
sensitive_info[line.split('<||>')[2].strip()] = line.split('<||>')[1]
Tweets.append(line.split('<||>')[2].strip())
Tweetids[line.split('<||>')[2].strip()] = line.split('<||>')[0]
def extractDocumentCorpus():
document_to_sentence_corpus = {}
Filename = './Temp/temptext.txt'
with open(Filename,'w') as file:
for tweet in Tweets:
file.write(tweet+'\n')
print('Writing done')
fileptr = open(Filename,'r')
fileText = fileptr.read()
fileptr.close()
document_to_sentence_corpus['source'] = fileText
return document_to_sentence_corpus
def generateClusterInputFile(corpus):
ClusterInputFile = "./Temp/SentencesToCluster.txt"
ClusterInputFile_ptr = open(ClusterInputFile,'w')
for each_doc in corpus:
current_doc = corpus[each_doc]
sentences = []
sentences = current_doc.split('\n')
print each_doc
for each_sentence in sentences:
if len(each_sentence)>1:
if each_sentence[0]==' ':
each_sentence = each_sentence[1:]
ClusterInputFile_ptr.write(each_sentence+'\n')
ClusterInputFile_ptr.close()
def convertFiletoMatFormat():
os.system("perl doc2mat/doc2mat -mystoplist=./Dataset/stopwords.txt -nlskip=1 -skipnumeric ./Temp/SentencesToCluster.txt ./Temp/ClutoInput.mat")
def clusterSentences():
line_count = 0
ClusterFile = open("./Temp/SentencesToCluster.txt",'r')
for line in ClusterFile.readlines():
line_count+=1
print line_count
os.system("./cluto/Linux-x86_64/vcluster -clmethod=bagglo -sim=cos -niter=100 -seed=45 ./Temp/ClutoInput.mat "+str(line_count/10))
return line_count/10
def mapSentencetoCluster(noOfClusters):
sentenceFile = open("./Temp/SentencesToCluster.txt",'r')
sentences = sentenceFile.readlines()
sentenceFile.close()
for idx in range(len(sentences)):
sentences[idx] = sentences[idx].split('\n')[0]
# Creating cluster number index.
clusterFile = open("./Temp/ClutoInput.mat.clustering."+str(noOfClusters),'r')
clusterIndex = clusterFile.readlines()
clusterFile.close()
for idx in range(len(clusterIndex)):
clusterIndex[idx] = clusterIndex[idx].split('\n')[0]
clusterSentenceIndex = []
for idx in range(len(clusterIndex)):
temp = []
temp.append(clusterIndex[idx])
temp.append(sentences[idx])
clusterSentenceIndex.append(temp)
clusterSentenceIndex.sort()
# Printing the sentences into the file.
outputIndexFile= open('./Temp/sentence-cluster-sorted-index.txt','w')
for idx in range(len(clusterSentenceIndex)):
if int(clusterSentenceIndex[idx][0]) >= 0: # Handles Unneccesary empty sentences
line = clusterSentenceIndex[idx][1]+'<||>'+clusterSentenceIndex[idx][0]+'\n'
outputIndexFile.write(line)
outputIndexFile.close()
def consolidateClusters():
clusterSentencesFile = open('./Temp/sentence-cluster-sorted-index.txt','r')
cluster_to_sentences_dict = defaultdict(list)
for line in clusterSentencesFile.readlines():
lin,cluster = line.split('<||>')
cluster = cluster.replace('\n','')
if cluster in cluster_to_sentences_dict:
cluster_to_sentences_dict[cluster].append(lin)
else:
cluster_to_sentences_dict[cluster] = [lin]
return cluster_to_sentences_dict
def calculateSimilarityWithSummary(summary):
Summary_similarity = 0
for i in summary:
if len(i)>1:
Summary_similarity += corpus_sim[Tweets.index(i)]
return Summary_similarity
def getTotalSenteces():
fp = open('Temp/sentence-cluster-sorted-index.txt','r')
text = fp.readlines()
return len(text)
def getDiversity(total_sentences, summary, cluster_to_sentences_dict):
#global cluster_to_sentences_dict
diversity_measure = 0
for cluster in cluster_to_sentences_dict:
current_cluster = cluster_to_sentences_dict[cluster]
intersection_set = set(summary).intersection(set(current_cluster))
cluster_diversity = 0
for sentence in intersection_set:
cluster_diversity += corpus_sim[Tweets.index(sentence)]/total_sentences
diversity_measure += math.sqrt(cluster_diversity)
return diversity_measure
def fair_stats(summary):
Final_Stat = {}
Classes = {}
for val in sensitive_info.values():
if val not in Classes.keys():
Classes[val] = 1
else:
Classes[val] += 1
for key in Classes:
Final_Stat[key] = 0
for sentence in summary:
if sentence not in sensitive_info.keys():
return False
else:
Final_Stat[sensitive_info[sentence]] += 1
print(Final_Stat)
# This is the segment where the current summary belongs to the matroid or not is checked (Fairness constraint)
def fairness(summary, Threshold, k):
Classes = {}
Current = {}
TruthValue = True
for val in sensitive_info.values():
if val not in Classes.keys():
Classes[val] = 1
else:
Classes[val] += 1
for key in Classes:
Current[key] = 0
#print(Current)
for sentence in summary:
if sentence not in sensitive_info.keys():
return False
else:
Current[sensitive_info[sentence]] += 1
for key in Classes.keys():
TruthValue = TruthValue and (Current[key] <= Threshold[key])
return TruthValue
def extractSummary(cluster_to_sentences_dict):
global lamda
global Summary
global inf
total_sentences = getTotalSenteces()
current_sentence = ""
current_score = 0
max_sentence = ""
max_score = 0
covereage = 0
max_cluster=-1
check_cluster=-1
curent_cov=-1
current_div=-1
max_cov=-1
max_div=-1
max_sumsin=-1
max_corsim =-1
d = max (corpus_sim)
delta = 100
L = (delta * d )/len(corpus_sim)
#Greedy implementation of sub-modular optimization of Algorithm 1.
for cluster in cluster_to_sentences_dict:
current_cluster = cluster_to_sentences_dict[cluster]
for each_sentence in current_cluster :
if (each_sentence not in Summary):
############################## Compute covereage ##############################
current_summary = deepcopy(Summary)
min_increase = d/(1+delta**(len(current_summary)+1))
if min_increase <= L:
min_increase = 0
current_summary.append(each_sentence)
if fairness(current_summary, Threshold, 50 ):
covereage = min(calculateSimilarityWithSummary(current_summary), (0.1/total_sentences) * sum(corpus_sim))
############################### Compute Diversity #############################
diversity = getDiversity(total_sentences,current_summary, cluster_to_sentences_dict)
############################### Greedily Check ################################
current_score = (lamda*covereage) + (lamda*diversity)
current_sentence = each_sentence
check_cluster =cluster
if current_score - max_score >= min_increase:
max_score = current_score
max_sentence= current_sentence
max_cluster=check_cluster
max_cov=covereage
max_div=diversity
Summary.append(max_sentence)
print(len(Summary))
#
def main():
if not os.path.exists("./Temp"):
os.makedirs("./Temp")
# cluster the input sentences for diversity evaluations
document_to_sentence_corpus = extractDocumentCorpus()
generateClusterInputFile(document_to_sentence_corpus)
convertFiletoMatFormat()
noOfClusters = clusterSentences()
print "No of clusters "
print noOfClusters
mapSentencetoCluster(noOfClusters)
cluster_to_sentences_dict = consolidateClusters()
# Start summarizing procedure
print('Starting to create the summary')
for i in xrange(length):
extractSummary(cluster_to_sentences_dict)
#Store the summary output
if not os.path.exists("./Summaries"):
os.makedirs("./Summaries")
filename = "./Summaries/"+inpt+".txt"
outfile = open(filename,'w')
print Summary
for i in Summary:
outfile.write(i+'\n')
outfile.close()
#Show the fairness statistics of the summary
fair_stats(Summary)
#Rouge evaluation
if args.evaluation==1:
output = subprocess.check_output("java -cp C_Rouge/C_ROUGE.jar executiverouge.C_ROUGE "+ filename +" ./Dataset/"+inpt+"/Test_Summaries"+"/ 1 B R",shell=True)
output = float(output)
output1 = subprocess.check_output("java -cp C_Rouge/C_ROUGE.jar executiverouge.C_ROUGE "+ filename +" ./Dataset/"+inpt+"/Test_Summaries"+"/ 1 B F",shell=True)
output1 = float(output1)
output2 = subprocess.check_output("java -cp C_Rouge/C_ROUGE.jar executiverouge.C_ROUGE "+ filename +" ./Dataset/"+inpt+"/Test_Summaries"+"/ 2 B R",shell=True)
output2 = float(output2)
output3 = subprocess.check_output("java -cp C_Rouge/C_ROUGE.jar executiverouge.C_ROUGE "+ filename +" ./Dataset/"+inpt+"/Test_Summaries"+"/ 2 B F",shell=True)
output3 = float(output3)
main_output_file = open("Final_Output.txt",'a')
main_output_file.write(inpt+'_'+"\t"+str(output)+"\t"+str(output1)+"\t"+str(output2)+"\t"+str(output3)+"\n")
print "\t"+str(output)+"\t"+str(output1)
main_output_file.close()
if __name__ == '__main__':
main()