File size: 1,987 Bytes
945a5ac d452fea 945a5ac 71258e9 945a5ac d452fea 50be3a0 945a5ac d452fea 945a5ac d452fea 945a5ac d452fea 6ce12d0 d452fea 945a5ac d452fea |
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 |
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',
'train': "https://home.ttic.edu/~kgimpel/comsense_resources/train600k.txt.gz"
}
os.makedirs("dataset", exist_ok=True)
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 = df[df['flag'] != '0']
df.pop('flag')
df = df[[not i.startswith("Not") for i in df.relation]]
return df
if __name__ == '__main__':
with open(f'dataset/test.jsonl', 'w') as f:
test = read_file(wget(urls['test']))
f.write("\n".join([json.dumps(i.to_dict()) for _, i in test.iterrows()]))
with open(f'dataset/train.jsonl', 'w') as f:
train = read_file(wget(urls['train']))
f.write("\n".join([json.dumps(i.to_dict()) for _, i in train.iterrows()]))
with open(f'dataset/valid.jsonl', 'w') as f:
dev1 = read_file(wget(urls['dev1']))
dev2 = read_file(wget(urls['dev2']))
f.write("\n".join([json.dumps(i.to_dict()) for _, i in dev1.iterrows()] + [json.dumps(i.to_dict()) for _, i in dev2.iterrows()]))
|