-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathevaluate_server.lua
More file actions
92 lines (87 loc) · 2.85 KB
/
evaluate_server.lua
File metadata and controls
92 lines (87 loc) · 2.85 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
require('json')
require('socket')
local beam = require 's2sa.beam'
local debugger = require('fb.debugger')
function main()
beam.init(arg)
local opt = beam.getOptions()
server = assert(socket.bind("*", opt.port))
server_state = "running"
ip, port = server:getsockname()
while server_state == "running" do
print("wait for connection on port: " .. opt.port)
local client = server:accept()
local input, err = client:receive()
local jsonInput = json.decode(input)
local flatInput = stringx.replace(jsonInput['input'], '\n', ' ')
flatInput = stringx.replace(flatInput, '\"', '\\"')
if (opt.verbose > 0) then
print('user: ' .. jsonInput['typeOfAmr'] .. '=' .. flatInput)
end
local action = jsonInput['typeOfAmr']
if action == 'full' then
action = 'anonymizeAmrFull'
elseif action == 'stripped' then
action = 'anonymizeAmrStripped'
else
action = 'anonymizeText'
end
-- anonymize and grab alignments
local f = io.popen('./anonDeAnon_java.sh ' .. action .. ' false \"' .. flatInput .. '\"', rw)
local anonymizedInput, alignments, graph
if action == 'anonymizeText' then
anonymizedInput, alignments = unpack(stringx.split(f:read('*all'), '#'))
else
anonymizedInput, alignments, graph = unpack(stringx.split(f:read('*all'), '#'))
end
alignments = stringx.replace(alignments, '\n', '')
if (opt.verbose > 0) then
print('anonymized: ' .. anonymizedInput)
print('alignments: ' .. alignments)
if (graph) then
print('graph: ' .. graph)
end
end
f:close()
if anonymizedInput == 'FAILED_TO_PARSE' then
if alignments == 'Failed to parse.' then
client:send('Failed to parse.\n')
else
client:send('Failed to parse. ' .. alignments .. '\n')
end
else
local pred, pred_score, attn, pred_out = beam.predSingleSentence(anonymizedInput)
pred_out = stringx.replace(pred_out, '\"', '\\"')
if (opt.verbose > 0) then
print('predicted: ' .. pred_out)
end
-- deAnonymize (always do the opposite action of what was given as input
if action == 'anonymizeText' then
action = 'deAnonymizeAmr'
else
action = 'deAnonymizeText'
end
if alignments == '\n' then
f = io.popen('./anonDeAnon_java.sh ' .. action .. ' false \"' .. pred_out .. '\"', rw)
else
f = io.popen('./anonDeAnon_java.sh ' .. action .. ' false \"' .. pred_out .. '#' .. alignments .. '\"', rw)
end
local deAnonymized
if action == 'deAnonymizeAmr' then
deAnonymized, graph = unpack(stringx.split(f:read('*all'), '#'))
else
deAnonymized = f:read('*all')
end
deAnonymized = stringx.replace(deAnonymized, '\n', '')
deAnonymized = stringx.replace(deAnonymized, '\"', '\\\"')
f:close()
if opt.verbose > 0 then
print('deAnonymized: ' .. deAnonymized)
print('graph: ' .. graph)
end
client:send('{\"predText\":\"' .. deAnonymized .. '\",' .. graph .. '}\n')
end
client:close()
end
end
main()