reader.py 3.6 KB
Newer Older
T
tangwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   Copyright (c) 2020 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.
T
tangwei 已提交
14

T
tangwei 已提交
15 16
from __future__ import print_function
import abc
T
tangwei 已提交
17
import os
X
test  
xjqbest 已提交
18
from functools import reduce
T
tangwei 已提交
19
import paddle.fluid.incubate.data_generator as dg
T
tangwei 已提交
20
import yaml
21
from paddlerec.core.utils import envs
T
tangwei 已提交
22 23


C
Chengmo 已提交
24
class ReaderBase(dg.MultiSlotDataGenerator):
T
tangwei 已提交
25 26
    __metaclass__ = abc.ABCMeta

T
tangwei 已提交
27
    def __init__(self, config):
T
tangwei 已提交
28
        dg.MultiSlotDataGenerator.__init__(self)
X
test  
xjqbest 已提交
29 30 31
        _config = envs.load_yaml(config)
        envs.set_global_envs(_config)
        envs.update_workspace()
T
tangwei 已提交
32

T
tangwei 已提交
33 34
    @abc.abstractmethod
    def init(self):
C
Chengmo 已提交
35
        """init """
T
tangwei 已提交
36 37 38 39 40
        pass

    @abc.abstractmethod
    def generate_sample(self, line):
        pass
X
xujiaqi01 已提交
41 42 43 44 45 46 47


class SlotReader(dg.MultiSlotDataGenerator):
    __metaclass__ = abc.ABCMeta

    def __init__(self, config):
        dg.MultiSlotDataGenerator.__init__(self)
X
test  
xjqbest 已提交
48 49 50
        _config = envs.load_yaml(config)
        envs.set_global_envs(_config)
        envs.update_workspace()
X
xujiaqi01 已提交
51 52 53

    def init(self, sparse_slots, dense_slots, padding=0):
        from operator import mul
X
fix  
xjqbest 已提交
54
        self.sparse_slots = []
X
fix  
xjqbest 已提交
55 56
        if sparse_slots.strip() != "#" and sparse_slots.strip(
        ) != "?" and sparse_slots.strip() != "":
X
fix  
xjqbest 已提交
57 58
            self.sparse_slots = sparse_slots.strip().split(" ")
        self.dense_slots = []
X
fix  
xjqbest 已提交
59 60
        if dense_slots.strip() != "#" and dense_slots.strip(
        ) != "?" and dense_slots.strip() != "":
X
fix  
xjqbest 已提交
61
            self.dense_slots = dense_slots.strip().split(" ")
T
tangwei 已提交
62 63 64 65 66
        self.dense_slots_shape = [
            reduce(mul,
                   [int(j) for j in i.split(":")[1].strip("[]").split(",")])
            for i in self.dense_slots
        ]
X
xujiaqi01 已提交
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
        self.dense_slots = [i.split(":")[0] for i in self.dense_slots]
        self.slots = self.dense_slots + self.sparse_slots
        self.slot2index = {}
        self.visit = {}
        for i in range(len(self.slots)):
            self.slot2index[self.slots[i]] = i
            self.visit[self.slots[i]] = False
        self.padding = padding

    def generate_sample(self, l):
        def reader():
            line = l.strip().split(" ")
            output = [(i, []) for i in self.slots]
            for i in line:
                slot_feasign = i.split(":")
                slot = slot_feasign[0]
                if slot not in self.slots:
                    continue
                if slot in self.sparse_slots:
                    feasign = int(slot_feasign[1])
                else:
                    feasign = float(slot_feasign[1])
                output[self.slot2index[slot]][1].append(feasign)
                self.visit[slot] = True
            for i in self.visit:
                slot = i
                if not self.visit[slot]:
                    if i in self.dense_slots:
T
tangwei 已提交
95 96 97
                        output[self.slot2index[i]][1].extend(
                            [self.padding] *
                            self.dense_slots_shape[self.slot2index[i]])
X
xujiaqi01 已提交
98 99 100 101 102
                    else:
                        output[self.slot2index[i]][1].extend([self.padding])
                else:
                    self.visit[slot] = False
            yield output
T
tangwei 已提交
103

X
xujiaqi01 已提交
104
        return reader