-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathquestion_exampler.py
179 lines (147 loc) · 6.57 KB
/
question_exampler.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
import os
import json
import time
from tqdm import tqdm
import copy
from searcher import search
from reader import read_pages
from src.rewriter import rewrite_query
from src.questioner import ask_news_question
from src.timeline_generator import generate_timeline
from tilse.data.timelines import Timeline as TilseTimeline
from tilse.data.timelines import GroundTruth as TilseGroundTruth
from datetime import datetime
from evaluation import evaluate_dates
from news_keywords import TARGET_KEYWORDS
def save_json(data, file_path):
with open(file_path, 'w') as file:
json.dump(data, file, indent=2, ensure_ascii=False)
def generate(input_text, model, search_engine='bing', n_max_query=6, n_max_doc=30, freshness=''):
news_timeline_all = {}
if search_engine != 'bing':
doc_list_all = search([input_text + ' timeline'], n_max_doc, search_engine)
else:
doc_list_all = search([input_text], n_max_doc, search_engine, freshness)
print(n_max_doc,len(doc_list_all))
if search_engine == 'bing':
doc_list_all = read_pages(doc_list_all)
for _ in range(10):
question_list = []
while question_list == []:
question_list = ask_news_question(model=model, news=input_text, docs=doc_list_all, questions=question_list) # question-based news background decomposition
for question in question_list:
queries = []
# print(question)
while queries == []:
queries = rewrite_query(question, n_max_query, model=model)
# if queries:
# print(queries)
if search_engine != 'bing':
doc_list = search(queries, n_max_doc*2, search_engine)
else:
doc_list = search(queries, n_max_doc*2, search_engine, freshness)
doc_list_curr_iter = []
for d in doc_list:
if d not in doc_list_all:
doc_list_curr_iter.append(d)
if len(doc_list_curr_iter) == 0:
continue
tic = time.time()
if search_engine == 'bing':
doc_list_curr_iter = read_pages(doc_list_curr_iter[:6])
print(len(doc_list), len(doc_list_all), len(doc_list_curr_iter), time.time() - tic)
if len(doc_list_curr_iter) == 0:
continue
news_timeline = []
gen_cnt = 0
tic = time.time()
while not news_timeline:
news_timeline = generate_timeline(model=model, news=input_text, docs=doc_list_curr_iter[:6]) # generate timeline
# print(news_timeline)
if not news_timeline:
time.sleep(5)
gen_cnt += 1
print("Generating times: ", gen_cnt)
if gen_cnt > 10:
break
news_timeline_all[question] = news_timeline
save_json(news_timeline_all, os.path.join(f'questions', f"{input_text.replace(' ', '_').replace('/', '')}.json"))
return news_timeline_all
def evaluate(dataset, model='gpt-4o'):
results = {}
for keyword, query, index in tqdm(TARGET_KEYWORDS[dataset][:3] + TARGET_KEYWORDS[dataset][6:]):
with open(f'data/{dataset}/{keyword}/timelines.jsonl', 'r') as f:
gt_timelines = []
for tl in f:
tl = eval(tl.strip('\n'))
if tl:
gt = {}
for ts, event in tl:
ts = ts.replace(' ', '')
if ts.count('-') == 2:
ts = datetime.strptime(ts, '%Y-%m-%dT00:00:00').date()
elif ts.count('-') == 1:
ts = datetime.strptime(ts, '%Y-%mT00:00:00').date()
else:
ts = datetime.strptime(ts, '%YT00:00:00').date()
gt[ts] = event
gt_timelines.append(gt)
# Evaluate summarization
ground_truth = TilseGroundTruth([TilseTimeline(g) for g in gt_timelines])
num_dates = len(ground_truth.get_dates())
begin_time = time.time()
if 'open' not in dataset:
predicts = generate(query, model, search_engine=f"{dataset} {keyword}")
else:
predicts = generate(query, model, freshness=keyword.split('_')[-1].replace('.', '-'))
selections = []
pred_timeline_all = {}
best_f1 = 0
best_timeline = {}
for _ in range(5):
best_question_tl = ('', '')
for question, tls in predicts.items():
pred_timeline = copy.deepcopy(pred_timeline_all)
if question in selections:
continue
for tl in tls:
try:
ts, event = tl['start'], tl['summary']
if ts.count('-') == 2:
ts = datetime.strptime(ts, '%Y-%m-%d').date()
elif ts.count('-') == 1:
ts = datetime.strptime(ts, '%Y-%m').date()
else:
ts = datetime.strptime(ts, '%Y').date()
if ts not in pred_timeline:
pred_timeline[ts] = [event]
else:
pred_timeline[ts].append(event)
except: # wrong timestamp format, discard it
pass
try:
date_scores = evaluate_dates(TilseTimeline(pred_timeline), ground_truth)['f_score']
except:
date_scores = 0
if date_scores > best_f1:
best_question_tl = (tls, question)
best_timeline = copy.deepcopy(pred_timeline)
best_f1 = date_scores
pred_timeline_all = copy.deepcopy(best_timeline)
selections.append(best_question_tl[1])
results[query] = selections
print(selections, best_f1)
return results
if __name__ == '__main__':
examples = {}
for dataset in [d for d in os.listdir('data') if os.path.isdir(os.path.join('data', d))]:
exs = evaluate(dataset)
for topic in exs:
cnt = 1
while topic+str(cnt) in examples.keys():
cnt += 1
if cnt == 1:
examples[topic] = exs[topic]
else:
examples[topic+str(cnt)] = exs[topic]
save_json(examples, 'data/question_examples.json')