-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
utils.py
1212 lines (990 loc) · 41.1 KB
/
utils.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import Counter
from io import open
import csv
import glob
import json
import os
import random
import re
import shutil
import subprocess
import sys
import numpy as np
from sentencepiece import SentencePieceTrainer as SPT
from tqdm import tqdm
from nemo.utils.exp_logging import get_logger
from ...utils.nlp_utils import (get_vocab,
write_vocab,
write_vocab_in_order,
label2idx)
logger = get_logger('')
LOGGING_TMP = '{} dataset has already been processed and stored at {}'
def get_stats(lengths):
lengths = np.asarray(lengths)
logger.info(f'Min: {np.min(lengths)} | \
Max: {np.max(lengths)} | \
Mean: {np.mean(lengths)} | \
Median: {np.median(lengths)}')
logger.info(f'75 percentile: {np.percentile(lengths, 75)}')
logger.info(f'99 percentile: {np.percentile(lengths, 99)}')
def get_label_stats(labels, outfile='stats.tsv'):
labels = Counter(labels)
total = sum(labels.values())
out = open(outfile, 'w')
i = 0
for k, v in labels.most_common():
out.write(f'{k}\t{v/total}\n')
if i < 3:
logger.info(f'{i} item: {k}, {v} out of {total}, {v/total}.')
i += 1
def list2str(l):
return ' '.join([str(x) for x in l])
def if_exist(outfold, files):
if not os.path.exists(outfold):
return False
for file in files:
if not os.path.exists(f'{outfold}/{file}'):
return False
return True
def process_sst_2(data_dir):
if not os.path.exists(data_dir):
link = 'https://gluebenchmark.com/tasks'
raise ValueError(f'Data not found at {data_dir}. '
f'Please download SST-2 from {link}.')
logger.info('Keep in mind that SST-2 is only available in lower case.')
return data_dir
def process_imdb(data_dir, uncased, modes=['train', 'test']):
if not os.path.exists(data_dir):
link = 'www.kaggle.com/iarunava/imdb-movie-reviews-dataset'
raise ValueError(f'Data not found at {data_dir}. '
f'Please download IMDB from {link}.')
outfold = f'{data_dir}/nemo-processed'
if uncased:
outfold = f'{outfold}_uncased'
if if_exist(outfold, [f'{mode}.tsv' for mode in modes]):
logger.info(LOGGING_TMP.format('IMDB', outfold))
return outfold
logger.info(f'Processing IMDB dataset and store at {outfold}')
os.makedirs(outfold, exist_ok=True)
outfiles = {}
for mode in modes:
outfiles[mode] = open(os.path.join(outfold, mode + '.tsv'), 'w')
outfiles[mode].write('sentence\tlabel\n')
for sent in ['neg', 'pos']:
if sent == 'neg':
label = 0
else:
label = 1
files = glob.glob(f'{data_dir}/{mode}/{sent}/*.txt')
for file in files:
with open(file, 'r') as f:
review = f.read().strip()
if uncased:
review = review.lower()
review = review.replace("<br />", "")
outfiles[mode].write(f'{review}\t{label}\n')
return outfold
def process_nlu(filename,
uncased,
modes=['train', 'test'],
dataset_name='nlu-ubuntu'):
""" Dataset has to be of:
- ubuntu
- chat
- web
"""
if not os.path.exists(filename):
link = 'https://github.com/sebischair/NLU-Evaluation-Corpora'
raise ValueError(f'Data not found at {filename}. '
'Please download IMDB from {link}.')
if dataset_name == 'nlu-ubuntu':
INTENT = {'makeupdate': 1,
'setupprinter': 2,
'shutdowncomputer': 3,
'softwarerecommendation': 4,
'none': 0}
elif dataset_name == 'nlu-chat':
INTENT = {'departuretime': 0, 'findconnection': 1}
elif dataset_name == 'nlu-web':
INTENT = {'changepassword': 1,
'deleteaccount': 2,
'downloadvideo': 3,
'exportdata': 4,
'filterspam': 5,
'findalternative': 6,
'syncaccounts': 7,
'none': 0}
else:
raise ValueError(f'{dataset_name}: Invalid dataset name')
infold = filename[:filename.rfind('/')]
outfold = f'{infold}/{dataset_name}-nemo-processed'
if uncased:
outfold = f'{outfold}_uncased'
if if_exist(outfold, [f'{mode}.tsv' for mode in modes]):
logger.info(LOGGING_TMP.format(dataset_name.upper(), outfold))
return outfold
logger.info(f'Processing data and store at {outfold}')
os.makedirs(outfold, exist_ok=True)
outfiles = {}
for mode in modes:
outfiles[mode] = open(os.path.join(outfold, mode + '.tsv'), 'w')
outfiles[mode].write('sentence\tlabel\n')
with open(filename, 'r') as f:
data = json.load(f)
for obj in data['sentences']:
sentence = obj['text'].strip()
if uncased:
sentence = sentence.lower()
intent = obj['intent'].lower().replace(' ', '')
label = INTENT[intent]
txt = f'{sentence}\t{label}\n'
if obj['training']:
outfiles['train'].write(txt)
else:
outfiles['test'].write(txt)
return outfold
def get_car_labels(intent_file):
labels = {}
with open(intent_file, 'r') as f:
for line in f:
intent, label = line.strip().split('\t')
labels[intent] = int(label)
return labels
def process_nvidia_car(infold,
uncased,
modes=['train', 'test'],
test_ratio=0.02):
infiles = {'train': f'{infold}/pytextTrainDataPOI_1_0.tsv',
'test': f'{infold}/test.tsv'}
outfold = f'{infold}/nvidia-car-nemo-processed'
intent_file = f'{outfold}/intent_labels.tsv'
if uncased:
outfold = f'{outfold}_uncased'
if if_exist(outfold, [f'{mode}.tsv' for mode in modes]):
logger.info(LOGGING_TMP.format('NVIDIA-CAR', outfold))
labels = get_car_labels(intent_file)
return outfold, labels
logger.info(f'Processing this dataset and store at {outfold}')
os.makedirs(outfold, exist_ok=True)
outfiles = {}
for mode in modes:
outfiles[mode] = open(os.path.join(outfold, mode + '.tsv'), 'w')
outfiles[mode].write('sentence\tlabel\n')
intents, sentences = [], []
start_index = 1
if mode == 'train':
all_intents = set()
start_index = 2
with open(infiles[mode], 'r') as f:
for line in f:
intent, _, sentence = line.strip().split('\t')
if uncased:
sentence = sentence.lower()
if mode == 'train':
all_intents.add(intent)
intents.append(intent)
sentences.append(' '.join(sentence.split()[start_index:-1]))
if mode == 'train':
i = 0
labels = {}
intent_out = open(intent_file, 'w')
for intent in all_intents:
labels[intent] = i
logger.info(f'{intent}\t{i}')
intent_out.write(f'{intent}\t{i}\n')
i += 1
seen, repeat = set(), 0
for intent, sentence in zip(intents, sentences):
if sentence in seen:
if mode == 'test':
print(sentence)
repeat += 1
continue
text = f'{sentence}\t{labels[intent]}\n'
outfiles[mode].write(text)
seen.add(sentence)
logger.info(f'{repeat} repeated sentences in {mode}')
return outfold, labels
def process_twitter_airline(filename, uncased, modes=['train', 'test']):
""" Dataset from Kaggle:
https://www.kaggle.com/crowdflower/twitter-airline-sentiment
"""
pass
def ids2text(ids, vocab):
return ' '.join([vocab[int(id_)] for id_ in ids])
def process_atis(infold, uncased, modes=['train', 'test'], dev_split=0):
""" MSFT's dataset, processed by Kaggle
https://www.kaggle.com/siddhadev/atis-dataset-from-ms-cntk
"""
outfold = f'{infold}/nemo-processed'
infold = f'{infold}/data/raw_data/ms-cntk-atis'
vocab = get_vocab(f'{infold}/atis.dict.vocab.csv')
if uncased:
outfold = f'{outfold}-uncased'
if if_exist(outfold, [f'{mode}.tsv' for mode in modes]):
logger.info(LOGGING_TMP.format('ATIS', outfold))
return outfold
logger.info(f'Processing ATIS dataset and store at {outfold}')
os.makedirs(outfold, exist_ok=True)
outfiles = {}
for mode in modes:
outfiles[mode] = open(os.path.join(outfold, mode + '.tsv'), 'w')
outfiles[mode].write('sentence\tlabel\n')
outfiles[mode + '_slots'] = open(f'{outfold}/{mode}_slots.tsv', 'w')
queries = open(f'{infold}/atis.{mode}.query.csv', 'r').readlines()
intents = open(f'{infold}/atis.{mode}.intent.csv', 'r').readlines()
slots = open(f'{infold}/atis.{mode}.slots.csv', 'r').readlines()
for i, query in enumerate(queries):
sentence = ids2text(query.strip().split()[1:-1], vocab)
outfiles[mode].write(f'{sentence}\t{intents[i].strip()}\n')
slot = ' '.join(slots[i].strip().split()[1:-1])
outfiles[mode + '_slots'].write(slot + '\n')
shutil.copyfile(f'{infold}/atis.dict.intent.csv',
f'{outfold}/dict.intents.csv')
shutil.copyfile(f'{infold}/atis.dict.slots.csv',
f'{outfold}/dict.slots.csv')
return outfold
def reverse_dict(entity2value):
value2entity = {}
for entity in entity2value:
for value in entity2value[entity]:
value2entity[value] = entity
return value2entity
def map_entities(entity2value, entities):
for key in entities:
if 'data' in entities[key]:
if key not in entity2value:
entity2value[key] = set([])
values = []
for value in entities[key]['data']:
values.append(value['value'])
values.extend(value['synonyms'])
entity2value[key] = entity2value[key] | set(values)
return entity2value
def get_entities(files):
entity2value = {}
for file in files:
with open(file, 'r') as json_file:
data = json.load(json_file)
entity2value = map_entities(entity2value, data['entities'])
value2entity = reverse_dict(entity2value)
return entity2value, value2entity
def get_data(files, entity2value, value2entity):
all_data, all_slots, all_intents = [], set(['O']), set()
for file in files:
file_data = []
with open(file, 'r') as json_file:
data = json.load(json_file)
for intent in data['intents']:
all_intents.add(intent)
utterances = data['intents'][intent]['utterances']
for utterance in utterances:
tokens, slots = [], []
for frag in utterance['data']:
frag_tokens = frag['text'].strip().split()
tokens.extend(frag_tokens)
if 'slot_name' not in frag:
slot = 'O'
else:
slot = frag['slot_name']
all_slots.add(slot)
slots.extend([slot] * len(frag_tokens))
file_data.append((tokens, slots, intent))
all_data.append(file_data)
return all_data, all_slots, all_intents
def get_dataset(files, dev_split=0.1):
entity2value, value2entity = get_entities(files)
data, slots, intents = get_data(files, entity2value, value2entity)
if len(data) == 1:
train, dev = partition(data[0], split=dev_split)
else:
train, dev = data[0], data[1]
return train, dev, slots, intents
def partition(data, split=0.1):
n = len(data)
n_dev = int(n * split)
dev_idx = set(random.sample(range(n), n_dev))
dev, train = [], []
for i, item in enumerate(data):
if i in dev_idx:
dev.append(item)
else:
train.append(item)
return train, dev
def write_data(data, slot_dict, intent_dict, outfold, mode, uncased):
intent_file = open(f'{outfold}/{mode}.tsv', 'w')
intent_file.write('sentence\tlabel\n')
slot_file = open(f'{outfold}/{mode}_slots.tsv', 'w')
for tokens, slots, intent in data:
text = ' '.join(tokens)
if uncased:
text = text.lower()
intent_file.write(f'{text}\t{intent_dict[intent]}\n')
slots = [str(slot_dict[slot]) for slot in slots]
slot_file.write(' '.join(slots) + '\n')
intent_file.close()
slot_file.close()
def create_dataset(train, dev, slots, intents, uncased, outfold):
os.makedirs(outfold, exist_ok=True)
if 'O' in slots:
slots.remove('O')
slots = sorted(list(slots)) + ['O']
intents = sorted(list(intents))
slots = write_vocab(slots, f'{outfold}/dict.slots.csv')
intents = write_vocab(intents, f'{outfold}/dict.intents.csv')
write_data(train, slots, intents, outfold, 'train', uncased)
write_data(dev, slots, intents, outfold, 'test', uncased)
def process_snips(data_dir, uncased, modes=['train', 'test'], dev_split=0.1):
if not os.path.exists(data_dir):
link = 'www.github.com/snipsco/spoken-language'
'-understanding-research-datasets'
raise ValueError(f'Data not found at {data_dir}. '
'Resquest to download the SNIPS dataset from {link}.')
outfold = f'{data_dir}/nemo-processed'
if uncased:
outfold = f'{outfold}-uncased'
exist = True
for dataset in ['light', 'speak', 'all']:
if if_exist(f'{outfold}/{dataset}', [f'{mode}.tsv' for mode in modes]):
logger.info(LOGGING_TMP.format(
'SNIPS-' + dataset.upper(), outfold))
else:
exist = False
if exist:
return outfold
logger.info(f'Processing SNIPS dataset and store at {outfold}')
os.makedirs(outfold, exist_ok=True)
speak_dir = 'smart-speaker-en-close-field'
light_dir = 'smart-lights-en-close-field'
light_files = [f'{data_dir}/{light_dir}/dataset.json']
speak_files = [f'{data_dir}/{speak_dir}/training_dataset.json']
speak_files.append(f'{data_dir}/{speak_dir}/test_dataset.json')
light_train, light_dev, light_slots, light_intents = get_dataset(
light_files, dev_split)
speak_train, speak_dev, speak_slots, speak_intents = get_dataset(
speak_files)
create_dataset(light_train, light_dev, light_slots,
light_intents, uncased, f'{outfold}/light')
create_dataset(speak_train, speak_dev, speak_slots,
speak_intents, uncased, f'{outfold}/speak')
create_dataset(light_train + speak_train, light_dev + speak_dev,
light_slots | speak_slots, light_intents | speak_intents,
uncased, f'{outfold}/all')
return outfold
def list2str(nums):
return ' '.join([str(num) for num in nums])
def merge(data_dir, subdirs, dataset_name, modes=['train', 'test']):
outfold = f'{data_dir}/{dataset_name}'
if if_exist(outfold, [f'{mode}.tsv' for mode in modes]):
logger.info(LOGGING_TMP.format('SNIPS-ATIS', outfold))
slots = get_vocab(f'{outfold}/dict.slots.csv')
none_slot = 0
for key in slots:
if slots[key] == 'O':
none_slot = key
break
return outfold, int(none_slot)
os.makedirs(outfold, exist_ok=True)
data_files, slot_files = {}, {}
for mode in modes:
data_files[mode] = open(f'{outfold}/{mode}.tsv', 'w')
data_files[mode].write('sentence\tlabel\n')
slot_files[mode] = open(f'{outfold}/{mode}_slots.tsv', 'w')
intents, slots = {}, {}
intent_shift, slot_shift = 0, 0
none_intent, none_slot = -1, -1
for subdir in subdirs:
curr_intents = get_vocab(f'{data_dir}/{subdir}/dict.intents.csv')
curr_slots = get_vocab(f'{data_dir}/{subdir}/dict.slots.csv')
for key in curr_intents:
if intent_shift > 0 and curr_intents[key] == 'O':
continue
if curr_intents[key] == 'O' and intent_shift == 0:
none_intent = int(key)
intents[int(key) + intent_shift] = curr_intents[key]
for key in curr_slots:
if slot_shift > 0 and curr_slots[key] == 'O':
continue
if slot_shift == 0 and curr_slots[key] == 'O':
none_slot = int(key)
slots[int(key) + slot_shift] = curr_slots[key]
for mode in modes:
with open(f'{data_dir}/{subdir}/{mode}.tsv', 'r') as f:
for line in f.readlines()[1:]:
text, label = line.strip().split('\t')
label = int(label)
if curr_intents[label] == 'O':
label = none_intent
else:
label = label + intent_shift
data_files[mode].write(f'{text}\t{label}\n')
with open(f'{data_dir}/{subdir}/{mode}_slots.tsv', 'r') as f:
for line in f.readlines():
labels = [int(label) for label in line.strip().split()]
shifted_labels = []
for label in labels:
if curr_slots[label] == 'O':
shifted_labels.append(none_slot)
else:
shifted_labels.append(label + slot_shift)
slot_files[mode].write(list2str(shifted_labels) + '\n')
intent_shift += len(curr_intents)
slot_shift += len(curr_slots)
write_vocab_in_order(intents, f'{outfold}/dict.intents.csv')
write_vocab_in_order(slots, f'{outfold}/dict.slots.csv')
return outfold, none_slot
class JointIntentSlotDataDesc:
""" Convert the raw data to the standard format supported by
JointIntentSlotDataset.
By default, the None label for slots is 'O'.
JointIntentSlotDataset requires two files:
input_file: file to sequence + label.
the first line is header (sentence [tab] label)
each line should be [sentence][tab][label]
slot_file: file to slot labels, each line corresponding to
slot labels for a sentence in input_file. No header.
To keep the mapping from label index to label consistent during
training and inferencing, we require the following files:
dicts.intents.csv: each line is an intent. The first line
corresponding to the 0 intent label, the second line
corresponding to the 1 intent label, and so on.
dicts.slots.csv: each line is a slot. The first line
corresponding to the 0 slot label, the second line
corresponding to the 1 slot label, and so on.
Args:
data_dir (str): the directory of the dataset
do_lower_case (bool): whether to set your dataset to lowercase
dataset_name (str): the name of the dataset. If it's a dataset
that follows the standard JointIntentSlotDataset format,
you can set the name as 'default'.
none_slot_label (str): the label for slots that aren't indentified
defaulted to 'O'
pad_label (int): the int used for padding. If set to -1,
it'll be set to the whatever the None label is.
"""
def __init__(self,
data_dir,
do_lower_case=False,
dataset_name='default',
none_slot_label='O',
pad_label=-1):
if dataset_name == 'atis':
self.data_dir = process_atis(data_dir, do_lower_case)
elif dataset_name == 'snips-atis':
self.data_dir, self.pad_label = merge(
data_dir,
['ATIS/nemo-processed-uncased',
'snips/nemo-processed-uncased/all'],
dataset_name)
elif dataset_name in set(['snips-light', 'snips-speak', 'snips-all']):
self.data_dir = process_snips(data_dir, do_lower_case)
if dataset_name.endswith('light'):
self.data_dir = f'{self.data_dir}/light'
elif dataset_name.endswith('speak'):
self.data_dir = f'{self.data_dir}/speak'
elif dataset_name.endswith('all'):
self.data_dir = f'{self.data_dir}/all'
else:
if not if_exist(data_dir, ['dict.intents.csv', 'dict.slots.csv']):
raise FileNotFoundError(
"Make sure that your data follows the standard format "
"supported by JointIntentSlotDataset. Your data must "
"contain dict.intents.csv and dict.slots.csv.")
self.data_dir = data_dir
self.intent_dict_file = self.data_dir + '/dict.intents.csv'
self.slot_dict_file = self.data_dir + '/dict.slots.csv'
self.num_intents = len(get_vocab(self.intent_dict_file))
slots = label2idx(self.slot_dict_file)
self.num_slots = len(slots)
if pad_label != -1:
self.pad_label = pad_label
else:
if none_slot_label not in slots:
raise ValueError(f'none_slot_label {none_slot_label} not '
f'found in {self.slot_dict_file}.')
self.pad_label = slots[none_slot_label]
class SentenceClassificationDataDesc:
def __init__(self, dataset_name, data_dir, do_lower_case):
if dataset_name == 'sst-2':
self.data_dir = process_sst_2(data_dir)
self.num_labels = 2
self.eval_file = self.data_dir + '/dev.tsv'
elif dataset_name == 'imdb':
self.num_labels = 2
self.data_dir = process_imdb(data_dir, do_lower_case)
self.eval_file = self.data_dir + '/test.tsv'
elif dataset_name.startswith('nlu-'):
if dataset_name.endswith('chat'):
self.data_dir = f'{data_dir}/ChatbotCorpus.json'
self.num_labels = 2
elif dataset_name.endswith('ubuntu'):
self.data_dir = f'{data_dir}/AskUbuntuCorpus.json'
self.num_labels = 5
elif dataset_name.endswith('web'):
data_dir = f'{data_dir}/WebApplicationsCorpus.json'
self.num_labels = 8
self.data_dir = process_nlu(data_dir,
do_lower_case,
dataset_name=dataset_name)
self.eval_file = self.data_dir + '/test.tsv'
elif dataset_name == 'nvidia-car':
self.data_dir, labels = process_nvidia_car(data_dir, do_lower_case)
for intent in labels:
idx = labels[intent]
logger.info(f'{intent}: {idx}')
self.num_labels = len(labels)
self.eval_file = self.data_dir + '/test.tsv'
else:
logger.info("Looks like you pass in a dataset name that isn't "
"already supported by NeMo. Please make sure that "
"you build the preprocessing method for it.")
self.train_file = self.data_dir + '/train.tsv'
def create_vocab_lm(data_dir, do_lower_case):
if if_exist(data_dir, ['train.txt', 'valid.txt', 'test.txt', 'vocab.txt']):
logger.info(LOGGING_TMP.format('WikiText', data_dir))
return data_dir
logger.info(f'Processing WikiText dataset and store at {data_dir}')
with open(f'{data_dir}/train.txt', 'r') as file:
txt = file.read()
if do_lower_case:
txt = txt.lower()
lines = re.split(r'[\n]', txt)
sentences = [line.strip().split() for line in lines if line.strip()]
vocab = {"[PAD]": 0, "[SEP]": 1, "[CLS]": 2, "[MASK]": 3}
idx = 4
for sentence in sentences:
for word in sentence:
if word not in vocab:
vocab[word] = idx
idx += 1
with open(f'{data_dir}/vocab.txt', 'w') as f:
for word in vocab.keys():
f.write(word + '\n')
logger.info(f"Created vocabulary of size {len(vocab)}")
return data_dir
def download_wkt2(data_dir):
os.makedirs('data/lm', exist_ok=True)
logger.warning(f'Data not found at {data_dir}. '
f'Download {dataset_name} to data/lm')
data_dir = 'data/lm/wikitext-2'
subprocess.call('scripts/get_wkt2.sh')
return data_dir
class LanguageModelDataDesc:
def __init__(self, dataset_name, data_dir, do_lower_case):
if dataset_name == 'wikitext-2':
if not os.path.exists(data_dir):
data_dir = download_wkt2(data_dir)
self.data_dir = create_vocab_lm(data_dir, do_lower_case)
else:
logger.info("Looks like you pass in a dataset name that isn't "
"already supported by NeMo. Please make sure that "
"you build the preprocessing method for it.")
def create_vocab_mlm(data_dir,
vocab_size,
sample_size,
special_tokens=['PAD', '[UNK]',
'[CLS]', '[SEP]', '[MASK]'],
train_file=''):
vocab = special_tokens[:]
bert_dir = f'{data_dir}/bert'
if if_exist(bert_dir, ['tokenizer.model']):
logger.info(LOGGING_TMP.format('WikiText_BERT', bert_dir))
return data_dir, f'{bert_dir}/tokenizer.model'
logger.info(f'Processing WikiText dataset and store at {bert_dir}')
os.makedirs(bert_dir, exist_ok=True)
if not train_file:
files = glob.glob(f'{data_dir}/*.txt')
train_file = f'{bert_dir}/merged.txt'
logger.info(f"Merging {len(files)} txt files into {train_file}")
with open(train_file, "w") as merged:
for file in tqdm(files):
with open(file, 'r') as inf:
content = inf.read().strip()
merged.write(content + '\n\n\n')
else:
train_file = f'{data_dir}/{train_file}'
cmd = (f"--input={train_file} --model_prefix={bert_dir}/tokenizer "
f"--vocab_size={vocab_size - len(vocab)} "
f"--input_sentence_size={sample_size} "
f"--shuffle_input_sentence=true --hard_vocab_limit=false "
f"--bos_id=-1 --eos_id=-1")
SPT.Train(cmd)
# Add BERT control symbols
tokens = []
with open(f"{bert_dir}/tokenizer.vocab", "r") as f:
f.readline() # skip first <unk> token
# Read tokens from each line and parse for vocab
for line in f:
piece = line.split("\t")[0]
token = piece[1:] if piece.startswith("▁") else f"##{piece}"
tokens.append(token)
vocab.extend(tokens)
# Save vocabulary to output file
with open(f'{bert_dir}/vocab.txt', "w") as f:
for token in vocab:
f.write(f"{token}\n".format())
return data_dir, f'{bert_dir}/tokenizer.model'
class BERTPretrainingDataDesc:
def __init__(self,
dataset_name,
data_dir,
vocab_size,
sample_size,
special_tokens,
train_file=''):
if dataset_name == 'wikitext-2':
if not os.path.exists(data_dir):
data_dir = download_wkt2(data_dir)
self.data_dir, self.tokenizer_model = create_vocab_mlm(
data_dir,
vocab_size,
sample_size,
special_tokens,
train_file)
else:
logger.info("Looks like you pass in a dataset name that isn't "
"already supported by NeMo. Please make sure that "
"you build the preprocessing method for it.")
self.train_file = f'{data_dir}/train.txt'
self.eval_file = f'{data_dir}/valid.txt'
self.test_file = f'{data_dir}/test.txt'
"""
Utility functions for GLUE tasks
This code was adapted from the HuggingFace library at
https://github.com/huggingface/transformers
"""
class InputExample(object):
"""A single training/test example for simple sequence classification."""
def __init__(self, guid, text_a, text_b=None, label=None):
"""Constructs a InputExample.
Args:
guid: Unique id for the example.
text_a: string. The untokenized text of the first sequence.
For single sequence tasks, only this sequence must be specified.
text_b: (Optional) string. The untokenized text of the second
sequence. Only must be specified for sequence pair tasks.
label: (Optional) string. The label of the example. This should be
specified for train and dev examples, but not for test examples.
"""
self.guid = guid
self.text_a = text_a
self.text_b = text_b
self.label = label
class DataProcessor(object):
"""Base class for data converters for sequence classification data sets."""
def get_train_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the train set."""
raise NotImplementedError()
def get_dev_examples(self, data_dir):
"""Gets a collection of `InputExample`s for the dev set."""
raise NotImplementedError()
def get_labels(self):
"""Gets the list of labels for this data set."""
raise NotImplementedError()
@classmethod
def _read_tsv(cls, input_file, quotechar=None):
"""Reads a tab separated value file."""
with open(input_file, "r", encoding="utf-8-sig") as f:
reader = csv.reader(f, delimiter="\t", quotechar=quotechar)
lines = []
for line in reader:
if sys.version_info[0] == 2:
line = list(unicode(cell, 'utf-8') for cell in line)
lines.append(line)
return lines
class MrpcProcessor(DataProcessor):
"""Processor for the MRPC data set (GLUE version)."""
def get_train_examples(self, data_dir):
"""See base class."""
logger.info(f'LOOKING AT {os.path.join(data_dir, "train.tsv")}')
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
def get_labels(self):
"""See base class."""
return ["0", "1"]
def _create_examples(self, lines, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, line) in enumerate(lines):
if i == 0:
continue
guid = "%s-%s" % (set_type, i)
text_a = line[3]
text_b = line[4]
label = line[0]
examples.append(InputExample(guid=guid,
text_a=text_a,
text_b=text_b,
label=label))
return examples
class MnliProcessor(DataProcessor):
"""Processor for the MultiNLI data set (GLUE version)."""
def get_train_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "dev_matched.tsv")),
"dev_matched")
def get_labels(self):
"""See base class."""
return ["contradiction", "entailment", "neutral"]
def _create_examples(self, lines, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, line) in enumerate(lines):
if i == 0:
continue
guid = "%s-%s" % (set_type, line[0])
text_a = line[8]
text_b = line[9]
label = line[-1]
examples.append(InputExample(guid=guid,
text_a=text_a,
text_b=text_b,
label=label))
return examples
class MnliMismatchedProcessor(MnliProcessor):
"""Processor for the MultiNLI Mismatched data set (GLUE version)."""
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "dev_mismatched.tsv")),
"dev_matched")
class ColaProcessor(DataProcessor):
"""Processor for the CoLA data set (GLUE version)."""
def get_train_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
def get_labels(self):
"""See base class."""
return ["0", "1"]
def _create_examples(self, lines, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, line) in enumerate(lines):
guid = "%s-%s" % (set_type, i)
text_a = line[3]
label = line[1]
examples.append(InputExample(guid=guid,
text_a=text_a,
text_b=None,
label=label))
return examples
class Sst2Processor(DataProcessor):
"""Processor for the SST-2 data set (GLUE version)."""
def get_train_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")
def get_dev_examples(self, data_dir):
"""See base class."""
return self._create_examples(
self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")
def get_labels(self):
"""See base class."""
return ["0", "1"]
def _create_examples(self, lines, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, line) in enumerate(lines):
if i == 0:
continue
guid = "%s-%s" % (set_type, i)
text_a = line[0]
label = line[1]
examples.append(InputExample(guid=guid,
text_a=text_a,
text_b=None,
label=label))
return examples