Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 1,778 Bytes
945a5ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = ['NotCapableOf', '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_positive.pop('flag')
    return df_positive


if __name__ == '__main__':
    test = read_file(wget(urls['test']))
    with open(f'dataset/test.jsonl', 'w') as f:
        f.write("\n".join([json.dumps(i.to_dict()) for _, i in test.iterrows()]))
    dev1 = read_file(wget(urls['dev1']))
    with open(f'dataset/train.jsonl', 'w') as f:
        f.write("\n".join([json.dumps(i.to_dict()) for _, i in dev1.iterrows()]))
    dev2 = read_file(wget(urls['dev2']))
    with open(f'dataset/valid.jsonl', 'w') as f:
        f.write("\n".join([json.dumps(i.to_dict()) for _, i in dev2.iterrows()]))