File size: 2,629 Bytes
f63efd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import os
import gzip
import requests

import pandas as pd

urls = {
    'dev1': 'https://home.ttic.edu/~kgimpel/comsense_resources/dev1.txt.gz',
    'dev2': 'https://home.ttic.edu/~kgimpel/comsense_resources/dev2.txt.gz',
    'test': 'https://home.ttic.edu/~kgimpel/comsense_resources/test.txt.gz'
}
exclude = ['NotCapable', 'NotDesires']


def wget(url, cache_dir: str = './cache'):
    """ wget and uncompress data_iterator """
    os.makedirs(cache_dir, exist_ok=True)
    filename = os.path.basename(url)
    path = f'{cache_dir}/{filename}'
    if os.path.exists(path):
        return path.replace('.gz', '')
    with open(path, "wb") as f:
        r = requests.get(url)
        f.write(r.content)
    with gzip.open(path, 'rb') as f:
        with open(path.replace('.gz', ''), 'wb') as f_write:
            f_write.write(f.read())
    os.remove(path)
    return path.replace('.gz', '')


def read_file(file_name):
    with open(file_name) as f_reader:
        df = pd.DataFrame([i.split('\t') for i in f_reader.read().split('\n') if len(i) > 0],
                          columns=['relation', 'head', 'tail', 'flag'])
    df_positive = df[df['flag'] == '1']
    df_negative = df[df['flag'] == '0']
    df_positive.pop('flag')
    df_negative.pop('flag')
    return df_positive, df_negative


if __name__ == '__main__':
    test_p, test_n = read_file(wget(urls['test']))
    dev1_p, dev1_n = read_file(wget(urls['dev1']))
    train_p = pd.concat([test_p, dev1_p])
    train_n = pd.concat([test_n, dev1_n])
    with open(f'dataset/train.jsonl', 'w') as f:
        for relation, df_p in train_p.groupby('relation'):
            if len(df_p) < 2:
                continue
            if relation in exclude:
                continue
            df_n = train_n[train_n['relation'] == relation]
            f.write(json.dumps({
                'relation_type': relation,
                'positives': df_p[['head', 'tail']].to_numpy().tolist(),
                'negatives': df_n[['head', 'tail']].to_numpy().tolist()
            }) + '\n')

    dev2_p, dev2_n = read_file(wget(urls['dev2']))
    with open(f'dataset/valid.jsonl', 'w') as f:
        for relation, df_p in dev2_p.groupby('relation'):
            if len(df_p) < 2:
                continue
            if relation in exclude:
                continue
            df_n = dev2_n[dev2_n['relation'] == relation]
            f.write(json.dumps({
                'relation_type': relation,
                'positives': df_p[['head', 'tail']].to_numpy().tolist(),
                'negatives': df_n[['head', 'tail']].to_numpy().tolist()
            }) + '\n')