-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
259 lines (234 loc) · 7.5 KB
/
filter.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
import math
import scipy.stats
file1 = open(r'train.src', 'r') # The sentence simplification training set (WikiLarge, e.g.)
file2 = open(r'train.dst', 'r')
store1 = []
store2 = []
lines = file1.readlines()
for line in lines:
store1.append(line.strip().lower())
lines = file2.readlines()
for line in lines:
store2.append(line.strip().lower())
def normal_distribution_function(x, mean, std):
if x <= mean:
return 1
else:
result = 2 * (1 - scipy.stats.norm(mean, std).cdf(x))
return round(result, 3)
def for_sari_normal_distribution_function(x, mean, std):
if x >= mean:
return 1
else:
result = 2 * scipy.stats.norm(mean, std).cdf(x)
return round(result, 3)
# word/sentence
import nltk
import numpy as np
length_ratio = []
for idx, line in enumerate(store1):
tmp_src = len(nltk.tokenize.word_tokenize(line))/len(nltk.tokenize.sent_tokenize(line))
tmp_dst = len(nltk.tokenize.word_tokenize(store2[idx]))/len(nltk.tokenize.sent_tokenize(store2[idx]))
length_ratio.append(tmp_src/tmp_dst)
length_ratio_mean = np.mean(length_ratio)
length_ratio_std = np.std(length_ratio,ddof=1)
# print("")
# print(length_ratio_mean)
# print(length_ratio_std)
# print("")
# avg_word_complexity
file3 = open(r'lexicon.tsv', 'r')
lines = file3.readlines()
lexicon = {}
for line in lines:
lexicon[line.split()[0].lower()] = float(line.split()[1])
complexity_disperion = []
for idx, line in enumerate(store1):
tmp_src = nltk.tokenize.word_tokenize(line)
tmp_dst = nltk.tokenize.word_tokenize(store2[idx])
tmp_src_complexity = 0
tmp_dst_complexity = 0
tmp_src_length = 0
tmp_dst_length = 0
for word in tmp_src:
if lexicon.get(word) != None:
tmp_src_complexity = tmp_src_complexity + lexicon.get(word)
tmp_src_length += 1
for word in tmp_dst:
if lexicon.get(word) != None:
tmp_dst_complexity = tmp_dst_complexity + lexicon.get(word)
tmp_dst_length += 1
if tmp_src_length == 0:
tmp_src_length += 1
if tmp_dst_length == 0:
tmp_dst_length += 1
# print(tmp_src_complexity/tmp_src_length)
# print(tmp_dst_complexity/tmp_dst_length)
# if idx == 4:
# break
complexity_disperion.append((tmp_dst_complexity/tmp_dst_length)-(tmp_src_complexity/tmp_src_length))
complex_mean = np.mean(complexity_disperion)
complex_std = np.std(complexity_disperion,ddof=1)
# print("")
# print(complex_mean)
# print(complex_std)
# print("")
# odds_ratio
file4 = open(r'dict.txt', 'r')
ratio_dict = {}
lines = file4.readlines()
for line in lines:
line = line.split()
ratio_dict[line[0]] = float(line[1])
file5 = open(r'valid.src', 'r')
file6 = open(r'valid.dst', 'r')
src_lines = file5.readlines()
dst_lines = file6.readlines()
store3 = []
store4 = []
for line in src_lines:
store3.append(line.strip().lower())
for line in dst_lines:
store4.append(line.strip().lower())
odds_ratio = []
for idx, line in enumerate(store3):
tmp_src = nltk.tokenize.word_tokenize(line)
tmp_dst = nltk.tokenize.word_tokenize(store4[idx])
# print(tmp_src)
# print(tmp_dst)
src_odds_ratio = 0
dst_odds_ratio = 0
src_count = 0
dst_count = 0
for word in tmp_src:
if ratio_dict.get(word) != None:
src_odds_ratio += ratio_dict.get(word)
src_count += 1
for word in tmp_dst:
if ratio_dict.get(word) != None:
dst_odds_ratio += ratio_dict.get(word)
dst_count += 1
if dst_count == 0:
dst_count = 1
if src_count == 0:
src_count = 1
odds_ratio.append(dst_odds_ratio/dst_count - src_odds_ratio/src_count)
odds_ratio_mean = np.mean(odds_ratio)
odds_ratio_std = np.std(odds_ratio,ddof=1)
# print("")
# print(odds_ratio_mean)
# print(odds_ratio_std)
# print("")
file1.close()
file2.close()
file3.close()
file4.close()
file5.close()
file6.close()
# Here is the results for WikiLarge, you can use them directly. The above codes can be commented out.
length_ratio_mean = 2.90
length_ratio_std = 5.78
complex_mean = -0.03
complex_std = 0.43
odds_ratio_mean = -0.19
odds_ratio_std = 1.16
SARI_mean = 37.61
SARI_std = 23.50
# Select suitable pairs
file1 = open(r'complex.txt', 'r') # Aligned sentence pairs
file2 = open(r'simple.txt', 'r')
store1 = []
store2 = []
lines = file1.readlines()
for line in lines:
store1.append(line.strip().lower())
lines = file2.readlines()
for line in lines:
store2.append(line.strip().lower())
import nltk
import numpy as np
file3 = open(r'lexicon.tsv', 'r')
lines = file3.readlines()
lexicon = {}
for line in lines:
lexicon[line.split()[0].lower()] = float(line.split()[1])
file4 = open(r'dict.txt', 'r')
ratio_dict = {}
lines = file4.readlines()
for line in lines:
line = line.split()
ratio_dict[line[0]] = float(line[1])
file5 = open(r'sari_value.txt', 'r')
sari_values = []
lines = file5.readlines()
for line in lines:
sari_values.append(float(line.strip()))
total_score = []
for idx, line in enumerate(store1):
if idx % 50000 == 0:
print(idx)
if len(nltk.tokenize.word_tokenize(line)) == 0 or len(nltk.tokenize.word_tokenize(store2[idx])) == 0:
total_score.append(0)
continue
# length_ratio
tmp_src = len(nltk.tokenize.word_tokenize(line))/len(nltk.tokenize.sent_tokenize(line))
tmp_dst = len(nltk.tokenize.word_tokenize(store2[idx]))/len(nltk.tokenize.sent_tokenize(store2[idx]))
tmp_length_ratio = tmp_src/tmp_dst
length_ratio_score = normal_distribution_function(tmp_length_ratio, length_ratio_mean, length_ratio_std)
# complex
tmp_src = nltk.tokenize.word_tokenize(line)
tmp_dst = nltk.tokenize.word_tokenize(store2[idx])
tmp_src_complexity = 0
tmp_dst_complexity = 0
tmp_src_length = 0
tmp_dst_length = 0
for word in tmp_src:
if lexicon.get(word) != None:
tmp_src_complexity = tmp_src_complexity + lexicon.get(word)
tmp_src_length += 1
for word in tmp_dst:
if lexicon.get(word) != None:
tmp_dst_complexity = tmp_dst_complexity + lexicon.get(word)
tmp_dst_length += 1
if tmp_src_length == 0:
tmp_src_length += 1
if tmp_dst_length == 0:
tmp_dst_length += 1
tmp_complexity = tmp_dst_complexity/tmp_dst_length - tmp_src_complexity/tmp_src_length
complexity_score = normal_distribution_function(tmp_complexity, complex_mean, complex_std)
# odds_ratio
tmp_src = nltk.tokenize.word_tokenize(line)
tmp_dst = nltk.tokenize.word_tokenize(store2[idx])
src_odds_ratio = 0
dst_odds_ratio = 0
src_count = 0
dst_count = 0
for word in tmp_src:
if ratio_dict.get(word) != None:
src_odds_ratio += ratio_dict.get(word)
src_count += 1
for word in tmp_dst:
if ratio_dict.get(word) != None:
dst_odds_ratio += ratio_dict.get(word)
dst_count += 1
if dst_count == 0:
dst_count = 1
if src_count == 0:
src_count = 1
tmp_odds_ratio = dst_odds_ratio/dst_count - src_odds_ratio/src_count
odds_ratio_score = normal_distribution_function(tmp_odds_ratio, odds_ratio_mean, odds_ratio_std)
# SARI
sari_value = sari_values[idx]
sari_score = for_sari_normal_distribution_function(sari_value, SARI_mean, SARI_std)
# Total score
total_score.append(length_ratio_score + complexity_score + odds_ratio_score + sari_score)
file6 = open(r'total_score.txt', 'w')
for score in total_score:
file6.write(str(score))
file6.write('\n')
file1.close()
file2.close()
file3.close()
file4.close()
file5.close()
file6.close()