imdb.py 1.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

D
dangqingqing 已提交
15 16 17 18 19 20
from __future__ import print_function
import six.moves.cPickle as pickle
import gzip
import os
import numpy

21

D
dangqingqing 已提交
22 23 24 25 26 27 28 29 30
def get_dataset_file(dataset, default_dataset, origin):
    data_dir, data_file = os.path.split(dataset)
    if (not os.path.isfile(dataset)) and data_file == default_dataset:
        from six.moves import urllib
        print('Downloading data from %s' % origin)
        urllib.request.urlretrieve(origin, dataset)

    return dataset

31

D
dangqingqing 已提交
32 33 34 35 36 37
def create_data(path="imdb.pkl"):

    if (not os.path.isfile('imdb.train.pkl')):
        path = get_dataset_file(
            path, "imdb.pkl",
            "http://www.iro.umontreal.ca/~lisa/deep/data/imdb.pkl")
38

D
dangqingqing 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        if path.endswith(".gz"):
            f = gzip.open(path, 'rb')
        else:
            f = open(path, 'rb')

        train_set = pickle.load(f)
        test_set = pickle.load(f)
        f.close()

        pickle.dump(train_set, open('imdb.train.pkl', 'wb'))
        pickle.dump(test_set, open('imdb.test.pkl', 'wb'))

    if (not os.path.isfile('train.list')):
        file('train.list', 'w').write('imdb.train.pkl\n')

54

D
dangqingqing 已提交
55 56 57
def main():
    create_data('imdb.pkl')

58

D
dangqingqing 已提交
59 60
if __name__ == "__main__":
    main()