| import json | |
| import os | |
| from datasets import load_dataset | |
| def process(split, output): | |
| data = load_dataset("relbert/nell", split=split) | |
| df = data.to_pandas() | |
| rel_sim_data = [{ | |
| "relation_type": pred, | |
| "positives": [list(y) for y in zip(g['head'], g['tail'])], | |
| "negatives": [] | |
| } for pred, g in df.groupby("relation") if len(g) >= 2] | |
| with open(output, "w") as f_writer: | |
| f_writer.write('\n'.join([json.dumps(i) for i in rel_sim_data])) | |
| if __name__ == '__main__': | |
| os.makedirs("data", exist_ok=True) | |
| for s in ['train', 'validation', 'test']: | |
| process(split=s, output=f"data/{s}.jsonl") | |