This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_encoder.py
342 lines (287 loc) · 16.1 KB
/
text_encoder.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import os, argparse, datetime
from pathlib import Path
import numpy as np
from core import utils
from basis import constants
from basis.logger import logger
from datasets.datasets import *
def get_model(encoder, model=None):
if model is None:
model = constants.ENCODERS[encoder]['default_model']
# if '/' in model:
if os.sep in model:
# parts = model.split('/')
parts = os.path.split(model)
source_model = parts[0]
model = parts[1]
else:
source_model = None
return source_model, model, os.path.join(source_model, model) if source_model is not None else model
def load_processed_dataset(name, question_id, **kwargs):
path = Path(config.ROOT_PATH) / 'data' / 'datasets' / 'processed'
if name in constants.DATASETS:
path /= constants.DATASETS[name]['processed_folder']
else:
path /= name.lower()
if not os.path.exists(path):
raise utils.InvalidNameError('dataset', name)
# dataset = utils.load(path / 'data.txt')
if question_id is not None:
dataset = utils.load(path / question_id / 'data.txt')
else:
dataset = utils.load(path / 'data.txt')
logger.info('text_encoder.load_processed_dataset: {}'.format(str(dataset)))
return dataset
def encode_dataset(name, question_id, encoder, model=None, save=True, **kwargs):
checkpoint_name = kwargs.get('checkpoint_name', None)
dimension = kwargs.get('dimension', None)
mapping = kwargs.get('mapping', None)
scale = 0.7 if name == 'USCIS' or (name == 'SEB3' and mapping == '2way') \
else (0.6 if (name == 'ASAPSAS' and mapping == '2way') else 0.5)
min_frequency = kwargs.get('min_frequency', 0)
lemmatize = not kwargs.get('not_lemmatize', False)
remove_stop_words = not kwargs.get('not_remove_stop_words', False)
# path = Path(config.ROOT_PATH) / 'data' / 'datasets' / 'processed'
# if name in constants.DATASETS:
# path /= constants.DATASETS[name]['processed_folder']
# else:
# path /= name.lower()
# if not os.path.exists(path):
# raise utils.InvalidNameError('dataset', name)
# source_model, model, _ = get_model(encoder, model=model)
# start_time = datetime.datetime.now()
# # dataset = utils.load(path / 'data.txt')
# if question_id is not None:
# dataset = utils.load(path / question_id / 'data.txt')
# else:
# dataset = utils.load(path / 'data.txt')
# logger.info(str(dataset))
start_time = datetime.datetime.now()
source_model, model, _ = get_model(encoder, model=model)
dataset = kwargs.get('dataset', None) or load_processed_dataset(name, question_id, **kwargs)
# output_path = 'data/datasets/encoded/{}/{}'.format(encoder, dataset.name)
if encoder == 'skip_thoughts':
from datasets.encoders import SkipThoughts
checkpoint_name = checkpoint_name if checkpoint_name is not None else constants.ENCODERS[encoder]['default_checkpoint_name']
encoded_dataset = SkipThoughts(model=model, checkpoint_name=checkpoint_name).encode(dataset)
# elif encoder == 'google_universal_sentence_encoder':
elif 'google_universal_sentence_encoder' in encoder:
from datasets.encoders import GoogleUniversalSentenceEncoder
encoded_dataset = GoogleUniversalSentenceEncoder(
model=model, multilingual='multilingual' in encoder).encode(dataset)
elif encoder == 'glove':
from datasets.encoders import Glove
encoded_dataset = Glove(source_model=source_model, ngram_range=(1, 3), min_frequency=min_frequency,
lemmatize=lemmatize, remove_stop_words=remove_stop_words).encode(dataset)
elif encoder == 'bert':
from datasets.encoders import Bert
encoded_dataset = Bert(source_model=source_model, dataset_name=model,
ngram_range=(1, 3), min_frequency=min_frequency, lemmatize=lemmatize,
remove_stop_words=remove_stop_words, oov_handler='avg').encode(dataset)
elif encoder == 'fasttext':
from datasets.encoders import FastText
encoded_dataset = FastText(remove_stop_words=remove_stop_words).encode(dataset)
elif encoder == 'tfidf':
from datasets.encoders import TFIDF
encoded_dataset = TFIDF(ngram_range=(1, 3), min_frequency=min_frequency, retrain=False).encode(dataset)
elif encoder == 'count':
from datasets.encoders import Count
encoded_dataset = Count(ngram_range=(1, 3), min_frequency=min_frequency, retrain=False).encode(dataset)
elif encoder == 'lsa':
from datasets.encoders import LSA
encoded_dataset = LSA(ngram_range=(1, 3), min_frequency=min_frequency, n_components=100, retrain=False).encode(dataset)
elif encoder == 'jaccard_similarity':
from datasets.encoders import JaccardSimilarity
encoded_dataset = JaccardSimilarity(lemmatize=lemmatize, remove_stop_words=remove_stop_words).encode(dataset)
else:
raise utils.InvalidNameError('encoder', encoder)
if save:
output_path = Path(config.ROOT_PATH) / 'data' / 'datasets' / 'encoded' \
/ encoded_dataset.encoder.name / encoded_dataset.encoder.model
if name in constants.DATASETS:
output_path /= constants.DATASETS[name]['processed_folder']
else:
output_path /= encoded_dataset.name.lower()
utils.save(encoded_dataset, output_path, 'dataset.txt')
is_preset = isinstance(dataset, PresetDataset)
for question in encoded_dataset.questions:
question_folder_path = output_path / question.id
dataset = SubDataset(encoded_dataset=encoded_dataset, question=question)
utils.save(dataset, question_folder_path, 'dataset.dat')
if not os.path.exists(question_folder_path):
utils.create_directories(question_folder_path)
np.save(question_folder_path / 'data', dataset.data, allow_pickle=True)
if not is_preset:
continue
if name == 'USCIS':
mapping = '2way'
elif name.startswith('SEB'):
if name == 'SEB2':
mapping = '2way'
elif name == 'SEB3' and (mapping is None or (mapping not in ['2way', '3way'])):
mapping = '3way'
elif name == 'SEB5' and (mapping is None or (mapping not in ['2way', '3way', '5way'])):
mapping = '5way'
labels = np.array([a.get_answer_class(mapping, scale, dataset=dataset,
question=question) for a in dataset.answers])
if (name == 'SEB3' and mapping != '3way') or (name == 'SEB5' and mapping != '5way') \
or (name == 'ASAPSAS' and mapping is not None):
# labels_path = '{}/{}/{}'.format(output_path, question.id, mapping)
labels_path = question_folder_path / mapping
else:
# labels_path = '{}/{}'.format(output_path, question.id)
labels_path = question_folder_path
rows = []
for i in range(dataset.num_data):
rows.append([dataset.answers[i].text, labels[i]])
utils.write_csv_file(labels_path, 'labels.tsv', rows, delimiter='\t')
# save reference answers separately
np.save(Path(output_path) / question.id / 'reference_answer_data', dataset.reference_answer_data, allow_pickle=True)
reference_answer_labels = np.array([a.get_answer_class(mapping, scale, dataset=dataset,
question=question) for a in dataset.reference_answers])
rows = []
for i in range(len(reference_answer_labels)):
rows.append([dataset.reference_answers[i].text, reference_answer_labels[i]])
utils.write_csv_file(labels_path, 'reference_answer_labels.tsv', rows, delimiter='\t')
end_time = datetime.datetime.now()
logger.info('Encoding Finished, time used: {}'.format(end_time - start_time))
return encoded_dataset if name is not None and question_id is None \
else SubDataset(encoded_dataset=encoded_dataset, question=encoded_dataset.questions[0])
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--name', help='dataset name, options={}'.format([key for key in constants.DATASETS.keys()]), required=True)
parser.add_argument('--question_id', help='question_id', required=False)
parser.add_argument('--encoder', help='encoder name, options={}'.format([key for key in constants.ENCODERS.keys()]), required=True)
parser.add_argument('--model', help='model name', required=False)
parser.add_argument('--checkpoint_name', help='checkpoint name (for skip_thoughts)', required=False)
# parser.add_argument('--dataset_name', help='model dataset name (for BERT)', required=False)
parser.add_argument('--dimension', help='encoded model dimension (for GloVE models)', required=False)
parser.add_argument('--mapping', help='mapping to be applied to original labels', required=False)
# for word embeddings
parser.add_argument('--min_frequency', help='min_frequency for word embeddings', type=int, default=0, required=False)
parser.add_argument('--not_lemmatize', help='not to lemmatize', action='store_true', default=False, required=False)
parser.add_argument('--not_remove_stop_words', help='not to remove stop words', action='store_true', default=False, required=False)
args = parser.parse_args()
encoded_dataset = encode_dataset(**vars(args))
# name = args.name
# encoder = args.encoder
# model = args.model
# checkpoint_name = args.checkpoint_name
# # dataset_name = args.dataset_name
# dimension = args.dimension
# mapping = args.mapping
# scale = 0.7 if name == 'USCIS' or (name == 'SEB3' and mapping == '2way') else 0.5
# min_frequency = args.min_frequency
# lemmatize = not args.not_lemmatize
# remove_stop_words = not args.not_remove_stop_words
# utils.validate_option(constants.DATASETS.keys(), name, 'dataset')
# if model is None:
# model = constants.ENCODERS[encoder]['default_model']
# if '/' in model:
# parts = model.split('/')
# source_model = parts[0]
# model = parts[1]
# else:
# source_model = None
# start_time = datetime.datetime.now()
# # dataset = utils.load(Path(os.path.abspath(os.path.dirname(__file__))) / 'data/datasets/processed' / constants.DATASETS[name]['processed_folder'] / 'data.txt')
# dataset = utils.load(Path(config.ROOT_PATH) / 'data/datasets/processed'
# / constants.DATASETS[name]['processed_folder'] / 'data.txt')
# print(dataset)
# # output_path = 'data/datasets/encoded/{}/{}'.format(encoder, dataset.name)
# if encoder == 'skip_thoughts':
# from datasets.encoders import SkipThoughts
# checkpoint_name = args.checkpoint_name if args.checkpoint_name is not None else constants.ENCODERS[encoder]['default_checkpoint_name']
# encoded_dataset = SkipThoughts(model=model, checkpoint_name=checkpoint_name).encode(dataset)
# elif encoder == 'google_universal_sentence_encoder':
# from datasets.encoders import GoogleUniversalSentenceEncoder
# encoded_dataset = GoogleUniversalSentenceEncoder(model=model).encode(dataset)
# elif encoder == 'glove':
# from datasets.encoders import Glove
# # encoded_dataset = Glove(model=model, dimension=dimension, remove_stop_words=remove_stop_words).encode(dataset)
# # output_path = 'data/datasets/encoded/{}{}/{}/{}'.format(encoder, '_without_stop_words' if remove_stop_words else '', dataset.name, encoded_dataset.encoder.model)
# # encoded_dataset = Glove(source_model=model, ngram_range=(1, 3), min_frequency=min_frequency,
# # retrain=False).encode(dataset)
# encoded_dataset = Glove(source_model=source_model, ngram_range=(1, 3), min_frequency=min_frequency,
# lemmatize=lemmatize, remove_stop_words=remove_stop_words).encode(dataset)
# elif encoder == 'bert':
# from datasets.encoders import Bert
# # model bert_12_768_12
# # dataset_name choices: book_corpus_wiki_en_uncased, book_corpus_wiki_en_cased, wiki_multilingual. wiki_multilingual_cased. wiki_cn
# # model: bert_24_1024_16
# # dataset_name choices: book_corpus_wiki_en_cased
# # dataset_name = args.dataset_name if args.dataset_name is not None else constants.ENCODERS[encoder]['default_dataset_name']
# # oov_handler choices: sum, avg, last
# # encoded_dataset = Bert(model=model, dataset_name=dataset_name, oov_handler='avg',
# # remove_stop_words=remove_stop_words).encode(dataset)
# # output_path = 'data/datasets/encoded/{}{}/{}/{}'.format(encoder, '_without_stop_words' if remove_stop_words else '', dataset.name, encoded_dataset.encoder.model)
# encoded_dataset = Bert(source_model=source_model, dataset_name=model,
# ngram_range=(1, 3), min_frequency=min_frequency, lemmatize=lemmatize,
# remove_stop_words=remove_stop_words, oov_handler='avg').encode(dataset)
# elif encoder == 'fasttext':
# from datasets.encoders import FastText
# encoded_dataset = FastText(remove_stop_words=remove_stop_words).encode(dataset)
# elif encoder == 'tfidf':
# from datasets.encoders import TFIDF
# encoded_dataset = TFIDF(ngram_range=(1, 3), min_frequency=min_frequency, retrain=False).encode(dataset)
# elif encoder == 'count':
# from datasets.encoders import Count
# encoded_dataset = Count(ngram_range=(1, 3), min_frequency=min_frequency, retrain=False).encode(dataset)
# elif encoder == 'lsa':
# from datasets.encoders import LSA
# encoded_dataset = LSA(ngram_range=(1, 3), min_frequency=min_frequency, n_components=100, retrain=False).encode(dataset)
# elif encoder == 'jaccard_similarity':
# from datasets.encoders import JaccardSimilarity
# encoded_dataset = JaccardSimilarity(lemmatize=lemmatize, remove_stop_words=remove_stop_words).encode(dataset)
# else:
# raise utils.InvalidNameError('encoder', encoder)
# output_path = '{}/data/datasets/encoded/{}/{}/{}'.format(os.path.abspath(os.path.dirname(__file__)),
# encoded_dataset.encoder.name, encoded_dataset.encoder.model,
# constants.DATASETS[name]['processed_folder'])
# utils.save(encoded_dataset, output_path, 'dataset.txt')
# # utils.save_zip(encoded_dataset, output_path, 'dataset.txt')
# for question in encoded_dataset.questions:
# dataset = SubDataset(encoded_dataset=encoded_dataset, question=question)
# # labels = np.array([a.get_answer_class('2way', 0.7 if name == 'USCIS' else 0.5, dataset=dataset, question=question) for a in dataset.answers])
# if not name.startswith('SEB'):
# mapping = '2way'
# else:
# if name == 'SEB2':
# mapping = '2way'
# elif name == 'SEB3' and (mapping is None or (mapping not in ['2way', '3way'])):
# mapping = '3way'
# elif name == 'SEB5' and (mapping is None or (mapping not in ['2way', '3way', '5way'])):
# mapping = '5way'
# # labels = np.array([a.get_answer_class(mapping, 0.7 if name == 'USCIS' else 0.5, dataset=dataset, question=question) for a in dataset.answers])
# labels = np.array([a.get_answer_class(mapping, scale, dataset=dataset,
# question=question) for a in dataset.answers])
# # utils.save(dataset.data, '{}/{}'.format(output_path, question.id), 'data.dat')
# # np.savetxt('{}/{}/labels.txt'.format(output_path, question.id), labels, fmt='%s', delimiter='\n', newline='\n')
# # value = np.array([a.text for a in dataset.answers])
# # np.savetxt('{}/{}/value.txt'.format(output_path, question.id), value, fmt='%s', delimiter='\n', newline='\n')
# if (name == 'SEB3' and mapping != '3way') or (name == 'SEB5' and mapping != '5way'):
# labels_path = '{}/{}/{}'.format(output_path, question.id, mapping)
# else:
# labels_path = '{}/{}'.format(output_path, question.id)
# question_folder_path = '{}/{}'.format(output_path, question.id)
# if not os.path.exists(question_folder_path):
# utils.create_directories(question_folder_path)
# np.save(Path(output_path) / question.id / 'data', dataset.data, allow_pickle=True)
# rows = []
# for i in range(dataset.num_data):
# rows.append([dataset.answers[i].text, labels[i]])
# # utils.write_csv_file('{}/{}'.format(output_path, question.id), 'labels.tsv', rows, delimiter='\t')
# utils.write_csv_file(labels_path, 'labels.tsv', rows, delimiter='\t')
# # save reference answers separately
# np.save(Path(output_path) / question.id / 'reference_answer_data', dataset.reference_answer_data, allow_pickle=True)
# # reference_answer_labels = np.array([a.get_answer_class('2way', 0.7 if name == 'USCIS' else 0.5, dataset=dataset, question=question) for a in dataset.reference_answers])
# # reference_answer_labels = np.array([a.get_answer_class(mapping, 0.7 if name == 'USCIS' else 0.5, dataset=dataset, question=question) for a in dataset.reference_answers])
# reference_answer_labels = np.array([a.get_answer_class(mapping, scale, dataset=dataset,
# question=question) for a in dataset.reference_answers])
# rows = []
# for i in range(len(reference_answer_labels)):
# rows.append([dataset.reference_answers[i].text, reference_answer_labels[i]])
# # utils.write_csv_file('{}/{}'.format(output_path, question.id), 'reference_answer_labels.tsv', rows, delimiter='\t')
# utils.write_csv_file(labels_path, 'reference_answer_labels.tsv', rows, delimiter='\t')
# end_time = datetime.datetime.now()
# print('Encoding Finished, time used: {}'.format(end_time - start_time))