-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateFineTuneData.py
141 lines (103 loc) · 4.77 KB
/
createFineTuneData.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
import os
import re
import sys
import csv
import copy
import json
import time
import random
from pathlib import Path
from collections import defaultdict
sys.path.append( '../../Utils/' )
from utils import tokenise_idiom as idiom_tokeniser
from utils import match_idioms, create_idiom_word_dict
def create_data( json_location, out_location, datasets, force_idiomatic, tokenise_idiom ) :
assert not force_idiomatic
with open( json_location, encoding='utf-8') as fh:
all_data = json.load(fh)
data = list()
for dataset in datasets :
data += all_data[ dataset ]
original = list()
correct = list()
incorrect = list()
sent_idioms = list() # Idioms per sent (example)
idioms = list() # All idioms
for idiom in data :
this_idiom = idiom[0][1]
idioms.append( this_idiom.lower().lstrip().rstrip() )
meanings = idiom[0][2:8]
del_meanings = [ 'None', 'Proper Noun', 'Meta Usage' ]
new_meanings = list()
for meaning in meanings :
if meaning in del_meanings :
continue
new_meanings.append( meaning )
meanings = new_meanings
for sent in idiom :
label = sent[9]
if not label in meanings :
continue
if force_idiomatic and sent[8] == 1 :
continue
this_sentence = sent[11]
not_label = copy.copy( meanings )
not_label.remove( label )
if len( not_label ) == 0 :
continue
### Should this be flags=re.I below ?
not_label = random.choice( not_label )
this_correct = re.sub( this_idiom, label, this_sentence )
this_incorrect = re.sub( this_idiom, not_label, this_sentence )
idiom_phrase = sent[1]
idiom_word_dict = create_idiom_word_dict( [ idiom_phrase ] )
matched_idioms = match_idioms( idiom_word_dict, this_sentence )
if len( matched_idioms ) == 0 :
print( "WARNING: Can't identify idiom {} in sentence {} (possibly because of tokenisation!)".format( idiom_phrase, this_sentence ) )
print()
# import pdb; pdb.set_trace()
# matched_idioms = match_idioms( idiom_word_dict, this_sentence )
continue
if tokenise_idiom :
this_sentence = this_sentence.replace( idiom_phrase, idiom_tokeniser( idiom_phrase ) )
original. append( this_sentence )
correct. append( this_correct )
incorrect.append( this_incorrect )
sent_idioms.append( idiom_phrase )
assert len( original ) == len( correct ) == len( incorrect ) == len( sent_idioms )
outdata = [ [ original[ i ], correct[ i ], incorrect[ i ], sent_idioms[ i ] ] for i in range( len( original ) ) ]
outfile_name = os.path.join( out_location, 'trainToPredict.csv' )
with open( outfile_name, 'w' ) as csvfile :
writer = csv.writer( csvfile )
writer.writerows( [ [ 'original', 'correct', 'incorrect', 'idiom' ] ] + outdata )
print( "Wrote: ", outfile_name )
outdata = [ [ original[ i ], sent_idioms[ i ], 1 ] for i in range( len( original ) ) ]
outfile_name = os.path.join( out_location, 'trainSentsForIdiomClassification.csv' )
with open( outfile_name, 'w' ) as csvfile :
writer = csv.writer( csvfile )
writer.writerows( [ [ 'sentence1', 'sentence2', 'label' ] ] + outdata )
print( "Wrote: ", outfile_name )
return
if __name__ == '__main__' :
out_location = 'trainData'
## Should always be False!!!
## We do this later (in combineCreate) based on "select" and "all"
tokenise_idiom = False
if len( sys.argv ) < 2 :
raise Exception( "Require language (EN, PT) as arg)" )
language = sys.argv[1]
assert language.lower() in [ 'en', 'pt' ]
json_location = '../../../TaskIndependentData/' + language.lower() + '_TaskIndependentData.json'
# [ 'train_one_shot', 'train_few_shot', 'train_zero_shot' ]
print( "WARNING: Using Few Shot data - edit here to change to one_shot" )
time.sleep( 1 )
datasets = [ 'train_few_shot', 'train_zero_shot' ]
params = {
'json_location' : json_location ,
'out_location' : out_location ,
'datasets' : datasets ,
'force_idiomatic' : False ,
'tokenise_idiom' : tokenise_idiom ,
}
Path( params[ 'out_location' ] ).mkdir(parents=True, exist_ok=True)
create_data( **params )