blob: 3390ff6ee0dec73c76ff62cf2faca9b31b34cd1e [file] [log] [blame]
Akron9cb13942020-02-14 07:39:54 +01001#!/usr/bin/env perl
Peter Hardersd892a582020-02-12 15:45:22 +01002use strict;
3use warnings;
Peter Harders6f526a32020-06-29 21:44:41 +02004
5use Pod::Usage;
6use Getopt::Long qw(GetOptions :config no_auto_abbrev);
7
8use File::Basename qw(dirname);
Peter Hardersd892a582020-02-12 15:45:22 +01009
10use open qw(:std :utf8); # assume utf-8 encoding
11use Encode qw(encode_utf8 decode_utf8);
12
Peter Hardersd892a582020-02-12 15:45:22 +010013use XML::CompactTree::XS;
14use XML::LibXML::Reader;
Peter Hardersd892a582020-02-12 15:45:22 +010015
Akron4f67cd42020-07-02 12:27:58 +020016use FindBin;
17BEGIN {
18 unshift @INC, "$FindBin::Bin/../lib";
19};
20
Akron95bc98a2020-07-11 12:00:12 +020021use KorAP::XML::TEI qw'remove_xml_comments';
Akron8b511f92020-07-09 17:28:08 +020022use KorAP::XML::TEI::Tokenizer::External;
Akrond9627472020-07-09 16:53:09 +020023use KorAP::XML::TEI::Tokenizer::Conservative;
24use KorAP::XML::TEI::Tokenizer::Aggressive;
Akron85717512020-07-08 11:19:19 +020025use KorAP::XML::TEI::Zipper;
Peter Hardersd892a582020-02-12 15:45:22 +010026
Akrond949e182020-02-14 12:23:57 +010027our $VERSION = '0.01';
Peter Harders6f526a32020-06-29 21:44:41 +020028
Akrond949e182020-02-14 12:23:57 +010029our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n";
30
Peter Hardersd892a582020-02-12 15:45:22 +010031
Peter Harders6f526a32020-06-29 21:44:41 +020032# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010033GetOptions(
Peter Harders6f526a32020-06-29 21:44:41 +020034 "root|r=s" => \(my $_root_dir = '.'), # name of root directory inside zip file
35 "input|i=s" => \(my $input_fname = ''), # input file (yet only TEI I5 Format accepted)
Akron8b511f92020-07-09 17:28:08 +020036 'tokenizer-call|tc=s' => \(my $tokenizer_call), # Temporary argument for testing purposes
37 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010038 pod2usage(
39 -verbose => 99,
40 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
41 -msg => $VERSION_MSG,
42 -output => '-'
43 )
44 },
45 'version|v' => sub {
46 pod2usage(
47 -verbose => 0,
48 -msg => $VERSION_MSG,
49 -output => '-'
50 )
51 }
Peter Hardersd892a582020-02-12 15:45:22 +010052);
53
Peter Harders6f526a32020-06-29 21:44:41 +020054#
55# ~~~ parameter (mandatory) ~~~
56#
Peter Hardersd892a582020-02-12 15:45:22 +010057
Peter Harders6f526a32020-06-29 21:44:41 +020058 # optional
59my $_CORP_SIGLE = "korpusSigle"; # opening and closing tags (without attributes) have to be in one line
60 # (e.g.: <korpusSigle>GOE</korpusSigle>)
61 # optional
62my $_DOC_SIGLE = "dokumentSigle"; # analog
63 # mandatory
64my $_TEXT_SIGLE = "textSigle"; # analog
65 # mandatory
66my $_TEXT_BODY = "text"; # tag (without attributes), which contains the primary text
67 # optional
68my $_CORP_HEADER_BEG = "idsHeader type=\"corpus\""; # just keep the correct order of the attributes and evtl. add an '.*' between them
69 # optional
70my $_DOC_HEADER_BEG = "idsHeader type=\"document\""; # analog
71 # mandatory
72my $_TEXT_HEADER_BEG = "idsHeader type=\"text\""; # analog
73
74#
75# ~~~ constants ~~~
76#
Peter Hardersd892a582020-02-12 15:45:22 +010077
Peter Harders41c35622020-07-12 01:16:22 +020078## extern tokenization
Akron8b511f92020-07-09 17:28:08 +020079my $_GEN_TOK_EXT = $tokenizer_call ? 1 : 0; # (used for IDS internal tokenization)
Akron8b511f92020-07-09 17:28:08 +020080 # TODO:
81 # Read tokenizer call from configuration file.
82 # was 'java -cp '. join(":", ".", glob(&dirname(__FILE__)."/../target/*.jar")). " de.ids_mannheim.korap.tokenizer.KorAPTokenizerImpl";
83 my $ext_tok;
84 if ($tokenizer_call) {
85 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
86 };
Peter Harders41c35622020-07-12 01:16:22 +020087 my $_tok_file_ext = "tokens.xml";
Peter Harders6f526a32020-06-29 21:44:41 +020088##
89
Akron8b511f92020-07-09 17:28:08 +020090## intern tokenization
Peter Harders41c35622020-07-12 01:16:22 +020091my $_GEN_TOK_INT = 1; # simple tokenization, recommended for testing (for use of an external tokenizer see $_GEN_TOK_EXT)
92 my $_tok_file_con = "tokens_conservative.xml";
93 my $_tok_file_agg = "tokens_aggressive.xml";
94 my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
95 my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Peter Harders41c35622020-07-12 01:16:22 +020096##
97
98my $_tok_dir = "base"; # name of directory for storing tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +020099
Peter Harders6f526a32020-06-29 21:44:41 +0200100my $_DEBUG = 0; # set to 1 for minimal more debug output (no need to be parametrized)
101my $_XCT_LN = 0; # only for debugging: include line numbers in elements of $tree_data
102 # (see also manpage of XML::CompactTree::XS)
103
104my $_header_file = "header.xml"; # name of files containing the text, document and corpus header
105my $_data_file = "data.xml"; # name of file containing the primary text data (tokens)
106my $_structure_dir = "struct"; # name of directory containing the $_structure_file
107my $_structure_file = "structure.xml"; # name of file containing all tags (except ${_TOKEN_TAG}'s) related information
108 # (= their names and byte offsets in $_data)
109## TODO: optional (different annotation tools can produce more zip-files for feeding into KorAP-XML-Krill)
110my $_TOKENS_PROC = 1; # on/off: processing of ${_TOKEN_TAG}'s (default: 1)
111my $_tokens_dir = "tokens"; # name of directory containing the $_tokens_file
112my $_tokens_file = "morpho.xml"; # name of file containing all ${_TOKEN_TAG}'s related information (=their byte offsets in $_data)
113 # - evtl. with additional inline annotations
114my $_TOKENS_TAG = "w"; # name of tag containing all information stored in $_tokens_file
115
116## TODO: optional
117# handling inline annotations (inside $_TOKENS_TAG)
118my $_INLINE_ANNOT = 0; # on/off: set to 1 if inline annotations are present and should be processed (default: 0)
119my $_INLINE_LEM_RD = "lemma"; # from which attribute to read LEMMA information
120my $_INLINE_ATT_RD = "ana"; # from which attribute to read POS information (and evtl. additional MSD - Morphosyntactic Descriptions)
121 # TODO: The format for the POS and MSD information has to suffice the regular expression ([^ ]+)( (.+))?
122 # - which means, that the POS information can be followed by an optional blank with additional
123 # MSD information; unlike the MSD part, the POS part may not contain any blanks.
124my $_INLINE_POS_WR = "pos"; # name (inside $_tokens_file) referring to POS information
125my $_INLINE_MSD_WR = "msd"; # name (inside $_tokens_file) referring to MSD information
126my $_INLINE_LEM_WR = "lemma"; # name (inside $_tokens_file) referring to LEMMA information
127##
128
129
130#
131# ~~~ variables ~~~
132#
133
Akron85717512020-07-08 11:19:19 +0200134# Initialize zipper
135my $zipper = KorAP::XML::TEI::Zipper->new;
Peter Harders6f526a32020-06-29 21:44:41 +0200136my $input_fh; # input file handle (default: stdin)
137
138my $buf_in; # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader
139my $data; # contains the primary text (created by func. 'retr_info' from $buf_in), which is written to '$data_file'
140
141my $dir; # text directory (below $_root_dir)
142my $dir_crp; # corpus directory (below $_root_dir)
143my $dir_doc; # document directory (below $_root_dir)
144
145my ( $text_id, $text_id_esc ); # '$text_id_esc' = escaped version of $text_id (see %ent)
146
147my %ent = ('"', '&quot;', '&','&amp;', # convert '&', '<' and '>' into their corresponding sgml-entities
148 '<','&lt;','>','&gt;');
149 # note: the index still refers to the 'single character'-versions, which are counted as 1
150 # (search for '&amp;' in data.xml and see corresponding indices in $_tokens_file)
151
152my $header_txt; # raw text header (written to '$_root_dir$dir/$_header_file')
153my $header_doc; # raw document header (written to '$_root_dir$dir_doc/$_header_file')
154my $header_crp; # raw corpus header (written to '$_root_dir$dir_crp/$_header_file')
155
156my ( $header_fl_crp, $header_fl_doc, # flags for tracking where we are in the input document
157 $header_fl_txt, $data_fl );
158
159my ( $header_prfx, $data_prfx1, # $header_prfx is written to $_header_file, $data_* are written to $_data_file
160 $data_prfx2, $data_sfx );
161
162my @structures; # list of arrays, where each array represents a TEI I5 tag (except $_TOKENS_TAG) from the input document
163 # - the input of this array is written in func. 'write_structures' into the file '$_structure_file'
164
165my @tokens; # list of arrays, where each array represents a $_TOKENS_TAG from the input document
166 # - the input of this array is written in func. 'write_tokens' into the file '$_tokens_file'
167
168my ( $ref, $idx, $att_idx ); # needed in func. 'write_structures' and 'write_tokens'
169
170my ( $reader, # instance of 'XML::LibXML::Reader->new' (on input '$buf_in')
171 $tree_data ); # instance of 'XML::CompactTree::XS::readSubtreeToPerl' (on input '$reader')
172
173# these are only used inside recursive function 'retr_info'
174my ( $_IDX, # value is set dependent on $_XCT_LN - for extracting array of child elements from element in $tree_data
175 $e, # element from $tree_data
176 $n, # tag name of actual processed element $e
177 $rl, # recursion level
178 $dl, # actual length of string $data
179 @oti, # oti='open tags indizes' - a stack of indizes into @structures, where the top index in @oti
180 # represents the actual processed element from @structures
181 @oti2, # analogously to @oti, but with reference to array @tokens
182 $inside_tokens_tag, # flag is set, when inside $_TOKENS_TAG
183 ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags)
184 $add_one, # ...
185 $fval, $fval2, # ...
Peter Harders41c35622020-07-12 01:16:22 +0200186 %ws); # hash for indices of whitespace-nodes (needed to recorrect from-values)
187 # idea: when closing element, check if it's from-index minus 1 refers to a whitespace-node
Peter Harders6f526a32020-06-29 21:44:41 +0200188 # (means: 'from-index - 1' is a key in %ws).
189 # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1
190
191my $output; # temporary variable needed in 'write_*'-functions for writing output to zip-stream $zip)
192
193my ( $i, $c ); # index variables used in loops
194
Peter Harders6f526a32020-06-29 21:44:41 +0200195my ( $_CORP_HEADER_END, $_DOC_HEADER_END, $_TEXT_HEADER_END );
196
197
198#
199# ~~~ main ~~~
200#
201
202# ~ initializations ~
203
204($_XCT_LN)?($_IDX=5):($_IDX=4);
205
206$header_prfx = $data_prfx1 = $data_prfx2 = $data_sfx = "";
207
208$header_fl_txt = $header_fl_doc = $header_fl_crp = 0;
209
210$inside_tokens_tag = -1;
211
212$fval = $fval2 = 0;
213
214$_root_dir .= '/'; # base dir must always end with a slash
215$_root_dir =~ s/^\.?\///; # remove leading / (only relative paths allowed in IO::Compress::Zip) and redundant ./
216
217$_CORP_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_CORP_HEADER_END = $1;
218$_DOC_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_DOC_HEADER_END = $1;
219$_TEXT_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_TEXT_HEADER_END = $1;
220
221## TODO: remove this, because it's IDS-specific
Peter Hardersd892a582020-02-12 15:45:22 +0100222$header_prfx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
223$header_prfx .= "<?xml-model href=\"header.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n";
224$header_prfx .= "<!DOCTYPE idsCorpus PUBLIC \"-//IDS//DTD IDS-XCES 1.0//EN\" \"http://corpora.ids-mannheim.de/idsxces1/DTD/ids.xcesdoc.dtd\">\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200225##
Peter Hardersd892a582020-02-12 15:45:22 +0100226
227$data_prfx1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
228$data_prfx1 .= "<?xml-model href=\"text.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n";
229$data_prfx1 .= "<raw_text docid=\"";
230$data_prfx2 .= "\" xmlns=\"http://ids-mannheim.de/ns/KorAP\">\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200231## TODO: can 'metadata.xml' change or is it constant?
Peter Hardersd892a582020-02-12 15:45:22 +0100232$data_prfx2 .= " <metadata file=\"metadata.xml\" />\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200233##
Peter Hardersd892a582020-02-12 15:45:22 +0100234$data_prfx2 .= " <text>";
235$data_sfx = "</text>\n</raw_text>";
236
Peter Hardersd892a582020-02-12 15:45:22 +0100237
Peter Harders6f526a32020-06-29 21:44:41 +0200238# ~ read input and write output (text by text) ~
Peter Harders41c35622020-07-12 01:16:22 +0200239main();
Peter Hardersd892a582020-02-12 15:45:22 +0100240
Peter Hardersd892a582020-02-12 15:45:22 +0100241
Peter Harders6f526a32020-06-29 21:44:41 +0200242#
243# ~~~ subs ~~~
244#
Peter Hardersd892a582020-02-12 15:45:22 +0100245
Peter Hardersd892a582020-02-12 15:45:22 +0100246
Peter Harders41c35622020-07-12 01:16:22 +0200247sub main {
Peter Hardersd892a582020-02-12 15:45:22 +0100248
Peter Harders6f526a32020-06-29 21:44:41 +0200249 my ( $pfx, $sfx );
Peter Hardersd892a582020-02-12 15:45:22 +0100250
Akron95bc98a2020-07-11 12:00:12 +0200251 # TODO:
252 # Replace all calls of $lc with $. or $input_fh->input_line_number,
253 # because otherwise remove_html_comments will
254 # move the lines forward without incrementing.
Peter Harders41c35622020-07-12 01:16:22 +0200255 my $lc = 0; # line counter (only for error handling and debugging)
Peter Harders6f526a32020-06-29 21:44:41 +0200256
Peter Harders41c35622020-07-12 01:16:22 +0200257 my $tl = 0; # text line (needed for whitespace handling)
Peter Harders6f526a32020-06-29 21:44:41 +0200258
259 $input_fh = *STDIN; # input file handle (default: stdin)
260
Akron85717512020-07-08 11:19:19 +0200261 $data_fl = 0;
Peter Harders6f526a32020-06-29 21:44:41 +0200262
263 $buf_in = $data = $dir = $dir_doc = $dir_crp = "";
264 $header_txt = $header_doc = $header_crp = "";
265
266
267 if ( $input_fname ne '' ){
268
269 open ( $input_fh, "<", "$input_fname") || die "File \'$input_fname\' could not be opened.\n";
270
271 }
272
273
Peter Harders41c35622020-07-12 01:16:22 +0200274 # prevents segfaulting of 'XML::LibXML::Reader' inside 'main()' - see notes on 'PerlIO layers' in 'man XML::LibXML')
Peter Harders90157342020-07-01 21:05:14 +0200275 # removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
276 binmode $input_fh;
277
278
Peter Harders6f526a32020-06-29 21:44:41 +0200279 # ~ loop (reading input document) ~
280
281 while ( <$input_fh> ){
282
283 $lc++; # line counter
284
285 # TODO: yet not tested fo big amounts of data
286 # must-have, otherwise comments in input could be fatal (e.g.: ...<!--\n<idsHeader...\n-->...)
Akron95bc98a2020-07-11 12:00:12 +0200287 remove_xml_comments( $input_fh, $_ ); # remove HTML comments (<!--...-->)
Peter Harders6f526a32020-06-29 21:44:41 +0200288
289 if ( $data_fl && m#^(.*)</${_TEXT_BODY}>(.*)$# ){
290
291
292 # ~ end of text body ~
293
294
Akron8b511f92020-07-09 17:28:08 +0200295 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files (s.a.: $_tok_file_ext, $_tok_file_con, $_tok_file_agg)
Peter Harders6f526a32020-06-29 21:44:41 +0200296
297 $pfx = $1; $sfx = $2;
298
299 die "ERROR ($0): main(): input line number $lc: line with closing text-body tag '${_TEXT_BODY}'"
300 ." contains additional information ... => Aborting\n\tline=$_"
301 if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/;
302
303 if ( $dir ne "" ){
304
Peter Harders6f526a32020-06-29 21:44:41 +0200305 $reader = XML::LibXML::Reader->new( string => "<text>$buf_in</text>", huge => 1 );
Peter Harders6f526a32020-06-29 21:44:41 +0200306
Peter Harders41c35622020-07-12 01:16:22 +0200307 # ~ whitespace handling ~
308 #
309 # Every whitespace inside the processed text is 'significant' and recognized as a node of type 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'
310 # (see function 'retr_info()').
311 #
312 # Definition of significant and insignificant whitespace
313 # (source: https://www.oracle.com/technical-resources/articles/wang-whitespace.html):
314 #
315 # Significant whitespace is part of the document content and should be preserved.
316 # Insignificant whitespace is used when editing XML documents for readability.
317 # These whitespaces are typically not intended for inclusion in the delivery of the document.
318 #
Peter Harders6f526a32020-06-29 21:44:41 +0200319 if ( $_XCT_LN ){ # _XCT_LINE_NUMBERS is only for debugging
320 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY | XCT_LINE_NUMBERS );
321 } else {
322 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY );
323 }
324
325 @structures = (); @oti = ();
326
327 if ( $_TOKENS_PROC ){
328 @tokens = (); @oti2 = ()
329 }
330
331 $dl = $rl = 0;
332
333 # ~ whitespace related issue ~
334 $add_one = 0;
335 %ws = ();
336
337
338 # ~ recursion ~
339
340 retr_info( \$tree_data->[2] ); # parse input data
341
342 $rl--;
343
344
345 # ~ write data.xml ~
346
Peter Harders41c35622020-07-12 01:16:22 +0200347 # TODO: should not be necessary, because whitespace at the end of every input line is removed: see 'whitespace handling' inside text body
348 # (...elsif ( $data_fl )....)
Peter Harders6f526a32020-06-29 21:44:41 +0200349 $data =~ tr/\n\r/ /; # note: 2 blanks - otherwise offset data would become corrupt
Peter Harders41c35622020-07-12 01:16:22 +0200350 #
Peter Harders6f526a32020-06-29 21:44:41 +0200351
Peter Harders6f526a32020-06-29 21:44:41 +0200352
Akron8b511f92020-07-09 17:28:08 +0200353 if ( $_GEN_TOK_EXT ){
Peter Hardersb1227172020-07-21 02:12:10 +0200354
355 $ext_tok->tokenize($data);
356
357 } elsif ( $_GEN_TOK_INT ){
358
359 $cons_tok->tokenize($data);
360 $aggr_tok->tokenize($data);
Peter Harders6f526a32020-06-29 21:44:41 +0200361 }
Akron8b511f92020-07-09 17:28:08 +0200362
Peter Hardersb1227172020-07-21 02:12:10 +0200363 $data = encode_utf8( $data );
364
Peter Harders6f526a32020-06-29 21:44:41 +0200365 print STDERR "DEBUG ($0): main(): Writing (utf8-formatted) xml file $_root_dir$dir/$_data_file\n" if $_DEBUG;
366
Peter Harders6f526a32020-06-29 21:44:41 +0200367
368 $data =~ s/(&|<|>)/$ent{$1}/g;
369
Akron85717512020-07-08 11:19:19 +0200370 $zipper->new_stream("$_root_dir$dir/$_data_file")
371 ->print("$data_prfx1$text_id_esc$data_prfx2$data$data_sfx");
Peter Harders6f526a32020-06-29 21:44:41 +0200372
373 # ~ write structures ~
374
375 write_structures() if @structures;
376
377
378 # ~ write tokens ~
379
380 write_tokens() if $_TOKENS_PROC && @tokens;
381
382
Akron8b511f92020-07-09 17:28:08 +0200383 # ~ tokenization ~
Peter Harders6f526a32020-06-29 21:44:41 +0200384
Peter Hardersb1227172020-07-21 02:12:10 +0200385 if ( $_GEN_TOK_EXT ) {
Peter Harders6f526a32020-06-29 21:44:41 +0200386
Peter Hardersb1227172020-07-21 02:12:10 +0200387 $ext_tok->to_zip(
388 $zipper->new_stream("$_root_dir$dir/$_tok_dir/$_tok_file_ext"),
389 $text_id_esc
390 )
Peter Harders6f526a32020-06-29 21:44:41 +0200391
Peter Hardersb1227172020-07-21 02:12:10 +0200392 } elsif ( $_GEN_TOK_INT ){
Akron997e9402020-07-11 12:06:13 +0200393
Peter Hardersb1227172020-07-21 02:12:10 +0200394 # Output token streams to zip streams
395 $cons_tok->to_zip(
396 $zipper->new_stream("$_root_dir$dir/$_tok_dir/$_tok_file_con"),
397 $text_id_esc
398 );
399 $aggr_tok->to_zip(
400 $zipper->new_stream("$_root_dir$dir/$_tok_dir/$_tok_file_agg"),
401 $text_id_esc
402 );
403 $aggr_tok->reset;
404 $cons_tok->reset;
Peter Harders6f526a32020-06-29 21:44:41 +0200405 }
406
Peter Hardersb1227172020-07-21 02:12:10 +0200407 #print STDERR "$0: write_tokenization(): DONE\n";
408
Peter Harders6f526a32020-06-29 21:44:41 +0200409 $data_fl = 0; $buf_in = $data = $dir = ""; # reinit.
410
411 } else { # $dir eq ""
412
413 print STDERR "WARNING ($0): main(): maybe empty textSigle => skipping this text ...\n";
414 print STDERR "WARNING ($0): main(): text header=$header_txt\n";
415 print STDERR "WARNING ($0): main(): data=$data\n";
Peter Hardersd892a582020-02-12 15:45:22 +0100416 }
417
Peter Harders6f526a32020-06-29 21:44:41 +0200418 } elsif ( $data_fl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100419
Peter Hardersd892a582020-02-12 15:45:22 +0100420
Peter Harders6f526a32020-06-29 21:44:41 +0200421 # ~ inside text body ~
Peter Hardersd892a582020-02-12 15:45:22 +0100422
Peter Hardersd892a582020-02-12 15:45:22 +0100423
Peter Harders6f526a32020-06-29 21:44:41 +0200424 #print STDERR "inside text body (\$data_fl set)\n";
425
426 # ~ whitespace handling ~
427
Peter Harders41c35622020-07-12 01:16:22 +0200428 # The idea for the below code fragment was to fix (recreate) missing whitespace in a poorly created corpus, in which linebreaks where inserted
429 # into the text with the addition that maybe (or not) whitespace before those linebreaks was unintenionally stripped.
Peter Hardersd892a582020-02-12 15:45:22 +0100430 #
Peter Harders41c35622020-07-12 01:16:22 +0200431 # It soon turned out, that it was best to suggest considering just avoiding linebreaks and putting all primary text tokens into one line (see
432 # example further down and notes on 'Input restrictions' in the manpage).
433 #
434 # Somehow an old first very poor approach remained, which is not stringent, but also doesn't affect one-line text.
435 #
436 # TODO: Maybe it's best, to keep the stripping of whitespace and to just remove the if-clause and to insert a blank by default (with possibly
437 # an option on how newlines in primary text should be handled (stripped or replaced by a whitespace)).
438 #
439 # Examples (how primary text with linebreaks would be converted by below code):
440 #
441 # '...<w>end</w>\n<w>.</w>...' -> '...<w>end</w> <w>.</w>...'
442 # '...<w>,</w>\n<w>this</w>\n<w>is</w>\n<w>it</w>\n<w>!</w>...' -> '<w>,<w> <w>this</w> <w>is</w> <w>it</w> <w>!</w>'.
Peter Hardersd892a582020-02-12 15:45:22 +0100443
Peter Harders41c35622020-07-12 01:16:22 +0200444 s/^\s+//; s/\s+$//; # remove consecutive whitespace at beginning and end (mostly one newline)
445
446 ### NOTE: this is only relevant, if a text consists of more than one line
447 ### TODO: find a better solution, or create a warning, if a text has more than one line ($tl > 1)
448 ### do testing with 2 different corpora (one with only one-line texts, the other with several lines per text)
Peter Harders6f526a32020-06-29 21:44:41 +0200449 if ( m/<[^>]+>[^<]/ ){ # line contains at least one tag with at least one character contents
Peter Hardersd892a582020-02-12 15:45:22 +0100450
Peter Harders41c35622020-07-12 01:16:22 +0200451 # NOTE: not stringent ('...' stands for text):
452 #
453 # beg1............................end1 => no blank before 'beg1'
454 # beg2....<pb/>...................end2 => no blank before 'beg2'
455 # beg3....<info attr1="val1"/>....end3 => no blank before 'beg3'
456 # beg4....<test>ok</test>.........end4 => blank before 'beg4'
457 #
458 # => beg1....end1beg2...<pb/>...end2beg3....<info attr1="val1"/>....end3 beg4...<test>ok</test>....end4
459 # ^
460 # |_blank between 'end3' and 'beg4'
Peter Hardersd892a582020-02-12 15:45:22 +0100461
Peter Harders41c35622020-07-12 01:16:22 +0200462 $tl++; # counter for text lines
463
464 s/^(.)/ $1/ if $tl > 1; # insert blank before 1st character (for 2nd line and consecutive lines)
Peter Hardersd892a582020-02-12 15:45:22 +0100465 }
Peter Harders41c35622020-07-12 01:16:22 +0200466 ###
Peter Hardersd892a582020-02-12 15:45:22 +0100467
Peter Harders6f526a32020-06-29 21:44:41 +0200468 # add line to buffer
469 $buf_in .= $_;
470
471 } elsif ( $header_fl_txt && m#^(.*</${_TEXT_HEADER_END}>)(.*)$# ){
472
473
474 # ~ end of text header ~
475
476
477 #print STDERR "end of text header\n";
478
479 # write it to header.xml
480
481 $sfx = $2;
482
483 $header_txt .= $1; $header_fl_txt = 0;
484
485
486 die "ERROR ($0): main(): input line number $lc: line with closing text-header tag '${_TEXT_HEADER_END}'"
487 ." contains additional information ... => Aborting\n\tline=$_"
488 if $sfx !~ /^\s*$/;
489
490 if ( $dir eq "" ){
491
492 print STDERR "WARNING ($0): main(): input line number $lc: empty textSigle in text header => nothing to do ...\ntext header=$header_txt\n";
493
494 } else {
495
496 print STDERR "DEBUG ($0): Writing file $_root_dir$dir/$_header_file\n" if $_DEBUG;
497
Peter Harders6f526a32020-06-29 21:44:41 +0200498 $header_txt = encode_utf8( $header_txt );
499
Akron85717512020-07-08 11:19:19 +0200500 $zipper->new_stream("$_root_dir$dir/$_header_file")
501 ->print("$header_prfx$header_txt");
Peter Harders6f526a32020-06-29 21:44:41 +0200502
503 $header_txt = "";
Peter Hardersd892a582020-02-12 15:45:22 +0100504 }
Peter Hardersd892a582020-02-12 15:45:22 +0100505
Peter Harders6f526a32020-06-29 21:44:41 +0200506 } elsif ( $header_fl_txt ){
Peter Hardersd892a582020-02-12 15:45:22 +0100507
Peter Harders6f526a32020-06-29 21:44:41 +0200508 # ~ inside text header ~
Peter Hardersd892a582020-02-12 15:45:22 +0100509
Peter Harders6f526a32020-06-29 21:44:41 +0200510
511 #print STDERR "inside text header\n";
512
513 if( m#^(.*)<${_TEXT_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
514
515 $pfx = $1; $sfx = $3;
516
517 $dir = $2; $text_id = $dir;
518
519 $text_id =~ tr/\//_/; $dir =~ s/("|&|<|>)/$ent{$1}/g;
520
521 $text_id = encode_utf8( $text_id );
522
523 die "ERROR ($0): main(): input line number $lc: line with text-sigle tag '$_TEXT_SIGLE' is not in expected format ... => Aborting\n\tline=$_"
524 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_TEXT_SIGLE}>\s*$# || $dir =~ /^\s*$/;
525
526 # log output for seeing progression
527 print STDERR "$0: main(): text_id=".decode_utf8( $text_id )."\n";
528
529 $text_id_esc = $text_id;
530
531 s#(<${_TEXT_SIGLE}(?: [^>]*)?>)[^<]+(</${_TEXT_SIGLE}>)#$1$dir$2# # to be consistent with escaping, escape also textSigle in text-header
532 if $text_id_esc =~ s/("|&|<|>)/$ent{$1}/g;
533
534 $dir =~ tr/\./\//;
535 }
536
537 $header_txt .= $_;
538
539 } elsif ( $header_fl_doc && m#^(.*</${_DOC_HEADER_END}>)(.*)$# ){
540
541
542 # ~ end of document header ~
543
Peter Harders6f526a32020-06-29 21:44:41 +0200544 #print STDERR "end of doc header\n";
545
546 # write it to header.xml
547
548 $sfx = $2;
549
550 $header_doc .= $1; $header_fl_doc = 0;
551
552 die "ERROR ($0): main(): input line number $lc: line with closing document-header tag '${_DOC_HEADER_END}'"
553 ." contains additional information ... => Aborting\n\tline=$_"
554 if $sfx !~ /^\s*$/;
555
556 if( $dir_doc eq "" ){
557
558 print STDERR "WARNING ($0): main(): input line number $lc: empty document sigle in document header"
559 ." => nothing to do ...\ndocument header=$header_doc\n";
560
561 } else {
562
563 print STDERR "DEBUG ($0): Writing file $_root_dir$dir_doc/$_header_file\n" if $_DEBUG;
564
Peter Harders6f526a32020-06-29 21:44:41 +0200565 $header_doc = encode_utf8( $header_doc );
566
Akron85717512020-07-08 11:19:19 +0200567 $zipper->new_stream("$_root_dir$dir_doc/$_header_file")
568 ->print("$header_prfx$header_doc");
Peter Harders6f526a32020-06-29 21:44:41 +0200569
570 $header_doc = $dir_doc = "";
571 }
572
573 } elsif ( $header_fl_doc ){
574
575
576 # ~ inside document header ~
577
578
579 #print STDERR "inside doc header\n";
580
581 if ( m#^(.*)<${_DOC_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
582
583 $pfx = $1; $sfx = $3;
584
585 $dir_doc = $2;
586
587 die "ERROR ($0): main(): input line number $lc: line with document-sigle tag '$_DOC_SIGLE' is not in expected format ... => Aborting\n\tline=$_"
588 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_DOC_SIGLE}>\s*$# || $dir_doc =~ /^\s*$/;
589
590 s#(<${_DOC_SIGLE}(?: [^>]*)?>)[^<]+(</${_DOC_SIGLE}>)#$1$dir_doc$2# # to be consistent with escaping, escape also textSigle in Document-Header
591 if $dir_doc =~ s/("|&|<|>)/$ent{$1}/g;
592 }
593
594 $header_doc .= $_;
595
596 } elsif ( m#^(.*)(<${_TEXT_HEADER_BEG}.*)$# ){
597
598 # ~ start of text header ~
599
600
601 #print STDERR "begin of text header\n";
602
603 $header_txt = $_; $header_fl_txt = 1; $pfx = $1;
604
Peter Harders41c35622020-07-12 01:16:22 +0200605 $tl = 0; # reset (needed for ~ whitespace handling ~)
Peter Harders6f526a32020-06-29 21:44:41 +0200606
607 die "ERROR ($0): main(): input line number $lc: line with opening text-header tag '${_TEXT_HEADER_BEG}'"
608 ." is not in expected format ... => Aborting\n\tline=$_"
609 if $pfx !~ /^\s*$/;
610
611 } elsif ( m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){
612
613
614 # ~ start of text body ~
615
616
617 #print STDERR "inside text body\n";
618
619 $pfx = $1; $sfx = $2;
620
621 $data_fl = 1;
622
623 die "ERROR ($0): main(): input line number $lc: line with opening text-body tag '${_TEXT_BODY}'"
624 ." contains additional information ... => Aborting\n\tline=$_"
625 if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/;
626
627 } elsif ( m#^(.*)(<${_DOC_HEADER_BEG}.*)$# ){
628
629
630 # ~ start of document header ~
631
632
633 #print STDERR "begin of doc header\n";
634
635 $header_doc = "$2\n"; $header_fl_doc = 1; $pfx = $1;
636
637 die "ERROR ($0): main(): input line number $lc: line with opening document-header tag '${_DOC_HEADER_BEG}'"
638 ."is not in expected format ... => Aborting\n\tline=$_"
639 if $pfx !~ /^\s*$/;
640
641 } elsif ( $header_fl_crp && m#^(.*</${_CORP_HEADER_END}>)(.*)$# ){
642
643
644 # ~ end of corpus header ~
645
646
647 #print STDERR "end of corp header\n";
648
649 $sfx = $2;
650
651 $header_crp .= $1; $header_fl_crp = 0;
652
653 die "ERROR ($0): main(): input line number $lc: line with closing corpus-header tag '${_CORP_HEADER_END}'"
654 ." contains additional information ... => Aborting\n\tline=$_"
655 if $sfx !~ /^\s*$/;
656
657 if ( $dir_crp eq "" ){
658
659 print STDERR "WARNING ($0): main(): input line number $lc: empty corpus sigle in corpus header => nothing to do ...\ncorpus header=$header_crp\n";
660
661 } else {
662
663 print STDERR "DEBUG ($0): Writing file $_root_dir$dir_crp/$_header_file\n" if $_DEBUG;
664
Peter Harders6f526a32020-06-29 21:44:41 +0200665 $header_crp = encode_utf8( $header_crp );
666
Akron85717512020-07-08 11:19:19 +0200667 $zipper->new_stream("$_root_dir$dir_crp/$_header_file")
668 ->print("$header_prfx$header_crp");
Peter Harders6f526a32020-06-29 21:44:41 +0200669
670 $header_crp = $dir_crp = "";
671 }
672
673 } elsif ( $header_fl_crp ){
674
675
676 # ~ inside corpus header ~
677
678
679 #print STDERR "inside corp header\n";
680
681 if ( m#^(.*)<${_CORP_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
682
683 $pfx = $1; $sfx = $3;
684
685 $dir_crp = $2;
686
687 die "ERROR ($0): main(): input line number $lc: line with korpusSigle-tag is not in expected format ... => Aborting\n\tline=$_"
688 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_CORP_SIGLE}>\s*$# || $dir_crp =~ /^\s*$/;
689
690 if ( $dir_crp =~ s/("|&|<|>)/$ent{$1}/g ){
691
692 s#(<${_CORP_SIGLE}(?: [^>]*)?>)[^<]+(</${_CORP_SIGLE}>)#$1$dir_crp$2# # to be consistent with escaping, escape also textSigle in Corpus-Header
Peter Hardersd892a582020-02-12 15:45:22 +0100693 }
694 }
695
Peter Harders6f526a32020-06-29 21:44:41 +0200696 $header_crp .= $_;
Peter Hardersd892a582020-02-12 15:45:22 +0100697
Peter Harders6f526a32020-06-29 21:44:41 +0200698 } elsif ( m#^(.*)(<${_CORP_HEADER_BEG}.*)$# ){
Peter Hardersd892a582020-02-12 15:45:22 +0100699
Peter Hardersd892a582020-02-12 15:45:22 +0100700
Peter Harders6f526a32020-06-29 21:44:41 +0200701 # ~ start of corpus header ~
Peter Hardersd892a582020-02-12 15:45:22 +0100702
Peter Harders6f526a32020-06-29 21:44:41 +0200703
704 #print STDERR "begin of corp header\n";
705
706 $header_crp = $2; $header_fl_crp = 1; $pfx = $1;
707
708 die "ERROR ($0): main(): input line number $lc: line with opening corpus-header tag '${_CORP_HEADER_BEG}'"
709 ." is not in expected format ... => Aborting\n\tline=$_"
710 if $pfx !~ /^\s*$/;
Peter Hardersd892a582020-02-12 15:45:22 +0100711 }
712
Peter Harders6f526a32020-06-29 21:44:41 +0200713 } #end: while
Peter Hardersd892a582020-02-12 15:45:22 +0100714
Akron85717512020-07-08 11:19:19 +0200715 $zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200716
Akron8b511f92020-07-09 17:28:08 +0200717 if( $_GEN_TOK_EXT ){
718 $ext_tok->close;
Peter Hardersd892a582020-02-12 15:45:22 +0100719 }
Peter Hardersd892a582020-02-12 15:45:22 +0100720
Peter Harders41c35622020-07-12 01:16:22 +0200721} # end: sub main
Peter Hardersd892a582020-02-12 15:45:22 +0100722
Peter Hardersd892a582020-02-12 15:45:22 +0100723
Peter Harders41c35622020-07-12 01:16:22 +0200724sub retr_info { # called from main()
Peter Hardersd892a582020-02-12 15:45:22 +0100725
Peter Harders41c35622020-07-12 01:16:22 +0200726 # Notes on how 'XML::CompactTree::XS' works
Peter Harders6f526a32020-06-29 21:44:41 +0200727 #
Peter Harders41c35622020-07-12 01:16:22 +0200728 # Example: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>
Peter Harders6f526a32020-06-29 21:44:41 +0200729 #
Peter Harders41c35622020-07-12 01:16:22 +0200730 # Print out name of 'node2' for the above example:
731 #
732 # echo '<node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>' | perl -e 'use XML::CompactTree::XS; use XML::LibXML::Reader; $reader = XML::LibXML::Reader->new(IO => STDIN); $data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_LINE_NUMBERS ); print "\x27".$data->[2]->[0]->[5]->[1]->[1]."\x27\n"'
733 #
734 # Exploring the structure of $data ( = reference to below array ):
Peter Harders6f526a32020-06-29 21:44:41 +0200735 #
736 # [ 0: XML_READER_TYPE_DOCUMENT,
737 # 1: ?
Peter Harders41c35622020-07-12 01:16:22 +0200738 # 2: [ 0: [ 0: XML_READER_TYPE_ELEMENT <- start recursion with array '$data->[2]' (see main(): retr_info( \$tree_data->[2] ))
Peter Harders6f526a32020-06-29 21:44:41 +0200739 # 1: 'node'
740 # 2: ?
741 # 3: HASH (attributes)
742 # 4: 1 (line number)
743 # 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT
744 # 1: 'node1'
745 # 2: ?
746 # 3: undefined (no attributes)
747 # 4: 1 (line number)
748 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
749 # 1: 'some '
750 # ]
751 # 1: [ 0: XML_READER_TYPE_ELEMENT
752 # 1: 'n'
753 # 2: ?
754 # 3: undefined (no attributes)
755 # 4: 1 (line number)
Peter Harders41c35622020-07-12 01:16:22 +0200756 # 5: undefined (no child-nodes)
Peter Harders6f526a32020-06-29 21:44:41 +0200757 # ]
758 # 2: [ 0: XML_READER_TYPE_TEXT
759 # 1: ' text'
760 # ]
761 # ]
762 # ]
763 # 1: [ 0: XML_READER_TYPE_ELEMENT
764 # 1: 'node2'
765 # 2: ?
766 # 3: undefined (not attributes)
767 # 4: 1 (line number)
768 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
769 # 1: 'more-text'
770 # ]
771 # ]
772 # ]
773 # ]
774 # ]
775 # ]
776 # ]
777 #
778 # $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT)
779 #
780 # ref($data->[2]) == ARRAY (with 1 element for 'node')
781 # ref($data->[2]->[0]) == ARRAY (with 6 elements)
782 #
783 # $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
784 # $data->[2]->[0]->[1] == 'node'
785 # ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v')
786 # $data->[2]->[0]->[4] == 1 (line number)
787 # ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2')
Peter Harders41c35622020-07-12 01:16:22 +0200788 # # child-nodes of actual node (see $_IDX)
Peter Harders6f526a32020-06-29 21:44:41 +0200789 #
790 # ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements)
791 # $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
792 # $data->[2]->[0]->[5]->[0]->[1] == 'node1'
793 # $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute)
794 # $data->[2]->[0]->[5]->[0]->[4] == 1 (line number)
795 # ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text')
796 #
797 # ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements)
798 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
799 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some '
800 #
801 # ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements)
802 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
803 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n'
804 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute)
805 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number)
Peter Harders41c35622020-07-12 01:16:22 +0200806 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child-nodes)
Peter Harders6f526a32020-06-29 21:44:41 +0200807 #
808 # ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements)
809 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
810 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text'
811 #
812 #
813 # retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example.
814 # Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to
815 # ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]).
Peter Hardersd892a582020-02-12 15:45:22 +0100816
Peter Harders41c35622020-07-12 01:16:22 +0200817
Peter Harders6f526a32020-06-29 21:44:41 +0200818 $rl++; # recursion level (1 = topmost level inside retr_info() = should always be level of tag $_TEXT_BODY)
Peter Hardersd892a582020-02-12 15:45:22 +0100819
Peter Hardersd892a582020-02-12 15:45:22 +0100820
Peter Harders6f526a32020-06-29 21:44:41 +0200821 foreach $e ( @{${$_[0]}} ){ # iteration through all array elements ($_[0] is a reference to an array reference)
Peter Hardersd892a582020-02-12 15:45:22 +0100822
Peter Hardersd892a582020-02-12 15:45:22 +0100823
Peter Harders41c35622020-07-12 01:16:22 +0200824 if ( $e->[0] == XML_READER_TYPE_ELEMENT ){ # element-node (see 'NODE TYPES' in manpage of XML::LibXML::Reader)
Peter Hardersd892a582020-02-12 15:45:22 +0100825
Peter Hardersd892a582020-02-12 15:45:22 +0100826
Peter Harders6f526a32020-06-29 21:44:41 +0200827 #~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200828 # from here: tag-node (opening)
Peter Harders6f526a32020-06-29 21:44:41 +0200829 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100830
Peter Hardersd892a582020-02-12 15:45:22 +0100831
Peter Harders6f526a32020-06-29 21:44:41 +0200832 # insert new array (for new tag) into @structures with tag-name and tag-attributes (if present)
833 # update @oti (open tags indizes) with @structures highest index (= $#structures); e.g.: @a=(1,2,3) => $#a = 2
Peter Hardersd892a582020-02-12 15:45:22 +0100834
Peter Harders6f526a32020-06-29 21:44:41 +0200835 # ~ tag name ~
Peter Hardersd892a582020-02-12 15:45:22 +0100836
Peter Harders6f526a32020-06-29 21:44:41 +0200837 $n = $e->[1];
Peter Hardersd892a582020-02-12 15:45:22 +0100838
Peter Hardersd892a582020-02-12 15:45:22 +0100839
Peter Harders6f526a32020-06-29 21:44:41 +0200840 # ~ handle structures ~
Peter Hardersd892a582020-02-12 15:45:22 +0100841
Peter Harders6f526a32020-06-29 21:44:41 +0200842 my @array;
843 push @array, $n;
844 push @structures, \@array;
845 push @oti, $#structures; # add highest index of @structures to @oti
Peter Hardersd892a582020-02-12 15:45:22 +0100846
Peter Hardersd892a582020-02-12 15:45:22 +0100847
Peter Harders6f526a32020-06-29 21:44:41 +0200848 # ~ handle tokens ~
Peter Hardersd892a582020-02-12 15:45:22 +0100849
Peter Harders6f526a32020-06-29 21:44:41 +0200850 $inside_tokens_tag = $rl if $_TOKENS_PROC && $n eq $_TOKENS_TAG; # wether to push entry also into @tokens array
Peter Hardersd892a582020-02-12 15:45:22 +0100851
Peter Harders6f526a32020-06-29 21:44:41 +0200852 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100853
Peter Harders6f526a32020-06-29 21:44:41 +0200854 my @array2;
855 push @array2, $n;
856 push @tokens, \@array2;
857 push @oti2, $#tokens;
858 }
Peter Hardersd892a582020-02-12 15:45:22 +0100859
Peter Hardersd892a582020-02-12 15:45:22 +0100860
Peter Harders6f526a32020-06-29 21:44:41 +0200861 # ~ handle attributes ~
Peter Hardersd892a582020-02-12 15:45:22 +0100862
Peter Harders6f526a32020-06-29 21:44:41 +0200863 if ( defined $e->[3] ){ # only if attributes exist
Peter Hardersd892a582020-02-12 15:45:22 +0100864
Peter Harders6f526a32020-06-29 21:44:41 +0200865 for ( $c = 0; $c < @{$e->[3]}; $c += 2 ){ # with 'XCT_ATTRIBUTE_ARRAY', $node->[3] is an array reference of the form
866 # [ name1, value1, name2, value2, ....] of attribute names and corresponding values.
867 # note: arrays are faster (see: http://makepp.sourceforge.net/2.0/perl_performance.html)
Peter Hardersd892a582020-02-12 15:45:22 +0100868
Peter Harders6f526a32020-06-29 21:44:41 +0200869 # '$c' references the 'key' and '$c+1' the 'value'
870 push @{$structures[$#structures]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1];
Peter Hardersd892a582020-02-12 15:45:22 +0100871
Peter Harders6f526a32020-06-29 21:44:41 +0200872 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100873
Peter Harders6f526a32020-06-29 21:44:41 +0200874 push @{$tokens[$#tokens]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1];
875 }
876
877 }
878 }
879
880
881 # ~ index 'from' ~
882
883 # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts
884
Peter Harders41c35622020-07-12 01:16:22 +0200885 push @{$structures[$#structures]}, ( $dl + $add_one ); # see below (text- and whitespace-nodes) for explanation on '$add_one'
Peter Harders6f526a32020-06-29 21:44:41 +0200886
887 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
888
889 push @{$tokens[$#tokens]}, ( $dl + $add_one );
890 }
891
892
893 #~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200894 # until here: tag-node (opening)
Peter Harders6f526a32020-06-29 21:44:41 +0200895 #~~~~
896
897
898 # ~~ RECURSION ~~
899
Peter Harders41c35622020-07-12 01:16:22 +0200900 if ( defined $e->[$_IDX] ){ # do no recursion, if $e->[$_IDX] is not defined (because we have no array of child-nodes, e.g.: <back/>)
Peter Harders6f526a32020-06-29 21:44:41 +0200901
Peter Harders41c35622020-07-12 01:16:22 +0200902 retr_info( \$e->[$_IDX] ); # recursion with array of child-nodes
Peter Harders6f526a32020-06-29 21:44:41 +0200903
904 $rl--; # return from recursion
905 }
906
907
908 #~~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200909 # from here: tag-node (closing)
Peter Harders6f526a32020-06-29 21:44:41 +0200910 #~~~~~
911
912
913 # ~ handle structures ~
914
915 {
916 my $ix = pop @oti; # index of just closed tag
917
918 my $aix = $#{$structures[$ix]}; # determine highest index from 'array referring to last closed tag' ...
919
920 $fval = ${$structures[$ix]}[ $aix ]; # ... and get it's from-value
921
922 if ( $fval > 0 && not exists $ws{ $fval - 1 } ){ # ~ whitespace related issue ~
923
Peter Harders41c35622020-07-12 01:16:22 +0200924 # ~ previous node was a text-node ~
Peter Harders6f526a32020-06-29 21:44:41 +0200925
Peter Harders41c35622020-07-12 01:16:22 +0200926 ${$structures[$ix]}[ $aix ] = $fval - 1; # recorrect from-value (see below: Notes on ~ whitespace related issue ~)
Peter Harders6f526a32020-06-29 21:44:41 +0200927 }
928
929 # in case this fails, check input
930 die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@structures: from-value ($fval) is 2 or more greater"
931 ." than to-value ($dl) => please check. aborting ...\n"
932 if ( $fval - 1 ) > $dl;
933
Peter Harders41c35622020-07-12 01:16:22 +0200934 # TODO: find example for which this case applies
Peter Harders6f526a32020-06-29 21:44:41 +0200935 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
936 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval - 1) >= $dl;
Peter Harders41c35622020-07-12 01:16:22 +0200937 # do testing with bigger corpus excerpt (wikipedia?)
Peter Harders6f526a32020-06-29 21:44:41 +0200938 ${$structures[$ix]}[ $aix ] = $dl if $fval == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl)
939
940 push @{$structures[$ix]}, $dl, $rl; # to-value and recursion-level
941
942 # note: use $dl, because the offsets are _between_ the characters (e.g.: word = 'Hello' => from = 0 (before 'H'), to = 5 (after 'o'))
943 }
944
945
946 # ~ handle tokens ~
947
948
949 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
950
951 my $ix = pop @oti2;
952
953 my $aix = $#{$tokens[$ix]};
954
955 $fval2 = ${$tokens[$ix]}[ $aix ]; # from-value
956
957 if( $fval2 > 0 && not exists $ws{ $fval2 - 1 } ){ # ~ whitespace related issue ~
958
Peter Harders41c35622020-07-12 01:16:22 +0200959 # ~ previous node was a text-node ~
Peter Harders6f526a32020-06-29 21:44:41 +0200960
Peter Harders41c35622020-07-12 01:16:22 +0200961 ${$tokens[$ix]}[ $aix ] = $fval2 - 1; # recorrect from-value (see below: Notes on ~ whitespace related issue ~)
Peter Harders6f526a32020-06-29 21:44:41 +0200962 }
963
964 # in case this fails, check input
965 die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@tokens: from-value ($fval2) is 2 or more greater"
966 ." than to-value ($dl) => please check. aborting ...\n"
967 if ( $fval2 - 1 ) > $dl;
968
Peter Harders41c35622020-07-12 01:16:22 +0200969 # TODO: find example for which this case applies
Peter Harders6f526a32020-06-29 21:44:41 +0200970 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
971 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval2 - 1) >= $dl;
Peter Harders41c35622020-07-12 01:16:22 +0200972 # do testing with bigger corpus excerpt (wikipedia?)
Peter Harders6f526a32020-06-29 21:44:41 +0200973 ${$tokens[$ix]}[ $aix ] = $dl if $fval2 == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl)
974
975 push @{$tokens[$ix]}, $dl, $rl; # to-value and recursion-level
976
977 $inside_tokens_tag = -1; # reset
978 }
979
980 # ~ whitespace related issue ~
Peter Hardersd892a582020-02-12 15:45:22 +0100981 # clean up
Peter Harders6f526a32020-06-29 21:44:41 +0200982 delete $ws{ $fval - 1 } if $fval > 0 && exists $ws{ $fval - 1 };
983 delete $ws{ $fval2 - 1 } if $_TOKENS_PROC && $fval2 > 0 && exists $ws{ $fval2 - 1 };
Peter Hardersd892a582020-02-12 15:45:22 +0100984
985
Peter Harders41c35622020-07-12 01:16:22 +0200986 #~~~~
987 # until here: tag-node (closing)
988 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100989
990
Peter Harders41c35622020-07-12 01:16:22 +0200991 #~~~~~
992 # from here: text- and whitespace-nodes
993 #~~~~~
994
995 # The 3rd form of nodes, besides text- (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes of the type
996 # 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'.
Peter Harders6f526a32020-06-29 21:44:41 +0200997 #
Peter Harders41c35622020-07-12 01:16:22 +0200998 # When modifiying the previous example (see: Notes on how 'XML::CompactTree::XS' works) by inserting an additional blank between
999 # '</node1>' and '<node2>', the output for '$data->[2]->[0]->[5]->[1]->[1]' is a blank (' ') and it's type is '14'
1000 # (XML_READER_TYPE_SIGNIFICANT_WHITESPACE, see 'man XML::LibXML::Reader'):
Peter Harders6f526a32020-06-29 21:44:41 +02001001 #
Peter Harders41c35622020-07-12 01:16:22 +02001002 # echo '<node a="v"><node1>some <n/> text</node1> <node2>more-text</node2></node>' | perl -e 'use XML::CompactTree::XS; use XML::LibXML::Reader; $reader = XML::LibXML::Reader->new(IO => STDIN); $data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_LINE_NUMBERS ); print "node=\x27".$data->[2]->[0]->[5]->[1]->[1]."\x27, type=".$data->[2]->[0]->[5]->[1]->[0]."\n"'
Peter Hardersd892a582020-02-12 15:45:22 +01001003
Peter Harders6f526a32020-06-29 21:44:41 +02001004 } elsif ( $e->[0] == XML_READER_TYPE_TEXT || $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){
Peter Hardersd892a582020-02-12 15:45:22 +01001005
Peter Harders41c35622020-07-12 01:16:22 +02001006 # Notes on ~ whitespace related issue ~ (referred to the code fragment below)
Peter Harders6f526a32020-06-29 21:44:41 +02001007 #
Peter Harders41c35622020-07-12 01:16:22 +02001008 # Example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...'
Peter Harders6f526a32020-06-29 21:44:41 +02001009 #
1010 # Two text-nodes should normally be separated by a blank. In the above example, that would be the 2 text-nodes
Peter Harders41c35622020-07-12 01:16:22 +02001011 # 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' ' (see [2]).
Peter Harders6f526a32020-06-29 21:44:41 +02001012 #
Peter Harders41c35622020-07-12 01:16:22 +02001013 # The text-node 'Campagne in Frankreich' leads to the setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag,
1014 # it's from-index gets set to the correct start-index of '1792' (and not to the start-index of the whitespace-node ' ').
Peter Harders6f526a32020-06-29 21:44:41 +02001015 #
Peter Harders41c35622020-07-12 01:16:22 +02001016 # The assumption here is, that in most cases there _is_ a whitespace node between 2 text-nodes. The below code fragment
1017 # enables a way, to check, if this really _was_ the case for the last 2 'non-tag'-nodes, when closing a tag:
Peter Harders6f526a32020-06-29 21:44:41 +02001018 #
Peter Harders41c35622020-07-12 01:16:22 +02001019 # When a whitespace-node is read, its from-index is stored as a hash-key (in %ws), to state that it belongs to a ws-node.
1020 # So when closing a tag, it can be checked, if the previous 'non-tag'-node (text or whitespace), which is the one before
1021 # the last read 'non-tag'-node, was a actually _not_ a ws-node, but instead a text-node. In that case, the from-value of
1022 # the last read 'non-tag'-node has to be corrected (see [1]),
Peter Harders6f526a32020-06-29 21:44:41 +02001023 #
Peter Harders41c35622020-07-12 01:16:22 +02001024 # For whitespace-nodes $add_one is set to 0, so when opening the next tag (in the above example the 2nd 's'-tag), no
1025 # additional 1 is added (because this was already done by the whitespace-node itself when incrementing the variable $dl).
Peter Harders6f526a32020-06-29 21:44:41 +02001026 #
Peter Harders41c35622020-07-12 01:16:22 +02001027 # [1]
1028 # Now, what happens, when 2 text-nodes are _not_ seperated by a whitespace-node (e.g.: <w>Augen<c>,</c></w>)?
1029 # In this case, the falsely increased from-value has to be decreased again by 1 when closing the enclosing tag
1030 # (see above code fragment '... not exists $ws{ $fval - 1 } ...').
Peter Harders6f526a32020-06-29 21:44:41 +02001031 #
Peter Harders41c35622020-07-12 01:16:22 +02001032 # [2]
1033 # Comparing the 2 examples '<w>fu</w> <w>bar</w>' and '<w>fu</w><w> </w><w>bar</w>', is ' ' in both cases handled as a
1034 # whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE).
Peter Harders6f526a32020-06-29 21:44:41 +02001035 #
Peter Harders41c35622020-07-12 01:16:22 +02001036 # The from-index of the 2nd w-tag in the second example refers to 'bar', which may not have been the intention
1037 # (even though '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug?
Peter Harders6f526a32020-06-29 21:44:41 +02001038 #
Peter Harders41c35622020-07-12 01:16:22 +02001039 # Empty tags also cling to the next text-token - e.g. in '<w>tok1</w> <w>tok2</w><a><b/></a> <w>tok3</w>' are the from-
1040 # and to-indizes for the tags 'a' and 'b' both 12, which is the start-index of the token 'tok3'.
Peter Harders6f526a32020-06-29 21:44:41 +02001041
1042 if( $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){
1043
Peter Harders41c35622020-07-12 01:16:22 +02001044 # ~ whitespace-node ~
1045
1046 # ~ whitespace related issue ~
Peter Harders6f526a32020-06-29 21:44:41 +02001047
1048 $add_one = 0;
1049
Peter Harders41c35622020-07-12 01:16:22 +02001050 $ws{ $dl }++; # state, that this from-index belongs to a whitespace-node
1051 # ('++' doesn't mean a thing here - maybe it could be used for a consistency check)
Peter Harders6f526a32020-06-29 21:44:41 +02001052
1053 }else{
1054
1055 # ~ text-node ~
1056
1057 $add_one = 1;
1058 }
1059
1060
1061 # ~ update $data and $dl ~
1062
1063 $data .= $e->[1];
1064
1065 $dl += length( $e->[1] ); # update length of $data
1066
1067
Peter Harders41c35622020-07-12 01:16:22 +02001068 #~~~~~
1069 # until here: text- and whitespace-nodes
1070 #~~~~~
1071
1072
1073 #elsif ( $e->[0] == XML_READER_TYPE_ATTRIBUTE ) # attribute-node
Peter Harders6f526a32020-06-29 21:44:41 +02001074 # note: attributes cannot be processed like this ( => use 'XCT_ATTRIBUTE_ARRAY' - see above )
1075
1076
1077 }else{ # not yet handled type
1078
1079 die "ERROR ($0): Not yet handled type (\$e->[0]=".$e->[0].") ... => Aborting\n";
1080 }
1081
1082 } # end: foreach iteration
1083
1084} # end: sub retr_info
1085
1086
Peter Harders41c35622020-07-12 01:16:22 +02001087sub write_structures { # called from main()
Peter Harders6f526a32020-06-29 21:44:41 +02001088
1089 # ~ write @structures ~
Peter Hardersd892a582020-02-12 15:45:22 +01001090
1091 #print STDERR "$0: write_structures(): ...\n";
1092
Peter Harders6f526a32020-06-29 21:44:41 +02001093 if ( $dir eq "" ){
1094
1095 print STDERR "WARNING ($0): write_structures(): empty textSigle => nothing to do ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001096 return;
1097 }
1098
Peter Harders6f526a32020-06-29 21:44:41 +02001099 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1100 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\""
1101 .decode_utf8($text_id_esc)."\" xmlns=\"http://ids-mannheim.de/ns/KorAP\" version=\"KorAP-0.4\">\n <spanList>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001102
1103 $c = 0;
1104
Peter Harders6f526a32020-06-29 21:44:41 +02001105 foreach $ref ( @structures ){
Peter Hardersd892a582020-02-12 15:45:22 +01001106
Peter Harders6f526a32020-06-29 21:44:41 +02001107 ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 ); # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4
1108 # (name, from, to, rec_level), otherwise >4
Peter Hardersd892a582020-02-12 15:45:22 +01001109
Peter Harders6f526a32020-06-29 21:44:41 +02001110 # correct last from-value ( if the 'second to last' from-value refers to an s-tag, then the last from-value is one to big - see retr_info )
1111
1112 if( $#structures == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){
1113
1114 ${$ref}[$idx] = ${$ref}[ $idx+1 ];
Peter Hardersd892a582020-02-12 15:45:22 +01001115 }
Peter Hardersd892a582020-02-12 15:45:22 +01001116
Peter Harders6f526a32020-06-29 21:44:41 +02001117 # this consistency check is already done in 'retr_info()'
1118 #elsif( ${$ref}[$idx] > ${$ref}[$idx+1] ){ # consistency check: abort, if this doesn't hold
1119 # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):"
1120 # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n";
1121 # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):"
1122 # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n\n$output" }
Peter Hardersd892a582020-02-12 15:45:22 +01001123
Peter Harders6f526a32020-06-29 21:44:41 +02001124 # at least 'POS' should always be there => remove constraint '$_TOKENS_PROC'
1125 #if( $_TOKENS_PROC && ${$ref}[0] ne $_TOKENS_TAG )
Peter Hardersd892a582020-02-12 15:45:22 +01001126
Peter Harders6f526a32020-06-29 21:44:41 +02001127 if( ${$ref}[0] ne $_TOKENS_TAG ){ # $_TOKENS_TAG is already written in 'write_tokens'
Peter Hardersd892a582020-02-12 15:45:22 +01001128
Peter Harders6f526a32020-06-29 21:44:41 +02001129 # l (level): insert information about depth of element in XML-tree (top element = level 1)
1130 $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n"
1131 ." <fs type=\"struct\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
1132 ." <f name=\"name\">${$ref}[ 0 ]</f>\n";
1133
1134 if ( $idx > 2 ) # attributes
1135 {
1136 $output .= " <f name=\"attr\">\n <fs type=\"attr\">\n";
1137
1138 for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){
1139
1140 ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # see explanation in func. 'write_tokens'
1141
1142 # attribute (at index $att_idx) with value (at index $att_idx+1)
1143 $output .= " <f name=\"${$ref}[ $att_idx ]\">${$ref}[ $att_idx+1 ]</f>\n";
1144 }
1145
1146 $output .= " </fs>\n </f>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001147 }
1148
Peter Harders6f526a32020-06-29 21:44:41 +02001149 $output .= " </fs>\n </span>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001150
Peter Harders6f526a32020-06-29 21:44:41 +02001151 } # fi: ... ne $_TOKENS_TAG
Peter Hardersd892a582020-02-12 15:45:22 +01001152
1153 $c++;
1154
Peter Harders6f526a32020-06-29 21:44:41 +02001155 } # end: foreach
Peter Hardersd892a582020-02-12 15:45:22 +01001156
1157 $output .= " </spanList>\n</layer>";
1158
Peter Harders6f526a32020-06-29 21:44:41 +02001159 $output = encode_utf8( $output );
Peter Hardersd892a582020-02-12 15:45:22 +01001160
Akron85717512020-07-08 11:19:19 +02001161 $zipper->new_stream("$_root_dir$dir/$_structure_dir/$_structure_file")
1162 ->print($output);
Peter Hardersd892a582020-02-12 15:45:22 +01001163
1164 #print STDERR "$0: write_structures(): DONE\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001165
Peter Hardersd892a582020-02-12 15:45:22 +01001166} # end: sub write_structures
1167
1168
Peter Harders41c35622020-07-12 01:16:22 +02001169sub write_tokens { # called from main()
Peter Harders6f526a32020-06-29 21:44:41 +02001170
1171 # ~ write @tokens ~
1172
1173 #print STDERR "$0: write_tokens(): ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001174
1175 if( $dir eq "" ){
Peter Harders6f526a32020-06-29 21:44:41 +02001176
1177 print STDERR "WARNING ($0): write_tokens(): empty textSigle => nothing to do ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001178 return;
1179 }
1180
Peter Harders6f526a32020-06-29 21:44:41 +02001181 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1182 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\""
1183 .decode_utf8($text_id_esc)."\" xmlns=\"http://ids-mannheim.de/ns/KorAP\" version=\"KorAP-0.4\">\n <spanList>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001184
1185 $c = 0;
1186
Peter Harders6f526a32020-06-29 21:44:41 +02001187 foreach $ref ( @tokens ){
Peter Hardersd892a582020-02-12 15:45:22 +01001188
Peter Harders6f526a32020-06-29 21:44:41 +02001189 # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4 (name, from, to, rec_level), otherwise >4
1190 ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 );
Peter Hardersd892a582020-02-12 15:45:22 +01001191
Peter Harders6f526a32020-06-29 21:44:41 +02001192 # correct last from-value (if the 'second to last' from-value refers to an s-tag, then the last from-value is one to big - see retr_info())
1193 if ( $#tokens == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){
1194
1195 ${$ref}[ $idx ] = ${$ref}[ $idx+1 ]; # TODO: check
Peter Hardersd892a582020-02-12 15:45:22 +01001196 }
Peter Hardersd892a582020-02-12 15:45:22 +01001197
Peter Harders6f526a32020-06-29 21:44:41 +02001198 # l (level): insert information about depth of element in XML-tree (top element = level 1)
1199 $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n"
1200 ." <fs type=\"lex\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
1201 ." <f name=\"lex\">\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001202
Peter Harders6f526a32020-06-29 21:44:41 +02001203 if ( $idx > 2 ){ # attributes
1204
Peter Hardersd892a582020-02-12 15:45:22 +01001205 $output .= " <fs>\n";
1206
Peter Harders6f526a32020-06-29 21:44:41 +02001207 for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){
Peter Hardersd892a582020-02-12 15:45:22 +01001208
Peter Harders6f526a32020-06-29 21:44:41 +02001209 ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # ... <w lemma="&gt;" ana="PUNCTUATION">&gt;</w> ...
1210 # the '&gt;' is translated to '>' and hence the result would be '<f name="lemma">></f>'
Peter Hardersd892a582020-02-12 15:45:22 +01001211
Peter Harders6f526a32020-06-29 21:44:41 +02001212 if ( $_INLINE_ANNOT && ${$ref}[ $att_idx ] eq "$_INLINE_ATT_RD" ){
Peter Hardersd892a582020-02-12 15:45:22 +01001213
Peter Harders6f526a32020-06-29 21:44:41 +02001214 ${$ref}[ $att_idx+1 ] =~ /^([^ ]+)(?: (.+))?$/;
1215
1216 die "ERROR (write_tokens()): unexpected format! => Aborting ... (att: ${$ref}[ $att_idx+1 ])\n"
1217 if ( $_INLINE_POS_WR && not defined $1 ) || ( $_INLINE_MSD_WR && not defined $2 );
1218
1219 if ( "$_INLINE_POS_WR" ){
1220
1221 $output .= " <f name=\"$_INLINE_POS_WR\">";
Peter Hardersd892a582020-02-12 15:45:22 +01001222 $output .= "$1" if defined $1;
1223 $output .= "</f>\n";
1224 }
Peter Harders6f526a32020-06-29 21:44:41 +02001225
1226 if ( "$_INLINE_MSD_WR" ){
1227
1228 $output .= " <f name=\"$_INLINE_MSD_WR\">";
Peter Hardersd892a582020-02-12 15:45:22 +01001229 $output .= "$2" if defined $2;
1230 $output .= "</f>\n";
1231 }
1232
Peter Harders6f526a32020-06-29 21:44:41 +02001233 } elsif ( $_INLINE_ANNOT && "$_INLINE_LEM_RD" && ${$ref}[ $att_idx ] eq "$_INLINE_LEM_RD" ){
Peter Hardersd892a582020-02-12 15:45:22 +01001234
Peter Harders6f526a32020-06-29 21:44:41 +02001235 $output .= " <f name=\"$_INLINE_LEM_WR\">${$ref}[ $att_idx+1 ]</f>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001236
Peter Harders6f526a32020-06-29 21:44:41 +02001237 } else { # all other attributes
Peter Hardersd892a582020-02-12 15:45:22 +01001238
Peter Harders6f526a32020-06-29 21:44:41 +02001239 $output .= " <f name=\"${$ref}[$att_idx]\">${$ref}[ $att_idx+1 ]</f>\n"; # attribute (at index $att_idx) with value (at index $att_idx+1)
Peter Hardersd892a582020-02-12 15:45:22 +01001240 }
1241
Peter Harders6f526a32020-06-29 21:44:41 +02001242 } # end: for
Peter Hardersd892a582020-02-12 15:45:22 +01001243
1244 $output .= " </fs>\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001245
1246 } # fi: attributes
Peter Hardersd892a582020-02-12 15:45:22 +01001247
1248 $output .= " </f>\n </fs>\n </span>\n";
1249
1250 $c++;
1251
Peter Harders6f526a32020-06-29 21:44:41 +02001252 } # end: foreach
Peter Hardersd892a582020-02-12 15:45:22 +01001253
1254 $output .= " </spanList>\n</layer>";
1255
Peter Harders6f526a32020-06-29 21:44:41 +02001256 $output = encode_utf8( $output );
Peter Hardersd892a582020-02-12 15:45:22 +01001257
Akron85717512020-07-08 11:19:19 +02001258 $zipper->new_stream("$_root_dir$dir/$_tokens_dir/$_tokens_file")
1259 ->print($output);
Peter Hardersd892a582020-02-12 15:45:22 +01001260
Peter Harders6f526a32020-06-29 21:44:41 +02001261 #print STDERR "$0: write_tokens(): DONE\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001262
Peter Harders6f526a32020-06-29 21:44:41 +02001263} # end: sub write_tokens
Peter Hardersd892a582020-02-12 15:45:22 +01001264
Peter Hardersd892a582020-02-12 15:45:22 +01001265
Akrond949e182020-02-14 12:23:57 +01001266__END__
1267
1268=pod
1269
1270=encoding utf8
1271
1272=head1 NAME
1273
1274tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
1275
1276=head1 SYNOPSIS
1277
1278 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
1279
1280=head1 DESCRIPTION
1281
Akronee434b12020-07-08 12:53:01 +02001282C<tei2korapxml> is a script to convert TEI P5 and
1283L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html>
1284based documents to the
1285L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
1286If no specific input is defined, data is
Akrond949e182020-02-14 12:23:57 +01001287read from C<STDIN>. If no specific output is defined, data is written
1288to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +02001289
Akrond949e182020-02-14 12:23:57 +01001290This program is usually called from inside another script.
1291
Akronee434b12020-07-08 12:53:01 +02001292=head1 FORMATS
1293
1294=head2 Input restrictions
1295
1296=over 2
1297
1298=item
1299
1300utf8 encoded
1301
1302=item
1303
1304TEI P5 formatted input with certain restrictions:
1305
1306=over 4
1307
1308=item
1309
1310B<mandatory>: text-header with integrated textsigle, text-body
1311
1312=item
1313
1314B<optional>: corp-header with integrated corpsigle,
1315doc-header with integrated docsigle
1316
1317=back
1318
1319=item
1320
1321all tokens inside the primary text (inside $data) may not be
1322newline seperated, because newlines are removed
1323(see code section C<~ inside text body ~>) and a conversion of newlines
1324into blanks between 2 tokens could lead to additional blanks,
1325where there should be none (e.g.: punctuation characters like C<,> or
1326C<.> should not be seperated from their predecessor token).
1327(see also code section C<~ whitespace handling ~>).
1328
1329=back
1330
1331=head2 Notes on the output
1332
1333=over 2
1334
1335=item
1336
1337zip file output (default on C<stdout>) with utf8 encoded entries
1338(which together form the KorAP-XML format)
1339
1340=back
1341
Akrond949e182020-02-14 12:23:57 +01001342=head1 INSTALLATION
1343
1344C<tei2korapxml> requires L<libxml2-dev> bindings to build. When
1345these bindings are available, the preferred way to install the script is
1346to use L<cpanm|App::cpanminus>.
1347
1348 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
1349
1350In case everything went well, the C<tei2korapxml> tool will
1351be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +02001352
Akrond949e182020-02-14 12:23:57 +01001353Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
1354
1355=head1 OPTIONS
1356
1357=over 2
1358
1359=item B<--base|-b>
1360
1361The base directory for output. Defaults to C<.>.
1362
1363=item B<--help|-h>
1364
1365Print help information.
1366
1367=item B<--version|-v>
1368
1369Print version information.
1370
1371=back
1372
1373=head1 COPYRIGHT AND LICENSE
1374
1375Copyright (C) 2020, L<IDS Mannheim|https://www.ids-mannheim.de/>
1376
1377Author: Peter Harders
1378
1379Contributors: Marc Kupietz, Carsten Schnober, Nils Diewald
1380
1381L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
1382Corpus Analysis Platform at the
1383L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
1384member of the
1385L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
1386
1387This program is free software published under the
1388L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-TEI/master/LICENSE>.
1389
1390=cut