-
Notifications
You must be signed in to change notification settings - Fork 45
/
data_loader.py
64 lines (47 loc) · 2.37 KB
/
data_loader.py
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
# -*- coding: utf-8 -*-
"""
@author: alexyang
@contact: [email protected]
@file: data_loader.py
@time: 2019/1/5 10:03
@desc:
"""
import os
from utils import pickle_load
def load_input_data(data_folder, data_kind, level, use_text_input, use_text_input_l, use_text_input_r,
use_text_input_r_with_pad, use_aspect_input, use_aspect_text_input, use_loc_input,
use_offset_input, use_mask):
dirname = os.path.join('./data', data_folder)
input_data = []
if use_text_input:
input_data.append(pickle_load(os.path.join(dirname, '{}_{}_input.pkl'.format(data_kind, level))))
if use_text_input_l:
input_data.append(pickle_load(os.path.join(dirname, '{}_{}_input_l.pkl'.format(data_kind, level))))
if use_text_input_r:
input_data.append(pickle_load(os.path.join(dirname, '{}_{}_input_r.pkl'.format(data_kind, level))))
if use_text_input_r_with_pad:
input_data.append(pickle_load(os.path.join(dirname, '{}_{}_input_r_with_pad.pkl'.format(data_kind, level))))
if use_aspect_input:
input_data.append(pickle_load(os.path.join(dirname, '{}_aspect_input.pkl'.format(data_kind))))
if use_aspect_text_input:
input_data.append(pickle_load(os.path.join(dirname, '{}_{}_aspect_input.pkl'.format(data_kind, level))))
if use_loc_input:
input_data.append(pickle_load(os.path.join(dirname, '{}_{}_pos_input.pkl'.format(data_kind, level))))
if use_offset_input:
input_data.append(pickle_load(os.path.join(dirname, '{}_{}_offset_input.pkl'.format(data_kind, level))))
if use_mask:
input_data.append(pickle_load(os.path.join(dirname, '{}_{}_mask.pkl'.format(data_kind, level))))
if len(input_data) == 1:
input_data = input_data[0]
if len(input_data) == 0:
raise Exception('No Input!')
return input_data
def load_label(data_folder, data_kind):
dirname = os.path.join('./data', data_folder)
return pickle_load(os.path.join(dirname, '{}_label.pkl'.format(data_kind)))
def load_vocab(data_folder, vocab_type):
dirname = os.path.join('./data', data_folder)
return pickle_load(os.path.join(dirname, vocab_type+'_vocab.pkl'))
def load_idx2token(data_folder, vocab_type):
vocab = load_vocab(data_folder, vocab_type)
return dict((idx, word) for word, idx in vocab.items())