reader.py 4.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
"""
The file_reader converts raw corpus to input.
"""
17

Y
Yibing Liu 已提交
18 19 20 21 22 23 24 25
import os
import argparse
import __future__
import io
import glob


def load_kv_dict(dict_path,
L
Li Fuchen 已提交
26 27 28 29
                 reverse=False,
                 delimiter="\t",
                 key_func=None,
                 value_func=None):
Y
Yibing Liu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    """
    Load key-value dict from file
    """
    result_dict = {}
    for line in io.open(dict_path, "r", encoding='utf8'):
        terms = line.strip("\n").split(delimiter)
        if len(terms) != 2:
            continue
        if reverse:
            value, key = terms
        else:
            key, value = terms
        if key in result_dict:
            raise KeyError("key duplicated with [%s]" % (key))
        if key_func:
            key = key_func(key)
        if value_func:
            value = value_func(value)
        result_dict[key] = value
    return result_dict


class Dataset(object):
    """data reader"""
L
Li Fuchen 已提交
54

Y
Yibing Liu 已提交
55 56
    def __init__(self, args, mode="train"):
        # read dict
L
Li Fuchen 已提交
57 58
        self.word2id_dict = load_kv_dict(
            args.word_dict_path, reverse=True, value_func=int)
Y
Yibing Liu 已提交
59
        self.id2word_dict = load_kv_dict(args.word_dict_path)
L
Li Fuchen 已提交
60 61
        self.label2id_dict = load_kv_dict(
            args.label_dict_path, reverse=True, value_func=int)
Y
Yibing Liu 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        self.id2label_dict = load_kv_dict(args.label_dict_path)
        self.word_replace_dict = load_kv_dict(args.word_rep_dict_path)

    @property
    def vocab_size(self):
        """vocabuary size"""
        return max(self.word2id_dict.values()) + 1

    @property
    def num_labels(self):
        """num_labels"""
        return max(self.label2id_dict.values()) + 1

    def get_num_examples(self, filename):
        """num of line of file"""
77
        return sum(1 for line in io.open(filename, "r", encoding='utf8'))
Y
Yibing Liu 已提交
78 79 80 81 82

    def word_to_ids(self, words):
        """convert word to word index"""
        word_ids = []
        for word in words:
83
            word = self.word_replace_dict.get(word, word)
Y
Yibing Liu 已提交
84 85 86 87
            if word not in self.word2id_dict:
                word = "OOV"
            word_id = self.word2id_dict[word]
            word_ids.append(word_id)
88

Y
Yibing Liu 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        return word_ids

    def label_to_ids(self, labels):
        """convert label to label index"""
        label_ids = []
        for label in labels:
            if label not in self.label2id_dict:
                label = "O"
            label_id = self.label2id_dict[label]
            label_ids.append(label_id)
        return label_ids

    def file_reader(self, filename, max_seq_len=64, mode="train"):
        """
        yield (word_idx, target_idx) one by one from file,
            or yield (word_idx, ) in `infer` mode
        """
L
Li Fuchen 已提交
106

Y
Yibing Liu 已提交
107 108 109 110
        def wrapper():
            fread = io.open(filename, "r", encoding="utf-8")
            if mode == "infer":
                for line in fread:
111
                    words = line.strip()
Y
Yibing Liu 已提交
112
                    word_ids = self.word_to_ids(words)
113
                    yield (word_ids[0:max_seq_len], )
Y
Yibing Liu 已提交
114
            else:
115 116
                headline = next(fread)
                headline = headline.strip().split('\t')
117 118
                assert len(headline) == 2 and headline[
                    0] == "text_a" and headline[1] == "label"
Y
Yibing Liu 已提交
119 120
                for line in fread:
                    words, labels = line.strip("\n").split("\t")
121
                    if len(words) < 1:
122
                        continue
Y
Yibing Liu 已提交
123 124 125 126 127 128 129 130 131 132 133
                    word_ids = self.word_to_ids(words.split("\002"))
                    label_ids = self.label_to_ids(labels.split("\002"))
                    assert len(word_ids) == len(label_ids)
                    yield word_ids[0:max_seq_len], label_ids[0:max_seq_len]
            fread.close()

        return wrapper


if __name__ == "__main__":
    parser = argparse.ArgumentParser(__doc__)
L
Li Fuchen 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    parser.add_argument(
        "--word_dict_path",
        type=str,
        default="./conf/word.dic",
        help="word dict")
    parser.add_argument(
        "--label_dict_path",
        type=str,
        default="./conf/tag.dic",
        help="label dict")
    parser.add_argument(
        "--word_rep_dict_path",
        type=str,
        default="./conf/q2b.dic",
        help="word replace dict")
Y
Yibing Liu 已提交
149 150 151 152 153 154 155
    args = parser.parse_args()
    dataset = Dataset(args)
    data_generator = dataset.file_reader("data/train.tsv")
    for word_idx, target_idx in data_generator():
        print(word_idx, target_idx)
        print(len(word_idx), len(target_idx))
        break