ZhuofengLi commited on
Commit
d690744
·
verified ·
1 Parent(s): 6a9627c

Create make_summarization_csv.py

Browse files
Files changed (1) hide show
  1. make_summarization_csv.py +90 -0
make_summarization_csv.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import os
3
+ import logging
4
+ import pickle
5
+ import argparse
6
+
7
+ import numpy as np
8
+ import pandas as pd
9
+
10
+ from tqdm import tqdm
11
+
12
+
13
+ def make_summarization_csv(args):
14
+ if args.for_qfid:
15
+ logging.info('Making csv files for QFiD...')
16
+ logging.info('Columns={"reference": literature review title <s> chapter title </s> literature review title <s> chapter title <s> abstract of cited paper 1 <s> BIB001 </s> literature review title <s> chapter title <s> abstract of cited paper 2 <s> BIB002 </s> ..., "target": literature review chapter}')
17
+ else:
18
+ logging.info('Making csv files for summarization...')
19
+ logging.info('Columns={"reference": literature review title <s> chapter title <s> abstract of cited paper 1 <s> BIB001 </s> literature review title <s> chapter title <s> abstract of cited paper 2 <s> BIB002 </s> ..., "target": literature review chapter}')
20
+ section_df = pd.read_pickle(os.path.join(args.dataset_path, 'split_survey_df.pkl'))
21
+
22
+ dataset_df = section_df[section_df['n_bibs'].apply(lambda n_bibs: n_bibs >= 2)]
23
+
24
+ dataset_df = dataset_df.rename(columns={'text': 'target'})
25
+ dataset_df = dataset_df.rename(columns={'bib_cinting_sentences': 'bib_citing_sentences'})
26
+
27
+ dataset_df['reference'] = dataset_df[['bib_abstracts', 'section', 'title']].apply(lambda bib_abstracts: ' '.join(['</s> {} <s> {} <s> {} <s> BIB{}'.format(bib_abstracts[2], bib_abstracts[1], abstract, bib) for bib, abstract in bib_abstracts[0].items()]), axis=1)
28
+ if args.for_qfid:
29
+ dataset_df['reference'] = dataset_df['title'] + ' <s> ' + dataset_df['section'] + ' ' + dataset_df['reference']
30
+ else:
31
+ dataset_df['reference'] = dataset_df['reference'].apply(lambda s: s[5:])
32
+
33
+ split_df = dataset_df['split']
34
+ dataset_df = dataset_df[['reference', 'target']]
35
+
36
+ train_df = dataset_df[split_df == 'train']
37
+ val_df = dataset_df[split_df == 'val']
38
+ test_df = dataset_df[split_df == 'test']
39
+
40
+ if args.for_qfid:
41
+ train_df.to_csv(os.path.join(args.dataset_path, 'train_qfid.csv'), index=False)
42
+ val_df.to_csv(os.path.join(args.dataset_path, 'val_qfid.csv'), index=False)
43
+ test_df.to_csv(os.path.join(args.dataset_path, 'test_qfid.csv'), index=False)
44
+ else:
45
+ train_df.to_csv(os.path.join(args.dataset_path, 'train.csv'), index=False)
46
+ val_df.to_csv(os.path.join(args.dataset_path, 'val.csv'), index=False)
47
+ test_df.to_csv(os.path.join(args.dataset_path, 'test.csv'), index=False)
48
+ logging.info('Done!')
49
+
50
+
51
+ def anonymize_bib(args):
52
+ logging.info('Converting BIB identifiers...')
53
+ for split in ['val', 'test', 'train']:
54
+ if args.for_qfid:
55
+ df = pd.read_csv(os.path.join(args.dataset_path, '{}_qfid.csv'.format(split)))
56
+ else:
57
+ df = pd.read_csv(os.path.join(args.dataset_path, '{}.csv'.format(split)))
58
+ bar = tqdm(total=len(df))
59
+ for row in df.itertuples():
60
+ cnt = 1
61
+ bib_dict = {}
62
+ for i in range(len(row.reference)):
63
+ if row.reference[i:i+7] == '<s> BIB':
64
+ bib_dict[row.reference[i+7:].split(' ')[0]] = cnt
65
+ cnt += 1
66
+ ref = row.reference
67
+ tgt = row.target
68
+ for key, value in bib_dict.items():
69
+ ref = re.sub('BIB{}'.format(key), 'BIB{:0>3}'.format(value), ref)
70
+ tgt = re.sub('BIB{}'.format(key), 'BIB{:0>3}'.format(value), tgt)
71
+ df.at[row.Index, 'reference'] = ref
72
+ df.at[row.Index, 'target'] = tgt
73
+ bar.update(1)
74
+ logging.info('Saving...')
75
+ if args.for_qfid:
76
+ df.to_csv(os.path.join(args.dataset_path, '{}_qfid.csv'.format(split)), index=False)
77
+ else:
78
+ df.to_csv(os.path.join(args.dataset_path, '{}.csv'.format(split)), index=False)
79
+
80
+
81
+ if __name__ == '__main__':
82
+ logging.basicConfig(format='%(message)s', level=logging.DEBUG)
83
+
84
+ parser = argparse.ArgumentParser(description='')
85
+ parser.add_argument('-dataset_path', default=".", help='Path to the generated dataset')
86
+ parser.add_argument('--for_qfid', action='store_true', help='Add if you train QFiD on the generated csv files')
87
+ args = parser.parse_args()
88
+
89
+ make_summarization_csv(args) # Convert split_survey_df into csv files suitable for summarization
90
+ anonymize_bib(args) # Converting BIB{paper_id} into BIB{001, 002, ...}