forked from stanfordnlp/treelstm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
download.py
137 lines (123 loc) · 4.28 KB
/
download.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
"""
Downloads the following:
- Stanford parser
- Stanford POS tagger
- Glove vectors
- SICK dataset (semantic relatedness task)
- Stanford Sentiment Treebank (sentiment classification task)
"""
from __future__ import print_function
import urllib2
import sys
import os
import shutil
import zipfile
import gzip
def download(url, dirpath):
filename = url.split('/')[-1]
filepath = os.path.join(dirpath, filename)
u = urllib2.urlopen(url)
f = open(filepath, 'wb')
filesize = int(u.info().getheaders("Content-Length")[0])
print("Downloading: %s Bytes: %s" % (filename, filesize))
downloaded = 0
block_sz = 8192
status_width = 70
while True:
buf = u.read(block_sz)
if not buf:
print('')
break
else:
print('', end='\r')
downloaded += len(buf)
f.write(buf)
status = (("[%-" + str(status_width + 1) + "s] %3.2f%%") %
('=' * int(float(downloaded) / filesize * status_width) + '>', downloaded * 100. / filesize))
print(status, end='')
sys.stdout.flush()
f.close()
return filepath
def unzip(filepath):
dirpath = os.path.dirname(filepath)
with zipfile.ZipFile(filepath) as zf:
zf.extractall(dirpath)
os.remove(filepath)
def download_tagger(dirpath):
tagger_dir = 'stanford-tagger'
if os.path.exists(os.path.join(dirpath, tagger_dir)):
print('Found Stanford POS Tagger - skip')
return
url = 'http://nlp.stanford.edu/software/stanford-postagger-2015-01-29.zip'
filepath = download(url, dirpath)
zip_dir = ''
with zipfile.ZipFile(filepath) as zf:
zip_dir = zf.namelist()[0]
zf.extractall(dirpath)
os.remove(filepath)
os.rename(os.path.join(dirpath, zip_dir), os.path.join(dirpath, tagger_dir))
def download_parser(dirpath):
parser_dir = 'stanford-parser'
if os.path.exists(os.path.join(dirpath, parser_dir)):
print('Found Stanford Parser - skip')
return
url = 'http://nlp.stanford.edu/software/stanford-parser-full-2015-01-29.zip'
filepath = download(url, dirpath)
zip_dir = ''
with zipfile.ZipFile(filepath) as zf:
zip_dir = zf.namelist()[0]
zf.extractall(dirpath)
os.remove(filepath)
os.rename(os.path.join(dirpath, zip_dir), os.path.join(dirpath, parser_dir))
def download_wordvecs(dirpath):
if os.path.exists(dirpath):
print('Found Glove vectors - skip')
return
else:
os.makedirs(dirpath)
url = 'http://www-nlp.stanford.edu/data/glove.840B.300d.txt.gz'
filepath = download(url, dirpath)
print('extracting ' + filepath)
with gzip.open(filepath, 'rb') as gf:
with open(filepath[:-3], 'w') as f:
for line in gf:
f.write(line)
os.remove(filepath)
def download_sick(dirpath):
if os.path.exists(dirpath):
print('Found SICK dataset - skip')
return
else:
os.makedirs(dirpath)
train_url = 'http://alt.qcri.org/semeval2014/task1/data/uploads/sick_train.zip'
trial_url = 'http://alt.qcri.org/semeval2014/task1/data/uploads/sick_trial.zip'
test_url = 'http://alt.qcri.org/semeval2014/task1/data/uploads/sick_test_annotated.zip'
unzip(download(train_url, dirpath))
unzip(download(trial_url, dirpath))
unzip(download(test_url, dirpath))
def download_sst(dirpath):
if os.path.exists(dirpath):
print('Found SST dataset - skip')
return
url = 'http://nlp.stanford.edu/~socherr/stanfordSentimentTreebank.zip'
parent_dir = os.path.dirname(dirpath)
unzip(download(url, parent_dir))
os.rename(
os.path.join(parent_dir, 'stanfordSentimentTreebank'),
os.path.join(parent_dir, 'sst'))
shutil.rmtree(os.path.join(parent_dir, '__MACOSX')) # remove extraneous dir
if __name__ == '__main__':
base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
# data
data_dir = os.path.join(base_dir, 'data')
wordvec_dir = os.path.join(data_dir, 'glove')
sick_dir = os.path.join(data_dir, 'sick')
sst_dir = os.path.join(data_dir, 'sst')
# libraries
lib_dir = os.path.join(base_dir, 'lib')
# download dependencies
# download_tagger(lib_dir)
# download_parser(lib_dir)
download_wordvecs(wordvec_dir)
# download_sick(sick_dir)
download_sst(sst_dir)