forked from keisks/robsut-wrod-reocginiton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarize.py
208 lines (190 loc) · 6.08 KB
/
binarize.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
#!/usr/bin/env python
#encoding: utf-8
import string
import numpy as np
import random
def hasnum(w):
for c_i in w:
if c_i.isdigit():
return True
return False
def noise_char(w, opt, alph):
if opt == "DELETE":
bin_all = [0]*len(alph)
if w == '<eos>':
bin_all[-1] += 1
elif w == '<unk>':
bin_all[-2] += 1
elif hasnum(w):
bin_all[-3] += 1
else:
idx = random.randint(0, len(w)-1)
w = w[:idx] + w[idx+1:]
for i in range(len(w)):
try:
bin_all[alph.index(w[i])] += 1
except ValueError:
print(w)
raise
return np.array(bin_all*3), w
if opt == "INSERT":
bin_all = [0]*len(alph)
if w == '<eos>':
bin_all[-1] += 1
elif w == '<unk>':
bin_all[-2] += 1
elif hasnum(w):
bin_all[-3] += 1
else:
ins_idx = random.randint(0, len(w)-1)
ins_char_idx = np.random.randint(0,len(string.ascii_lowercase))
ins_char = list(string.ascii_lowercase)[ins_char_idx]
w = w[:ins_idx] + ins_char + w[ins_idx:]
for i in range(len(w)):
try:
bin_all[alph.index(w[i])] += 1
except ValueError:
print(w)
raise
return np.array(bin_all*3), w
if opt == "REPLACE":
bin_all = [0]*len(alph)
if w == '<eos>':
bin_all[-1] += 1
elif w == '<unk>':
bin_all[-2] += 1
elif hasnum(w):
bin_all[-3] += 1
else:
target_idx = random.randint(0, len(w)-1)
rep_char_idx = np.random.randint(0,len(string.ascii_lowercase))
rep_char = list(string.ascii_lowercase)[rep_char_idx]
w = w[:target_idx] + rep_char + w[target_idx+1:]
for i in range(len(w)):
try:
bin_all[alph.index(w[i])] += 1
except ValueError:
print(w)
raise
return np.array(bin_all*3), w
def jumble_char(w, opt, alph):
if opt == "WHOLE":
bin_all = [0]*len(alph)
bin_filler = [0]*(len(alph)*2)
if w == '<eos>':
bin_all[-1] += 1
elif w == '<unk>':
bin_all[-2] += 1
elif hasnum(w):
bin_all[-3] += 1
else:
w = ''.join(random.sample(w, len(w)))
for i in range(len(w)):
try:
bin_all[alph.index(w[i])] += 1
except ValueError:
print(w)
raise
bin_all = bin_all + bin_filler
return np.array(bin_all), w
elif opt == "BEG":
bin_initial = [0]*len(alph)
bin_end = [0]*len(alph)
bin_filler = [0]*len(alph)
if w == '<eos>':
bin_initial[-1] += 1
bin_end[-1] += 1
elif w == '<unk>':
bin_initial[-2] += 1
bin_end[-2] += 1
elif hasnum(w):
bin_initial[-3] += 1
bin_end[-3] += 1
else:
if len(w) > 3:
w_init = ''.join(random.sample(w[:-1], len(w[:-1])))
w = w_init + w[-1]
for i in range(len(w)):
try:
if i==len(w)-1:
bin_end[alph.index(w[i])] += 1
else:
bin_initial[alph.index(w[i])] += 1
except ValueError:
print(w)
raise
bin_all = bin_initial + bin_end + bin_filler
return np.array(bin_all), w
elif opt == "END":
bin_initial = [0]*len(alph)
bin_end = [0]*len(alph)
bin_filler = [0]*len(alph)
if w == '<eos>':
bin_initial[-1] += 1
bin_end[-1] += 1
elif w == '<unk>':
bin_initial[-2] += 1
bin_end[-2] += 1
elif hasnum(w):
bin_initial[-3] += 1
bin_end[-3] += 1
else:
if len(w) > 3:
w_end = ''.join(random.sample(w[1:], len(w[1:])))
w = w[0] + w_end
for i in range(len(w)):
try:
if i==0:
bin_initial[alph.index(w[i])] += 1
else:
bin_end[alph.index(w[i])] += 1
except ValueError:
print(w)
raise
bin_all = bin_initial + bin_end + bin_filler
return np.array(bin_all), w
elif opt == "INT":
bin_initial = [0]*len(alph)
bin_middle = [0]*len(alph)
bin_end = [0]*len(alph)
if w == '<eos>':
bin_initial[-1] += 1
bin_middle[-1] += 1
bin_end[-1] += 1
elif w == '<unk>':
bin_initial[-2] += 1
bin_middle[-2] += 1
bin_end[-2] += 1
elif hasnum(w):
bin_initial[-3] += 1
bin_middle[-3] += 1
bin_end[-3] += 1
else:
if len(w) > 3:
w_mid = ''.join(random.sample(w[1:-1], len(w[1:-1])))
w = w[0] + w_mid + w[-1]
for i in range(len(w)):
try:
if i==0:
bin_initial[alph.index(w[i])] += 1
elif i==len(w)-1:
bin_end[alph.index(w[i])] += 1
else:
bin_middle[alph.index(w[i])] += 1
except ValueError:
print(w)
raise
bin_all = bin_initial + bin_middle + bin_end
return np.array(bin_all), w
else:
raise
if __name__ == "__main__":
alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,:;'*!?`$%&(){}[]-/\@_#"
word = 'research'
print(word)
v, w = noise_char(word, 'DELETE', alph)
print(w)
v, w = noise_char(word, 'INSERT', alph)
print(w)
v, w = noise_char(word, 'REPLACE', alph)
print(w)