Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 2,393 Bytes
4d962fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a5609e
 
 
4d962fc
 
 
 
 
4a5609e
4d962fc
 
 
681de16
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from itertools import product

import pandas as pd
from random import shuffle, seed


def get_test_predicate(_data):
    tmp_df = pd.DataFrame(_data)
    predicates_count = tmp_df.groupby("predicate")['text'].count().sort_values(ascending=False).to_dict()
    total_num = sum(predicates_count.values())
    pre_k = list(predicates_count.keys())
    seed(42)
    shuffle(pre_k)
    predicates_train = []
    for k in pre_k:
        predicates_train.append(k)
        if sum([predicates_count[i] for i in predicates_train]) > total_num * 0.8:
            break
    predicates_test = sorted([i for i in pre_k if i not in predicates_train])
    return predicates_test


if not os.path.exists("data/t_rex.filter_unified.test.jsonl"):
    with open(f"data/t_rex.filter_unified.min_entity_{max(parameters_min_e_freq)}_max_predicate_{min(parameters_max_p_freq)}.jsonl") as f:
        data = [json.loads(i) for i in f.read().split('\n') if len(i) > 0]
    pred_test = get_test_predicate(data)
    data_test = [i for i in data if i['predicate'] in pred_test]
    f_writer = open("data/t_rex.filter_unified.test.jsonl", 'w')
    for n, i in enumerate(data_test):
        print(f"\n[{n+1}/{len(data_test)}]")
        print(f"{json.dumps(i, indent=4)}")
        flag = input(">>> (enter to add to test)")
        if flag == '':
            i['relation'] = i.pop('predicate')
            i['head'] = i.pop('subject')
            i['tail'] = i.pop('object')
            f_writer.write(json.dumps(i) + '\n')
    f_writer.close()

with open("data/t_rex.filter_unified.test.jsonl") as f:
    data_test = [json.loads(i) for i in f.read().split('\n') if len(i) > 0]
    test_predicate = set([i['relation'] for i in data_test])


seed(42)
with open(f"data/t_rex.filter_unified.jsonl") as f:
    data = [json.loads(i) for i in f.read().split('\n') if len(i) > 0]
for i in data:
    i['relation'] = i.pop('predicate')
    i['head'] = i.pop('subject')
    i['tail'] = i.pop('object')
data = [i for i in data if i['relation'] not in test_predicate]
shuffle(data)
data_train = data[:int(len(data) * 0.8)]
data_valid = data[int(len(data) * 0.8):]
with open(f"data/t_rex.filter_unified.train.jsonl", "w") as f:
    f.write('\n'.join([json.dumps(i) for i in data_train]))
with open(f"data/t_rex.filter_unified.validation.jsonl", "w") as f:
    f.write('\n'.join([json.dumps(i) for i in data_valid]))