reader.py 8.1 KB
Newer Older
Y
Yibing Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
"""
SimNet reader
"""

import logging
import numpy as np


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([])

    def get_reader(self, mode):
        """
        Get Reader
        """

        def reader_with_pairwise():
            """
                Reader with Pairwise
            """
            if mode == "valid":
                with open(self.args.valid_data_dir) 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]
                        if len(query) == 0:
                            query = [0]
                        if len(title) == 0:
                            title = [0]
                        yield [query, title]
            elif mode == "test":
                with open(self.args.test_data_dir) 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]
                        if len(query) == 0:
                            query = [0]
                        if len(title) == 0:
                            title = [0]
                        yield [query, title]
            else:
                with open(self.args.train_data_dir) 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]

        def reader_with_pointwise():
            """
            Reader with Pointwise
            """
            if mode == "valid":
                with open(self.args.valid_data_dir) 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]
                        if len(query) == 0:
                            query = [0]
                        if len(title) == 0:
                            title = [0]
                        yield [query, title]
            elif mode == "test":
                with open(self.args.test_data_dir) 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]
                        if len(query) == 0:
                            query = [0]
                        if len(title) == 0:
                            title = [0]
                        yield [query, title]
            else:
                with open(self.args.train_data_dir) 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]

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

    def get_infer_reader(self):
        """
        get infer reader
        """
        with open(self.args.infer_data_dir, "r") as file:
            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
                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]
                if len(query) == 0:
                    query = [0]
                if len(title) == 0:
                    title = [0]
                yield [query, title]

    def get_infer_data(self):
        """
        get infer data
        """
        with open(self.args.infer_data_dir, "r") as file:
            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 = []
            with open(self.args.valid_data_dir, "r") as f:
                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 = []
            with open(self.args.test_data_dir, "r") as f:
                for line in f:
                    labels.append([int(line.strip().split("\t")[-1])])
            self.test_label = np.array(labels)
        return self.test_label