-
Notifications
You must be signed in to change notification settings - Fork 40
/
opcodes.py
223 lines (182 loc) · 6.67 KB
/
opcodes.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
# maps edits to their ids
import pickle
import utils
class Opcodes():
def __init__(self,path_common_inserts,
path_common_multitoken_inserts,
use_transforms=True):
USE_TRANSFORMS=use_transforms #turn this to false if use transforms are not be used
print("path_common_inserts: {}".format(path_common_inserts))
print("path_common_multitoken_inserts: {}".format(path_common_multitoken_inserts))
self.UNK = 0 #dummy
self.SOS = 1 #dummy
self.EOS = 2 #dummy
self.CPY = 3 #copy
self.DEL = 4 #delete
APPEND_BEGIN = 5
self.APPEND_BEGIN = APPEND_BEGIN
self.APPEND = {} #appends to their edit ids
self.REP = {} #replaces to their edit ids
APPEND = self.APPEND
REP = self.REP
common_inserts = pickle.load(open(path_common_inserts,"rb"))
common_multitoken_inserts = pickle.load(open(path_common_multitoken_inserts,"rb"))
for item in common_inserts:
self.reg_append(item)
for item in common_multitoken_inserts:
self.reg_append(item)
for item in common_inserts:
self.reg_rep(item)
for item in common_multitoken_inserts:
self.reg_rep(item)
APPEND_END = APPEND_BEGIN + len(APPEND) - 1
vocab_size = APPEND_BEGIN + len(APPEND)
REP_BEGIN = APPEND_END + 1
vocab_size += len(REP)
REP_END = REP_BEGIN + len(REP) -1
# TRANSFORM_SUFFIXES
if USE_TRANSFORMS:
self.APPEND_SUFFIX={}
APPEND_SUFFIX = self.APPEND_SUFFIX
# APPEND_SUFFIX is different from APPEND
# APPEND_SUFFIX maps suffix of APPEND-based-suffix-transformation to its edit id
# This is used to map all the APPENDs to corresponding APPEND based suffix transformation
self.APPEND_s = vocab_size
APPEND_SUFFIX["##s"]=self.APPEND_s
vocab_size += 1
self.REMOVE_s = vocab_size
vocab_size += 1
self.APPEND_d = vocab_size
APPEND_SUFFIX["##d"]=self.APPEND_d
vocab_size += 1
self.REMOVE_d = vocab_size
vocab_size += 1
self.APPEND_es = vocab_size
APPEND_SUFFIX["##es"]=self.APPEND_es
vocab_size += 1
self.REMOVE_es = vocab_size
vocab_size += 1
self.APPEND_ing = vocab_size
APPEND_SUFFIX["##ing"]=self.APPEND_ing
vocab_size += 1
self.REMOVE_ing = vocab_size
vocab_size += 1
self.APPEND_ed = vocab_size
APPEND_SUFFIX["##ed"]=self.APPEND_ed
vocab_size += 1
self.REMOVE_ed = vocab_size
vocab_size += 1
self.APPEND_ly = vocab_size
APPEND_SUFFIX["##ly"]=self.APPEND_ly
vocab_size += 1
self.REMOVE_ly = vocab_size
vocab_size += 1
self.APPEND_er = vocab_size
APPEND_SUFFIX["##er"]=self.APPEND_er
vocab_size += 1
self.REMOVE_er = vocab_size
vocab_size += 1
self.APPEND_al = vocab_size
APPEND_SUFFIX["##al"]=self.APPEND_al
vocab_size += 1
self.REMOVE_al = vocab_size
vocab_size += 1
self.APPEND_n = vocab_size
APPEND_SUFFIX["##n"]=self.APPEND_n
vocab_size += 1
self.REMOVE_n = vocab_size
vocab_size += 1
self.APPEND_y = vocab_size
APPEND_SUFFIX["##y"]=self.APPEND_y
vocab_size += 1
self.REMOVE_y = vocab_size
vocab_size += 1
self.APPEND_ation = vocab_size
APPEND_SUFFIX["##ation"]=self.APPEND_ation
vocab_size += 1
self.REMOVE_ation = vocab_size
vocab_size += 1
self.E_TO_ING = vocab_size
vocab_size += 1
self.ING_TO_E = vocab_size
vocab_size += 1
self.D_TO_T = vocab_size
vocab_size += 1
self.T_TO_D = vocab_size
vocab_size += 1
self.D_TO_S = vocab_size
vocab_size += 1
self.S_TO_D = vocab_size
vocab_size += 1
self.S_TO_ING = vocab_size
vocab_size += 1
self.ING_TO_S = vocab_size
vocab_size += 1
self.N_TO_ING = vocab_size
vocab_size += 1
self.ING_TO_N = vocab_size
vocab_size += 1
self.T_TO_NCE = vocab_size
vocab_size += 1
self.NCE_TO_T = vocab_size
vocab_size += 1
self.S_TO_ED = vocab_size
vocab_size += 1
self.ED_TO_S = vocab_size
vocab_size += 1
self.ING_TO_ED = vocab_size
vocab_size += 1
self.ED_TO_ING = vocab_size
vocab_size += 1
self.ING_TO_ION = vocab_size
vocab_size += 1
self.ION_TO_ING = vocab_size
vocab_size += 1
self.ING_TO_ATION = vocab_size
vocab_size += 1
self.ATION_TO_ING = vocab_size
vocab_size += 1
self.T_TO_CE = vocab_size
vocab_size += 1
self.CE_TO_T = vocab_size
vocab_size += 1
self.Y_TO_IC = vocab_size
vocab_size += 1
self.IC_TO_Y = vocab_size
vocab_size += 1
self.T_TO_S = vocab_size
vocab_size += 1
self.S_TO_T = vocab_size
vocab_size += 1
self.E_TO_AL = vocab_size
vocab_size += 1
self.AL_TO_E = vocab_size
vocab_size += 1
self.Y_TO_ILY = vocab_size
vocab_size += 1
self.ILY_TO_Y = vocab_size
vocab_size += 1
self.Y_TO_IED = vocab_size
vocab_size += 1
self.IED_TO_Y = vocab_size
vocab_size += 1
self.Y_TO_ICAL = vocab_size
vocab_size += 1
self.ICAL_TO_Y = vocab_size
vocab_size += 1
self.Y_TO_IES = vocab_size
vocab_size += 1
self.IES_TO_Y = vocab_size
vocab_size += 1
def reg_append(self,word):
#registers an APPEND
if word not in self.APPEND:
self.APPEND[word] = len(self.APPEND) + self.APPEND_BEGIN
else:
print("Skipping duplicate opcode", word)
def reg_rep(self,word):
#registers a REPLACE
if word not in self.REP:
self.REP[word] = len(self.REP) + len(self.APPEND) + self.APPEND_BEGIN
else:
print("Skipping duplicate opcode", word)