-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data.py
182 lines (145 loc) · 5.54 KB
/
get_data.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
176
177
178
179
180
181
182
import pandas as pd
import numpy as np
from gensim.models import Word2Vec
import os
def _join_path(path, *paths):
if not paths:
return os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
else:
rst = os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
for i in paths:
rst = os.path.join(rst, i)
return rst
def get_label():
label = []
with open(_join_path('txt_data','id2tag.txt')) as f:
for l in f:
label.append(l.split('\t')[0])
return label
class Seqdata():
def __init__(self, embedding_size, is_train):
self._M_train, self._M_test, self._max_len = np.load(os.path.join(os.getcwd(), 'meta.npy'))[()]
self._label = self.get_label()
self._model = Word2Vec.load(_join_path('w2v_model', 'word2vec.model'))
self._vocab = self.get_vocab()
self._index_in_epoch = 0
self._embedding_size = embedding_size
if is_train:
self._data = self._M_train
else:
self._data = self._M_test
self._num_examples = len(self._data)
self._epochs_completed = 0
def get_label(self):
label = []
with open(_join_path('txt_data','id2tag.txt')) as f:
for l in f:
label.append(l.split('\t')[0])
return label
def get_vocab(self):
vocab_list = []
with open(_join_path('vocab.txt'), 'r', encoding='utf-8') as f:
for l in f:
if int(l.split('\t')[1].split('\n')[0]) > 2:
vocab_list.append(l.split('\t')[0])
return list(set(vocab_list))
def next_batch(self, batch_size):
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
self._epochs_completed += 1
self._data = self._data.sample(frac=1).reset_index(drop=True)
start = 0
self._index_in_epoch = batch_size
end = self._index_in_epoch
M_batch = self._data[start : end]
X_batch, Y_batch = self.seq2batch(M_batch, batch_size)
return X_batch, Y_batch
def seq2batch(self, M_batch, batch_size):
X_batch = np.zeros(shape=[batch_size, self._max_len, self._embedding_size])
Y_batch = np.zeros(shape=[batch_size])
for m, row in enumerate(M_batch.iterrows()):
seq = row[1].seq
class_n = np.array([self._label.index(row[1].class_number)], np.int64)
X_array = np.zeros(shape=[len(seq), self._embedding_size])
for n, word in enumerate(seq):
try:
vec = np.array(self._model[word], dtype=np.float32)[np.newaxis, :]
except KeyError:
vec = np.zeros(shape=[1, self._embedding_size], dtype=np.float32)
X_array[n, :] = vec
X_batch[m, 0:X_array.shape[0], :] = X_array
Y_batch[m] = class_n
X_batch = X_batch[:, :, :, np.newaxis]
return X_batch, Y_batch
@property
def M_train(self):
return self._M_train
@property
def M_test(self):
return self._M_test
@property
def max_len(self):
return self._max_len
@property
def label(self):
return self._label
@property
def model(self):
return self._model
@property
def vocab(self):
return self._vocab
def get_meta():
meta_train_data = []
with open(_join_path('train_cut.txt'), 'r', encoding='utf-8') as f:
for l in f:
label = l.split('\t')[0]
data = l.split('\t')[1].split('\n')[0].split(' ')
meta_train_data.append([label, data])
M_train = pd.DataFrame(meta_train_data)
M_train.columns = ['class_number', 'seq']
M_train = M_train.sample(frac=1).reset_index(drop=True)
meta_test_data = []
with open(_join_path('test_cut.txt'), 'r', encoding='utf-8') as f:
for l in f:
label = l.split('\t')[0]
data = l.split('\t')[1].split('\n')[0].split(' ')
meta_test_data.append([label, data])
M_test = pd.DataFrame(meta_test_data)
M_test.columns = ['class_number', 'seq']
M_test = M_test.sample(frac=1).reset_index(drop=True)
max_len = np.max([len(row[1].seq) for row in M_train.iterrows()] +
[len(row[1].seq) for row in M_test.iterrows()])
return M_train, M_test, max_len
def get_batch2(vocab, batch_size = None, M_train = None, M_test= None, label= None, max_len = None, is_train = True):
if is_train == True:
M_batch = M_train.sample(32)
else:
M_batch = M_test.sample(32)
X_batch = np.zeros(shape=[batch_size, max_len])
Y_batch = np.zeros(shape=[batch_size])
for m, row in enumerate(M_batch.iterrows()):
seq = row[1].seq
class_n = np.array([label.index(row[1].class_number)], np.int64)
X_array = np.zeros(shape=[max_len])
for n, word in enumerate(seq):
try:
X_array[n] = int(vocab.index(word))
except ValueError:
pass
X_batch[m, :] = X_array
Y_batch[m] = class_n
X_batch = np.array(X_batch, np.int32)
return X_batch, Y_batch
import jieba
def process(str, model):
str = jieba.cut(str, cut_all=False)
X_batch = np.zeros(shape=[1, 25, 64])
for n, word in enumerate(str):
try:
vec = np.array(model[word], dtype=np.float32)[np.newaxis, :]
except KeyError:
vec = np.zeros(shape=[1, 64], dtype=np.float32)
X_batch[0, n, :] = vec
return X_batch