File size: 788 Bytes
bd607ef
 
 
 
 
 
 
 
 
8196f05
 
bd607ef
 
 
e646b2e
bd607ef
 
 
e646b2e
 
bd607ef
 
 
 
 
 
8196f05
 
bd607ef
 
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
import json
import os
from itertools import product
from statistics import mean

import pandas as pd
from datasets import load_dataset


def process(split, output):
    data = load_dataset("relbert/t_rex", split=split)
    df = data.to_pandas()
    df.pop('text')
    df.pop('title')
    df['pairs'] = [[i, j] for i, j in zip(df.pop('head'), df.pop('tail'))]
    rel_sim_data = [{
        "relation_type": pred,
        "positives": g['pairs'].values.tolist(),
        "negatives": []
    } for pred, g in df.groupby("relation") if len(g) >= 2]
    with open(output, "w") as f:
        f.write('\n'.join([json.dumps(i) for i in rel_sim_data]))


os.makedirs("data", exist_ok=True)

for s in ['train', 'validation', 'test']:
    process(split=s, output=f"data/filter_unified.{s}.jsonl")