reader.py 4.0 KB
Newer Older
H
Hongyu Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
# 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.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
import os
import sys
import numpy as np
22
import paddle.fluid as fluid
H
Hongyu Liu 已提交
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

EOS = "</eos>"


def build_vocab(filename):

    vocab_dict = {}
    ids = 0
    vocab_dict[EOS] = ids
    ids += 1

    with open(filename, "r") as f:
        for line in f.readlines():
            for w in line.strip().split():
                if w not in vocab_dict:
                    vocab_dict[w] = ids
                    ids += 1

    print("vocab word num", ids)

    return vocab_dict


def file_to_ids(src_file, src_vocab):

    src_data = []
    with open(src_file, "r") as f_src:
        for line in f_src.readlines():
            arra = line.strip().split()
            ids = [src_vocab[w] for w in arra if w in src_vocab]

            src_data += ids + [0]
    return src_data


def get_ptb_data(data_path=None):

    train_file = os.path.join(data_path, "ptb.train.txt")
    valid_file = os.path.join(data_path, "ptb.valid.txt")
    test_file = os.path.join(data_path, "ptb.test.txt")

    vocab_dict = build_vocab(train_file)
    train_ids = file_to_ids(train_file, vocab_dict)
    valid_ids = file_to_ids(valid_file, vocab_dict)
    test_ids = file_to_ids(test_file, vocab_dict)

    return train_ids, valid_ids, test_ids

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
def get_ptb_vocab_dict(data_path=None):
    train_file = os.path.join(data_path, "ptb.train.txt")
    vocab_dict = build_vocab(train_file)
    return vocab_dict

def get_size_of_ptb_train_data(vocab_dict, data_path=None):
    train_file = os.path.join(data_path, "ptb.train.txt")
    train_ids = file_to_ids(train_file, vocab_dict)
    return len(train_ids) 

def get_ptb_train_data(vocab_dict, data_path=None):
    train_file = os.path.join(data_path, "ptb.train.txt")
    train_ids = file_to_ids(train_file, vocab_dict)
    return train_ids

def get_ptb_valid_data(vocab_dict, data_path=None):
    valid_file = os.path.join(data_path, "ptb.valid.txt")
    valid_ids = file_to_ids(valid_file, vocab_dict)
    return valid_ids

def get_ptb_test_data(vocab_dict, data_path=None):
    test_file = os.path.join(data_path, "ptb.test.txt")
    test_ids = file_to_ids(test_file, vocab_dict)
    return test_ids    

def mapper(sample):
    return sample

def get_reader(data_type, batch_size, num_steps, data_path, vocab_dict):
    def get_data_reader():
        def get_data_iter():
            if data_type == "train":
                raw_data = get_ptb_train_data(vocab_dict, data_path)
            elif data_type == "valid":
                raw_data = get_ptb_valid_data(vocab_dict, data_path)
            else:
                raw_data = get_ptb_test_data(vocab_dict, data_path)

            data_len = len(raw_data)
            raw_data = np.asarray(raw_data, dtype="int64")

            batch_len = data_len // batch_size

            data = raw_data[0:batch_size * batch_len].reshape((batch_size, batch_len))

            epoch_size = (batch_len - 1) // num_steps
            for i in range(epoch_size):
                start = i * num_steps
                x = np.copy(data[:, i * num_steps:(i + 1) * num_steps])
                y = np.copy(data[:, i * num_steps + 1:(i + 1) * num_steps + 1])

                x = x.reshape((-1, num_steps, 1))
                y = y.reshape((-1, num_steps, 1))
H
Hongyu Liu 已提交
124

125
                yield (x, y)
H
Hongyu Liu 已提交
126

127
        return get_data_iter
H
Hongyu Liu 已提交
128

129 130
    data_reader = get_data_reader()
    
J
JepsonWong 已提交
131
    return data_reader
H
Hongyu Liu 已提交
132 133