-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
220 lines (189 loc) · 7.28 KB
/
Copy pathutils.py
File metadata and controls
220 lines (189 loc) · 7.28 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import random
import torch
import numpy as np
def read_corpus(filepath):
""" Read corpus from the given file path.
Args:
filepath: file path of the corpus
Returns:
sentences: a list of sentences, each sentence is a list of str
tags: corresponding tags
"""
sentences, tags = [], []
sent, tag = ['<START>'], ['<START>']
with open(filepath, 'r', encoding='utf8') as f:
for line in f:
if line == '\n':
if len(sent) > 1:
sentences.append(sent + ['<END>'])
tags.append(tag + ['<END>'])
sent, tag = ['<START>'], ['<START>']
else:
line = line.split()
sent.append(line[0])
tag.append(line[1])
return sentences, tags
def get_word_embedding(pretrained_word_embedding_file):
word_embedding = {}
f = open(pretrained_word_embedding_file, encoding="utf-8")
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
word_embedding[word] = coefs
f.close()
return word_embedding
def read_inference(filepath):
sentences = []
sent= ['<START>']
with open(filepath, 'r', encoding='utf8') as f:
for line in f:
if line == '\n':
if len(sent) > 1:
sentences.append(sent + ['<END>'])
sent = ['<START>']
else:
line = line.split()
sent.append(line[0])
return sentences
def read_inference_docid(filepath):
sentences = []
ids = []
sent= ['<START>']
id = [""]
with open(filepath, 'r', encoding='utf8') as f:
for line in f:
if line == '\n':
if len(sent) > 1:
sentences.append(sent + ['<END>'])
ids.append(id + [""])
sent = ['<START>']
id = [""]
else:
line = line.split()
sent.append(line[0])
id.append(line[1])
return sentences, ids
def generate_train_dev_dataset(filepath, sent_vocab, tag_vocab, train_proportion=0.8):
""" Read corpus from given file path and split it into train and dev parts
Args:
filepath: file path
sent_vocab: sentence vocab
tag_vocab: tag vocab
train_proportion: proportion of training data
Returns:
train_data: data for training, list of tuples, each containing a sentence and corresponding tag.
dev_data: data for development, list of tuples, each containing a sentence and corresponding tag.
"""
sentences, tags = read_corpus(filepath)
sentences = words2indices(sentences, sent_vocab)
tags = words2indices(tags, tag_vocab)
data = list(zip(sentences, tags))
random.shuffle(data)
n_train = int(len(data) * train_proportion)
train_data, dev_data = data[: n_train], data[n_train:]
return train_data, dev_data
def generate_train_or_dev_dataset(filepath, sent_vocab, tag_vocab):
sentences, tags = read_corpus(filepath)
sentences = words2indices(sentences, sent_vocab)
tags = words2indices(tags, tag_vocab)
data = list(zip(sentences, tags))
random.shuffle(data)
return data
def batch_iter(data, batch_size=32, shuffle=True):
""" Yield batch of (sent, tag), by the reversed order of source length.
Args:
data: list of tuples, each tuple contains a sentence and corresponding tag.
batch_size: batch size
shuffle: bool value, whether to random shuffle the data
"""
data_size = len(data)
indices = list(range(data_size))
if shuffle:
random.shuffle(indices)
batch_num = (data_size + batch_size - 1) // batch_size
for i in range(batch_num):
batch = [data[idx] for idx in indices[i * batch_size: (i + 1) * batch_size]]
batch = sorted(batch, key=lambda x: len(x[0]), reverse=True)
sentences = [x[0] for x in batch]
tags = [x[1] for x in batch]
yield sentences, tags
def batch_iter_inf(data, batch_size=32, shuffle=True):
""" Yield batch of (sent, tag), by the reversed order of source length.
Args:
data: list of tuples, each tuple contains a sentence and corresponding tag.
batch_size: batch size
shuffle: bool value, whether to random shuffle the data
"""
data_size = len(data)
indices = list(range(data_size))
if shuffle:
random.shuffle(indices)
batch_num = (data_size + batch_size - 1) // batch_size
for i in range(batch_num):
batch = [data[idx] for idx in indices[i * batch_size: (i + 1) * batch_size]]
batch = sorted(batch, key=lambda x: len(x[0]), reverse=True)
sentences = [x[0] for x in batch]
yield sentences
def words2indices(origin, vocab):
""" Transform a sentence or a list of sentences from str to int
Args:
origin: a sentence of type list[str], or a list of sentences of type list[list[str]]
vocab: Vocab instance
Returns:
a sentence or a list of sentences represented with int
"""
if isinstance(origin[0], list):
result = [[vocab[w] for w in sent] for sent in origin]
else:
result = [vocab[w] for w in origin]
return result
def indices2words(origin, vocab):
""" Transform a sentence or a list of sentences from int to str
Args:
origin: a sentence of type list[int], or a list of sentences of type list[list[int]]
vocab: Vocab instance
Returns:
a sentence or a list of sentences represented with str
"""
if isinstance(origin[0], list):
result = [[vocab.id2word(w) for w in sent] for sent in origin]
else:
result = [vocab.id2word(w) for w in origin]
return result
def pad(data, padded_token, device):
""" pad data so that each sentence has the same length as the longest sentence
Args:
data: list of sentences, List[List[word]]
padded_token: padded token
device: device to store data
Returns:
padded_data: padded data, a tensor of shape (max_len, b)
lengths: lengths of batches, a list of length b.
"""
lengths = [len(sent) for sent in data]
max_len = max(lengths) #lengths[0]
padded_data = []
for s in data:
padded_data.append(s + [padded_token] * (max_len - len(s)))
return torch.tensor(padded_data, device=device), lengths
def print_var(**kwargs):
for k, v in kwargs.items():
print(k, v)
def generate_weights_metrics(sent_vocab):
word_emb = get_word_embedding('./vocab/glove.6B.50d.txt')
#sent_vocab = Vocab.load(args_sent_vocab)
word2id = sent_vocab.get_word2id()
matrix_len = len(word2id)
weights_matrix = np.zeros((matrix_len, 50))
for i, word in enumerate(word2id):
try:
weights_matrix[i] = word_emb[word]
except KeyError:
weights_matrix[i] = np.random.normal(scale=0.6, size=(50,))
return torch.tensor(weights_matrix)
def main():
sentences, tags = read_corpus('data/train.txt')
print(len(sentences), len(tags))
if __name__ == '__main__':
main()