import json | |
import os | |
from itertools import combinations | |
from random import shuffle, seed | |
from datasets import load_dataset | |
# # create analogy from `relbert/semeval2012_relational_similarity` | |
# data = load_dataset("relbert/semeval2012_relational_similarity", split="validation") | |
# analogy_data = [{ | |
# "stem": i['positives'][0], "choice": i["negatives"] + [i['positives'][1]], "answer": 2, "prefix": i["relation_type"] | |
# } for i in data] | |
# os.makedirs("dataset/semeval2012_relational_similarity", exist_ok=True) | |
# with open("dataset/semeval2012_relational_similarity/valid.jsonl", "w") as f: | |
# f.write("\n".join([json.dumps(i) for i in analogy_data])) | |
# # create analogy from `relbert/t_rex_relational_similarity` | |
# data = load_dataset("relbert/t_rex_relational_similarity", "filter_unified.min_entity_1_max_predicate_100", split="test") | |
# analogy_data = [] | |
# for i in data: | |
# if len(i['positives']) < 2: | |
# continue | |
# for m, (q, c) in enumerate(combinations(i['positives'], 2)): | |
# if m > 5: | |
# break | |
# negative = i['negatives'] | |
# for n in range(6): | |
# seed(n) | |
# shuffle(negative) | |
# analogy_data.append({ | |
# "stem": q, "choice": [c] + negative[:5], "answer": 0, "prefix": i["relation_type"] | |
# }) | |
# os.makedirs("dataset/t_rex_relational_similarity", exist_ok=True) | |
# with open("dataset/t_rex_relational_similarity/test.jsonl", "w") as f: | |
# f.write("\n".join([json.dumps(i) for i in analogy_data])) | |
# | |
# data = load_dataset("relbert/t_rex_relational_similarity", "filter_unified.min_entity_4_max_predicate_100", split="validation") | |
# analogy_data = [] | |
# for i in data: | |
# if len(i['positives']) < 5: | |
# continue | |
# for m, (q, c) in enumerate(combinations(i['positives'], 2)): | |
# if m > 5: | |
# break | |
# negative = i['negatives'] | |
# for n in range(3): | |
# seed(n) | |
# shuffle(negative) | |
# analogy_data.append({ | |
# "stem": q, "choice": [c] + negative[:5], "answer": 0, "prefix": i["relation_type"] | |
# }) | |
# os.makedirs("dataset/t_rex_relational_similarity", exist_ok=True) | |
# with open("dataset/t_rex_relational_similarity/valid.jsonl", "w") as f: | |
# f.write("\n".join([json.dumps(i) for i in analogy_data])) | |
# create analogy from `relbert/conceptnet_relational_similarity` | |
for s in ['test', 'validation']: | |
data = load_dataset("relbert/conceptnet_relational_similarity", split=s) | |
analogy_data = [] | |
for i in data: | |
if len(i['positives']) < 2: | |
continue | |
for m, (q, c) in enumerate(combinations(i['positives'], 2)): | |
if m > 5: | |
break | |
negative = i['negatives'] | |
for n in range(6): | |
seed(n) | |
shuffle(negative) | |
analogy_data.append({ | |
"stem": q, "choice": [c] + negative[:5], "answer": 0, "prefix": i["relation_type"] | |
}) | |
print(len(analogy_data)) | |
os.makedirs("dataset/conceptnet_relational_similarity", exist_ok=True) | |
with open(f"dataset/conceptnet_relational_similarity/{s if s == 'test' else 'valid'}.jsonl", "w") as f: | |
f.write("\n".join([json.dumps(i) for i in analogy_data])) |