-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretrain_data_prep.py
82 lines (56 loc) · 2.21 KB
/
pretrain_data_prep.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
import os
import json
import random
import argparse
from pathlib import Path
from tqdm import tqdm
def extract_cell_contents(table_json_path):
with open(table_json_path) as f:
data = json.load(f)
cells = []
headers = data['headers']
for header in headers:
for col in header['columns']:
content = col['content'].strip()
if len(content) > 0:
cells.append(content)
rows = data['rows']
for row in rows:
for col in row:
content = col['content'].strip()
if len(content) > 0:
cells.append(content)
return cells
def handle_dir(in_dir: Path, out_dir: Path, val_frac=0.005):
table_paths = list(in_dir.glob("*.json"))
total = len(table_paths)
val_len = max(1, int(val_frac * total))
random.shuffle(table_paths)
train_file = out_dir / "train.txt"
val_file = out_dir / "val.txt"
val_table_paths = table_paths[:val_len]
tr_table_paths = table_paths[val_len:]
out_dir.mkdir(parents=True, exist_ok=True)
mode = 'a' if train_file.exists() else 'w'
with open(train_file, mode) as f:
for tr_table_path in tqdm(tr_table_paths, desc="Train"):
cells = extract_cell_contents(tr_table_path)
f.write('\n'.join(cells))
mode = 'a' if val_file.exists() else 'w'
with open(val_file, mode) as f:
for val_table_path in tqdm(val_table_paths, desc="Test"):
cells = extract_cell_contents(val_table_path)
f.write('\n'.join(cells))
def handle_dirs(in_root_dir: Path, out_dir: Path, val_frac=0.005):
sub_dirs = [Path(f.path) for f in os.scandir(in_root_dir) if f.is_dir()]
for sub_dir in sub_dirs:
handle_dir(sub_dir, out_dir, val_frac=val_frac)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="prepares pretraining data (one cell content per line) from table "
"JSON files")
parser.add_argument("-i", metavar="<input-dir>", required=True)
parser.add_argument("-o", metavar="<output-dir>", required=True)
args = parser.parse_args()
in_root = Path(args.i)
output_dir = Path(args.o)
handle_dirs(in_root, output_dir)