forked from c-amr/camr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containerized.sh
executable file
·171 lines (156 loc) · 3.91 KB
/
containerized.sh
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
#!/bin/bash
basedir=`dirname $0`
datadir=/data
input=input
function usage {
echo "CAMR parser runner"
echo
echo "usage: $0 [--amrfmt] [--preserve] [--input NAME] [--output NAME]"
echo
echo "REST mode:"
echo " $0 --rest [--help] [more REST mode options...]"
echo
echo "--amrfmt expect input file to be in AMR format"
echo "--preserve preserve pre-processed file and parsed AMR (before post-processing)"
echo "--preserve-all preserve all intermediate files"
echo "--input NAME input filename relative to data directory (default: $input)"
echo "--output NAME output filename relative to data directory (default: $input.final)"
echo "--rest REST API mode, see --rest --help for more additional options"
echo
echo "required docker configuration: mount input and output file directory to $datadir"
echo
echo "for input and output:"
echo "* input file must be mounted to $datadir/input"
echo "* output file must be mounted to $datadir/output.final"
echo
echo "note: requires up to 22GB of RAM"
echo
}
# REST mode
for arg in $@; do
if [ "$arg" == "--rest" ] ; then
# remove --rest argument
args=()
for arg in "$@" ; do
if [ "$arg" != "--rest" ]; then
args=("${args[@]}" "$arg")
fi
done
cd $basedir
basedir=.
$basedir/rest.py "${args[@]}"
exit $?
fi
done
for arg in $@; do
if [ "$arg" == "--help" ] || [ "$arg" == "-h" ]; then
usage
exit 0
fi
done
preserve=0
while [ $# -gt 0 ]; do
if [ "$1" == "--" ]; then
shift
# break
elif [ "$1" == "--input" ]; then
shift
input="$1"
shift
elif [ "$1" == "--output" ]; then
shift
output="$1"
shift
elif [ "$1" == "--amrfmt" ] || [ "$1" == "--amr" ]; then
amrfmt="--amrfmt"
shift
elif [ "$1" == "--plain" ]; then
amrfmt=
shift
elif [ "$1" == "--preserve" ] && [ $preserve -eq 0 ]; then
preserve=1
shift
elif [ "$1" == "--preserve-all" ]; then
preserve=2
shift
else
echo "unknown argument: $1"
exit 1
fi
done
function clean {
local base="$1"
# echo "Cleaning $base"
if [ "${base: -4}" == ".amr" ]; then
# ends with .amr
rm -f "$base.tok" 2> /dev/null
rm -f "$base.tok.aligned" 2> /dev/null
else
# does not end with .amr
rm -f "$base.amr.tok" 2> /dev/null
rm -f "$base.amr.tok.aligned" 2> /dev/null
fi
rm -f "$base.sent" 2> /dev/null
rm -f "$base.sent.prp" 2> /dev/null
rm -f "$base.sent.tok" 2> /dev/null
rm -f "$base.sent.tok.charniak.parse" 2> /dev/null
rm -f "$base.sent.tok.charniak.parse.dep" 2> /dev/null
}
if [ ! -f "$datadir/$input" ]; then
echo "error: input file not found: $input"
exit 1
fi
input="$datadir/$input"
if [ "$output" == "" ]; then
output_parsed="$input.parsed"
output_final="$input.final"
else
output_parsed="$input.parsed"
output_final="$datadir/$output"
fi
if [ "$amrfmt" == "" ]; then
plain="--plain"
fi
cd $basedir
basedir=.
echo -n "Preprocessing ... "
$basedir/preprocess.py $plain "$input" > "$input.preprocessed" 2> $datadir/preprocess.log
if [ $? -eq 0 ]; then
echo "ok"
else
echo "error"
echo "--- begin of preprocess.log ---"
cat $datadir/preprocess.log
echo "--- end of preprocess.log ---"
echo "note: preprocess did run with errors"
echo
exit 1
fi
time $basedir/amr_parsing.py --model $basedir/model.m.bz2 --mode parse $amrfmt "$input.preprocessed" -o "$output_parsed" -W
if [ $? -ne 0 ]; then
echo "Error parsing"
exit 1
fi
if [ $preserve -lt 1 ]; then
rm -f "$input.preprocessed"
fi
if [ $preserve -lt 2 ]; then
clean "$input.preprocessed"
fi
# Post-processing
echo -n "Post-processing ... "
$basedir/postprocess.py "$output_parsed" > "$output_final" 2> $datadir/postprocess.log
if [ $? -eq 0 ]; then
echo "ok"
if [ $preserve -lt 1 ]; then
rm -f "$output_parsed"
fi
else
echo "error"
echo "--- begin of postprocess.log ---"
cat $datadir/postprocess.log
echo "--- end of postprocess.log ---"
echo "note: postprocess did run with errors"
echo
exit 1
fi