|
""" |
|
wget https://figshare.com/ndownloader/files/8760241 |
|
unzip 8760241 |
|
""" |
|
|
|
import json |
|
import os |
|
from glob import glob |
|
from tqdm import tqdm |
|
from random import shuffle, seed |
|
|
|
os.makedirs('data', exist_ok=True) |
|
f_writer = open('data/t_rex.raw.jsonl', 'w') |
|
for i in tqdm(glob("*.json")): |
|
with open(i) as f: |
|
data = json.load(f) |
|
for _data in data: |
|
for triple in _data['triples']: |
|
p = triple['predicate']['surfaceform'] |
|
o = triple['object']['surfaceform'] |
|
s = triple['subject']['surfaceform'] |
|
if p is None or o is None or s is None: |
|
continue |
|
out = {"predicate": p, "object": o, "subject": s, "title": _data["title"], "text": _data["text"]} |
|
f_writer.write(json.dumps(out) + "\n") |
|
f_writer.close() |
|
|
|
seed(0) |
|
with open('data/t_rex.raw.jsonl') as f: |
|
data = [json.loads(i) for i in f.read().split('\n') if len(i) > 0] |
|
shuffle(data) |
|
train = data[:int(len(data) * 0.7)] |
|
val = data[int(len(data) * 0.7):int(len(data) * 0.85)] |
|
test = data[int(len(data) * 0.85):] |
|
for i, j in zip([train, val, test], ['train', 'validation', 'test']): |
|
with open(f'data/t_rex.raw.{j}.jsonl', 'w') as f: |
|
f.write('\n'.join([json.dumps(l) for l in i])) |
|
|