-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxtx.sh
More file actions
executable file
·275 lines (240 loc) · 9.36 KB
/
xtx.sh
File metadata and controls
executable file
·275 lines (240 loc) · 9.36 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/bash
#the default saxon
s=saxon9he.jar
export tmpDir="tmp_$$"
export pathToLogfile="$tmpDir/log.txt"
methodKeys="'makeXSL', 'get-profile','rmNl', 'tokenize', 'vert-xml' or 'vert-txt'"
usage() {
echo "xtx.sh - a shell script frontend for xml tokenization"
echo "====================================================="
echo "Parameters:"
echo " -p / --profile: The name of the tokenization profile. (MANDADORY)"
echo " -pv / --paramValue: read profile and return the value of given parameter (implies -m = get-profile)"
echo " -i / --input: The path to the XML document to be tokenized."
echo " -o / --output: The path to the tokenized dokument. If not given, the scripts outputs to the shell."
echo " -m / --method: One of $methodKeys (MANDADORY)"
echo " * makeXSL: Re-compile the wrapper stylesheets (done automatically, if the profile definition document has changed)."
echo " * get-profile: Return the XML definition of the tokenizeation profile."
echo " * rmNl: Return the input document with pretty-print newlines stripped"
echo " * tokenize: Return the input document with added tokens."
echo " * vert-xml: Return a vertical of the tokens as an XML tokument."
echo " * vert-txt: Return a vertical of the tokens as a text file."
echo " -s/ --saxon: The path to a JAR distribution of the Saxon XSLT Processor. If this is not set, Saxon 9 HE (saxon9he.jar) must be present in your \$PATH for this script to work."
echo " -f / --force: Force rebuilding the wrapper stylesheets"
echo "This script is merely provided for convenience - other methods of invoking the stylesheets are most probably more efficient."
echo ""
}
# write to log file
# $1 = message to be written
log () {
echo "$1" >> $pathToLogfile
}
# retrieve parameter value from profile
# $1 = profile name
# $2 = parameter name
profileParamValue() {
profile $1 | grep "param key=\"$2\"" | grep -oP '(?<=value=").+(?=\")'
}
# return profile definition document by profile name
# $1 = profile name
profile() {
cat "`profilePath $1`"
}
# return path to profile definition directory by profile name
# $1 = profile name
profileDir() {
echo "profiles/$1"
}
# return path to profile definition document by profile name
# $1 = profile name
profilePath() {
echo "profiles/$1/profile.xml"
}
# return path to the cached post-tokenization XSLT scripts extracted from tokenization profile
# $1 = profile name
postTokXSLDir() {
echo "`profileDir $1`/postTokenization"
}
# prepare profile wrapper stylesheets
# $1 = profile name
mkXSL() {
profileDir=`profileDir $1`
profilePath=`profilePath $1`
postTokXSLDir=`postTokXSLDir $1`
[ -d "$postTokXSLDir" ] && rm -r $postTokXSLDir
$s -xi -s:"$profilePath" -xsl:xsl/make_xsl.xsl "output-base-path=$profileDir" "postTokXSLDir=$postTokXSLDir" $d
eval "md5sum `profilePath $1`" > "`profileDir $1`/compiledFrom"
}
# remove new lines
# $1 = input file
rmNl() {
$s -s:$1 -xsl:xsl/rmNl.xsl $d
}
# tokenize
# $1 = input file
# $2 = profile name
# $3 = override tokenNamespace parameter (needed by vert-txt())
# $4 = override preserveWs parameter (needed by vert-txt())
tokenize() {
profileDir="`profileDir $2`"
log "starting rmNl"
eval "rmNl $1" > $tmpDir/rmnl.xml
log "stopped rmNl"
xsl="wrapper_toks.xsl"
log "starting $xsl"
eval "$s -s:$tmpDir/rmnl.xml -xsl:$profileDir/$xsl $d" > $tmpDir/0_toks.xml
log "stopped $xsl"
xsl="wrapper_addP.xsl"
eval log "starting $xsl"
$s -s:"$tmpDir/0_toks.xml" -xsl:"$profileDir/$xsl" -o:"$tmpDir/0_toks_PAdded.xml" $d
log "stopped $xsl"
# apply post-tokenization stylesheets
postTokXSLDir=`postTokXSLDir $2`
echo "\$postTokXSLDir=$postTokXSLDir" >> $pathToLogfile
if [ -e $postTokXSLDir ];
then
noOfPostTokenizationXSLs=`ls $postTokXSLDir/*.xsl | wc -l`
echo $noOfPostTokenizationXSLs > "$tmpDir/noOfPostTokenizationXSLs.txt"
for i in `ls $postTokXSLDir/*.xsl`; do
pos=`basename $i .xsl`
log "starting $pos"
[ $pos = "1" ] && input="$tmpDir/0_toks_PAdded.xml" || input="$tmpDir/0_toks_PAdded_$((pos - 1)).xml"
$s -s:$input -xsl:"$i" $d > "$tmpDir/0_toks_PAdded_$pos.xml"
log "stopped $xsl"
done
pathToPostProcessedTok="$tmpDir/0_toks_PAdded_$noOfPostTokenizationXSLs.xml"
else
pathToPostProcessedTok="$tmpDir/0_toks_PAdded.xml"
fi
# create TEI version if wanted; otherwise just remove whitespace nodes if needed
[ -z $3 ] && tokenNamespace=`profileParamValue $2 "token-namespace"` || tokenNamespace=$3
[ -z $4 ] && preserveWs=`profileParamValue $2 "preserve-ws"` || preserveWs=$4
if [ $tokenNamespace = 'tei' ];
then
$s -s:$pathToPostProcessedTok -xsl:xsl/xtoks2tei.xsl "preserve-ws=$preserveWs" $d
else
if [ $preserveWs = 'true' ];
then
cat $pathToPostProcessedTok
else
$s -s:$pathToPostProcessedTok -xsl:xsl/rmWs.xsl "preserve-ws=$preserveWs"
fi
fi
}
# verticalize to XML
# $1 = input file
# $2 = profile name
# $3 = override tokenNamespace parameter (needed by vert-txt())
# $4 = override preserveWs parameter (needed by vert-txt())
vert-xml() {
eval "tokenize $1 $2 xtoks" > $tmpDir/1_toks.xml
xsl="wrapper_xtoks2vert.xsl"
$s -s:$tmpDir/1_toks.xml -xsl:"`profileDir $2`/$xsl" -o:"$tmpDir/2_vert.xml" $d
# create TEI version if wanted, otherwise just remove whitespace nodes
[ -z $3 ] && tokenNamespace=`profileParamValue $2 "token-namespace"` || tokenNamespace=$3
[ -z $3 ] && preserveWs=`profileParamValue $2 "preserve-ws"` || preserveWs=$4
if [ $tokenNamespace = 'tei' ];
then
# whitespace is handled by xtoks2tei.xsl
$s -s:$tmpDir/2_vert.xml -xsl:xsl/xtoks2tei.xsl "preserve-ws=$preserveWs" $d
else
# when outputting tokens in xtoks namespace, we need to remove whitespace separately
if [ $preserveWs = 'true' ];
then
cat $tmpDir/2_vert.xml
else
$s -s:$pathToPostProcessedTok -xsl:xsl/rmWs.xsl "preserve-ws=$preserveWs"
fi
fi
}
# verticalize to txt
# $1 = input file
# $2 = profile name
vert-txt() {
eval "vert-xml $1 $2 xtoks true" > $tmpDir/2_vert.xml
xsl="wrapper_vert2txt.xsl"
$s -s:$tmpDir/2_vert.xml -xsl:"`profileDir $2`/$xsl" $d
}
# returns if the profile has changed since last wrapper stylesheet compilation
# $1 = profile name
profile-has-changed(){
if [ -e "`profileDir $profile`/compiledFrom" ]; then
! md5sum -c "`profileDir $profile`/compiledFrom" --status
else
true
fi
}
# Test if there is any parameter given, otherwise report usage.
if [ $# -eq 0 ]
then { usage; exit; }
fi
# Parse user-provided arguments
while [ "$1" != "" ]; do
case $1 in
-i | --input ) shift
input=$1
;;
-p | --profile ) shift
profile=$1
;;
-pv | --paramValue ) shift
paramValue=$1
;;
-f | --force ) shift
forceRebuild="true"
;;
-o | --output ) shift
output=$1
;;
-m | --method ) shift
method=$1
;;
-s | --saxon ) shift
s=$(cd "$(dirname "$1")"; pwd)/$(basename "$1")
;;
-d | --debug ) shift
d="debug=$1"
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
#Testing for required parameters
[ -z $profile ] && { echo "ERROR: Missing profile name. Please provide parameter -p or --profile."; exit 1; }
[ -z $method ] && { echo "ERROR: Missing method name. Please provide parameter -m or --method. Must be $methodKeys. "; exit 1; }
[ ! -e "profiles/$profile/profile.xml" ] && { echo "ERROR: Unknown profile named '$profile'. Profile definition file not found at profiles/$profile/profile.xml"; exit 1; }
[ ! -e $s ] && { echo "Saxon not found at $s"; exit 1; }
s="java -jar $s"
case $method in
get-profile) [ -z $paramValue ] && `profile $profile` || profileParamValue $profile $paramValue ;;
makeXSL) mkXSL $profile ;;
rmNl|tokenize|vert-xml|vert-txt)
echo "Using Saxon at $s"
[ -z $input ] && { echo "ERROR: Missing parameter for input file. Please provide parameter -i or --input"; exit 1; }
#Checking existence of input document
[ ! -e $input ] && { echo "ERROR: Input file $input not found."; exit 1; }
# Only re-make wrapper Stylesheets if there is no compiledFrom file in the profile directory or if the hash of the profile definition document has changed.
if [ ! -e "`profileDir $profile`/compiledFrom" ] || profile-has-changed $profile ;
then
echo "The profile has changed. Re-compiling the wrapper stylesheets in `profileDir $profile`."
mkXSL $profile
else
if [[ "$forceRebuild" = "true" ]];
then
echo "-f / --force flag is set: forcing stylesheet build"
mkXSL $profile
else
echo "There are no profile updates. Using the existing wrapper stylesheets in `profileDir $profile`."
fi
fi
mkdir $tmpDir
echo "made $tmpDir"
[ -z $output ] && $method $input $profile || { eval "$method $input $profile" > $output ; }
[ -z $d ] && rm -rf "$tmpDir"
;;
*) echo "Invalid method $method. Must be either $methodKeys"; exit 1 ;;
esac