blob: d50dc2420906b87f9051df1883b5824c72d6a2eb [file] [log] [blame]
Akron9cb13942020-02-14 07:39:54 +01001#!/usr/bin/env perl
Peter Hardersd892a582020-02-12 15:45:22 +01002
Peter Hardersd892a582020-02-12 15:45:22 +01003###
Peter Harders6f526a32020-06-29 21:44:41 +02004### converts input in TEI P5 format (https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html)
5### into output in KorAP-XML format (https://github.com/KorAP/KorAP-XML-Krill -> 'KorAP-XML document')
Peter Hardersd892a582020-02-12 15:45:22 +01006###
Peter Harders6f526a32020-06-29 21:44:41 +02007### input restrictions:
Peter Hardersd892a582020-02-12 15:45:22 +01008###
Peter Harders6f526a32020-06-29 21:44:41 +02009### . utf8 encoded
10### . TEI P5 formatted input with certain restrictions:
11### . mandatory: text-header with integrated textsigle, text-body
12### . optional: corp-header with integrated corpsigle, doc-header with integrated docsigle
13###
14### . all tokens inside the primary text (inside $data) may not be newline seperated, because newlines
15### are removed (see below: 'inside text body') and a conversion of newlines into blanks between 2 tokens
16### could lead to additional blanks, where there should be none (e.g.: punctuation characters like ',' or
17### '.' should not be seperated from their predecessor token).
18### - see also '~ whitespace handling ~'
19###
20### . POS and MSD inline annotations handling (see below: expected format)
21### ...
22###
23### notes on the output:
24###
25### . zip file output (default on stdout) with utf8 encoded entries (which together form the KorAP-XML format)
26### ...
27###
Peter Hardersd892a582020-02-12 15:45:22 +010028
29use strict;
30use warnings;
Peter Harders6f526a32020-06-29 21:44:41 +020031
32use Pod::Usage;
33use Getopt::Long qw(GetOptions :config no_auto_abbrev);
34
35use File::Basename qw(dirname);
36use IO::Handle;
Peter Hardersd892a582020-02-12 15:45:22 +010037use IO::Select;
38
39use open qw(:std :utf8); # assume utf-8 encoding
40use Encode qw(encode_utf8 decode_utf8);
41
Peter Hardersd892a582020-02-12 15:45:22 +010042use XML::CompactTree::XS;
43use XML::LibXML::Reader;
44use IO::Compress::Zip qw(zip $ZipError :constants);
Peter Harders6f526a32020-06-29 21:44:41 +020045use IPC::Open2 qw(open2);
Peter Hardersd892a582020-02-12 15:45:22 +010046
Akron4f67cd42020-07-02 12:27:58 +020047use FindBin;
48BEGIN {
49 unshift @INC, "$FindBin::Bin/../lib";
50};
51
52use KorAP::XML::TEI;
Peter Hardersd892a582020-02-12 15:45:22 +010053
Akrond949e182020-02-14 12:23:57 +010054our $VERSION = '0.01';
Peter Harders6f526a32020-06-29 21:44:41 +020055
Akrond949e182020-02-14 12:23:57 +010056our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n";
57
Peter Hardersd892a582020-02-12 15:45:22 +010058
Peter Harders6f526a32020-06-29 21:44:41 +020059# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010060GetOptions(
Peter Harders6f526a32020-06-29 21:44:41 +020061 "root|r=s" => \(my $_root_dir = '.'), # name of root directory inside zip file
62 "input|i=s" => \(my $input_fname = ''), # input file (yet only TEI I5 Format accepted)
Akrond949e182020-02-14 12:23:57 +010063 'help|h' => sub {
64 pod2usage(
65 -verbose => 99,
66 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
67 -msg => $VERSION_MSG,
68 -output => '-'
69 )
70 },
71 'version|v' => sub {
72 pod2usage(
73 -verbose => 0,
74 -msg => $VERSION_MSG,
75 -output => '-'
76 )
77 }
Peter Hardersd892a582020-02-12 15:45:22 +010078);
79
Peter Harders6f526a32020-06-29 21:44:41 +020080#
81# ~~~ parameter (mandatory) ~~~
82#
Peter Hardersd892a582020-02-12 15:45:22 +010083
Peter Harders6f526a32020-06-29 21:44:41 +020084 # optional
85my $_CORP_SIGLE = "korpusSigle"; # opening and closing tags (without attributes) have to be in one line
86 # (e.g.: <korpusSigle>GOE</korpusSigle>)
87 # optional
88my $_DOC_SIGLE = "dokumentSigle"; # analog
89 # mandatory
90my $_TEXT_SIGLE = "textSigle"; # analog
91 # mandatory
92my $_TEXT_BODY = "text"; # tag (without attributes), which contains the primary text
93 # optional
94my $_CORP_HEADER_BEG = "idsHeader type=\"corpus\""; # just keep the correct order of the attributes and evtl. add an '.*' between them
95 # optional
96my $_DOC_HEADER_BEG = "idsHeader type=\"document\""; # analog
97 # mandatory
98my $_TEXT_HEADER_BEG = "idsHeader type=\"text\""; # analog
99
100#
101# ~~~ constants ~~~
102#
Peter Hardersd892a582020-02-12 15:45:22 +0100103
Peter Harders6f526a32020-06-29 21:44:41 +0200104## DEPRECATED (only IDS-intern - the tokenization is normally done by external tools)
105my $_GEN_TOK_BAS = 0; # IDS internal tokenization
106 my( $chld_out, $chld_in, $pid, $select );
107##
108
109## dummy tokenization (only for testing)
110my $_GEN_TOK_DUMMY = 0; # use dummy base tokenization for testing (base tokenization is normally done by external tools)
111 my $_tok_file_con = "tokens_conservative.xml";
112 my $_tok_file_agg = "tokens_aggressive.xml";
113 my ( @tok_tokens_con, @tok_tokens_agg, $m1, $m2, $m3, $m4, $tmp, $p1, $p2, $pr, $txt, $offset );
114my $_base_tokenization_dir = "base"; # name of directory for storing files of dummy tokenization (only used in func. select_tokenization)
115
116# man IO::Compress::Zip
117# At present three compression methods are supported by IO::Compress::Zip, namely
118# Store (no compression at all), Deflate, Bzip2 and LZMA.
119# Note that to create Bzip2 content, the module "IO::Compress::Bzip2" must be installed.
120# Note that to create LZMA content, the module "IO::Compress::Lzma" must be installed.
121my $_COMPRESSION_METHOD = ZIP_CM_DEFLATE; # The symbols, ZIP_CM_STORE, ZIP_CM_DEFLATE, ZIP_CM_BZIP2 and ZIP_CM_LZMA are used to select the compression method.
122
123my $_DEBUG = 0; # set to 1 for minimal more debug output (no need to be parametrized)
124my $_XCT_LN = 0; # only for debugging: include line numbers in elements of $tree_data
125 # (see also manpage of XML::CompactTree::XS)
126
127my $_header_file = "header.xml"; # name of files containing the text, document and corpus header
128my $_data_file = "data.xml"; # name of file containing the primary text data (tokens)
129my $_structure_dir = "struct"; # name of directory containing the $_structure_file
130my $_structure_file = "structure.xml"; # name of file containing all tags (except ${_TOKEN_TAG}'s) related information
131 # (= their names and byte offsets in $_data)
132## TODO: optional (different annotation tools can produce more zip-files for feeding into KorAP-XML-Krill)
133my $_TOKENS_PROC = 1; # on/off: processing of ${_TOKEN_TAG}'s (default: 1)
134my $_tokens_dir = "tokens"; # name of directory containing the $_tokens_file
135my $_tokens_file = "morpho.xml"; # name of file containing all ${_TOKEN_TAG}'s related information (=their byte offsets in $_data)
136 # - evtl. with additional inline annotations
137my $_TOKENS_TAG = "w"; # name of tag containing all information stored in $_tokens_file
138
139## TODO: optional
140# handling inline annotations (inside $_TOKENS_TAG)
141my $_INLINE_ANNOT = 0; # on/off: set to 1 if inline annotations are present and should be processed (default: 0)
142my $_INLINE_LEM_RD = "lemma"; # from which attribute to read LEMMA information
143my $_INLINE_ATT_RD = "ana"; # from which attribute to read POS information (and evtl. additional MSD - Morphosyntactic Descriptions)
144 # TODO: The format for the POS and MSD information has to suffice the regular expression ([^ ]+)( (.+))?
145 # - which means, that the POS information can be followed by an optional blank with additional
146 # MSD information; unlike the MSD part, the POS part may not contain any blanks.
147my $_INLINE_POS_WR = "pos"; # name (inside $_tokens_file) referring to POS information
148my $_INLINE_MSD_WR = "msd"; # name (inside $_tokens_file) referring to MSD information
149my $_INLINE_LEM_WR = "lemma"; # name (inside $_tokens_file) referring to LEMMA information
150##
151
152
153#
154# ~~~ variables ~~~
155#
156
157my $zip; # IO::Compress::Zip object
158my $zip_outh; # handle for zip file output (stdout)
159my $first_write; # needed to decide wether to call '$zip->newStream' (for appending to zip file)
160my $input_fh; # input file handle (default: stdin)
161
162my $buf_in; # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader
163my $data; # contains the primary text (created by func. 'retr_info' from $buf_in), which is written to '$data_file'
164
165my $dir; # text directory (below $_root_dir)
166my $dir_crp; # corpus directory (below $_root_dir)
167my $dir_doc; # document directory (below $_root_dir)
168
169my ( $text_id, $text_id_esc ); # '$text_id_esc' = escaped version of $text_id (see %ent)
170
171my %ent = ('"', '&quot;', '&','&amp;', # convert '&', '<' and '>' into their corresponding sgml-entities
172 '<','&lt;','>','&gt;');
173 # note: the index still refers to the 'single character'-versions, which are counted as 1
174 # (search for '&amp;' in data.xml and see corresponding indices in $_tokens_file)
175
176my $header_txt; # raw text header (written to '$_root_dir$dir/$_header_file')
177my $header_doc; # raw document header (written to '$_root_dir$dir_doc/$_header_file')
178my $header_crp; # raw corpus header (written to '$_root_dir$dir_crp/$_header_file')
179
180my ( $header_fl_crp, $header_fl_doc, # flags for tracking where we are in the input document
181 $header_fl_txt, $data_fl );
182
183my ( $header_prfx, $data_prfx1, # $header_prfx is written to $_header_file, $data_* are written to $_data_file
184 $data_prfx2, $data_sfx );
185
186my @structures; # list of arrays, where each array represents a TEI I5 tag (except $_TOKENS_TAG) from the input document
187 # - the input of this array is written in func. 'write_structures' into the file '$_structure_file'
188
189my @tokens; # list of arrays, where each array represents a $_TOKENS_TAG from the input document
190 # - the input of this array is written in func. 'write_tokens' into the file '$_tokens_file'
191
192my ( $ref, $idx, $att_idx ); # needed in func. 'write_structures' and 'write_tokens'
193
194my ( $reader, # instance of 'XML::LibXML::Reader->new' (on input '$buf_in')
195 $tree_data ); # instance of 'XML::CompactTree::XS::readSubtreeToPerl' (on input '$reader')
196
197# these are only used inside recursive function 'retr_info'
198my ( $_IDX, # value is set dependent on $_XCT_LN - for extracting array of child elements from element in $tree_data
199 $e, # element from $tree_data
200 $n, # tag name of actual processed element $e
201 $rl, # recursion level
202 $dl, # actual length of string $data
203 @oti, # oti='open tags indizes' - a stack of indizes into @structures, where the top index in @oti
204 # represents the actual processed element from @structures
205 @oti2, # analogously to @oti, but with reference to array @tokens
206 $inside_tokens_tag, # flag is set, when inside $_TOKENS_TAG
207 ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags)
208 $add_one, # ...
209 $fval, $fval2, # ...
210 %ws); # hash for indices of whitespace nodes (needed to recorrect from-values)
211 # idea: when closing element, check if it's from-index minus 1 refers to a whitespace node
212 # (means: 'from-index - 1' is a key in %ws).
213 # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1
214
215my $output; # temporary variable needed in 'write_*'-functions for writing output to zip-stream $zip)
216
217my ( $i, $c ); # index variables used in loops
218
219## DEPRECATED (only IDS-intern)
220my $_tok_file_bas = "tokens.xml";
221##
222
223my ( $_CORP_HEADER_END, $_DOC_HEADER_END, $_TEXT_HEADER_END );
224
225
226#
227# ~~~ main ~~~
228#
229
230# ~ initializations ~
231
232($_XCT_LN)?($_IDX=5):($_IDX=4);
233
234$header_prfx = $data_prfx1 = $data_prfx2 = $data_sfx = "";
235
236$header_fl_txt = $header_fl_doc = $header_fl_crp = 0;
237
238$inside_tokens_tag = -1;
239
240$fval = $fval2 = 0;
241
242$_root_dir .= '/'; # base dir must always end with a slash
243$_root_dir =~ s/^\.?\///; # remove leading / (only relative paths allowed in IO::Compress::Zip) and redundant ./
244
245$_CORP_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_CORP_HEADER_END = $1;
246$_DOC_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_DOC_HEADER_END = $1;
247$_TEXT_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_TEXT_HEADER_END = $1;
248
249## TODO: remove this, because it's IDS-specific
Peter Hardersd892a582020-02-12 15:45:22 +0100250$header_prfx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
251$header_prfx .= "<?xml-model href=\"header.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n";
252$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 +0200253##
Peter Hardersd892a582020-02-12 15:45:22 +0100254
255$data_prfx1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
256$data_prfx1 .= "<?xml-model href=\"text.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n";
257$data_prfx1 .= "<raw_text docid=\"";
258$data_prfx2 .= "\" xmlns=\"http://ids-mannheim.de/ns/KorAP\">\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200259## TODO: can 'metadata.xml' change or is it constant?
Peter Hardersd892a582020-02-12 15:45:22 +0100260$data_prfx2 .= " <metadata file=\"metadata.xml\" />\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200261##
Peter Hardersd892a582020-02-12 15:45:22 +0100262$data_prfx2 .= " <text>";
263$data_sfx = "</text>\n</raw_text>";
264
Peter Hardersd892a582020-02-12 15:45:22 +0100265
Peter Harders6f526a32020-06-29 21:44:41 +0200266## DEPRECATED (only IDS-intern)
267startTokenizer() if $_GEN_TOK_BAS;
268##
Peter Hardersd892a582020-02-12 15:45:22 +0100269
Peter Harders6f526a32020-06-29 21:44:41 +0200270# ~ read input and write output (text by text) ~
Peter Harders90157342020-07-01 21:05:14 +0200271process();
Peter Hardersd892a582020-02-12 15:45:22 +0100272
Peter Hardersd892a582020-02-12 15:45:22 +0100273
Peter Harders6f526a32020-06-29 21:44:41 +0200274#
275# ~~~ subs ~~~
276#
Peter Hardersd892a582020-02-12 15:45:22 +0100277
Peter Hardersd892a582020-02-12 15:45:22 +0100278
Peter Harders90157342020-07-01 21:05:14 +0200279sub process {
Peter Hardersd892a582020-02-12 15:45:22 +0100280
Peter Harders6f526a32020-06-29 21:44:41 +0200281 my ( $pfx, $sfx );
Peter Hardersd892a582020-02-12 15:45:22 +0100282
Peter Harders6f526a32020-06-29 21:44:41 +0200283 my $lc = 0; # line counter
284
285 my $tc = 0; # text counter
286
287 $input_fh = *STDIN; # input file handle (default: stdin)
288
289 $zip_outh = *STDOUT; # output file handle (default: stdout)
290
291 $data_fl = 0; $first_write = 1;
292
293 $buf_in = $data = $dir = $dir_doc = $dir_crp = "";
294 $header_txt = $header_doc = $header_crp = "";
295
296
297 if ( $input_fname ne '' ){
298
299 open ( $input_fh, "<", "$input_fname") || die "File \'$input_fname\' could not be opened.\n";
300
301 }
302
303
Peter Harders90157342020-07-01 21:05:14 +0200304 # prevents segfaulting of 'XML::LibXML::Reader' inside 'process()' - see notes on 'PerlIO layers' in 'man XML::LibXML')
305 # removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
306 binmode $input_fh;
307
308
Peter Harders6f526a32020-06-29 21:44:41 +0200309 # ~ loop (reading input document) ~
310
311 while ( <$input_fh> ){
312
313 $lc++; # line counter
314
315 # TODO: yet not tested fo big amounts of data
316 # must-have, otherwise comments in input could be fatal (e.g.: ...<!--\n<idsHeader...\n-->...)
Akron4f67cd42020-07-02 12:27:58 +0200317 KorAP::XML::TEI::delHTMLcom ( $input_fh, $_ ); # remove HTML comments (<!--...-->)
Peter Harders6f526a32020-06-29 21:44:41 +0200318
319 if ( $data_fl && m#^(.*)</${_TEXT_BODY}>(.*)$# ){
320
321
322 # ~ end of text body ~
323
324
325 # write data.xml, structure.xml and evtl. morpho.xml and/or the dummy tokenization files (s.a.: $_tok_file_con and $_tok_file_agg)
326
327 $pfx = $1; $sfx = $2;
328
329 die "ERROR ($0): main(): input line number $lc: line with closing text-body tag '${_TEXT_BODY}'"
330 ." contains additional information ... => Aborting\n\tline=$_"
331 if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/;
332
333 if ( $dir ne "" ){
334
Peter Harders6f526a32020-06-29 21:44:41 +0200335 $reader = XML::LibXML::Reader->new( string => "<text>$buf_in</text>", huge => 1 );
Peter Harders6f526a32020-06-29 21:44:41 +0200336
337 if ( $_XCT_LN ){ # _XCT_LINE_NUMBERS is only for debugging
338 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY | XCT_LINE_NUMBERS );
339 } else {
340 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY );
341 }
342
343 @structures = (); @oti = ();
344
345 if ( $_TOKENS_PROC ){
346 @tokens = (); @oti2 = ()
347 }
348
349 $dl = $rl = 0;
350
351 # ~ whitespace related issue ~
352 $add_one = 0;
353 %ws = ();
354
355
356 # ~ recursion ~
357
358 retr_info( \$tree_data->[2] ); # parse input data
359
360 $rl--;
361
362
363 # ~ write data.xml ~
364
365 $data =~ tr/\n\r/ /; # note: 2 blanks - otherwise offset data would become corrupt
366
367 $data = encode_utf8( $data );
368
369 ## DEPRECATED (only IDS-intern)
370 # first write it to tokenization pipe to give it some time
371 if ( $_GEN_TOK_BAS ){
372 print $chld_in "$data\n\x03\n";
373 }
374 ##
375
376 print STDERR "DEBUG ($0): main(): Writing (utf8-formatted) xml file $_root_dir$dir/$_data_file\n" if $_DEBUG;
377
378 if ( $first_write ){
379
380 $first_write = 0;
381
382 # 1st time: create instance
383 $zip = new IO::Compress::Zip $zip_outh, Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 0, Name => "$_root_dir$dir/$_data_file"
384 or die "ERROR ('$_root_dir$dir/$_data_file'): zip failed: $ZipError\n"
385
386 } else {
387
388 # closes the current compressed data stream and starts a new one.
389 $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir/$_data_file" )
390 or die "ERROR ('$_root_dir$dir/$_data_file'): zip failed: $ZipError\n"
391 }
392
393 $data =~ s/(&|<|>)/$ent{$1}/g;
394
395 $zip->print( "$data_prfx1$text_id_esc$data_prfx2$data$data_sfx" );
396
397
398 # ~ write structures ~
399
400 write_structures() if @structures;
401
402
403 # ~ write tokens ~
404
405 write_tokens() if $_TOKENS_PROC && @tokens;
406
407
408 # ~ dummy tokenization ~
409
410 if ( $_GEN_TOK_BAS || $_GEN_TOK_DUMMY ){ ## DEPRECATED ($_GEN_TOK_BAS: only IDS-intern)
411
412 select_tokenization();
413
414 if ( $_GEN_TOK_DUMMY ){
415 $offset = 0; @tok_tokens_con=(); @tok_tokens_agg=();
416 }
417 }
418
419 $data_fl = 0; $buf_in = $data = $dir = ""; # reinit.
420
421 } else { # $dir eq ""
422
423 print STDERR "WARNING ($0): main(): maybe empty textSigle => skipping this text ...\n";
424 print STDERR "WARNING ($0): main(): text header=$header_txt\n";
425 print STDERR "WARNING ($0): main(): data=$data\n";
Peter Hardersd892a582020-02-12 15:45:22 +0100426 }
427
Peter Harders6f526a32020-06-29 21:44:41 +0200428 } elsif ( $data_fl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100429
Peter Hardersd892a582020-02-12 15:45:22 +0100430
Peter Harders6f526a32020-06-29 21:44:41 +0200431 # ~ inside text body ~
Peter Hardersd892a582020-02-12 15:45:22 +0100432
Peter Hardersd892a582020-02-12 15:45:22 +0100433
Peter Harders6f526a32020-06-29 21:44:41 +0200434 #print STDERR "inside text body (\$data_fl set)\n";
435
436 # ~ whitespace handling ~
437
438 # remove consecutive whitespace at beginning and end (mostly one newline)
439 # to let 'XML::CompactTree::XS' recognize these blanks as 'text-nodes', the option 'XCT_IGNORE_WS' may not be used (see above).
440 s/^\s+//; s/\s+$//;
441
442 # There's nothing wrong with inserting an additional blank at the start of the 2nd and all consecutive lines (which contain at least one tag),
443 # because it helps for better readability of the text in the '$_data_file' (e.g.: assure blanks between sentences).
444 # Furthermore, the input lines should avoid primary text tokens, which span across several lines, unless the line breaks doesn't lead
445 # to a situation which produces unwanted blanks - e.g.: '...<w>end</w>\n<w>.</w>...' would lead to '...<w>end</w> <w>.</w>...', or
446 # '...<w>,</w>\n<w>this</w>\n<w>is</w>\n<w>it</w>\n<w>!</w>...' to '<w>,<w> <w>this</w> <w>is</w> <w>it</w> <w>!</w>'. Even when considering
447 # to correct those unwanted effects, there would be lots of examples aside punctuation, where there would not exist an easy way or unarbitrary
448 # solution regarding the elimination of the false blanks.
Peter Hardersd892a582020-02-12 15:45:22 +0100449 #
Peter Harders6f526a32020-06-29 21:44:41 +0200450 # So, the best way to avoid having false blanks in the output, is to assure that linebreaks between word-tags doesn't occur in the input
451 # (see also comments on 'input restrictions' at the top of this script).
Peter Hardersd892a582020-02-12 15:45:22 +0100452
Peter Harders6f526a32020-06-29 21:44:41 +0200453 if ( m/<[^>]+>[^<]/ ){ # line contains at least one tag with at least one character contents
Peter Hardersd892a582020-02-12 15:45:22 +0100454
Peter Harders6f526a32020-06-29 21:44:41 +0200455 $tc++; # text counter
Peter Hardersd892a582020-02-12 15:45:22 +0100456
Peter Harders6f526a32020-06-29 21:44:41 +0200457 s/^(.)/ $1/ if $tc > 1; # add blank before 1st character for 2nd line and consecutive lines (which contain at least one tag)
Peter Hardersd892a582020-02-12 15:45:22 +0100458 }
459
Peter Harders6f526a32020-06-29 21:44:41 +0200460 # add line to buffer
461 $buf_in .= $_;
462
463 } elsif ( $header_fl_txt && m#^(.*</${_TEXT_HEADER_END}>)(.*)$# ){
464
465
466 # ~ end of text header ~
467
468
469 #print STDERR "end of text header\n";
470
471 # write it to header.xml
472
473 $sfx = $2;
474
475 $header_txt .= $1; $header_fl_txt = 0;
476
477
478 die "ERROR ($0): main(): input line number $lc: line with closing text-header tag '${_TEXT_HEADER_END}'"
479 ." contains additional information ... => Aborting\n\tline=$_"
480 if $sfx !~ /^\s*$/;
481
482 if ( $dir eq "" ){
483
484 print STDERR "WARNING ($0): main(): input line number $lc: empty textSigle in text header => nothing to do ...\ntext header=$header_txt\n";
485
486 } else {
487
488 print STDERR "DEBUG ($0): Writing file $_root_dir$dir/$_header_file\n" if $_DEBUG;
489
490 if ( $first_write ){
491
492 $first_write = 0;
493
494 $zip = new IO::Compress::Zip $zip_outh, Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD,
495 Append => 0, Name => "$_root_dir$dir/$_header_file"
496 or die "ERROR ('$_root_dir$dir/$_header_file'): zip failed: $ZipError\n"
497
498 } else {
499
500 $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir/$_header_file" )
501 or die "ERROR ('$_root_dir$dir/$_header_file'): zip failed: $ZipError\n"
502 }
503
504 $header_txt = encode_utf8( $header_txt );
505
506 $zip->print( "$header_prfx$header_txt" );
507
508 $header_txt = "";
Peter Hardersd892a582020-02-12 15:45:22 +0100509 }
Peter Hardersd892a582020-02-12 15:45:22 +0100510
Peter Harders6f526a32020-06-29 21:44:41 +0200511 } elsif ( $header_fl_txt ){
Peter Hardersd892a582020-02-12 15:45:22 +0100512
Peter Harders6f526a32020-06-29 21:44:41 +0200513 # ~ inside text header ~
Peter Hardersd892a582020-02-12 15:45:22 +0100514
Peter Harders6f526a32020-06-29 21:44:41 +0200515
516 #print STDERR "inside text header\n";
517
518 if( m#^(.*)<${_TEXT_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
519
520 $pfx = $1; $sfx = $3;
521
522 $dir = $2; $text_id = $dir;
523
524 $text_id =~ tr/\//_/; $dir =~ s/("|&|<|>)/$ent{$1}/g;
525
526 $text_id = encode_utf8( $text_id );
527
528 die "ERROR ($0): main(): input line number $lc: line with text-sigle tag '$_TEXT_SIGLE' is not in expected format ... => Aborting\n\tline=$_"
529 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_TEXT_SIGLE}>\s*$# || $dir =~ /^\s*$/;
530
531 # log output for seeing progression
532 print STDERR "$0: main(): text_id=".decode_utf8( $text_id )."\n";
533
534 $text_id_esc = $text_id;
535
536 s#(<${_TEXT_SIGLE}(?: [^>]*)?>)[^<]+(</${_TEXT_SIGLE}>)#$1$dir$2# # to be consistent with escaping, escape also textSigle in text-header
537 if $text_id_esc =~ s/("|&|<|>)/$ent{$1}/g;
538
539 $dir =~ tr/\./\//;
540 }
541
542 $header_txt .= $_;
543
544 } elsif ( $header_fl_doc && m#^(.*</${_DOC_HEADER_END}>)(.*)$# ){
545
546
547 # ~ end of document header ~
548
549
550 #print STDERR "end of doc header\n";
551
552 # write it to header.xml
553
554 $sfx = $2;
555
556 $header_doc .= $1; $header_fl_doc = 0;
557
558 die "ERROR ($0): main(): input line number $lc: line with closing document-header tag '${_DOC_HEADER_END}'"
559 ." contains additional information ... => Aborting\n\tline=$_"
560 if $sfx !~ /^\s*$/;
561
562 if( $dir_doc eq "" ){
563
564 print STDERR "WARNING ($0): main(): input line number $lc: empty document sigle in document header"
565 ." => nothing to do ...\ndocument header=$header_doc\n";
566
567 } else {
568
569 print STDERR "DEBUG ($0): Writing file $_root_dir$dir_doc/$_header_file\n" if $_DEBUG;
570
571 if ( $first_write ){
572
573 $first_write = 0;
574
575 $zip = new IO::Compress::Zip $zip_outh, Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 0,
576 Name => "$_root_dir$dir_doc/$_header_file"
577 or die "ERROR ('$_root_dir$dir_doc/$_header_file'): zip failed: $ZipError\n"
578
579 } else {
580
581 $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir_doc/$_header_file" )
582 or die "ERROR ('$_root_dir$dir_doc/$_header_file'): zip failed: $ZipError\n"
583 }
584
585 $header_doc = encode_utf8( $header_doc );
586
587 $zip->print( "$header_prfx$header_doc" );
588
589 $header_doc = $dir_doc = "";
590 }
591
592 } elsif ( $header_fl_doc ){
593
594
595 # ~ inside document header ~
596
597
598 #print STDERR "inside doc header\n";
599
600 if ( m#^(.*)<${_DOC_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
601
602 $pfx = $1; $sfx = $3;
603
604 $dir_doc = $2;
605
606 die "ERROR ($0): main(): input line number $lc: line with document-sigle tag '$_DOC_SIGLE' is not in expected format ... => Aborting\n\tline=$_"
607 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_DOC_SIGLE}>\s*$# || $dir_doc =~ /^\s*$/;
608
609 s#(<${_DOC_SIGLE}(?: [^>]*)?>)[^<]+(</${_DOC_SIGLE}>)#$1$dir_doc$2# # to be consistent with escaping, escape also textSigle in Document-Header
610 if $dir_doc =~ s/("|&|<|>)/$ent{$1}/g;
611 }
612
613 $header_doc .= $_;
614
615 } elsif ( m#^(.*)(<${_TEXT_HEADER_BEG}.*)$# ){
616
617 # ~ start of text header ~
618
619
620 #print STDERR "begin of text header\n";
621
622 $header_txt = $_; $header_fl_txt = 1; $pfx = $1;
623
624 $tc = 0; # reset (needed for ~ whitespace handling ~)
625
626 die "ERROR ($0): main(): input line number $lc: line with opening text-header tag '${_TEXT_HEADER_BEG}'"
627 ." is not in expected format ... => Aborting\n\tline=$_"
628 if $pfx !~ /^\s*$/;
629
630 } elsif ( m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){
631
632
633 # ~ start of text body ~
634
635
636 #print STDERR "inside text body\n";
637
638 $pfx = $1; $sfx = $2;
639
640 $data_fl = 1;
641
642 die "ERROR ($0): main(): input line number $lc: line with opening text-body tag '${_TEXT_BODY}'"
643 ." contains additional information ... => Aborting\n\tline=$_"
644 if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/;
645
646 } elsif ( m#^(.*)(<${_DOC_HEADER_BEG}.*)$# ){
647
648
649 # ~ start of document header ~
650
651
652 #print STDERR "begin of doc header\n";
653
654 $header_doc = "$2\n"; $header_fl_doc = 1; $pfx = $1;
655
656 die "ERROR ($0): main(): input line number $lc: line with opening document-header tag '${_DOC_HEADER_BEG}'"
657 ."is not in expected format ... => Aborting\n\tline=$_"
658 if $pfx !~ /^\s*$/;
659
660 } elsif ( $header_fl_crp && m#^(.*</${_CORP_HEADER_END}>)(.*)$# ){
661
662
663 # ~ end of corpus header ~
664
665
666 #print STDERR "end of corp header\n";
667
668 $sfx = $2;
669
670 $header_crp .= $1; $header_fl_crp = 0;
671
672 die "ERROR ($0): main(): input line number $lc: line with closing corpus-header tag '${_CORP_HEADER_END}'"
673 ." contains additional information ... => Aborting\n\tline=$_"
674 if $sfx !~ /^\s*$/;
675
676 if ( $dir_crp eq "" ){
677
678 print STDERR "WARNING ($0): main(): input line number $lc: empty corpus sigle in corpus header => nothing to do ...\ncorpus header=$header_crp\n";
679
680 } else {
681
682 print STDERR "DEBUG ($0): Writing file $_root_dir$dir_crp/$_header_file\n" if $_DEBUG;
683
684 if ( $first_write ){
685
686 $first_write = 0;
687
Peter Harders6f526a32020-06-29 21:44:41 +0200688 $zip = new IO::Compress::Zip $zip_outh, Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD,
689 Append => 0, Name => "$_root_dir$dir_crp/$_header_file"
690 or die "ERROR ('$_root_dir$dir_crp/$_header_file'): zip failed: $ZipError\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200691
692 } else {
693
694 $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir_crp/$_header_file" )
695 or die "ERROR ('$_root_dir$dir_crp/$_header_file'): zip failed: $ZipError\n"
696 }
697
698 $header_crp = encode_utf8( $header_crp );
699
700 $zip->print( "$header_prfx$header_crp" );
701
702 $header_crp = $dir_crp = "";
703 }
704
705 } elsif ( $header_fl_crp ){
706
707
708 # ~ inside corpus header ~
709
710
711 #print STDERR "inside corp header\n";
712
713 if ( m#^(.*)<${_CORP_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
714
715 $pfx = $1; $sfx = $3;
716
717 $dir_crp = $2;
718
719 die "ERROR ($0): main(): input line number $lc: line with korpusSigle-tag is not in expected format ... => Aborting\n\tline=$_"
720 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_CORP_SIGLE}>\s*$# || $dir_crp =~ /^\s*$/;
721
722 if ( $dir_crp =~ s/("|&|<|>)/$ent{$1}/g ){
723
724 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 +0100725 }
726 }
727
Peter Harders6f526a32020-06-29 21:44:41 +0200728 $header_crp .= $_;
Peter Hardersd892a582020-02-12 15:45:22 +0100729
Peter Harders6f526a32020-06-29 21:44:41 +0200730 } elsif ( m#^(.*)(<${_CORP_HEADER_BEG}.*)$# ){
Peter Hardersd892a582020-02-12 15:45:22 +0100731
Peter Hardersd892a582020-02-12 15:45:22 +0100732
Peter Harders6f526a32020-06-29 21:44:41 +0200733 # ~ start of corpus header ~
Peter Hardersd892a582020-02-12 15:45:22 +0100734
Peter Harders6f526a32020-06-29 21:44:41 +0200735
736 #print STDERR "begin of corp header\n";
737
738 $header_crp = $2; $header_fl_crp = 1; $pfx = $1;
739
740 die "ERROR ($0): main(): input line number $lc: line with opening corpus-header tag '${_CORP_HEADER_BEG}'"
741 ." is not in expected format ... => Aborting\n\tline=$_"
742 if $pfx !~ /^\s*$/;
Peter Hardersd892a582020-02-12 15:45:22 +0100743 }
744
Peter Harders6f526a32020-06-29 21:44:41 +0200745 } #end: while
Peter Hardersd892a582020-02-12 15:45:22 +0100746
Peter Harders6f526a32020-06-29 21:44:41 +0200747 $zip->close();
748
749 ## DEPRECATED (only IDS-intern)
750 if( $_GEN_TOK_BAS ){
751 close($chld_in);
752 close($chld_out);
Peter Hardersd892a582020-02-12 15:45:22 +0100753 }
Peter Harders6f526a32020-06-29 21:44:41 +0200754 ##
Peter Hardersd892a582020-02-12 15:45:22 +0100755
Peter Harders90157342020-07-01 21:05:14 +0200756} # end: sub process
Peter Hardersd892a582020-02-12 15:45:22 +0100757
Peter Hardersd892a582020-02-12 15:45:22 +0100758
Peter Harders6f526a32020-06-29 21:44:41 +0200759sub retr_info { # called from process()
Peter Hardersd892a582020-02-12 15:45:22 +0100760
Peter Harders6f526a32020-06-29 21:44:41 +0200761 # EXAMPLE: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>
762 #
763 # print out values of above example:
764 # 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 $data->[2]->[0]->[5]->[1]->[1]'
765 #
766 # $data = reference to below array
767 #
768 # [ 0: XML_READER_TYPE_DOCUMENT,
769 # 1: ?
770 # 2: [ 0: [ 0: XML_READER_TYPE_ELEMENT <- start recursion with array '$data->[2]' (see process(): retr_info( \$tree_data->[2] ))
771 # 1: 'node'
772 # 2: ?
773 # 3: HASH (attributes)
774 # 4: 1 (line number)
775 # 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT
776 # 1: 'node1'
777 # 2: ?
778 # 3: undefined (no attributes)
779 # 4: 1 (line number)
780 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
781 # 1: 'some '
782 # ]
783 # 1: [ 0: XML_READER_TYPE_ELEMENT
784 # 1: 'n'
785 # 2: ?
786 # 3: undefined (no attributes)
787 # 4: 1 (line number)
788 # 5: undefined (no child nodes)
789 # ]
790 # 2: [ 0: XML_READER_TYPE_TEXT
791 # 1: ' text'
792 # ]
793 # ]
794 # ]
795 # 1: [ 0: XML_READER_TYPE_ELEMENT
796 # 1: 'node2'
797 # 2: ?
798 # 3: undefined (not attributes)
799 # 4: 1 (line number)
800 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
801 # 1: 'more-text'
802 # ]
803 # ]
804 # ]
805 # ]
806 # ]
807 # ]
808 # ]
809 #
810 # $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT)
811 #
812 # ref($data->[2]) == ARRAY (with 1 element for 'node')
813 # ref($data->[2]->[0]) == ARRAY (with 6 elements)
814 #
815 # $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
816 # $data->[2]->[0]->[1] == 'node'
817 # ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v')
818 # $data->[2]->[0]->[4] == 1 (line number)
819 # ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2')
820 # # child nodes of actual node (see $_IDX)
821 #
822 # ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements)
823 # $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
824 # $data->[2]->[0]->[5]->[0]->[1] == 'node1'
825 # $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute)
826 # $data->[2]->[0]->[5]->[0]->[4] == 1 (line number)
827 # ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text')
828 #
829 # ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements)
830 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
831 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some '
832 #
833 # ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements)
834 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
835 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n'
836 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute)
837 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number)
838 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child nodes)
839 #
840 # ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements)
841 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
842 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text'
843 #
844 #
845 # retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example.
846 # Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to
847 # ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]).
Peter Hardersd892a582020-02-12 15:45:22 +0100848
Peter Harders6f526a32020-06-29 21:44:41 +0200849 $rl++; # recursion level (1 = topmost level inside retr_info() = should always be level of tag $_TEXT_BODY)
Peter Hardersd892a582020-02-12 15:45:22 +0100850
Peter Hardersd892a582020-02-12 15:45:22 +0100851
Peter Harders6f526a32020-06-29 21:44:41 +0200852 foreach $e ( @{${$_[0]}} ){ # iteration through all array elements ($_[0] is a reference to an array reference)
Peter Hardersd892a582020-02-12 15:45:22 +0100853
Peter Hardersd892a582020-02-12 15:45:22 +0100854
Peter Harders6f526a32020-06-29 21:44:41 +0200855 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 +0100856
Peter Hardersd892a582020-02-12 15:45:22 +0100857
Peter Harders6f526a32020-06-29 21:44:41 +0200858 #~~~~
859 # from here: opening tag
860 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100861
Peter Hardersd892a582020-02-12 15:45:22 +0100862
Peter Harders6f526a32020-06-29 21:44:41 +0200863 # insert new array (for new tag) into @structures with tag-name and tag-attributes (if present)
864 # 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 +0100865
Peter Harders6f526a32020-06-29 21:44:41 +0200866 # ~ tag name ~
Peter Hardersd892a582020-02-12 15:45:22 +0100867
Peter Harders6f526a32020-06-29 21:44:41 +0200868 $n = $e->[1];
Peter Hardersd892a582020-02-12 15:45:22 +0100869
Peter Hardersd892a582020-02-12 15:45:22 +0100870
Peter Harders6f526a32020-06-29 21:44:41 +0200871 # ~ handle structures ~
Peter Hardersd892a582020-02-12 15:45:22 +0100872
Peter Harders6f526a32020-06-29 21:44:41 +0200873 my @array;
874 push @array, $n;
875 push @structures, \@array;
876 push @oti, $#structures; # add highest index of @structures to @oti
Peter Hardersd892a582020-02-12 15:45:22 +0100877
Peter Hardersd892a582020-02-12 15:45:22 +0100878
Peter Harders6f526a32020-06-29 21:44:41 +0200879 # ~ handle tokens ~
Peter Hardersd892a582020-02-12 15:45:22 +0100880
Peter Harders6f526a32020-06-29 21:44:41 +0200881 $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 +0100882
Peter Harders6f526a32020-06-29 21:44:41 +0200883 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100884
Peter Harders6f526a32020-06-29 21:44:41 +0200885 my @array2;
886 push @array2, $n;
887 push @tokens, \@array2;
888 push @oti2, $#tokens;
889 }
Peter Hardersd892a582020-02-12 15:45:22 +0100890
Peter Hardersd892a582020-02-12 15:45:22 +0100891
Peter Harders6f526a32020-06-29 21:44:41 +0200892 # ~ handle attributes ~
Peter Hardersd892a582020-02-12 15:45:22 +0100893
Peter Harders6f526a32020-06-29 21:44:41 +0200894 if ( defined $e->[3] ){ # only if attributes exist
Peter Hardersd892a582020-02-12 15:45:22 +0100895
Peter Harders6f526a32020-06-29 21:44:41 +0200896 for ( $c = 0; $c < @{$e->[3]}; $c += 2 ){ # with 'XCT_ATTRIBUTE_ARRAY', $node->[3] is an array reference of the form
897 # [ name1, value1, name2, value2, ....] of attribute names and corresponding values.
898 # note: arrays are faster (see: http://makepp.sourceforge.net/2.0/perl_performance.html)
Peter Hardersd892a582020-02-12 15:45:22 +0100899
Peter Harders6f526a32020-06-29 21:44:41 +0200900 # '$c' references the 'key' and '$c+1' the 'value'
901 push @{$structures[$#structures]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1];
Peter Hardersd892a582020-02-12 15:45:22 +0100902
Peter Harders6f526a32020-06-29 21:44:41 +0200903 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100904
Peter Harders6f526a32020-06-29 21:44:41 +0200905 push @{$tokens[$#tokens]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1];
906 }
907
908 }
909 }
910
911
912 # ~ index 'from' ~
913
914 # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts
915
916 push @{$structures[$#structures]}, ( $dl + $add_one ); # see below (text and whitespace nodes) for explanation on '$add_one'
917
918 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
919
920 push @{$tokens[$#tokens]}, ( $dl + $add_one );
921 }
922
923
924 #~~~~
925 # until here: opening tag
926 #~~~~
927
928
929 # ~~ RECURSION ~~
930
931 if ( defined $e->[$_IDX] ){ # do no recursion, if $e->[$_IDX] is not defined (because we have no array of child nodes, e.g.: <back/>)
932
933 retr_info( \$e->[$_IDX] ); # recursion with array of child nodes
934
935 $rl--; # return from recursion
936 }
937
938
939 #~~~~~
940 # from here: closing tag
941 #~~~~~
942
943
944 # ~ handle structures ~
945
946 {
947 my $ix = pop @oti; # index of just closed tag
948
949 my $aix = $#{$structures[$ix]}; # determine highest index from 'array referring to last closed tag' ...
950
951 $fval = ${$structures[$ix]}[ $aix ]; # ... and get it's from-value
952
953 if ( $fval > 0 && not exists $ws{ $fval - 1 } ){ # ~ whitespace related issue ~
954
955 # previous node was a text-node
956
957 ${$structures[$ix]}[ $aix ] = $fval - 1; # recorrect from-value (see below: notes on ~ whitespace related issue ~)
958 }
959
960 # in case this fails, check input
961 die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@structures: from-value ($fval) is 2 or more greater"
962 ." than to-value ($dl) => please check. aborting ...\n"
963 if ( $fval - 1 ) > $dl;
964
965 # TODO: construct example for which this case applies
966 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
967 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval - 1) >= $dl;
968 ${$structures[$ix]}[ $aix ] = $dl if $fval == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl)
969
970 push @{$structures[$ix]}, $dl, $rl; # to-value and recursion-level
971
972 # note: use $dl, because the offsets are _between_ the characters (e.g.: word = 'Hello' => from = 0 (before 'H'), to = 5 (after 'o'))
973 }
974
975
976 # ~ handle tokens ~
977
978
979 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
980
981 my $ix = pop @oti2;
982
983 my $aix = $#{$tokens[$ix]};
984
985 $fval2 = ${$tokens[$ix]}[ $aix ]; # from-value
986
987 if( $fval2 > 0 && not exists $ws{ $fval2 - 1 } ){ # ~ whitespace related issue ~
988
989 # previous node was a text-node
990
991 ${$tokens[$ix]}[ $aix ] = $fval2 - 1; # recorrect from-value
992 }
993
994 # in case this fails, check input
995 die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@tokens: from-value ($fval2) is 2 or more greater"
996 ." than to-value ($dl) => please check. aborting ...\n"
997 if ( $fval2 - 1 ) > $dl;
998
999 # TODO: construct example for which this case applies
1000 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
1001 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval2 - 1) >= $dl;
1002 ${$tokens[$ix]}[ $aix ] = $dl if $fval2 == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl)
1003
1004 push @{$tokens[$ix]}, $dl, $rl; # to-value and recursion-level
1005
1006 $inside_tokens_tag = -1; # reset
1007 }
1008
1009 # ~ whitespace related issue ~
Peter Hardersd892a582020-02-12 15:45:22 +01001010 # clean up
Peter Harders6f526a32020-06-29 21:44:41 +02001011 delete $ws{ $fval - 1 } if $fval > 0 && exists $ws{ $fval - 1 };
1012 delete $ws{ $fval2 - 1 } if $_TOKENS_PROC && $fval2 > 0 && exists $ws{ $fval2 - 1 };
Peter Hardersd892a582020-02-12 15:45:22 +01001013
1014
Peter Harders6f526a32020-06-29 21:44:41 +02001015 #~~~~~
1016 # from here: text (and whitespace) nodes
1017 #~~~~~
Peter Hardersd892a582020-02-12 15:45:22 +01001018
1019
Peter Harders6f526a32020-06-29 21:44:41 +02001020 # the 3rd form of nodes, next to text-nodes (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes
1021 # of the type 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'
1022 #
1023 # when modifiying the above example (at the top of this sub) by inserting an additional blank between '</node1>' and '<node2>',
1024 # the output for '$data->[2]->[0]->[5]->[1]->[1]' becomes a blank (' ') and it's type is '14' (see manpage of XML::LibXML::Reader):
1025 #
1026 # 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=".$data->[2]->[0]->[5]->[1]->[1].", type=".$data->[2]->[0]->[5]->[1]->[0]."\n"'
Peter Hardersd892a582020-02-12 15:45:22 +01001027
Peter Harders6f526a32020-06-29 21:44:41 +02001028 } elsif ( $e->[0] == XML_READER_TYPE_TEXT || $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){
Peter Hardersd892a582020-02-12 15:45:22 +01001029
Peter Harders6f526a32020-06-29 21:44:41 +02001030 # notes on ~ whitespace related issue ~ (see below source code)
1031 #
1032 # example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...'
1033 #
1034 # Two text-nodes should normally be separated by a blank. In the above example, that would be the 2 text-nodes
1035 # 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' '.
1036 #
1037 # Assumed, that the above example marks the general case, then the text-node 'Campagne in Frankreich' leads to the
1038 # setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag and setting it's from-index, it gets the right
1039 # offset, which is the start-index of '1792'.
1040 #
1041 # To check, that the above consideration holds, we save the from-index of a read whitespace-node into the hash %ws.
1042 # By this, it can be checked when closing a tag, if the 'non-tag'-node (text or whitespace) before the last 'non-tag'-
1043 # node was actually a whitespace-node ($ws{ $fval - 1 }).
1044 #
1045 # For whitespace-nodes, also $add_one has to be set to 0, so when opening the next tag (in the above example the 2nd
1046 # 's'-tag), no additional 1 is added, because this was already done by the whitespace-node itself (by incrementing the
1047 # variable $dl).
1048 #
1049 # Now, what happens, when 2 text-nodes are not seperated by a whitespace-node (blank)? (e.g.: <w>Augen<c>,</c></w>)
1050 #
1051 # In this case, the falsely increased from-value has to be decreased again by 1 when closing the referring tag
1052 # (...$fval - 1; # recorrect).
1053 #
1054 # Comparing the 2 examples '<w>fu</w> <w>bar</w>' and '<w>fu</w><w> </w><w>bar</w>' (even though, the 2nd one makes less
1055 # sense, because of '<w> </w>'), in both the ' ' is handled as a whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE).
1056 #
1057 # So the from-index of the 2nd w-tag (in the second example) would refer to 'bar', which may not have been the intention
1058 # (even, if '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug, which needs to be fixed?
1059 #
1060 # Empty tags also cling to the next text-token - e.g. in '...<w>tok1</w> <w>tok2</w><a><b/></a><w>tok3</w>...' the from-
1061 # and to-indizes for the tags 'a' and 'b' are both 9, which is the start-index of the token 'tok3'.
1062
1063 if( $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){
1064
1065 # ~ whitespace related issue ~
1066
1067 $add_one = 0;
1068
1069 $ws{ $dl }++; # '++' does not mean a thing here (could be used for consistency checking)
1070
1071 }else{
1072
1073 # ~ text-node ~
1074
1075 $add_one = 1;
1076 }
1077
1078
1079 # ~ update $data and $dl ~
1080
1081 $data .= $e->[1];
1082
1083 $dl += length( $e->[1] ); # update length of $data
1084
1085
1086
1087 #~~~~~
1088 # from here (until end): dummy tokenization
1089 #~~~~~
1090
1091 if ( $_GEN_TOK_DUMMY ){
1092
1093 $txt = $e->[1];
1094
1095 if ( substr( $txt, 0, 1 ) ne ' ' || substr( $txt, 1, 1) ne ' ' ){ # $txt has at least 2 chars, if it's not empty or equal to ' '
1096
1097
1098 # ~ start: conservative tokenization ~
1099
1100
1101 # '\p{Punct}' is equal to the character class '[-!"#%&'()*,./:;?@[\\\]_{}]'
1102 while ( $txt =~ /([\p{Punct}]*)([^\p{Punct} \x{9}\n]+(?:([\p{Punct}]+)[^\p{Punct} \x{9}\n]+)*)?([\p{Punct}]*)(?:[ \x{9}\n])?/g ){
1103
1104 $m1 = $1; $m2 = $2; $m3 = $3; $m4 = $4;
1105
1106 if ( "$m1" ne "" ){ # special chars before token
1107
1108 $p1 = $-[1]; $p2 = $+[1];
1109
1110 #print STDERR "A1: ".$m1." -> from $p1 to $p2\n";
1111
1112 if ( $p2 == $p1+1 ){
1113
1114 if ( $p1 != 0 ){ $tmp = substr( $txt, $p1-1, 1 ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) } else { $pr = 0 };
1115
1116 if ( not $pr ){ $tmp = substr( $txt, $p2, 1 ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) };
1117
1118 if ( $pr ){ push @tok_tokens_con, $p1+$offset; push @tok_tokens_con, $p2+$offset }; # from and to
1119
1120 } else {
1121
1122 for ( $i = 0; $i < ( $p2-$p1 ); $i++ ){
1123
1124 #print STDERR "A2: ".substr($m1,$i,1)." -> from $p1 to $p2\n";
1125
1126 push @tok_tokens_con, $p1+$i+$offset; push @tok_tokens_con, $p1+$i+1+$offset; # from and to
1127 }
1128 }
1129
1130 } # fi: "$m1" ne ""
1131
1132 #print STDERR "B: "."$m2 -> from ".($-[2]+$offset)." to ".($+[2]+$offset)."\n" if defined $m2; # token (wordform)
1133
1134 if ( defined $m2 ){ push @tok_tokens_con, $-[2]+$offset; push @tok_tokens_con, $+[2]+$offset }; # from and to
1135
1136 if ( defined $m3 ){
1137
1138 $p1 = $-[3]; $p2 = $+[3];
1139
1140 #print STDERR "C: ".$m3." -> from $p1 to $p2\n";
1141
1142 if ( $p2 == $p1+1 ){
1143
1144 $tmp = substr( $txt, $p2, 1); $pr = ( $tmp =~ /^$/ ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) if not $pr; # char after match
1145
1146 if ( not $pr ){ $tmp = substr( $txt, $p1-1, 1 ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) }; # char before match
1147
1148 if ( $pr ){ push @tok_tokens_con, $p1+$offset; push @tok_tokens_con, $p2+$offset }; # from and to
1149
1150 } else { # length($m3)>1 => print all chars
1151
1152 for ( $i = 0; $i < ( $p2-$p1 ); $i++ ){
1153
1154 #$tmp=substr($m3,$i,1);
1155 #print STDERR "C2: $tmp -> from $p1 to $p2\n";
1156
1157 push @tok_tokens_con, $p1+$i+$offset; push @tok_tokens_con, $p1+$i+1+$offset; # from and to
1158 }
1159
1160 }
1161
1162 } # fi: defined $m3
1163
1164 if ( "$m4" ne "" ){ # special chars after token
1165
1166 $p1 = $-[4]; $p2 = $+[4];
1167
1168 #print STDERR "D1: ".$m4." -> from ".($p1+$offset)." to ".($p2+$offset)."\n";
1169
1170 if ( $p2 == $p1+1 ){
1171
1172 $tmp = substr( $txt, $p2, 1 ); $pr = ( $tmp =~ /^$/ ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) if not $pr; # char after match
1173
1174 if ( not $pr ){ $tmp = substr ( $txt, $p1-1, 1 ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) }; # char before match
1175
1176 if ( $pr ){ push @tok_tokens_con, $p1+$offset; push @tok_tokens_con, $p2+$offset } # from and to
1177
1178 }else{
1179
1180 for ( $i = 0; $i < ( $p2-$p1 ); $i++ ){
1181
1182 #print STDERR "D2: ".substr($m4,$i,1)." -> from ".($p1+$i+$offset)." to ".($p1+$i+1+$offset)."\n";
1183
1184 push @tok_tokens_con, $p1+$i+$offset; push @tok_tokens_con, $p1+$i+1+$offset; # from and to
1185 }
1186 }
1187
1188 }# fi: "$m4" ne ""
1189
1190 }# end: while
1191
1192
1193 # ~ end: conservative tokenization ~
1194
1195
1196 # ~ start: aggressive tokenization ~
1197
1198
1199 while ( $txt =~ /([^\p{Punct} \x{9}\n]+)(?:([\p{Punct}])|(?:[ \x{9}\n])?)|([\p{Punct}])/g ){
1200
1201 if ( defined $1 ){
1202
1203 push @tok_tokens_agg, $-[1]+$offset; push @tok_tokens_agg, $+[1]+$offset; # from and to
1204
1205 if ( defined $2 ){ push @tok_tokens_agg, $-[2]+$offset; push @tok_tokens_agg, $+[2]+$offset } # from and to
1206
1207 }else{ # defined $3
1208
1209 push @tok_tokens_agg, $-[3]+$offset; push @tok_tokens_agg, $+[3]+$offset # from and to
1210 }
1211
1212 } # end: while
1213
1214 # ~ end: aggressive tokenization ~
1215
1216 ##$offset = $dl+1;
1217
1218 $offset = $dl;
1219
1220 } # fi
1221
1222 } # fi: $_GEN_TOK_DUMMY
1223
1224
1225 #elsif ( $e->[0] == XML_READER_TYPE_ATTRIBUTE ) # attribute node
1226 # note: attributes cannot be processed like this ( => use 'XCT_ATTRIBUTE_ARRAY' - see above )
1227
1228
1229 }else{ # not yet handled type
1230
1231 die "ERROR ($0): Not yet handled type (\$e->[0]=".$e->[0].") ... => Aborting\n";
1232 }
1233
1234 } # end: foreach iteration
1235
1236} # end: sub retr_info
1237
1238
1239sub select_tokenization { # called from process()
1240
1241 #print STDERR "$0: select_tokenization() ...\n";
1242
1243 ## DEPRECATED (only IDS-intern)
Peter Hardersd892a582020-02-12 15:45:22 +01001244 if( $_GEN_TOK_BAS ) {
1245 if( $select->can_read(3600) ){ # wait 60m for external tokenizer
1246 $_ = <$chld_out>;
1247 my @bounds = split;
Peter Harders6f526a32020-06-29 21:44:41 +02001248 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_bas", $text_id_esc, \@bounds);
Peter Hardersd892a582020-02-12 15:45:22 +01001249 while($select->can_read(0)) {
1250 $_ = <$chld_out>;
1251 if (defined $_ && $_ ne '') {
1252 print STDERR "WARNING: extra output: $_\n"
1253 } else {
1254 print STDERR "WARNING: tokenizer seems to have crashed, restarting.\n";
1255 startTokenizer();
1256 }
1257 }
1258 }else{
1259 $zip->close();
1260 die "ERROR ($0): cannot retrieve token bounds from external tokenizer for text '$text_id' => Aborting ...\n";
1261 }
Peter Harders6f526a32020-06-29 21:44:41 +02001262 ##
Peter Hardersd892a582020-02-12 15:45:22 +01001263 }elsif( $_GEN_TOK_DUMMY ){
Peter Harders6f526a32020-06-29 21:44:41 +02001264 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_con", $text_id_esc, \@tok_tokens_con);
1265 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_agg", $text_id_esc, \@tok_tokens_agg);
Peter Hardersd892a582020-02-12 15:45:22 +01001266 }
Peter Hardersd892a582020-02-12 15:45:22 +01001267
Peter Harders6f526a32020-06-29 21:44:41 +02001268 #print STDERR "$0: write_tokenization(): DONE\n";
1269
1270} # end: select_tokenization
1271
1272sub write_tokenization { # called from select_tokenization()
1273
1274 my ( $fname, $textid_esc, $bounds ) = @_;
Peter Hardersd892a582020-02-12 15:45:22 +01001275
1276 $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => $fname)
1277 or die "ERROR ('$fname'): zip failed: $ZipError\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001278
1279 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1280 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\"$text_id_esc\" xmlns=\"http://ids-mannheim.de/ns/KorAP\""
1281 ." version=\"KorAP-0.4\">\n <spanList>\n";
1282
1283 $c = 0;
1284
1285 for( $i = 0; $i < ($#$bounds + 1); $i += 2 ){
1286
Peter Hardersd892a582020-02-12 15:45:22 +01001287 $output .= " <span id=\"t_$c\" from=\"".$bounds->[$i]."\" to=\"".$bounds->[$i+1]."\" />\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001288
Peter Hardersd892a582020-02-12 15:45:22 +01001289 $c++;
1290 }
Peter Hardersd892a582020-02-12 15:45:22 +01001291
Peter Harders6f526a32020-06-29 21:44:41 +02001292 $output .= " </spanList>\n</layer>";
1293
1294 $zip->print ( "$output" );
1295
1296} # end: sub write_tokenization
1297
1298
1299sub write_structures { # called from process()
1300
1301 # ~ write @structures ~
Peter Hardersd892a582020-02-12 15:45:22 +01001302
1303 #print STDERR "$0: write_structures(): ...\n";
1304
Peter Harders6f526a32020-06-29 21:44:41 +02001305 if ( $dir eq "" ){
1306
1307 print STDERR "WARNING ($0): write_structures(): empty textSigle => nothing to do ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001308 return;
1309 }
1310
1311 $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir/$_structure_dir/$_structure_file" )
1312 or die "ERROR ('$_root_dir$dir/$_structure_dir/$_structure_file'): zip failed: $ZipError\n";
1313
Peter Harders6f526a32020-06-29 21:44:41 +02001314 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1315 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\""
1316 .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 +01001317
1318 $c = 0;
1319
Peter Harders6f526a32020-06-29 21:44:41 +02001320 foreach $ref ( @structures ){
Peter Hardersd892a582020-02-12 15:45:22 +01001321
Peter Harders6f526a32020-06-29 21:44:41 +02001322 ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 ); # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4
1323 # (name, from, to, rec_level), otherwise >4
Peter Hardersd892a582020-02-12 15:45:22 +01001324
Peter Harders6f526a32020-06-29 21:44:41 +02001325 # 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 )
1326
1327 if( $#structures == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){
1328
1329 ${$ref}[$idx] = ${$ref}[ $idx+1 ];
Peter Hardersd892a582020-02-12 15:45:22 +01001330 }
Peter Hardersd892a582020-02-12 15:45:22 +01001331
Peter Harders6f526a32020-06-29 21:44:41 +02001332 # this consistency check is already done in 'retr_info()'
1333 #elsif( ${$ref}[$idx] > ${$ref}[$idx+1] ){ # consistency check: abort, if this doesn't hold
1334 # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):"
1335 # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n";
1336 # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):"
1337 # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n\n$output" }
Peter Hardersd892a582020-02-12 15:45:22 +01001338
Peter Harders6f526a32020-06-29 21:44:41 +02001339 # at least 'POS' should always be there => remove constraint '$_TOKENS_PROC'
1340 #if( $_TOKENS_PROC && ${$ref}[0] ne $_TOKENS_TAG )
Peter Hardersd892a582020-02-12 15:45:22 +01001341
Peter Harders6f526a32020-06-29 21:44:41 +02001342 if( ${$ref}[0] ne $_TOKENS_TAG ){ # $_TOKENS_TAG is already written in 'write_tokens'
Peter Hardersd892a582020-02-12 15:45:22 +01001343
Peter Harders6f526a32020-06-29 21:44:41 +02001344 # l (level): insert information about depth of element in XML-tree (top element = level 1)
1345 $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n"
1346 ." <fs type=\"struct\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
1347 ." <f name=\"name\">${$ref}[ 0 ]</f>\n";
1348
1349 if ( $idx > 2 ) # attributes
1350 {
1351 $output .= " <f name=\"attr\">\n <fs type=\"attr\">\n";
1352
1353 for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){
1354
1355 ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # see explanation in func. 'write_tokens'
1356
1357 # attribute (at index $att_idx) with value (at index $att_idx+1)
1358 $output .= " <f name=\"${$ref}[ $att_idx ]\">${$ref}[ $att_idx+1 ]</f>\n";
1359 }
1360
1361 $output .= " </fs>\n </f>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001362 }
1363
Peter Harders6f526a32020-06-29 21:44:41 +02001364 $output .= " </fs>\n </span>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001365
Peter Harders6f526a32020-06-29 21:44:41 +02001366 } # fi: ... ne $_TOKENS_TAG
Peter Hardersd892a582020-02-12 15:45:22 +01001367
1368 $c++;
1369
Peter Harders6f526a32020-06-29 21:44:41 +02001370 } # end: foreach
Peter Hardersd892a582020-02-12 15:45:22 +01001371
1372 $output .= " </spanList>\n</layer>";
1373
Peter Harders6f526a32020-06-29 21:44:41 +02001374 $output = encode_utf8( $output );
Peter Hardersd892a582020-02-12 15:45:22 +01001375
Peter Harders6f526a32020-06-29 21:44:41 +02001376 $zip->print( "$output" );
Peter Hardersd892a582020-02-12 15:45:22 +01001377
1378 #print STDERR "$0: write_structures(): DONE\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001379
Peter Hardersd892a582020-02-12 15:45:22 +01001380} # end: sub write_structures
1381
1382
Peter Harders6f526a32020-06-29 21:44:41 +02001383sub write_tokens { # called from process()
1384
1385 # ~ write @tokens ~
1386
1387 #print STDERR "$0: write_tokens(): ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001388
1389 if( $dir eq "" ){
Peter Harders6f526a32020-06-29 21:44:41 +02001390
1391 print STDERR "WARNING ($0): write_tokens(): empty textSigle => nothing to do ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001392 return;
1393 }
1394
Peter Harders6f526a32020-06-29 21:44:41 +02001395 $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir/$_tokens_dir/$_tokens_file" )
1396 or die "ERROR ('$_root_dir$dir/$_tokens_dir/$_tokens_file'): zip failed: $ZipError\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001397
Peter Harders6f526a32020-06-29 21:44:41 +02001398 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1399 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\""
1400 .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 +01001401
1402 $c = 0;
1403
Peter Harders6f526a32020-06-29 21:44:41 +02001404 foreach $ref ( @tokens ){
Peter Hardersd892a582020-02-12 15:45:22 +01001405
Peter Harders6f526a32020-06-29 21:44:41 +02001406 # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4 (name, from, to, rec_level), otherwise >4
1407 ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 );
Peter Hardersd892a582020-02-12 15:45:22 +01001408
Peter Harders6f526a32020-06-29 21:44:41 +02001409 # 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())
1410 if ( $#tokens == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){
1411
1412 ${$ref}[ $idx ] = ${$ref}[ $idx+1 ]; # TODO: check
Peter Hardersd892a582020-02-12 15:45:22 +01001413 }
Peter Hardersd892a582020-02-12 15:45:22 +01001414
Peter Harders6f526a32020-06-29 21:44:41 +02001415 # l (level): insert information about depth of element in XML-tree (top element = level 1)
1416 $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n"
1417 ." <fs type=\"lex\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
1418 ." <f name=\"lex\">\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001419
Peter Harders6f526a32020-06-29 21:44:41 +02001420 if ( $idx > 2 ){ # attributes
1421
Peter Hardersd892a582020-02-12 15:45:22 +01001422 $output .= " <fs>\n";
1423
Peter Harders6f526a32020-06-29 21:44:41 +02001424 for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){
Peter Hardersd892a582020-02-12 15:45:22 +01001425
Peter Harders6f526a32020-06-29 21:44:41 +02001426 ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # ... <w lemma="&gt;" ana="PUNCTUATION">&gt;</w> ...
1427 # the '&gt;' is translated to '>' and hence the result would be '<f name="lemma">></f>'
Peter Hardersd892a582020-02-12 15:45:22 +01001428
Peter Harders6f526a32020-06-29 21:44:41 +02001429 if ( $_INLINE_ANNOT && ${$ref}[ $att_idx ] eq "$_INLINE_ATT_RD" ){
Peter Hardersd892a582020-02-12 15:45:22 +01001430
Peter Harders6f526a32020-06-29 21:44:41 +02001431 ${$ref}[ $att_idx+1 ] =~ /^([^ ]+)(?: (.+))?$/;
1432
1433 die "ERROR (write_tokens()): unexpected format! => Aborting ... (att: ${$ref}[ $att_idx+1 ])\n"
1434 if ( $_INLINE_POS_WR && not defined $1 ) || ( $_INLINE_MSD_WR && not defined $2 );
1435
1436 if ( "$_INLINE_POS_WR" ){
1437
1438 $output .= " <f name=\"$_INLINE_POS_WR\">";
Peter Hardersd892a582020-02-12 15:45:22 +01001439 $output .= "$1" if defined $1;
1440 $output .= "</f>\n";
1441 }
Peter Harders6f526a32020-06-29 21:44:41 +02001442
1443 if ( "$_INLINE_MSD_WR" ){
1444
1445 $output .= " <f name=\"$_INLINE_MSD_WR\">";
Peter Hardersd892a582020-02-12 15:45:22 +01001446 $output .= "$2" if defined $2;
1447 $output .= "</f>\n";
1448 }
1449
Peter Harders6f526a32020-06-29 21:44:41 +02001450 } elsif ( $_INLINE_ANNOT && "$_INLINE_LEM_RD" && ${$ref}[ $att_idx ] eq "$_INLINE_LEM_RD" ){
Peter Hardersd892a582020-02-12 15:45:22 +01001451
Peter Harders6f526a32020-06-29 21:44:41 +02001452 $output .= " <f name=\"$_INLINE_LEM_WR\">${$ref}[ $att_idx+1 ]</f>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001453
Peter Harders6f526a32020-06-29 21:44:41 +02001454 } else { # all other attributes
Peter Hardersd892a582020-02-12 15:45:22 +01001455
Peter Harders6f526a32020-06-29 21:44:41 +02001456 $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 +01001457 }
1458
Peter Harders6f526a32020-06-29 21:44:41 +02001459 } # end: for
Peter Hardersd892a582020-02-12 15:45:22 +01001460
1461 $output .= " </fs>\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001462
1463 } # fi: attributes
Peter Hardersd892a582020-02-12 15:45:22 +01001464
1465 $output .= " </f>\n </fs>\n </span>\n";
1466
1467 $c++;
1468
Peter Harders6f526a32020-06-29 21:44:41 +02001469 } # end: foreach
Peter Hardersd892a582020-02-12 15:45:22 +01001470
1471 $output .= " </spanList>\n</layer>";
1472
Peter Harders6f526a32020-06-29 21:44:41 +02001473 $output = encode_utf8( $output );
Peter Hardersd892a582020-02-12 15:45:22 +01001474
Peter Harders6f526a32020-06-29 21:44:41 +02001475 $zip->print( "$output" );
Peter Hardersd892a582020-02-12 15:45:22 +01001476
Peter Harders6f526a32020-06-29 21:44:41 +02001477 #print STDERR "$0: write_tokens(): DONE\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001478
Peter Harders6f526a32020-06-29 21:44:41 +02001479} # end: sub write_tokens
Peter Hardersd892a582020-02-12 15:45:22 +01001480
Peter Hardersd892a582020-02-12 15:45:22 +01001481
Peter Harders6f526a32020-06-29 21:44:41 +02001482## DEPRECATED ($_GEN_TOK_BAS: only IDS-intern)
Peter Hardersd892a582020-02-12 15:45:22 +01001483sub startTokenizer {
1484 $pid = open2($chld_out, $chld_in, 'java -cp '. join(":", ".", glob(&dirname(__FILE__)."/../target/*.jar"))." de.ids_mannheim.korap.tokenizer.KorAPTokenizerImpl");
1485 $select = IO::Select->new();
1486 $select->add(*$chld_out);
1487}
Peter Harders6f526a32020-06-29 21:44:41 +02001488##
Akrond949e182020-02-14 12:23:57 +01001489
1490__END__
1491
1492=pod
1493
1494=encoding utf8
1495
1496=head1 NAME
1497
1498tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
1499
1500=head1 SYNOPSIS
1501
1502 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
1503
1504=head1 DESCRIPTION
1505
1506C<tei2korapxml> is a script to convert TEI P5 and I5 based documents
Peter Harders6f526a32020-06-29 21:44:41 +02001507
Akrond949e182020-02-14 12:23:57 +01001508to the KorAP-XML format. If no specific input is defined, data is
Peter Harders6f526a32020-06-29 21:44:41 +02001509
Akrond949e182020-02-14 12:23:57 +01001510read from C<STDIN>. If no specific output is defined, data is written
Peter Harders6f526a32020-06-29 21:44:41 +02001511
Akrond949e182020-02-14 12:23:57 +01001512to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +02001513
Akrond949e182020-02-14 12:23:57 +01001514This program is usually called from inside another script.
1515
1516=head1 INSTALLATION
1517
1518C<tei2korapxml> requires L<libxml2-dev> bindings to build. When
Peter Harders6f526a32020-06-29 21:44:41 +02001519
Akrond949e182020-02-14 12:23:57 +01001520these bindings are available, the preferred way to install the script is
Peter Harders6f526a32020-06-29 21:44:41 +02001521
Akrond949e182020-02-14 12:23:57 +01001522to use L<cpanm|App::cpanminus>.
1523
1524 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
1525
1526In case everything went well, the C<tei2korapxml> tool will
Peter Harders6f526a32020-06-29 21:44:41 +02001527
Akrond949e182020-02-14 12:23:57 +01001528be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +02001529
Akrond949e182020-02-14 12:23:57 +01001530Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
1531
1532=head1 OPTIONS
1533
1534=over 2
1535
1536=item B<--base|-b>
1537
1538The base directory for output. Defaults to C<.>.
1539
1540=item B<--help|-h>
1541
1542Print help information.
1543
1544=item B<--version|-v>
1545
1546Print version information.
1547
1548=back
1549
1550=head1 COPYRIGHT AND LICENSE
1551
1552Copyright (C) 2020, L<IDS Mannheim|https://www.ids-mannheim.de/>
1553
1554Author: Peter Harders
1555
1556Contributors: Marc Kupietz, Carsten Schnober, Nils Diewald
1557
1558L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
Peter Harders6f526a32020-06-29 21:44:41 +02001559
Akrond949e182020-02-14 12:23:57 +01001560Corpus Analysis Platform at the
Peter Harders6f526a32020-06-29 21:44:41 +02001561
Akrond949e182020-02-14 12:23:57 +01001562L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
Peter Harders6f526a32020-06-29 21:44:41 +02001563
Akrond949e182020-02-14 12:23:57 +01001564member of the
Peter Harders6f526a32020-06-29 21:44:41 +02001565
Akrond949e182020-02-14 12:23:57 +01001566L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
1567
1568This program is free software published under the
Peter Harders6f526a32020-06-29 21:44:41 +02001569
Akrond949e182020-02-14 12:23:57 +01001570L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-TEI/master/LICENSE>.
1571
1572=cut