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') df_positive = df_positive[[i not in exclude for i in df_positive.relation]] 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()]))