reader.py 3.0 KB
Newer Older
L
Li Fuchen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
Y
Yibing Liu 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27
"""
Senta Reader
"""

import os
import types
import csv
import numpy as np
from utils import load_vocab
from utils import data_reader

import paddle
import paddle.fluid as fluid

L
Li Fuchen 已提交
28

Y
Yibing Liu 已提交
29 30 31 32 33
class SentaProcessor(object):
    """
    Processor class for data convertors for senta
    """

L
Li Fuchen 已提交
34
    def __init__(self, data_dir, vocab_path, random_seed=None):
Y
Yibing Liu 已提交
35 36 37 38 39 40 41 42 43
        self.data_dir = data_dir
        self.vocab = load_vocab(vocab_path)
        self.num_examples = {"train": -1, "dev": -1, "infer": -1}
        np.random.seed(random_seed)

    def get_train_examples(self, data_dir, epoch):
        """
        Load training examples
        """
L
Li Fuchen 已提交
44 45
        return data_reader((self.data_dir + "/train.tsv"), self.vocab,
                           self.num_examples, "train", epoch)
Y
Yibing Liu 已提交
46 47 48 49 50

    def get_dev_examples(self, data_dir, epoch):
        """
        Load dev examples
        """
L
Li Fuchen 已提交
51 52
        return data_reader((self.data_dir + "/dev.tsv"), self.vocab,
                           self.num_examples, "dev", epoch)
Y
Yibing Liu 已提交
53 54 55 56 57

    def get_test_examples(self, data_dir, epoch):
        """
        Load test examples
        """
L
Li Fuchen 已提交
58 59
        return data_reader((self.data_dir + "/test.tsv"), self.vocab,
                           self.num_examples, "infer", epoch)
Y
Yibing Liu 已提交
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

    def get_labels(self):
        """
        Return Labels
        """
        return ["0", "1"]

    def get_num_examples(self, phase):
        """
        Return num of examples in train, dev, test set
        """
        if phase not in ['train', 'dev', 'infer']:
            raise ValueError(
                "Unknown phase, which should be in ['train', 'dev', 'infer'].")
        return self.num_examples[phase]

    def get_train_progress(self):
        """
        Get train progress
        """
        return self.current_train_example, self.current_train_epoch

    def data_generator(self, batch_size, phase='train', epoch=1, shuffle=True):
        """
        Generate data for train, dev or infer
        """
        if phase == "train":
L
Li Fuchen 已提交
87 88
            return paddle.batch(
                self.get_train_examples(self.data_dir, epoch), batch_size)
Y
Yibing Liu 已提交
89
        elif phase == "dev":
L
Li Fuchen 已提交
90 91
            return paddle.batch(
                self.get_dev_examples(self.data_dir, epoch), batch_size)
Y
Yibing Liu 已提交
92
        elif phase == "infer":
L
Li Fuchen 已提交
93 94
            return paddle.batch(
                self.get_test_examples(self.data_dir, epoch), batch_size)
Y
Yibing Liu 已提交
95 96 97
        else:
            raise ValueError(
                "Unknown phase, which should be in ['train', 'dev', 'infer'].")