-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_labels.py
261 lines (216 loc) · 7.93 KB
/
generate_labels.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
import pdb
import argparse
from nltk.tokenize import sent_tokenize
import time
import Queue
from threading import Thread
from collections import namedtuple
import cPickle as pkl
import glob
import numpy
from random import shuffle, sample
import re
# Import pythonrouge package
from pythonrouge import PythonROUGE
ROUGE_path = "/qydata/ywubw/download/RELEASE-1.5.5"
# Input data format
url_tag = "<url>"
document_tag = "<doc>"
summary_tag = "<summary>"
paragraph_sep = "</p>"
sentence_sep = "</s>"
re_sent_sep = sentence_sep + "|" + paragraph_sep
ExtSummary = namedtuple('ExtSummary',
'doc_sents summary_sents selected_ids rouge_2')
DocSummary = namedtuple('DocSummary',
'url document summary extract_ids rouge_2')
# Use SSD for acceleration
temp_root = ""
select_window = 10
def plot_hist(count, title):
import matplotlib.pyplot as plt
plt.hist(count, bins='auto')
plt.title(title)
plt.show()
def greedy_selection(doc_sent_list, ref_sent_list, rouge):
selected = set()
selected_sents = []
max_rouge = 0.0
current_candidates = range(len(doc_sent_list))
while True:
max_idx = -1
id_score_list = []
for i in current_candidates:
if i not in selected:
current_sents_ids = selected_sents + [(i, doc_sent_list[i])]
current_sents = [
t[1] for t in sorted(current_sents_ids, key=lambda s: s[0])
]
rouge_dict = rouge.evaluate([[current_sents]], [[ref_sent_list]], True)
rouge_score = rouge_dict["ROUGE-2-F"]
id_score_list.append((i, rouge_score))
if rouge_score > max_rouge:
max_idx = i
max_rouge = rouge_score
if max_idx >= 0:
selected.add(max_idx)
selected_sents.append((max_idx, doc_sent_list[max_idx]))
selected_sents = sorted(selected_sents, key=lambda s: s[0])
else:
break
top_ids_scores = sorted(
id_score_list, key=lambda x: x[1], reverse=True)[:select_window]
current_candidates = [t[0] for t in top_ids_scores]
return sorted(list(selected)), max_rouge
def generate_thread(doc_summary_queue, output_queue):
rouge = PythonROUGE(
ROUGE_path,
n_gram=2,
ROUGE_SU4=False,
ROUGE_L=False,
stemming=True,
stopwords=False,
length_limit=False,
length=75,
word_level=False,
use_cf=True,
cf=95,
ROUGE_W=False,
scoring_formula="average",
resampling=False,
samples=1000,
favor=False,
p=0.5)
count = 0
while not doc_summary_queue.empty():
url, doc_sents, ref_sents = doc_summary_queue.get()
try:
selected, score = greedy_selection(doc_sents, ref_sents, rouge)
# print selected, score
except Exception as e:
print e
continue
result = DocSummary(url, doc_sents, ref_sents, selected, score)
output_queue.put(result)
count += 1
if count % 100 == 0:
print "Processed %d documents." % count
def generate_labels(in_path, out_path, num_threads):
doc_summary_queue = Queue.Queue()
output_queue = Queue.Queue()
url_start = len(url_tag)
with open(in_path, "r") as in_file:
# for l in in_file.readlines()[:10]: #TODO
for l in in_file.readlines():
url_end = l.find(document_tag)
url = l[url_start:url_end].strip()
doc_start = url_end + len(document_tag)
doc_end = l.find(summary_tag)
doc_str = l[doc_start:doc_end].strip()
doc_sent_list = re.split(re_sent_sep, doc_str)
summary_start = doc_end + len(summary_tag)
summary_str = l[summary_start:].strip()
summary_sent_list = summary_str.split(sentence_sep)
doc_summary_queue.put((url, doc_sent_list, summary_sent_list))
start_time = time.time()
generate_threads = []
for i in range(num_threads):
t = Thread(target=generate_thread, args=(doc_summary_queue, output_queue))
t.daemon = True
t.start()
generate_threads.append(t)
for t in generate_threads:
t.join()
results = list(output_queue.queue)
print "Used %fs to process %d documents." % (time.time() - start_time,
len(results))
with open(out_path, "w") as f:
pkl.dump(results, f)
print "Results writen to %s." % out_path
def merge_labels(in_path, out_path, use_shuffle, plot):
filelist = glob.glob(in_path)
dataset = []
for fn in filelist:
with open(fn, 'r') as f:
try:
split_set = pkl.load(f)
except ValueError as e:
print "Error when reading %s" % fn
raise e
dataset += split_set
print "Data size: %d" % len(dataset)
print "Computing statistics:"
doc_sent_lens, doc_lens, sum_lens, num_ext_ids, rouges = [], [], [], [], []
# Log the lengths
if type(dataset[0]) == ExtSummary:
for d in dataset:
for s in d.doc_sents:
doc_sent_lens.append(len(s.split()))
doc_lens.append(len(d.doc_sents))
sum_lens.append(len(d.summary_sents))
num_ext_ids.append(len(d.selected_ids))
rouges.append(d.rouge_2)
elif type(dataset[0]) == DocSummary:
for d in dataset:
for s in d.document:
doc_sent_lens.append(len(s.split()))
doc_lens.append(len(d.document))
sum_lens.append(len(d.summary))
num_ext_ids.append(len(d.extract_ids))
rouges.append(d.rouge_2)
# Print the statistics
print "Sentence length: mean %f stddev %f" % (numpy.mean(doc_sent_lens),
numpy.std(doc_sent_lens))
print "Document length: mean %f stddev %f" % (numpy.mean(doc_lens),
numpy.std(doc_lens))
print "Summary length: mean %f stddev %f" % (numpy.mean(sum_lens),
numpy.std(sum_lens))
print "Number of extracted ids: mean %f stddev %f" % (numpy.mean(num_ext_ids),
numpy.std(num_ext_ids))
print "ROUGE-2: mean %f stddev %f" % (numpy.mean(rouges), numpy.std(rouges))
# Plot the statistics
if plot:
plot_hist(doc_sent_lens, 'Sentence lengths')
plot_hist(doc_lens, 'Document lengths')
plot_hist(sum_lens, 'Summary lengths')
plot_hist(num_ext_ids, 'No. extracted ids')
plot_hist(rouges, 'ROUGE-2')
if use_shuffle: # random shuffle the data points
print "Shuffling..."
for _ in xrange(3):
shuffle(dataset)
with open(out_path, 'w') as f:
pkl.dump(dataset, f)
print "Merged labels written to %s" % out_path
def sample_labels(in_path, out_path, num_samples):
with open(in_path, 'r') as f:
dataset = pkl.load(f)
print "Data size: %d" % len(dataset)
sampled_dataset = sample(dataset, num_samples)
print "Sampled %d data instances." % len(sampled_dataset)
with open(out_path, 'w') as f:
pkl.dump(sampled_dataset, f)
print "Sampled dataset written to %s" % out_path
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Generate labels for extractive summarization.')
parser.add_argument('in_path', type=str, help='Path of input file.')
parser.add_argument('out_path', type=str, help='Path of output file.')
parser.add_argument(
'-m', '--mode', choices=['map', 'reduce', 'sample'], default='map')
parser.add_argument(
'-t', '--temp_root', type=str, default='', help='Root of temp files.')
parser.add_argument(
'-n', '--num_threads', type=int, default=10, help='Number of threads.')
parser.add_argument('-s', '--shuffle', action='store_true', default=False)
parser.add_argument('-p', '--plot', action='store_true', default=False)
parser.add_argument(
'--num_samples', type=int, default=0, help='Number of samples drawn.')
args = parser.parse_args()
if args.mode == "map": # generate labels for each split
temp_root = args.temp_root
generate_labels(args.in_path, args.out_path, args.num_threads)
elif args.mode == "reduce": # merge all labels from splits
merge_labels(args.in_path, args.out_path, args.shuffle, args.plot)
else:
sample_labels(args.in_path, args.out_path, args.num_samples)