-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdata_helper.py
175 lines (134 loc) · 5.52 KB
/
data_helper.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
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
import numpy as np
import random
import json
from cnn_multilabel_classification.config import BaseConfig
import copy
config = BaseConfig()
append_tag = config.append_tag
# load chinese data from json file
def load_cn_data_from_json_file(_data_path, word_cut, data_limit):
q_label_file = _data_path.get('data_path', None)
labels_file = _data_path.get('labels_path', None)
# 保存模型的输出对应类别
_classes = list()
with open(labels_file, "r") as f:
for line in f.readlines():
line_tmp = line.strip()
_classes.append(line_tmp)
num_classes = len(_classes)
with open(q_label_file, "r") as f_json:
json_data = json.load(f_json)
if data_limit:
json_data = dict(data for i, data in enumerate(json_data.items()) if i < data_limit)
sentences_cut = list()
outputs = list()
sources = list()
if word_cut:
from tools.word_cut import WordCutHelper
wh = WordCutHelper(1)
print('cut sentences')
sentences_tag = list()
for sentence, values in json_data.items():
tag_word = wh.getTagAndWord(sentence)
if append_tag:
# process tag
tag = tag_word['tag'][:config.sentence_words_num]
tag_tmp = np.zeros((config.sentence_words_num, len(config.tags_table)))
idx_list = map(lambda x: config.tags_table.index(x), tag)
for i, idx in enumerate(idx_list):
tag_tmp[i, idx] = 1
sentences_tag.append(tag_tmp)
else:
pass
# process sentence
value = tag_word['word'][:config.sentence_words_num]
sentences_cut.append(' '.join(value))
# process label
indices = map(lambda x: _classes.index(x), values['label'])
labels_tmp = np.zeros(num_classes)
# process source
sources.append(values.get('source', None))
for idx in indices:
labels_tmp[idx] = 1
outputs.append(labels_tmp)
if len(sentences_cut) == len(sentences_tag):
return list(zip(sentences_cut, outputs, sources, sentences_tag)), _classes
else:
return list(zip(sentences_cut, outputs, sources)), _classes
else:
for sentence, values in json_data.items():
# process sentence
sentences_cut.append(sentence.strip())
# process label
indices = map(lambda x: _classes.index(x), values['label'])
labels_tmp = np.zeros(num_classes)
# process source
sources.append(values.get('source', None))
for idx in indices:
labels_tmp[idx] = 1
outputs.append(labels_tmp)
return list(zip(sentences_cut, outputs, sources)), _classes
def load_data(_data_path, valid_portion,
sort_by_len=False, enhance=True, reverse=False,
word_cut=False, data_limit=None):
data_set, _classes = load_cn_data_from_json_file(_data_path, word_cut, data_limit)
# 数据集扩增(打乱词顺序,增加新样本)
if enhance:
# shuffle
enhanced_data = list()
for i, data in enumerate(data_set):
data_new = list(copy.deepcopy(data))
sentence_cut = data_new[0].split(' ')
random.shuffle(sentence_cut)
data_new[0] = ' '.join(sentence_cut)
enhanced_data.append(tuple(data_new))
data_set.extend(enhanced_data)
else:
pass
if reverse:
for data in data_set:
data[0].reverse()
else:
pass
random.shuffle(data_set)
n_samples = len(data_set)
n_train = int(np.round(n_samples * (1.0 - valid_portion)))
print("Train/Dev split: {:d}/{:d}".format(n_train, (n_samples - n_train)))
train_set = [data for data in data_set[: n_train]]
dev_set = [data for data in data_set[n_train:]]
if sort_by_len:
sorted_indices = len_argsort(dev_set[0])
dev_set = [dev_set[i] for i in sorted_indices]
sorted_indices = len_argsort(train_set[0])
train_set = [train_set[i] for i in sorted_indices]
return train_set, dev_set, _classes
def len_argsort(seq):
return sorted(range(len(seq)), key=lambda x: len(seq[x]))
def batch_iter(data, batch_size, num_epochs, shuffle=True):
"""
Generates a batch iterator for a data set.
"""
data_size = len(data)
num_batches_per_epoch = int((len(data)-1)/batch_size) + 1
for epoch in range(num_epochs):
# Shuffle the data at each epoch
if shuffle:
shuffle_indices = list(range(data_size))
random.shuffle(shuffle_indices)
shuffled_data = []
for shuffle_indice in shuffle_indices:
shuffled_data.append(data[shuffle_indice])
else:
shuffled_data = data
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]
if __name__ == '__main__':
data_path = {'train_data_path': '../work_space/cmrc2018/dataset/out_ann.txt2',
'labels_path': '../work_space/cmrc2018/dataset/labels'}
# data, classes = load_cn_data_from_json_file(data_path, word_cut=True, data_limit=None)
load_data(data_path, valid_portion=0.2,
sort_by_len=True, enhance=True, reverse=True,
word_cut=False, data_limit=100)
print('====Done====')