reader.py 10.9 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
"""
SimNet reader
"""

import logging
import numpy as np
20
import io
Y
Yibing Liu 已提交
21 22 23 24 25 26 27 28 29 30


class SimNetProcessor(object):
    def __init__(self, args, vocab):
        self.args = args
        # load vocab
        self.vocab = vocab
        self.valid_label = np.array([])
        self.test_label = np.array([])

D
Dilyar 已提交
31
    def get_reader(self, mode, epoch=0):
Y
Yibing Liu 已提交
32 33 34 35 36 37 38 39 40
        """
        Get Reader
        """

        def reader_with_pairwise():
            """
                Reader with Pairwise
            """
            if mode == "valid":
41 42
                with io.open(self.args.valid_data_dir, "r",
                                 encoding="utf8") as file:
Y
Yibing Liu 已提交
43 44
                    for line in file:
                        query, title, label = line.strip().split("\t")
L
Li Fuchen 已提交
45 46 47 48 49
                        if len(query) == 0 or len(title) == 0 or len(
                                label) == 0 or not label.isdigit() or int(
                                    label) not in [0, 1]:
                            logging.warning(
                                "line not match format in test file")
Y
Yibing Liu 已提交
50
                            continue
L
Li Fuchen 已提交
51 52 53 54 55 56 57 58
                        query = [
                            self.vocab[word] for word in query.split(" ")
                            if word in self.vocab
                        ]
                        title = [
                            self.vocab[word] for word in title.split(" ")
                            if word in self.vocab
                        ]
Y
Yibing Liu 已提交
59 60 61 62 63 64
                        if len(query) == 0:
                            query = [0]
                        if len(title) == 0:
                            title = [0]
                        yield [query, title]
            elif mode == "test":
65
                with io.open(self.args.test_data_dir, "r", encoding="utf8") as file:
Y
Yibing Liu 已提交
66 67
                    for line in file:
                        query, title, label = line.strip().split("\t")
L
Li Fuchen 已提交
68 69 70 71 72
                        if len(query) == 0 or len(title) == 0 or len(
                                label) == 0 or not label.isdigit() or int(
                                    label) not in [0, 1]:
                            logging.warning(
                                "line not match format in test file")
Y
Yibing Liu 已提交
73
                            continue
L
Li Fuchen 已提交
74 75 76 77 78 79 80 81
                        query = [
                            self.vocab[word] for word in query.split(" ")
                            if word in self.vocab
                        ]
                        title = [
                            self.vocab[word] for word in title.split(" ")
                            if word in self.vocab
                        ]
Y
Yibing Liu 已提交
82 83 84 85 86 87
                        if len(query) == 0:
                            query = [0]
                        if len(title) == 0:
                            title = [0]
                        yield [query, title]
            else:
D
Dilyar 已提交
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
                for idx in range(epoch):
                    with io.open(self.args.train_data_dir, "r",
                                    encoding="utf8") as file:
                        for line in file:
                            query, pos_title, neg_title = line.strip().split("\t")
                            if len(query) == 0 or len(pos_title) == 0 or len(
                                    neg_title) == 0:
                                logging.warning(
                                    "line not match format in test file")
                                continue
                            query = [
                                self.vocab[word] for word in query.split(" ")
                                if word in self.vocab
                            ]
                            pos_title = [
                                self.vocab[word] for word in pos_title.split(" ")
                                if word in self.vocab
                            ]
                            neg_title = [
                                self.vocab[word] for word in neg_title.split(" ")
                                if word in self.vocab
                            ]
                            if len(query) == 0:
                                query = [0]
                            if len(pos_title) == 0: 
                                pos_title = [0]
                            if len(neg_title) == 0:
                                neg_title = [0]
                            yield [query, pos_title, neg_title]
Y
Yibing Liu 已提交
117 118 119 120 121 122

        def reader_with_pointwise():
            """
            Reader with Pointwise
            """
            if mode == "valid":
123 124
                with io.open(self.args.valid_data_dir, "r",
                                 encoding="utf8") as file:
Y
Yibing Liu 已提交
125 126
                    for line in file:
                        query, title, label = line.strip().split("\t")
L
Li Fuchen 已提交
127 128 129 130 131
                        if len(query) == 0 or len(title) == 0 or len(
                                label) == 0 or not label.isdigit() or int(
                                    label) not in [0, 1]:
                            logging.warning(
                                "line not match format in test file")
Y
Yibing Liu 已提交
132
                            continue
L
Li Fuchen 已提交
133 134 135 136 137 138 139 140
                        query = [
                            self.vocab[word] for word in query.split(" ")
                            if word in self.vocab
                        ]
                        title = [
                            self.vocab[word] for word in title.split(" ")
                            if word in self.vocab
                        ]
Y
Yibing Liu 已提交
141 142 143 144 145 146
                        if len(query) == 0:
                            query = [0]
                        if len(title) == 0:
                            title = [0]
                        yield [query, title]
            elif mode == "test":
147
                with io.open(self.args.test_data_dir, "r", encoding="utf8") as file:
Y
Yibing Liu 已提交
148 149
                    for line in file:
                        query, title, label = line.strip().split("\t")
L
Li Fuchen 已提交
150 151 152 153 154
                        if len(query) == 0 or len(title) == 0 or len(
                                label) == 0 or not label.isdigit() or int(
                                    label) not in [0, 1]:
                            logging.warning(
                                "line not match format in test file")
Y
Yibing Liu 已提交
155
                            continue
L
Li Fuchen 已提交
156 157 158 159 160 161 162 163
                        query = [
                            self.vocab[word] for word in query.split(" ")
                            if word in self.vocab
                        ]
                        title = [
                            self.vocab[word] for word in title.split(" ")
                            if word in self.vocab
                        ]
Y
Yibing Liu 已提交
164 165 166 167 168 169
                        if len(query) == 0:
                            query = [0]
                        if len(title) == 0:
                            title = [0]
                        yield [query, title]
            else:
D
Dilyar 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
                for idx in range(epoch):
                    with io.open(self.args.train_data_dir, "r",
                                    encoding="utf8") as file:
                        for line in file:
                            query, title, label = line.strip().split("\t")
                            if len(query) == 0 or len(title) == 0 or len(
                                    label) == 0 or not label.isdigit() or int(
                                        label) not in [0, 1]:
                                logging.warning(
                                    "line not match format in test file")
                                continue
                            query = [
                                self.vocab[word] for word in query.split(" ")
                                if word in self.vocab
                            ]
                            title = [
                                self.vocab[word] for word in title.split(" ")
                                if word in self.vocab
                            ]
                            label = int(label)
                            if len(query) == 0:
                                query = [0]
                            if len(title) == 0:
                                title = [0]
                            yield [query, title, label]
Y
Yibing Liu 已提交
195 196 197 198 199 200 201 202 203 204

        if self.args.task_mode == "pairwise":
            return reader_with_pairwise
        else:
            return reader_with_pointwise

    def get_infer_reader(self):
        """
        get infer reader
        """
205
        with io.open(self.args.infer_data_dir, "r", encoding="utf8") as file:
Y
Yibing Liu 已提交
206 207 208 209 210
            for line in file:
                query, title = line.strip().split("\t")
                if len(query) == 0 or len(title) == 0:
                    logging.warning("line not match format in test file")
                    continue
L
Li Fuchen 已提交
211 212 213 214 215 216 217 218
                query = [
                    self.vocab[word] for word in query.split(" ")
                    if word in self.vocab
                ]
                title = [
                    self.vocab[word] for word in title.split(" ")
                    if word in self.vocab
                ]
Y
Yibing Liu 已提交
219 220 221 222 223 224 225 226 227 228
                if len(query) == 0:
                    query = [0]
                if len(title) == 0:
                    title = [0]
                yield [query, title]

    def get_infer_data(self):
        """
        get infer data
        """
229
        with io.open(self.args.infer_data_dir, "r", encoding="utf8") as file:
Y
Yibing Liu 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242
            for line in file:
                query, title = line.strip().split("\t")
                if len(query) == 0 or len(title) == 0:
                    logging.warning("line not match format in test file")
                    continue
                yield line.strip()

    def get_valid_label(self):
        """
        get valid data label
        """
        if self.valid_label.size == 0:
            labels = []
243
            with io.open(self.args.valid_data_dir, "r", encoding="utf8") as f:
Y
Yibing Liu 已提交
244 245 246 247 248 249 250 251 252 253 254
                for line in f:
                    labels.append([int(line.strip().split("\t")[-1])])
            self.valid_label = np.array(labels)
        return self.valid_label

    def get_test_label(self):
        """
        get test data label
        """
        if self.test_label.size == 0:
            labels = []
255
            with io.open(self.args.test_data_dir, "r", encoding="utf8") as f:
Y
Yibing Liu 已提交
256 257 258 259
                for line in f:
                    labels.append([int(line.strip().split("\t")[-1])])
            self.test_label = np.array(labels)
        return self.test_label