dataprovider.py 2.7 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# 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.

from paddle.trainer.PyDataProvider2 import *

UNK_IDX = 0


Z
zhangjinchao01 已提交
20
def hook(settings, word_dict, label_dict, predicate_dict, **kwargs):
Z
zhangjinchao01 已提交
21 22
    settings.word_dict = word_dict
    settings.label_dict = label_dict
Z
zhangjinchao01 已提交
23
    settings.predicate_dict = predicate_dict
Y
Yu Yang 已提交
24

Z
zhangjinchao01 已提交
25 26
    #all inputs are integral and sequential type
    settings.slots = [
Z
zhangjinchao01 已提交
27
        integer_value_sequence(len(word_dict)),
28 29 30 31
        integer_value_sequence(len(word_dict)),
        integer_value_sequence(len(word_dict)),
        integer_value_sequence(len(word_dict)),
        integer_value_sequence(len(word_dict)),
Y
Yu Yang 已提交
32 33
        integer_value_sequence(len(word_dict)),
        integer_value_sequence(len(predicate_dict)), integer_value_sequence(2),
34 35
        integer_value_sequence(len(label_dict))
    ]
Z
zhangjinchao01 已提交
36 37


Z
zhangjinchao01 已提交
38 39 40
def get_batch_size(yeild_data):
    return len(yeild_data[0])

Y
Yu Yang 已提交
41 42 43 44 45

@provider(
    init_hook=hook,
    should_shuffle=True,
    calc_batch_size=get_batch_size,
P
Peng Li 已提交
46
    can_over_batch_size=True,
Y
Yu Yang 已提交
47
    cache=CacheType.CACHE_PASS_IN_MEM)
Z
zhangjinchao01 已提交
48
def process(settings, file_name):
Z
zhangjinchao01 已提交
49 50
    with open(file_name, 'r') as fdata:
        for line in fdata:
Z
zhangjinchao01 已提交
51
            sentence, predicate, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2,  mark, label = \
Z
zhangjinchao01 已提交
52
                line.strip().split('\t')
Y
Yu Yang 已提交
53

Z
zhangjinchao01 已提交
54 55
            words = sentence.split()
            sen_len = len(words)
Z
zhangjinchao01 已提交
56
            word_slot = [settings.word_dict.get(w, UNK_IDX) for w in words]
Z
zhangjinchao01 已提交
57

Z
zhangjinchao01 已提交
58 59 60 61 62 63
            predicate_slot = [settings.predicate_dict.get(predicate)] * sen_len
            ctx_n2_slot = [settings.word_dict.get(ctx_n2, UNK_IDX)] * sen_len
            ctx_n1_slot = [settings.word_dict.get(ctx_n1, UNK_IDX)] * sen_len
            ctx_0_slot = [settings.word_dict.get(ctx_0, UNK_IDX)] * sen_len
            ctx_p1_slot = [settings.word_dict.get(ctx_p1, UNK_IDX)] * sen_len
            ctx_p2_slot = [settings.word_dict.get(ctx_p2, UNK_IDX)] * sen_len
Z
zhangjinchao01 已提交
64 65 66 67 68

            marks = mark.split()
            mark_slot = [int(w) for w in marks]

            label_list = label.split()
Z
zhangjinchao01 已提交
69
            label_slot = [settings.label_dict.get(w) for w in label_list]
Z
zhangjinchao01 已提交
70 71
            yield word_slot, ctx_n2_slot, ctx_n1_slot, \
                  ctx_0_slot, ctx_p1_slot, ctx_p2_slot, predicate_slot, mark_slot, label_slot