-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprepare_MIND_dataset.py
140 lines (122 loc) · 7.51 KB
/
prepare_MIND_dataset.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import shutil
import random
import numpy as np
random.seed(0)
np.random.seed(0)
MIND_small_dataset_root = '../MIND-small'
MIND_large_dataset_root = '../MIND-large'
MIND_small_train_ratio = 0.95
def download_extract_MIND_small():
if not os.path.exists(MIND_small_dataset_root):
os.mkdir(MIND_small_dataset_root)
if not os.path.exists(MIND_small_dataset_root + '/download'):
os.mkdir(MIND_small_dataset_root + '/download')
if not os.path.exists(MIND_small_dataset_root + '/download/train'):
if not os.path.exists(MIND_small_dataset_root + '/download/MINDsmall_train.zip'):
os.system('wget https://mind201910small.blob.core.windows.net/release/MINDsmall_train.zip -P %s/download' % MIND_small_dataset_root)
assert os.path.exists(MIND_small_dataset_root + '/download/MINDsmall_train.zip'), 'Train set zip not found'
os.mkdir(MIND_small_dataset_root + '/download/train')
os.system('unzip %s/download/MINDsmall_train.zip -d %s/download/train' % (MIND_small_dataset_root, MIND_small_dataset_root))
if not os.path.exists(MIND_small_dataset_root + '/download/dev'):
if not os.path.exists(MIND_small_dataset_root + '/download/MINDsmall_dev.zip'):
os.system('wget https://mind201910small.blob.core.windows.net/release/MINDsmall_dev.zip -P %s/download' % MIND_small_dataset_root)
assert os.path.exists(MIND_small_dataset_root + '/download/MINDsmall_dev.zip'), 'Dev set zip not found'
os.mkdir(MIND_small_dataset_root + '/download/dev')
os.system('unzip %s/download/MINDsmall_dev.zip -d %s/download/dev' % (MIND_small_dataset_root, MIND_small_dataset_root))
def download_extract_MIND_large():
if not os.path.exists(MIND_large_dataset_root):
os.mkdir(MIND_large_dataset_root)
if not os.path.exists(MIND_large_dataset_root + '/download'):
os.mkdir(MIND_large_dataset_root + '/download')
if not os.path.exists(MIND_large_dataset_root + '/download/train'):
if not os.path.exists(MIND_large_dataset_root + '/download/MINDlarge_train.zip'):
os.system('wget https://mind201910small.blob.core.windows.net/release/MINDlarge_train.zip -P %s/download' % MIND_large_dataset_root)
assert os.path.exists(MIND_large_dataset_root + '/download/MINDlarge_train.zip'), 'Train set zip not found'
os.mkdir(MIND_large_dataset_root + '/download/train')
os.system('unzip %s/download/MINDlarge_train.zip -d %s/train' % (MIND_large_dataset_root, MIND_large_dataset_root))
if not os.path.exists(MIND_large_dataset_root + '/download/dev'):
if not os.path.exists(MIND_large_dataset_root + '/download/MINDlarge_dev.zip'):
os.system('wget https://mind201910small.blob.core.windows.net/release/MINDlarge_dev.zip -P %s/download' % MIND_large_dataset_root)
assert os.path.exists(MIND_large_dataset_root + '/download/MINDlarge_dev.zip'), 'Dev set zip not found'
os.mkdir(MIND_large_dataset_root + '/download/dev')
os.system('unzip %s/download/MINDlarge_dev.zip -d %s/dev' % (MIND_large_dataset_root, MIND_large_dataset_root))
if not os.path.exists(MIND_large_dataset_root + '/download/test'):
if not os.path.exists(MIND_large_dataset_root + '/download/MINDlarge_test.zip'):
os.system('wget https://mind201910small.blob.core.windows.net/release/MINDlarge_test.zip -P %s/download' % MIND_large_dataset_root)
assert os.path.exists(MIND_large_dataset_root + '/download/MINDlarge_test.zip'), 'Test set zip not found'
os.mkdir(MIND_large_dataset_root + '/download/test')
os.system('unzip %s/download/MINDlarge_test.zip -d %s/test' % (MIND_large_dataset_root, MIND_large_dataset_root))
def split_training_behaviors():
train_behavior_lines = []
dev_behavior_lines = []
behavior_lines = []
with open(MIND_small_dataset_root + '/download/train/behaviors.tsv', 'r', encoding='utf-8') as f:
for line in f:
if len(line.strip()) > 0:
behavior_lines.append(line)
random.shuffle(behavior_lines)
behavior_num = len(behavior_lines)
behavior_id = [i for i in range(behavior_num)]
random.shuffle(behavior_id)
train_num = int(behavior_num * MIND_small_train_ratio)
train_behavior_id = random.sample(behavior_id, train_num)
train_behavior_id = set(train_behavior_id)
for i, line in enumerate(behavior_lines):
if i in train_behavior_id:
train_behavior_lines.append(line)
else:
dev_behavior_lines.append(line)
return train_behavior_lines, dev_behavior_lines
def preprocess_MIND_small():
train_behavior_lines, dev_behavior_lines = split_training_behaviors()
# train set
train_set_root = MIND_small_dataset_root + '/train'
if not os.path.exists(train_set_root):
os.mkdir(train_set_root)
with open(train_set_root + '/behaviors.tsv', 'w', encoding='utf-8') as f:
for line in train_behavior_lines:
f.write(line)
if not os.path.exists(train_set_root):
os.mkdir(train_set_root)
if not os.path.exists(train_set_root + '/entity_embedding.vec'):
shutil.copyfile(MIND_small_dataset_root + '/download/train/entity_embedding.vec', train_set_root + '/entity_embedding.vec')
if not os.path.exists(train_set_root + '/news.tsv'):
shutil.copyfile(MIND_small_dataset_root + '/download/train/news.tsv', train_set_root + '/news.tsv')
if not os.path.exists(train_set_root + '/relation_embedding.vec'):
shutil.copyfile(MIND_small_dataset_root + '/download/train/relation_embedding.vec', train_set_root + '/relation_embedding.vec')
# dev set
dev_set_root = MIND_small_dataset_root + '/dev'
if not os.path.exists(dev_set_root):
os.mkdir(dev_set_root)
with open(dev_set_root + '/behaviors.tsv', 'w', encoding='utf-8') as f:
for line in dev_behavior_lines:
f.write(line)
if not os.path.exists(dev_set_root):
os.mkdir(dev_set_root)
if not os.path.exists(dev_set_root + '/entity_embedding.vec'):
shutil.copyfile(MIND_small_dataset_root + '/download/train/entity_embedding.vec', dev_set_root + '/entity_embedding.vec')
if not os.path.exists(dev_set_root + '/news.tsv'):
shutil.copyfile(MIND_small_dataset_root + '/download/train/news.tsv', dev_set_root + '/news.tsv')
if not os.path.exists(dev_set_root + '/relation_embedding.vec'):
shutil.copyfile(MIND_small_dataset_root + '/download/train/relation_embedding.vec', dev_set_root + '/relation_embedding.vec')
# test set
test_set_root = MIND_small_dataset_root + '/test'
if not os.path.exists(test_set_root):
os.mkdir(test_set_root)
if not os.path.exists(test_set_root + '/behaviors.tsv'):
shutil.copyfile(MIND_small_dataset_root + '/download/dev/behaviors.tsv', test_set_root + '/behaviors.tsv')
if not os.path.exists(test_set_root + '/entity_embedding.vec'):
shutil.copyfile(MIND_small_dataset_root + '/download/dev/entity_embedding.vec', test_set_root + '/entity_embedding.vec')
if not os.path.exists(test_set_root + '/news.tsv'):
shutil.copyfile(MIND_small_dataset_root + '/download/dev/news.tsv', test_set_root + '/news.tsv')
if not os.path.exists(test_set_root + '/relation_embedding.vec'):
shutil.copyfile(MIND_small_dataset_root + '/download/dev/relation_embedding.vec', test_set_root + '/relation_embedding.vec')
def prepare_MIND_small():
download_extract_MIND_small()
preprocess_MIND_small()
def prepare_MIND_large():
download_extract_MIND_large()
if __name__ == '__main__':
prepare_MIND_small()
prepare_MIND_large()