forked from sinantie/NeuralAmr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.lua
More file actions
206 lines (193 loc) · 5.95 KB
/
evaluate.lua
File metadata and controls
206 lines (193 loc) · 5.95 KB
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
local beam = require 's2sa.beam'
function main()
beam.init(arg)
local opt = beam.getOptions()
local typeOfInput = opt.input_type
if opt.interactive_mode == 0 then
assert(path.exists(opt.src_file), 'src_file does not exist')
local sent_id = 0
file_size = 0
for _ in io.lines(opt.src_file) do
file_size = file_size + 1
end
-- produce anonymized version of AMR file and the corresponding alignments in two separate files
if typeOfInput ~= 'anonymized' and typeOfInput ~= 'textAnonymized' then
anonymizeFile(typeOfInput, opt.src_file)
end
local file = io.open(opt.src_file .. '.anonymized', "r")
local out_file = io.open(opt.output_file .. '.pred.anonymized','w')
for line in file:lines() do
sent_id = sent_id + 1
xlua.progress(sent_id, file_size)
if typeOfInput == 'text' or typeOfInput == 'textAnonymized' then
out_file:write(parse('textAnonymized', line, opt.verbose) .. '\n')
else
out_file:write(generate('anonymized', line, opt.verbose) .. '\n')
end
end
out_file:close()
file:close()
-- deAnonymize predictions using alignments
if typeOfInput ~= 'anonymized' and typeOfInput ~= 'textAnonymized' then
deAnonymizeFile(typeOfInput, opt.src_file)
end
if typeOfInput == 'text' then
killNerServer()
end
else
if typeOfInput == 'text' or typeOfInput == 'textAnonymized' then
print('Input text [Type q to exit]:')
else
print('Input AMR in ' .. typeOfInput .. ' format [Type q to exit]:')
end
while true do
local input = io.read()
if input == 'q' then
if typeOfInput == 'text' then
killNerServer()
end
break
end
if typeOfInput == 'text' or typeOfInput == 'textAnonymized' then
print(parse(typeOfInput, input, opt.verbose))
else
print(generate(typeOfInput, input, opt.verbose))
end
end
end
end
function generate(typeOfInput, input, verbose)
if typeOfInput == 'anonymized' then
local pred, pred_score, attn, pred_out = predSingleSentence(input)
return pred_out
else
-- clean
input = clean(input)
-- anonymize
local result, anonymizedInput, alignments = anonymizeAmr(typeOfInput, input, verbose)
if not result then
return anonymizedInput -- error message
else
-- predict
local pred, pred_score, attn, pred_out = predSingleSentence(anonymizedInput)
pred_out = stringx.replace(pred_out, '\"', '\\"')
if verbose > 0 then
print('predicted (anonymized): ' .. pred_out)
end
-- deAnonymize
return deAnonymize(pred_out, alignments, true)
end
end
end
function parse(typeOfInput, input, verbose)
if typeOfInput == 'textAnonymized' then
local pred, pred_score, attn, pred_out = predSingleSentence(input)
return pred_out
else
-- clean
input = clean(input)
-- anonymize
local anonymizedInput, alignments = anonymizeText(input, verbose)
-- predict
local pred, pred_score, attn, pred_out = predSingleSentence(anonymizedInput)
pred_out = stringx.replace(pred_out, '\"', '\\"')
if verbose > 0 then
print('predicted (anonymized): ' .. pred_out)
end
-- deAnonymize
output, _ = unpack(stringx.split(deAnonymize(pred_out, alignments, false), '#'))
return output
end
end
function clean(input)
local flatInput = stringx.replace(input, '\n', ' ')
flatInput = stringx.replace(flatInput, '\"', '\\"')
return flatInput
end
function anonymizeFile(typeOfInput, path)
local action
if typeOfInput == 'stripped' then
action = 'anonymizeAmrStripped'
elseif typeOfInput == 'full' then
action = 'anonymizeAmrFull'
else
action = 'anonymizeText'
end
local f = io.popen('./anonDeAnon_java.sh ' .. action .. ' true \"' .. path .. '\"', w)
f:close()
end
function anonymizeAmr(typeOfInput, input, verbose)
-- anonymize and grab alignments
local action
if typeOfInput == 'stripped' then
action = 'anonymizeAmrStripped'
else
action = 'anonymizeAmrFull'
end
local f = io.popen('./anonDeAnon_java.sh ' .. action .. ' false \"' .. input .. '\"', rw)
local anonymizedInput, alignments = unpack(stringx.split(f:read('*all'), '#'))
alignments = stringx.replace(alignments, '\n', '')
if verbose > 0 then
print('anonymized: ' .. anonymizedInput)
print('alignments: ' .. alignments)
end
f:close()
if anonymizedInput == 'FAILED_TO_PARSE' then
if alignments == 'Failed to parse.' then
return false, 'Failed to parse.', ""
else
return false, 'Failed to parse. ' .. alignments, ""
end
else
return true, anonymizedInput, alignments
end
end
function deAnonymizeFile(typeOfInput, path)
local action
if typeOfInput == 'text' then
action = 'deAnonymizeAmr'
else
action = 'deAnonymizeText'
end
local f = io.popen('./anonDeAnon_java.sh ' .. action .. ' true \"' .. path .. '\"', w)
f:close()
end
function deAnonymize(pred_out, alignments, isText)
local f
if alignments == '\n' then
if isText then
f = io.popen('./anonDeAnon_java.sh deAnonymizeText false \"' .. pred_out .. '\"', rw)
else
f = io.popen('./anonDeAnon_java.sh deAnonymizeAmr false \"' .. pred_out .. '\"', rw)
end
else
if isText then
f = io.popen('./anonDeAnon_java.sh deAnonymizeText false \"' .. pred_out .. '#' .. alignments .. '\"', rw)
else
f = io.popen('./anonDeAnon_java.sh deAnonymizeAmr false \"' .. pred_out .. '#' .. alignments .. '\"', rw)
end
end
local deAnonymized = f:read('*all')
deAnonymized = stringx.replace(deAnonymized, '\n', '')
f:close()
return deAnonymized
end
function deAnonymizeAmr(pred_out, alignments)
end
function anonymizeText(input, verbose)
-- anonymize and grab alignments
local f = io.popen('./anonDeAnon_java.sh anonymizeText false \"' .. input .. '\"', rw)
local anonymizedInput, alignments = unpack(stringx.split(f:read('*all'), '#'))
alignments = stringx.replace(alignments, '\n', '')
if verbose > 0 then
print('anonymized: ' .. anonymizedInput)
print('alignments: ' .. alignments)
end
f:close()
return anonymizedInput, alignments
end
function killNerServer()
local f = io.popen('./anonDeAnon_java.sh anonymizeText false \"terminate_server\"', w)
f:close()
end
main()