-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcwb-make-safe
executable file
·242 lines (215 loc) · 5.68 KB
/
cwb-make-safe
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
#! /bin/sh
# cwb-make-safe
#
# A wrapper script for cwb-make provided in the CWB Perl package
# working around a bug in cwb-huffcode that causes an error if a
# positional attribute has only a single value in the corpus.
#
# Run "cwb-make-safe -h" to get a usage message.
# TODO:
# - Use cwb-make also for single-value attributes if available and if
# CWB is recent enough to have been fixed in that respect (version
# 3.4.11 or later).
progname=`basename $0`
progdir=`dirname $0`
cmdline="$@"
shortopts="r:M:Vg:p:Dh"
longopts="registry:,memory:,validate,group:,permissions:,debug,help"
. $progdir/korp-lib.sh
registry_opt=
cwb_make_opts=
cwb_makeall_opts=
group=
perms=
corpus=
attrs=
find_prog_cwb () {
if ! find_prog cwb-$1 $CWB_BINDIR $default_cwb_bindirs; then
$2 "Program cwb-$1 not found on PATH or in directories $(echo $CWB_BINDIR $default_cwb_bindirs | sed -e 's/ \+/, /g')"
fi
}
# Use set -e to propagate the possible error exit from $(find_prog_cwb ...)
set -e
cwb_describe_corpus=$(find_prog_cwb describe-corpus error)
cwb_makeall=$(find_prog_cwb makeall error)
cwb_huffcode=$(find_prog_cwb huffcode error)
cwb_compress_rdx=$(find_prog_cwb compress-rdx error)
set +e
cwb_make=$(find_prog_cwb make warn)
# Try to find the library directory for CWB Perl based on the location
# of cwb-make (which needs it): replace trailing /bin with
# /lib/site_perl.
# FIXME: The library might not always be in that default location.
# What if it is not found? Should we use find to try to find CWB.pm?
cwb_perl_libdir=${cwb_make%/bin/*}/lib/site_perl
if [ -d $cwb_perl_libdir ]; then
if [ "x$PERL5LIB" != x ]; then
PERL5LIB=$PERL5LIB:$cwb_perl_libdir
else
PERL5LIB=$cwb_perl_libdir
fi
fi
export PERL5LIB
usage () {
cat <<EOF
Usage: $progname [options] corpus [attributes]
Index and compress the attributes of an encoded CWB corpus.
This is a wrapper script for cwb-make provided in the CWB Perl package
working around a bug in cwb-huffcode that causes an error if a
positional attribute has only a single value in the corpus.
The options are the same as for cwb-make:
EOF
if [ "x$cwb_make" = x ]; then
echo "cwb-make not found; cannot list options"
else
$cwb_make -h 2>&1 |
awk '/^Options:/ {p = 1; next} p && /./ {print}'
fi
cat <<EOF
However, please note that the registry directory name may not contain
whitespace characters.
EOF
exit 0
}
# Process options
while [ "x$1" != "x" ] ; do
case "$1" in
-h | --help )
usage
;;
-r | --registry )
shift
cwb_makeall_opts="$cwb_makeall_opts -r $1"
registry_opt="-r $1"
;;
-M | --memory )
shift
cwb_makeall_opts="$cwb_makeall_opts -M $1"
;;
-V | --validate )
cwb_makeall_opts="$cwb_makeall_opts -V"
;;
-g | --group )
shift
group=$1
cwb_make_opts="$cwb_make_opts -g $1"
;;
-p | --permissions )
shift
perms=$1
cwb_make_opts="$cwb_make_opts -p $1"
;;
-D | --debug )
cwb_makeall_opts="$cwb_makeall_opts -D"
;;
-- )
shift
break
;;
--* )
warn "Unrecognized option: $1"
;;
* )
break
;;
esac
shift
done
if [ "x$1" = x ]; then
error "No corpus specified"
fi
corpus=$1
shift
attrs=$@
cwb_make_opts="$cwb_makeall_opts $cwb_make_opts"
get_single_valued_pos_attrs () {
opt=
case $1 in
-* )
opt=$1
shift
;;
esac
$cwb_describe_corpus $registry_opt -s $corpus |
grep '^p-ATT' |
grep $opt '.*\b1 types' |
awk '{print $2}' |
if [ "x$1" != x ]; then
grep -F "$(echo $@ | tr ' ' '\n')"
else
cat
fi
}
get_corpus_data_dir () {
$cwb_describe_corpus $registry_opt $1 |
grep -E '^home directory: *' |
sed -e 's/home directory: *//'
}
cwb_make_manual () {
datadir=$(get_corpus_data_dir $corpus)
datafiles_patt=
make_attrs=
for attr in "$@"; do
if [ ! -e $datadir/$attr.corpus.cnt ] ||
[ ! -e $datadir/$attr.lexicon.srt ] ||
[ ! -e $datadir/$attr.huf ] ||
[ ! -e $datadir/$attr.crx ] ||
[ $datadir/$attr.corpus -nt $datadir/$attr.corpus.cnt ] ||
[ $datadir/$attr.lexicon -nt $datadir/$attr.lexicon.srt ] ||
[ $datadir/$attr.corpus -nt $datadir/$attr.huf ] ||
[ $datadir/$attr.corpus.rev -nt $datadir/$attr.crx ];
then
make_attrs="$make_attrs $attr"
rm_files=
for suf in corpus.cnt corpus.rdx corpus.rev lexicon.srt \
hcd huf syn crc crx;
do
rm_files="$rm_files $datadir/$attr.$suf"
done
rm -f $rm_files
datafiles_patt="$datafiles $datadir/$attr.*"
fi
done
if [ "x$make_attrs" != x ]; then
$cwb_makeall $cwb_makeall_opts $corpus $make_attrs
# echo to convert newlines to spaces
single_valued=$(echo $single_valued_attrs)
for attr in $make_attrs; do
if ! word_in $attr $single_valued_attrs; then
$cwb_huffcode -P $attr $corpus
if [ $? = 0 ]; then
rm $datadir/$attr.corpus
fi
$cwb_compress_rdx -P $attr $corpus
if [ $? = 0 ]; then
rm $datadir/$attr.corpus.rev $datadir/$attr.corpus.rdx
fi
fi
done
datafiles=$(ls $datafiles_patt)
if [ "x$perms" != x ]; then
chmod $perms $datafiles
fi
if [ "x$group" != x ]; then
chgrp $group $datafiles
fi
fi
}
single_valued_attrs=$(get_single_valued_pos_attrs $attrs)
if [ "x$cwb_make" != x ]; then
if [ "x$single_valued_attrs" = x ]; then
$cwb_make $cwb_make_opts $corpus $attrs
else
non_single_valued_attrs=$(get_single_valued_pos_attrs -v $attrs)
if [ "x$non_single_valued_attrs" != x ]; then
$cwb_make $cwb_make_opts $corpus $non_single_valued_attrs
fi
cwb_make_manual $single_valued_attrs
fi
else
warn "cwb-make not found: using cwb-makeall for all attributes"
if [ "x$attrs" = x ]; then
attrs=$(corpus_list_attrs $corpus pos)
fi
cwb_make_manual $attrs
fi