reader.py 3.7 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
"""
EmoTect Reader, data converters for classification data.
"""
import os

import paddle
import paddle.fluid as fluid
import numpy as np

from utils import load_vocab
from utils import data_reader

L
Li Fuchen 已提交
26

Y
Yibing Liu 已提交
27 28 29 30
class EmoTectProcessor(object):
    """
    Processor class for data convertors for EmoTect.
    """
L
Li Fuchen 已提交
31

32
    def __init__(self, data_dir, vocab_path, random_seed=None, max_seq_len=128):
Y
Yibing Liu 已提交
33 34 35 36
        self.data_dir = data_dir
        self.vocab = load_vocab(vocab_path)
        self.num_examples = {"train": -1, "dev": -1, "test": -1, "infer": -1}
        np.random.seed(random_seed)
37
        self.max_seq_len = max_seq_len
Y
Yibing Liu 已提交
38

39
    def get_train_examples(self, data_dir, epoch, max_seq_len):
Y
Yibing Liu 已提交
40 41 42
        """
        Load training examples
        """
L
Li Fuchen 已提交
43 44
        return data_reader(
            os.path.join(self.data_dir, "train.tsv"), self.vocab,
45
            self.num_examples, "train", epoch, max_seq_len)
Y
Yibing Liu 已提交
46

47
    def get_dev_examples(self, data_dir, epoch, max_seq_len):
Y
Yibing Liu 已提交
48 49 50
        """
        Load dev examples
        """
L
Li Fuchen 已提交
51 52
        return data_reader(
            os.path.join(self.data_dir, "dev.tsv"), self.vocab,
53
            self.num_examples, "dev", epoch, max_seq_len)
Y
Yibing Liu 已提交
54

55
    def get_test_examples(self, data_dir, epoch, max_seq_len):
Y
Yibing Liu 已提交
56 57 58
        """
        Load test examples
        """
L
Li Fuchen 已提交
59 60
        return data_reader(
            os.path.join(self.data_dir, "test.tsv"), self.vocab,
61
            self.num_examples, "test", epoch, max_seq_len)
Y
Yibing Liu 已提交
62

63
    def get_infer_examples(self, data_dir, epoch, max_seq_len):
Y
Yibing Liu 已提交
64 65 66
        """
        Load infer querys
        """
L
Li Fuchen 已提交
67 68
        return data_reader(
            os.path.join(self.data_dir, "infer.tsv"), self.vocab,
69 70
            self.num_examples, "infer", epoch, max_seq_len)

Y
Yibing Liu 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83

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

    def get_num_examples(self, phase):
        """
        Return num of examples in train, dev, test set
        """
        if phase not in ['train', 'dev', 'test', 'infer']:
            raise ValueError(
L
Li Fuchen 已提交
84 85
                "Unknown phase, which should be in ['train', 'dev', 'test', 'infer']."
            )
Y
Yibing Liu 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
        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):
        """
        Generate data for train, dev or test
        """
        if phase == "train":
99
            return fluid.io.batch(
100
                self.get_train_examples(self.data_dir, epoch, self.max_seq_len), batch_size)
Y
Yibing Liu 已提交
101
        elif phase == "dev":
102
            return fluid.io.batch(
103
                self.get_dev_examples(self.data_dir, epoch, self.max_seq_len), batch_size)
Y
Yibing Liu 已提交
104
        elif phase == "test":
105
            return fluid.io.batch(
106
                self.get_test_examples(self.data_dir, epoch, self.max_seq_len), batch_size)
Y
Yibing Liu 已提交
107
        elif phase == "infer":
108
            return fluid.io.batch(
109
                self.get_infer_examples(self.data_dir, epoch, self.max_seq_len), batch_size)
Y
Yibing Liu 已提交
110 111
        else:
            raise ValueError(
L
Li Fuchen 已提交
112 113
                "Unknown phase, which should be in ['train', 'dev', 'test', 'infer']."
            )