blob: 4f2035edb0800d28eaf961fcd41521cedfe95e47 [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);
9use IO::Handle;
Peter Hardersd892a582020-02-12 15:45:22 +010010use IO::Select;
11
12use open qw(:std :utf8); # assume utf-8 encoding
13use Encode qw(encode_utf8 decode_utf8);
14
Peter Hardersd892a582020-02-12 15:45:22 +010015use XML::CompactTree::XS;
16use XML::LibXML::Reader;
Peter Harders6f526a32020-06-29 21:44:41 +020017use IPC::Open2 qw(open2);
Peter Hardersd892a582020-02-12 15:45:22 +010018
Akron4f67cd42020-07-02 12:27:58 +020019use FindBin;
20BEGIN {
21 unshift @INC, "$FindBin::Bin/../lib";
22};
23
24use KorAP::XML::TEI;
Akroneac374d2020-07-07 09:00:44 +020025use KorAP::XML::TEI::Tokenization;
Akron85717512020-07-08 11:19:19 +020026use KorAP::XML::TEI::Zipper;
Peter Hardersd892a582020-02-12 15:45:22 +010027
Akrond949e182020-02-14 12:23:57 +010028our $VERSION = '0.01';
Peter Harders6f526a32020-06-29 21:44:41 +020029
Akrond949e182020-02-14 12:23:57 +010030our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n";
31
Peter Hardersd892a582020-02-12 15:45:22 +010032
Peter Harders6f526a32020-06-29 21:44:41 +020033# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010034GetOptions(
Peter Harders6f526a32020-06-29 21:44:41 +020035 "root|r=s" => \(my $_root_dir = '.'), # name of root directory inside zip file
36 "input|i=s" => \(my $input_fname = ''), # input file (yet only TEI I5 Format accepted)
Akrond949e182020-02-14 12:23:57 +010037 'help|h' => sub {
38 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 Harders6f526a32020-06-29 21:44:41 +020078## DEPRECATED (only IDS-intern - the tokenization is normally done by external tools)
79my $_GEN_TOK_BAS = 0; # IDS internal tokenization
80 my( $chld_out, $chld_in, $pid, $select );
81##
82
83## dummy tokenization (only for testing)
Akroneac374d2020-07-07 09:00:44 +020084my $_GEN_TOK_DUMMY = 1; # use dummy base tokenization for testing (base tokenization is normally done by external tools)
Peter Harders6f526a32020-06-29 21:44:41 +020085 my $_tok_file_con = "tokens_conservative.xml";
86 my $_tok_file_agg = "tokens_aggressive.xml";
Akron510a88c2020-07-07 10:16:50 +020087 my ( @tok_tokens_con, @tok_tokens_agg, $txt, $offset );
Peter Harders6f526a32020-06-29 21:44:41 +020088my $_base_tokenization_dir = "base"; # name of directory for storing files of dummy tokenization (only used in func. select_tokenization)
89
Peter Harders6f526a32020-06-29 21:44:41 +020090my $_DEBUG = 0; # set to 1 for minimal more debug output (no need to be parametrized)
91my $_XCT_LN = 0; # only for debugging: include line numbers in elements of $tree_data
92 # (see also manpage of XML::CompactTree::XS)
93
94my $_header_file = "header.xml"; # name of files containing the text, document and corpus header
95my $_data_file = "data.xml"; # name of file containing the primary text data (tokens)
96my $_structure_dir = "struct"; # name of directory containing the $_structure_file
97my $_structure_file = "structure.xml"; # name of file containing all tags (except ${_TOKEN_TAG}'s) related information
98 # (= their names and byte offsets in $_data)
99## TODO: optional (different annotation tools can produce more zip-files for feeding into KorAP-XML-Krill)
100my $_TOKENS_PROC = 1; # on/off: processing of ${_TOKEN_TAG}'s (default: 1)
101my $_tokens_dir = "tokens"; # name of directory containing the $_tokens_file
102my $_tokens_file = "morpho.xml"; # name of file containing all ${_TOKEN_TAG}'s related information (=their byte offsets in $_data)
103 # - evtl. with additional inline annotations
104my $_TOKENS_TAG = "w"; # name of tag containing all information stored in $_tokens_file
105
106## TODO: optional
107# handling inline annotations (inside $_TOKENS_TAG)
108my $_INLINE_ANNOT = 0; # on/off: set to 1 if inline annotations are present and should be processed (default: 0)
109my $_INLINE_LEM_RD = "lemma"; # from which attribute to read LEMMA information
110my $_INLINE_ATT_RD = "ana"; # from which attribute to read POS information (and evtl. additional MSD - Morphosyntactic Descriptions)
111 # TODO: The format for the POS and MSD information has to suffice the regular expression ([^ ]+)( (.+))?
112 # - which means, that the POS information can be followed by an optional blank with additional
113 # MSD information; unlike the MSD part, the POS part may not contain any blanks.
114my $_INLINE_POS_WR = "pos"; # name (inside $_tokens_file) referring to POS information
115my $_INLINE_MSD_WR = "msd"; # name (inside $_tokens_file) referring to MSD information
116my $_INLINE_LEM_WR = "lemma"; # name (inside $_tokens_file) referring to LEMMA information
117##
118
119
120#
121# ~~~ variables ~~~
122#
123
Akron85717512020-07-08 11:19:19 +0200124# Initialize zipper
125my $zipper = KorAP::XML::TEI::Zipper->new;
Peter Harders6f526a32020-06-29 21:44:41 +0200126my $input_fh; # input file handle (default: stdin)
127
128my $buf_in; # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader
129my $data; # contains the primary text (created by func. 'retr_info' from $buf_in), which is written to '$data_file'
130
131my $dir; # text directory (below $_root_dir)
132my $dir_crp; # corpus directory (below $_root_dir)
133my $dir_doc; # document directory (below $_root_dir)
134
135my ( $text_id, $text_id_esc ); # '$text_id_esc' = escaped version of $text_id (see %ent)
136
137my %ent = ('"', '&quot;', '&','&amp;', # convert '&', '<' and '>' into their corresponding sgml-entities
138 '<','&lt;','>','&gt;');
139 # note: the index still refers to the 'single character'-versions, which are counted as 1
140 # (search for '&amp;' in data.xml and see corresponding indices in $_tokens_file)
141
142my $header_txt; # raw text header (written to '$_root_dir$dir/$_header_file')
143my $header_doc; # raw document header (written to '$_root_dir$dir_doc/$_header_file')
144my $header_crp; # raw corpus header (written to '$_root_dir$dir_crp/$_header_file')
145
146my ( $header_fl_crp, $header_fl_doc, # flags for tracking where we are in the input document
147 $header_fl_txt, $data_fl );
148
149my ( $header_prfx, $data_prfx1, # $header_prfx is written to $_header_file, $data_* are written to $_data_file
150 $data_prfx2, $data_sfx );
151
152my @structures; # list of arrays, where each array represents a TEI I5 tag (except $_TOKENS_TAG) from the input document
153 # - the input of this array is written in func. 'write_structures' into the file '$_structure_file'
154
155my @tokens; # list of arrays, where each array represents a $_TOKENS_TAG from the input document
156 # - the input of this array is written in func. 'write_tokens' into the file '$_tokens_file'
157
158my ( $ref, $idx, $att_idx ); # needed in func. 'write_structures' and 'write_tokens'
159
160my ( $reader, # instance of 'XML::LibXML::Reader->new' (on input '$buf_in')
161 $tree_data ); # instance of 'XML::CompactTree::XS::readSubtreeToPerl' (on input '$reader')
162
163# these are only used inside recursive function 'retr_info'
164my ( $_IDX, # value is set dependent on $_XCT_LN - for extracting array of child elements from element in $tree_data
165 $e, # element from $tree_data
166 $n, # tag name of actual processed element $e
167 $rl, # recursion level
168 $dl, # actual length of string $data
169 @oti, # oti='open tags indizes' - a stack of indizes into @structures, where the top index in @oti
170 # represents the actual processed element from @structures
171 @oti2, # analogously to @oti, but with reference to array @tokens
172 $inside_tokens_tag, # flag is set, when inside $_TOKENS_TAG
173 ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags)
174 $add_one, # ...
175 $fval, $fval2, # ...
176 %ws); # hash for indices of whitespace nodes (needed to recorrect from-values)
177 # idea: when closing element, check if it's from-index minus 1 refers to a whitespace node
178 # (means: 'from-index - 1' is a key in %ws).
179 # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1
180
181my $output; # temporary variable needed in 'write_*'-functions for writing output to zip-stream $zip)
182
183my ( $i, $c ); # index variables used in loops
184
185## DEPRECATED (only IDS-intern)
186my $_tok_file_bas = "tokens.xml";
187##
188
189my ( $_CORP_HEADER_END, $_DOC_HEADER_END, $_TEXT_HEADER_END );
190
191
192#
193# ~~~ main ~~~
194#
195
196# ~ initializations ~
197
198($_XCT_LN)?($_IDX=5):($_IDX=4);
199
200$header_prfx = $data_prfx1 = $data_prfx2 = $data_sfx = "";
201
202$header_fl_txt = $header_fl_doc = $header_fl_crp = 0;
203
204$inside_tokens_tag = -1;
205
206$fval = $fval2 = 0;
207
208$_root_dir .= '/'; # base dir must always end with a slash
209$_root_dir =~ s/^\.?\///; # remove leading / (only relative paths allowed in IO::Compress::Zip) and redundant ./
210
211$_CORP_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_CORP_HEADER_END = $1;
212$_DOC_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_DOC_HEADER_END = $1;
213$_TEXT_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_TEXT_HEADER_END = $1;
214
215## TODO: remove this, because it's IDS-specific
Peter Hardersd892a582020-02-12 15:45:22 +0100216$header_prfx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
217$header_prfx .= "<?xml-model href=\"header.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n";
218$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 +0200219##
Peter Hardersd892a582020-02-12 15:45:22 +0100220
221$data_prfx1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
222$data_prfx1 .= "<?xml-model href=\"text.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n";
223$data_prfx1 .= "<raw_text docid=\"";
224$data_prfx2 .= "\" xmlns=\"http://ids-mannheim.de/ns/KorAP\">\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200225## TODO: can 'metadata.xml' change or is it constant?
Peter Hardersd892a582020-02-12 15:45:22 +0100226$data_prfx2 .= " <metadata file=\"metadata.xml\" />\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200227##
Peter Hardersd892a582020-02-12 15:45:22 +0100228$data_prfx2 .= " <text>";
229$data_sfx = "</text>\n</raw_text>";
230
Peter Hardersd892a582020-02-12 15:45:22 +0100231
Peter Harders6f526a32020-06-29 21:44:41 +0200232## DEPRECATED (only IDS-intern)
233startTokenizer() if $_GEN_TOK_BAS;
234##
Peter Hardersd892a582020-02-12 15:45:22 +0100235
Peter Harders6f526a32020-06-29 21:44:41 +0200236# ~ read input and write output (text by text) ~
Peter Harders90157342020-07-01 21:05:14 +0200237process();
Peter Hardersd892a582020-02-12 15:45:22 +0100238
Peter Hardersd892a582020-02-12 15:45:22 +0100239
Peter Harders6f526a32020-06-29 21:44:41 +0200240#
241# ~~~ subs ~~~
242#
Peter Hardersd892a582020-02-12 15:45:22 +0100243
Peter Hardersd892a582020-02-12 15:45:22 +0100244
Peter Harders90157342020-07-01 21:05:14 +0200245sub process {
Peter Hardersd892a582020-02-12 15:45:22 +0100246
Peter Harders6f526a32020-06-29 21:44:41 +0200247 my ( $pfx, $sfx );
Peter Hardersd892a582020-02-12 15:45:22 +0100248
Peter Harders6f526a32020-06-29 21:44:41 +0200249 my $lc = 0; # line counter
250
251 my $tc = 0; # text counter
252
253 $input_fh = *STDIN; # input file handle (default: stdin)
254
Akron85717512020-07-08 11:19:19 +0200255 $data_fl = 0;
Peter Harders6f526a32020-06-29 21:44:41 +0200256
257 $buf_in = $data = $dir = $dir_doc = $dir_crp = "";
258 $header_txt = $header_doc = $header_crp = "";
259
260
261 if ( $input_fname ne '' ){
262
263 open ( $input_fh, "<", "$input_fname") || die "File \'$input_fname\' could not be opened.\n";
264
265 }
266
267
Peter Harders90157342020-07-01 21:05:14 +0200268 # prevents segfaulting of 'XML::LibXML::Reader' inside 'process()' - see notes on 'PerlIO layers' in 'man XML::LibXML')
269 # removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
270 binmode $input_fh;
271
272
Peter Harders6f526a32020-06-29 21:44:41 +0200273 # ~ loop (reading input document) ~
274
275 while ( <$input_fh> ){
276
277 $lc++; # line counter
278
279 # TODO: yet not tested fo big amounts of data
280 # must-have, otherwise comments in input could be fatal (e.g.: ...<!--\n<idsHeader...\n-->...)
Akron4f67cd42020-07-02 12:27:58 +0200281 KorAP::XML::TEI::delHTMLcom ( $input_fh, $_ ); # remove HTML comments (<!--...-->)
Peter Harders6f526a32020-06-29 21:44:41 +0200282
283 if ( $data_fl && m#^(.*)</${_TEXT_BODY}>(.*)$# ){
284
285
286 # ~ end of text body ~
287
288
289 # write data.xml, structure.xml and evtl. morpho.xml and/or the dummy tokenization files (s.a.: $_tok_file_con and $_tok_file_agg)
290
291 $pfx = $1; $sfx = $2;
292
293 die "ERROR ($0): main(): input line number $lc: line with closing text-body tag '${_TEXT_BODY}'"
294 ." contains additional information ... => Aborting\n\tline=$_"
295 if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/;
296
297 if ( $dir ne "" ){
298
Peter Harders6f526a32020-06-29 21:44:41 +0200299 $reader = XML::LibXML::Reader->new( string => "<text>$buf_in</text>", huge => 1 );
Peter Harders6f526a32020-06-29 21:44:41 +0200300
301 if ( $_XCT_LN ){ # _XCT_LINE_NUMBERS is only for debugging
302 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY | XCT_LINE_NUMBERS );
303 } else {
304 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY );
305 }
306
307 @structures = (); @oti = ();
308
309 if ( $_TOKENS_PROC ){
310 @tokens = (); @oti2 = ()
311 }
312
313 $dl = $rl = 0;
314
315 # ~ whitespace related issue ~
316 $add_one = 0;
317 %ws = ();
318
319
320 # ~ recursion ~
321
322 retr_info( \$tree_data->[2] ); # parse input data
323
324 $rl--;
325
326
327 # ~ write data.xml ~
328
329 $data =~ tr/\n\r/ /; # note: 2 blanks - otherwise offset data would become corrupt
330
331 $data = encode_utf8( $data );
332
333 ## DEPRECATED (only IDS-intern)
334 # first write it to tokenization pipe to give it some time
335 if ( $_GEN_TOK_BAS ){
336 print $chld_in "$data\n\x03\n";
337 }
338 ##
339
340 print STDERR "DEBUG ($0): main(): Writing (utf8-formatted) xml file $_root_dir$dir/$_data_file\n" if $_DEBUG;
341
Peter Harders6f526a32020-06-29 21:44:41 +0200342
343 $data =~ s/(&|<|>)/$ent{$1}/g;
344
Akron85717512020-07-08 11:19:19 +0200345 $zipper->new_stream("$_root_dir$dir/$_data_file")
346 ->print("$data_prfx1$text_id_esc$data_prfx2$data$data_sfx");
Peter Harders6f526a32020-06-29 21:44:41 +0200347
348 # ~ write structures ~
349
350 write_structures() if @structures;
351
352
353 # ~ write tokens ~
354
355 write_tokens() if $_TOKENS_PROC && @tokens;
356
357
358 # ~ dummy tokenization ~
359
360 if ( $_GEN_TOK_BAS || $_GEN_TOK_DUMMY ){ ## DEPRECATED ($_GEN_TOK_BAS: only IDS-intern)
361
362 select_tokenization();
363
364 if ( $_GEN_TOK_DUMMY ){
365 $offset = 0; @tok_tokens_con=(); @tok_tokens_agg=();
366 }
367 }
368
369 $data_fl = 0; $buf_in = $data = $dir = ""; # reinit.
370
371 } else { # $dir eq ""
372
373 print STDERR "WARNING ($0): main(): maybe empty textSigle => skipping this text ...\n";
374 print STDERR "WARNING ($0): main(): text header=$header_txt\n";
375 print STDERR "WARNING ($0): main(): data=$data\n";
Peter Hardersd892a582020-02-12 15:45:22 +0100376 }
377
Peter Harders6f526a32020-06-29 21:44:41 +0200378 } elsif ( $data_fl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100379
Peter Hardersd892a582020-02-12 15:45:22 +0100380
Peter Harders6f526a32020-06-29 21:44:41 +0200381 # ~ inside text body ~
Peter Hardersd892a582020-02-12 15:45:22 +0100382
Peter Hardersd892a582020-02-12 15:45:22 +0100383
Peter Harders6f526a32020-06-29 21:44:41 +0200384 #print STDERR "inside text body (\$data_fl set)\n";
385
386 # ~ whitespace handling ~
387
388 # remove consecutive whitespace at beginning and end (mostly one newline)
389 # to let 'XML::CompactTree::XS' recognize these blanks as 'text-nodes', the option 'XCT_IGNORE_WS' may not be used (see above).
390 s/^\s+//; s/\s+$//;
391
392 # 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),
393 # because it helps for better readability of the text in the '$_data_file' (e.g.: assure blanks between sentences).
394 # Furthermore, the input lines should avoid primary text tokens, which span across several lines, unless the line breaks doesn't lead
395 # to a situation which produces unwanted blanks - e.g.: '...<w>end</w>\n<w>.</w>...' would lead to '...<w>end</w> <w>.</w>...', or
396 # '...<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
397 # to correct those unwanted effects, there would be lots of examples aside punctuation, where there would not exist an easy way or unarbitrary
398 # solution regarding the elimination of the false blanks.
Peter Hardersd892a582020-02-12 15:45:22 +0100399 #
Peter Harders6f526a32020-06-29 21:44:41 +0200400 # 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
401 # (see also comments on 'input restrictions' at the top of this script).
Peter Hardersd892a582020-02-12 15:45:22 +0100402
Peter Harders6f526a32020-06-29 21:44:41 +0200403 if ( m/<[^>]+>[^<]/ ){ # line contains at least one tag with at least one character contents
Peter Hardersd892a582020-02-12 15:45:22 +0100404
Peter Harders6f526a32020-06-29 21:44:41 +0200405 $tc++; # text counter
Peter Hardersd892a582020-02-12 15:45:22 +0100406
Peter Harders6f526a32020-06-29 21:44:41 +0200407 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 +0100408 }
409
Peter Harders6f526a32020-06-29 21:44:41 +0200410 # add line to buffer
411 $buf_in .= $_;
412
413 } elsif ( $header_fl_txt && m#^(.*</${_TEXT_HEADER_END}>)(.*)$# ){
414
415
416 # ~ end of text header ~
417
418
419 #print STDERR "end of text header\n";
420
421 # write it to header.xml
422
423 $sfx = $2;
424
425 $header_txt .= $1; $header_fl_txt = 0;
426
427
428 die "ERROR ($0): main(): input line number $lc: line with closing text-header tag '${_TEXT_HEADER_END}'"
429 ." contains additional information ... => Aborting\n\tline=$_"
430 if $sfx !~ /^\s*$/;
431
432 if ( $dir eq "" ){
433
434 print STDERR "WARNING ($0): main(): input line number $lc: empty textSigle in text header => nothing to do ...\ntext header=$header_txt\n";
435
436 } else {
437
438 print STDERR "DEBUG ($0): Writing file $_root_dir$dir/$_header_file\n" if $_DEBUG;
439
Peter Harders6f526a32020-06-29 21:44:41 +0200440 $header_txt = encode_utf8( $header_txt );
441
Akron85717512020-07-08 11:19:19 +0200442 $zipper->new_stream("$_root_dir$dir/$_header_file")
443 ->print("$header_prfx$header_txt");
Peter Harders6f526a32020-06-29 21:44:41 +0200444
445 $header_txt = "";
Peter Hardersd892a582020-02-12 15:45:22 +0100446 }
Peter Hardersd892a582020-02-12 15:45:22 +0100447
Peter Harders6f526a32020-06-29 21:44:41 +0200448 } elsif ( $header_fl_txt ){
Peter Hardersd892a582020-02-12 15:45:22 +0100449
Peter Harders6f526a32020-06-29 21:44:41 +0200450 # ~ inside text header ~
Peter Hardersd892a582020-02-12 15:45:22 +0100451
Peter Harders6f526a32020-06-29 21:44:41 +0200452
453 #print STDERR "inside text header\n";
454
455 if( m#^(.*)<${_TEXT_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
456
457 $pfx = $1; $sfx = $3;
458
459 $dir = $2; $text_id = $dir;
460
461 $text_id =~ tr/\//_/; $dir =~ s/("|&|<|>)/$ent{$1}/g;
462
463 $text_id = encode_utf8( $text_id );
464
465 die "ERROR ($0): main(): input line number $lc: line with text-sigle tag '$_TEXT_SIGLE' is not in expected format ... => Aborting\n\tline=$_"
466 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_TEXT_SIGLE}>\s*$# || $dir =~ /^\s*$/;
467
468 # log output for seeing progression
469 print STDERR "$0: main(): text_id=".decode_utf8( $text_id )."\n";
470
471 $text_id_esc = $text_id;
472
473 s#(<${_TEXT_SIGLE}(?: [^>]*)?>)[^<]+(</${_TEXT_SIGLE}>)#$1$dir$2# # to be consistent with escaping, escape also textSigle in text-header
474 if $text_id_esc =~ s/("|&|<|>)/$ent{$1}/g;
475
476 $dir =~ tr/\./\//;
477 }
478
479 $header_txt .= $_;
480
481 } elsif ( $header_fl_doc && m#^(.*</${_DOC_HEADER_END}>)(.*)$# ){
482
483
484 # ~ end of document header ~
485
Peter Harders6f526a32020-06-29 21:44:41 +0200486 #print STDERR "end of doc header\n";
487
488 # write it to header.xml
489
490 $sfx = $2;
491
492 $header_doc .= $1; $header_fl_doc = 0;
493
494 die "ERROR ($0): main(): input line number $lc: line with closing document-header tag '${_DOC_HEADER_END}'"
495 ." contains additional information ... => Aborting\n\tline=$_"
496 if $sfx !~ /^\s*$/;
497
498 if( $dir_doc eq "" ){
499
500 print STDERR "WARNING ($0): main(): input line number $lc: empty document sigle in document header"
501 ." => nothing to do ...\ndocument header=$header_doc\n";
502
503 } else {
504
505 print STDERR "DEBUG ($0): Writing file $_root_dir$dir_doc/$_header_file\n" if $_DEBUG;
506
Peter Harders6f526a32020-06-29 21:44:41 +0200507 $header_doc = encode_utf8( $header_doc );
508
Akron85717512020-07-08 11:19:19 +0200509 $zipper->new_stream("$_root_dir$dir_doc/$_header_file")
510 ->print("$header_prfx$header_doc");
Peter Harders6f526a32020-06-29 21:44:41 +0200511
512 $header_doc = $dir_doc = "";
513 }
514
515 } elsif ( $header_fl_doc ){
516
517
518 # ~ inside document header ~
519
520
521 #print STDERR "inside doc header\n";
522
523 if ( m#^(.*)<${_DOC_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
524
525 $pfx = $1; $sfx = $3;
526
527 $dir_doc = $2;
528
529 die "ERROR ($0): main(): input line number $lc: line with document-sigle tag '$_DOC_SIGLE' is not in expected format ... => Aborting\n\tline=$_"
530 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_DOC_SIGLE}>\s*$# || $dir_doc =~ /^\s*$/;
531
532 s#(<${_DOC_SIGLE}(?: [^>]*)?>)[^<]+(</${_DOC_SIGLE}>)#$1$dir_doc$2# # to be consistent with escaping, escape also textSigle in Document-Header
533 if $dir_doc =~ s/("|&|<|>)/$ent{$1}/g;
534 }
535
536 $header_doc .= $_;
537
538 } elsif ( m#^(.*)(<${_TEXT_HEADER_BEG}.*)$# ){
539
540 # ~ start of text header ~
541
542
543 #print STDERR "begin of text header\n";
544
545 $header_txt = $_; $header_fl_txt = 1; $pfx = $1;
546
547 $tc = 0; # reset (needed for ~ whitespace handling ~)
548
549 die "ERROR ($0): main(): input line number $lc: line with opening text-header tag '${_TEXT_HEADER_BEG}'"
550 ." is not in expected format ... => Aborting\n\tline=$_"
551 if $pfx !~ /^\s*$/;
552
553 } elsif ( m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){
554
555
556 # ~ start of text body ~
557
558
559 #print STDERR "inside text body\n";
560
561 $pfx = $1; $sfx = $2;
562
563 $data_fl = 1;
564
565 die "ERROR ($0): main(): input line number $lc: line with opening text-body tag '${_TEXT_BODY}'"
566 ." contains additional information ... => Aborting\n\tline=$_"
567 if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/;
568
569 } elsif ( m#^(.*)(<${_DOC_HEADER_BEG}.*)$# ){
570
571
572 # ~ start of document header ~
573
574
575 #print STDERR "begin of doc header\n";
576
577 $header_doc = "$2\n"; $header_fl_doc = 1; $pfx = $1;
578
579 die "ERROR ($0): main(): input line number $lc: line with opening document-header tag '${_DOC_HEADER_BEG}'"
580 ."is not in expected format ... => Aborting\n\tline=$_"
581 if $pfx !~ /^\s*$/;
582
583 } elsif ( $header_fl_crp && m#^(.*</${_CORP_HEADER_END}>)(.*)$# ){
584
585
586 # ~ end of corpus header ~
587
588
589 #print STDERR "end of corp header\n";
590
591 $sfx = $2;
592
593 $header_crp .= $1; $header_fl_crp = 0;
594
595 die "ERROR ($0): main(): input line number $lc: line with closing corpus-header tag '${_CORP_HEADER_END}'"
596 ." contains additional information ... => Aborting\n\tline=$_"
597 if $sfx !~ /^\s*$/;
598
599 if ( $dir_crp eq "" ){
600
601 print STDERR "WARNING ($0): main(): input line number $lc: empty corpus sigle in corpus header => nothing to do ...\ncorpus header=$header_crp\n";
602
603 } else {
604
605 print STDERR "DEBUG ($0): Writing file $_root_dir$dir_crp/$_header_file\n" if $_DEBUG;
606
Peter Harders6f526a32020-06-29 21:44:41 +0200607 $header_crp = encode_utf8( $header_crp );
608
Akron85717512020-07-08 11:19:19 +0200609 $zipper->new_stream("$_root_dir$dir_crp/$_header_file")
610 ->print("$header_prfx$header_crp");
Peter Harders6f526a32020-06-29 21:44:41 +0200611
612 $header_crp = $dir_crp = "";
613 }
614
615 } elsif ( $header_fl_crp ){
616
617
618 # ~ inside corpus header ~
619
620
621 #print STDERR "inside corp header\n";
622
623 if ( m#^(.*)<${_CORP_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
624
625 $pfx = $1; $sfx = $3;
626
627 $dir_crp = $2;
628
629 die "ERROR ($0): main(): input line number $lc: line with korpusSigle-tag is not in expected format ... => Aborting\n\tline=$_"
630 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_CORP_SIGLE}>\s*$# || $dir_crp =~ /^\s*$/;
631
632 if ( $dir_crp =~ s/("|&|<|>)/$ent{$1}/g ){
633
634 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 +0100635 }
636 }
637
Peter Harders6f526a32020-06-29 21:44:41 +0200638 $header_crp .= $_;
Peter Hardersd892a582020-02-12 15:45:22 +0100639
Peter Harders6f526a32020-06-29 21:44:41 +0200640 } elsif ( m#^(.*)(<${_CORP_HEADER_BEG}.*)$# ){
Peter Hardersd892a582020-02-12 15:45:22 +0100641
Peter Hardersd892a582020-02-12 15:45:22 +0100642
Peter Harders6f526a32020-06-29 21:44:41 +0200643 # ~ start of corpus header ~
Peter Hardersd892a582020-02-12 15:45:22 +0100644
Peter Harders6f526a32020-06-29 21:44:41 +0200645
646 #print STDERR "begin of corp header\n";
647
648 $header_crp = $2; $header_fl_crp = 1; $pfx = $1;
649
650 die "ERROR ($0): main(): input line number $lc: line with opening corpus-header tag '${_CORP_HEADER_BEG}'"
651 ." is not in expected format ... => Aborting\n\tline=$_"
652 if $pfx !~ /^\s*$/;
Peter Hardersd892a582020-02-12 15:45:22 +0100653 }
654
Peter Harders6f526a32020-06-29 21:44:41 +0200655 } #end: while
Peter Hardersd892a582020-02-12 15:45:22 +0100656
Akron85717512020-07-08 11:19:19 +0200657 $zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200658
659 ## DEPRECATED (only IDS-intern)
660 if( $_GEN_TOK_BAS ){
661 close($chld_in);
662 close($chld_out);
Peter Hardersd892a582020-02-12 15:45:22 +0100663 }
Peter Harders6f526a32020-06-29 21:44:41 +0200664 ##
Peter Hardersd892a582020-02-12 15:45:22 +0100665
Peter Harders90157342020-07-01 21:05:14 +0200666} # end: sub process
Peter Hardersd892a582020-02-12 15:45:22 +0100667
Peter Hardersd892a582020-02-12 15:45:22 +0100668
Peter Harders6f526a32020-06-29 21:44:41 +0200669sub retr_info { # called from process()
Peter Hardersd892a582020-02-12 15:45:22 +0100670
Peter Harders6f526a32020-06-29 21:44:41 +0200671 # EXAMPLE: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>
672 #
673 # print out values of above example:
674 # 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]'
675 #
676 # $data = reference to below array
677 #
678 # [ 0: XML_READER_TYPE_DOCUMENT,
679 # 1: ?
680 # 2: [ 0: [ 0: XML_READER_TYPE_ELEMENT <- start recursion with array '$data->[2]' (see process(): retr_info( \$tree_data->[2] ))
681 # 1: 'node'
682 # 2: ?
683 # 3: HASH (attributes)
684 # 4: 1 (line number)
685 # 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT
686 # 1: 'node1'
687 # 2: ?
688 # 3: undefined (no attributes)
689 # 4: 1 (line number)
690 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
691 # 1: 'some '
692 # ]
693 # 1: [ 0: XML_READER_TYPE_ELEMENT
694 # 1: 'n'
695 # 2: ?
696 # 3: undefined (no attributes)
697 # 4: 1 (line number)
698 # 5: undefined (no child nodes)
699 # ]
700 # 2: [ 0: XML_READER_TYPE_TEXT
701 # 1: ' text'
702 # ]
703 # ]
704 # ]
705 # 1: [ 0: XML_READER_TYPE_ELEMENT
706 # 1: 'node2'
707 # 2: ?
708 # 3: undefined (not attributes)
709 # 4: 1 (line number)
710 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
711 # 1: 'more-text'
712 # ]
713 # ]
714 # ]
715 # ]
716 # ]
717 # ]
718 # ]
719 #
720 # $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT)
721 #
722 # ref($data->[2]) == ARRAY (with 1 element for 'node')
723 # ref($data->[2]->[0]) == ARRAY (with 6 elements)
724 #
725 # $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
726 # $data->[2]->[0]->[1] == 'node'
727 # ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v')
728 # $data->[2]->[0]->[4] == 1 (line number)
729 # ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2')
730 # # child nodes of actual node (see $_IDX)
731 #
732 # ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements)
733 # $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
734 # $data->[2]->[0]->[5]->[0]->[1] == 'node1'
735 # $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute)
736 # $data->[2]->[0]->[5]->[0]->[4] == 1 (line number)
737 # ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text')
738 #
739 # ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements)
740 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
741 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some '
742 #
743 # ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements)
744 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
745 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n'
746 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute)
747 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number)
748 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child nodes)
749 #
750 # ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements)
751 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
752 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text'
753 #
754 #
755 # retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example.
756 # Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to
757 # ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]).
Peter Hardersd892a582020-02-12 15:45:22 +0100758
Peter Harders6f526a32020-06-29 21:44:41 +0200759 $rl++; # recursion level (1 = topmost level inside retr_info() = should always be level of tag $_TEXT_BODY)
Peter Hardersd892a582020-02-12 15:45:22 +0100760
Peter Hardersd892a582020-02-12 15:45:22 +0100761
Peter Harders6f526a32020-06-29 21:44:41 +0200762 foreach $e ( @{${$_[0]}} ){ # iteration through all array elements ($_[0] is a reference to an array reference)
Peter Hardersd892a582020-02-12 15:45:22 +0100763
Peter Hardersd892a582020-02-12 15:45:22 +0100764
Peter Harders6f526a32020-06-29 21:44:41 +0200765 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 +0100766
Peter Hardersd892a582020-02-12 15:45:22 +0100767
Peter Harders6f526a32020-06-29 21:44:41 +0200768 #~~~~
769 # from here: opening tag
770 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100771
Peter Hardersd892a582020-02-12 15:45:22 +0100772
Peter Harders6f526a32020-06-29 21:44:41 +0200773 # insert new array (for new tag) into @structures with tag-name and tag-attributes (if present)
774 # 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 +0100775
Peter Harders6f526a32020-06-29 21:44:41 +0200776 # ~ tag name ~
Peter Hardersd892a582020-02-12 15:45:22 +0100777
Peter Harders6f526a32020-06-29 21:44:41 +0200778 $n = $e->[1];
Peter Hardersd892a582020-02-12 15:45:22 +0100779
Peter Hardersd892a582020-02-12 15:45:22 +0100780
Peter Harders6f526a32020-06-29 21:44:41 +0200781 # ~ handle structures ~
Peter Hardersd892a582020-02-12 15:45:22 +0100782
Peter Harders6f526a32020-06-29 21:44:41 +0200783 my @array;
784 push @array, $n;
785 push @structures, \@array;
786 push @oti, $#structures; # add highest index of @structures to @oti
Peter Hardersd892a582020-02-12 15:45:22 +0100787
Peter Hardersd892a582020-02-12 15:45:22 +0100788
Peter Harders6f526a32020-06-29 21:44:41 +0200789 # ~ handle tokens ~
Peter Hardersd892a582020-02-12 15:45:22 +0100790
Peter Harders6f526a32020-06-29 21:44:41 +0200791 $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 +0100792
Peter Harders6f526a32020-06-29 21:44:41 +0200793 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100794
Peter Harders6f526a32020-06-29 21:44:41 +0200795 my @array2;
796 push @array2, $n;
797 push @tokens, \@array2;
798 push @oti2, $#tokens;
799 }
Peter Hardersd892a582020-02-12 15:45:22 +0100800
Peter Hardersd892a582020-02-12 15:45:22 +0100801
Peter Harders6f526a32020-06-29 21:44:41 +0200802 # ~ handle attributes ~
Peter Hardersd892a582020-02-12 15:45:22 +0100803
Peter Harders6f526a32020-06-29 21:44:41 +0200804 if ( defined $e->[3] ){ # only if attributes exist
Peter Hardersd892a582020-02-12 15:45:22 +0100805
Peter Harders6f526a32020-06-29 21:44:41 +0200806 for ( $c = 0; $c < @{$e->[3]}; $c += 2 ){ # with 'XCT_ATTRIBUTE_ARRAY', $node->[3] is an array reference of the form
807 # [ name1, value1, name2, value2, ....] of attribute names and corresponding values.
808 # note: arrays are faster (see: http://makepp.sourceforge.net/2.0/perl_performance.html)
Peter Hardersd892a582020-02-12 15:45:22 +0100809
Peter Harders6f526a32020-06-29 21:44:41 +0200810 # '$c' references the 'key' and '$c+1' the 'value'
811 push @{$structures[$#structures]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1];
Peter Hardersd892a582020-02-12 15:45:22 +0100812
Peter Harders6f526a32020-06-29 21:44:41 +0200813 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100814
Peter Harders6f526a32020-06-29 21:44:41 +0200815 push @{$tokens[$#tokens]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1];
816 }
817
818 }
819 }
820
821
822 # ~ index 'from' ~
823
824 # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts
825
826 push @{$structures[$#structures]}, ( $dl + $add_one ); # see below (text and whitespace nodes) for explanation on '$add_one'
827
828 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
829
830 push @{$tokens[$#tokens]}, ( $dl + $add_one );
831 }
832
833
834 #~~~~
835 # until here: opening tag
836 #~~~~
837
838
839 # ~~ RECURSION ~~
840
841 if ( defined $e->[$_IDX] ){ # do no recursion, if $e->[$_IDX] is not defined (because we have no array of child nodes, e.g.: <back/>)
842
843 retr_info( \$e->[$_IDX] ); # recursion with array of child nodes
844
845 $rl--; # return from recursion
846 }
847
848
849 #~~~~~
850 # from here: closing tag
851 #~~~~~
852
853
854 # ~ handle structures ~
855
856 {
857 my $ix = pop @oti; # index of just closed tag
858
859 my $aix = $#{$structures[$ix]}; # determine highest index from 'array referring to last closed tag' ...
860
861 $fval = ${$structures[$ix]}[ $aix ]; # ... and get it's from-value
862
863 if ( $fval > 0 && not exists $ws{ $fval - 1 } ){ # ~ whitespace related issue ~
864
865 # previous node was a text-node
866
867 ${$structures[$ix]}[ $aix ] = $fval - 1; # recorrect from-value (see below: notes on ~ whitespace related issue ~)
868 }
869
870 # in case this fails, check input
871 die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@structures: from-value ($fval) is 2 or more greater"
872 ." than to-value ($dl) => please check. aborting ...\n"
873 if ( $fval - 1 ) > $dl;
874
875 # TODO: construct example for which this case applies
876 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
877 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval - 1) >= $dl;
878 ${$structures[$ix]}[ $aix ] = $dl if $fval == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl)
879
880 push @{$structures[$ix]}, $dl, $rl; # to-value and recursion-level
881
882 # note: use $dl, because the offsets are _between_ the characters (e.g.: word = 'Hello' => from = 0 (before 'H'), to = 5 (after 'o'))
883 }
884
885
886 # ~ handle tokens ~
887
888
889 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
890
891 my $ix = pop @oti2;
892
893 my $aix = $#{$tokens[$ix]};
894
895 $fval2 = ${$tokens[$ix]}[ $aix ]; # from-value
896
897 if( $fval2 > 0 && not exists $ws{ $fval2 - 1 } ){ # ~ whitespace related issue ~
898
899 # previous node was a text-node
900
901 ${$tokens[$ix]}[ $aix ] = $fval2 - 1; # recorrect from-value
902 }
903
904 # in case this fails, check input
905 die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@tokens: from-value ($fval2) is 2 or more greater"
906 ." than to-value ($dl) => please check. aborting ...\n"
907 if ( $fval2 - 1 ) > $dl;
908
909 # TODO: construct example for which this case applies
910 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
911 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval2 - 1) >= $dl;
912 ${$tokens[$ix]}[ $aix ] = $dl if $fval2 == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl)
913
914 push @{$tokens[$ix]}, $dl, $rl; # to-value and recursion-level
915
916 $inside_tokens_tag = -1; # reset
917 }
918
919 # ~ whitespace related issue ~
Peter Hardersd892a582020-02-12 15:45:22 +0100920 # clean up
Peter Harders6f526a32020-06-29 21:44:41 +0200921 delete $ws{ $fval - 1 } if $fval > 0 && exists $ws{ $fval - 1 };
922 delete $ws{ $fval2 - 1 } if $_TOKENS_PROC && $fval2 > 0 && exists $ws{ $fval2 - 1 };
Peter Hardersd892a582020-02-12 15:45:22 +0100923
924
Peter Harders6f526a32020-06-29 21:44:41 +0200925 #~~~~~
926 # from here: text (and whitespace) nodes
927 #~~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100928
929
Peter Harders6f526a32020-06-29 21:44:41 +0200930 # the 3rd form of nodes, next to text-nodes (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes
931 # of the type 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'
932 #
933 # when modifiying the above example (at the top of this sub) by inserting an additional blank between '</node1>' and '<node2>',
934 # the output for '$data->[2]->[0]->[5]->[1]->[1]' becomes a blank (' ') and it's type is '14' (see manpage of XML::LibXML::Reader):
935 #
936 # 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 +0100937
Peter Harders6f526a32020-06-29 21:44:41 +0200938 } elsif ( $e->[0] == XML_READER_TYPE_TEXT || $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){
Peter Hardersd892a582020-02-12 15:45:22 +0100939
Peter Harders6f526a32020-06-29 21:44:41 +0200940 # notes on ~ whitespace related issue ~ (see below source code)
941 #
942 # example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...'
943 #
944 # Two text-nodes should normally be separated by a blank. In the above example, that would be the 2 text-nodes
945 # 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' '.
946 #
947 # Assumed, that the above example marks the general case, then the text-node 'Campagne in Frankreich' leads to the
948 # setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag and setting it's from-index, it gets the right
949 # offset, which is the start-index of '1792'.
950 #
951 # To check, that the above consideration holds, we save the from-index of a read whitespace-node into the hash %ws.
952 # By this, it can be checked when closing a tag, if the 'non-tag'-node (text or whitespace) before the last 'non-tag'-
953 # node was actually a whitespace-node ($ws{ $fval - 1 }).
954 #
955 # For whitespace-nodes, also $add_one has to be set to 0, so when opening the next tag (in the above example the 2nd
956 # 's'-tag), no additional 1 is added, because this was already done by the whitespace-node itself (by incrementing the
957 # variable $dl).
958 #
959 # Now, what happens, when 2 text-nodes are not seperated by a whitespace-node (blank)? (e.g.: <w>Augen<c>,</c></w>)
960 #
961 # In this case, the falsely increased from-value has to be decreased again by 1 when closing the referring tag
962 # (...$fval - 1; # recorrect).
963 #
964 # 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
965 # sense, because of '<w> </w>'), in both the ' ' is handled as a whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE).
966 #
967 # So the from-index of the 2nd w-tag (in the second example) would refer to 'bar', which may not have been the intention
968 # (even, if '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug, which needs to be fixed?
969 #
970 # 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-
971 # and to-indizes for the tags 'a' and 'b' are both 9, which is the start-index of the token 'tok3'.
972
973 if( $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){
974
975 # ~ whitespace related issue ~
976
977 $add_one = 0;
978
979 $ws{ $dl }++; # '++' does not mean a thing here (could be used for consistency checking)
980
981 }else{
982
983 # ~ text-node ~
984
985 $add_one = 1;
986 }
987
988
989 # ~ update $data and $dl ~
990
991 $data .= $e->[1];
992
993 $dl += length( $e->[1] ); # update length of $data
994
995
996
997 #~~~~~
998 # from here (until end): dummy tokenization
999 #~~~~~
1000
1001 if ( $_GEN_TOK_DUMMY ){
1002
1003 $txt = $e->[1];
1004
Akroneac374d2020-07-07 09:00:44 +02001005
Peter Harders6f526a32020-06-29 21:44:41 +02001006 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 ' '
1007
Akroneac374d2020-07-07 09:00:44 +02001008 my $tok = KorAP::XML::TEI::Tokenization::conservative($txt, $offset);
1009 push @tok_tokens_con, @$tok;
Peter Harders6f526a32020-06-29 21:44:41 +02001010
Akroneac374d2020-07-07 09:00:44 +02001011 $tok = KorAP::XML::TEI::Tokenization::aggressive($txt, $offset);
1012 push @tok_tokens_agg, @$tok;
Peter Harders6f526a32020-06-29 21:44:41 +02001013
1014 ##$offset = $dl+1;
1015
1016 $offset = $dl;
1017
1018 } # fi
1019
1020 } # fi: $_GEN_TOK_DUMMY
1021
1022
1023 #elsif ( $e->[0] == XML_READER_TYPE_ATTRIBUTE ) # attribute node
1024 # note: attributes cannot be processed like this ( => use 'XCT_ATTRIBUTE_ARRAY' - see above )
1025
1026
1027 }else{ # not yet handled type
1028
1029 die "ERROR ($0): Not yet handled type (\$e->[0]=".$e->[0].") ... => Aborting\n";
1030 }
1031
1032 } # end: foreach iteration
1033
1034} # end: sub retr_info
1035
1036
1037sub select_tokenization { # called from process()
1038
1039 #print STDERR "$0: select_tokenization() ...\n";
1040
1041 ## DEPRECATED (only IDS-intern)
Peter Hardersd892a582020-02-12 15:45:22 +01001042 if( $_GEN_TOK_BAS ) {
1043 if( $select->can_read(3600) ){ # wait 60m for external tokenizer
1044 $_ = <$chld_out>;
1045 my @bounds = split;
Peter Harders6f526a32020-06-29 21:44:41 +02001046 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_bas", $text_id_esc, \@bounds);
Peter Hardersd892a582020-02-12 15:45:22 +01001047 while($select->can_read(0)) {
1048 $_ = <$chld_out>;
1049 if (defined $_ && $_ ne '') {
1050 print STDERR "WARNING: extra output: $_\n"
1051 } else {
1052 print STDERR "WARNING: tokenizer seems to have crashed, restarting.\n";
1053 startTokenizer();
1054 }
1055 }
1056 }else{
Akron85717512020-07-08 11:19:19 +02001057 $zipper->close;
Peter Hardersd892a582020-02-12 15:45:22 +01001058 die "ERROR ($0): cannot retrieve token bounds from external tokenizer for text '$text_id' => Aborting ...\n";
1059 }
Peter Harders6f526a32020-06-29 21:44:41 +02001060 ##
Peter Hardersd892a582020-02-12 15:45:22 +01001061 }elsif( $_GEN_TOK_DUMMY ){
Peter Harders6f526a32020-06-29 21:44:41 +02001062 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_con", $text_id_esc, \@tok_tokens_con);
1063 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_agg", $text_id_esc, \@tok_tokens_agg);
Peter Hardersd892a582020-02-12 15:45:22 +01001064 }
Peter Hardersd892a582020-02-12 15:45:22 +01001065
Peter Harders6f526a32020-06-29 21:44:41 +02001066 #print STDERR "$0: write_tokenization(): DONE\n";
1067
1068} # end: select_tokenization
1069
1070sub write_tokenization { # called from select_tokenization()
1071
1072 my ( $fname, $textid_esc, $bounds ) = @_;
Peter Hardersd892a582020-02-12 15:45:22 +01001073
Peter Harders6f526a32020-06-29 21:44:41 +02001074 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1075 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\"$text_id_esc\" xmlns=\"http://ids-mannheim.de/ns/KorAP\""
1076 ." version=\"KorAP-0.4\">\n <spanList>\n";
1077
1078 $c = 0;
1079
1080 for( $i = 0; $i < ($#$bounds + 1); $i += 2 ){
1081
Peter Hardersd892a582020-02-12 15:45:22 +01001082 $output .= " <span id=\"t_$c\" from=\"".$bounds->[$i]."\" to=\"".$bounds->[$i+1]."\" />\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001083
Peter Hardersd892a582020-02-12 15:45:22 +01001084 $c++;
1085 }
Peter Hardersd892a582020-02-12 15:45:22 +01001086
Peter Harders6f526a32020-06-29 21:44:41 +02001087 $output .= " </spanList>\n</layer>";
1088
Akron85717512020-07-08 11:19:19 +02001089 $zipper->new_stream($fname)->print($output);
Peter Harders6f526a32020-06-29 21:44:41 +02001090
1091} # end: sub write_tokenization
1092
1093
1094sub write_structures { # called from process()
1095
1096 # ~ write @structures ~
Peter Hardersd892a582020-02-12 15:45:22 +01001097
1098 #print STDERR "$0: write_structures(): ...\n";
1099
Peter Harders6f526a32020-06-29 21:44:41 +02001100 if ( $dir eq "" ){
1101
1102 print STDERR "WARNING ($0): write_structures(): empty textSigle => nothing to do ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001103 return;
1104 }
1105
Peter Harders6f526a32020-06-29 21:44:41 +02001106 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1107 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\""
1108 .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 +01001109
1110 $c = 0;
1111
Peter Harders6f526a32020-06-29 21:44:41 +02001112 foreach $ref ( @structures ){
Peter Hardersd892a582020-02-12 15:45:22 +01001113
Peter Harders6f526a32020-06-29 21:44:41 +02001114 ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 ); # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4
1115 # (name, from, to, rec_level), otherwise >4
Peter Hardersd892a582020-02-12 15:45:22 +01001116
Peter Harders6f526a32020-06-29 21:44:41 +02001117 # 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 )
1118
1119 if( $#structures == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){
1120
1121 ${$ref}[$idx] = ${$ref}[ $idx+1 ];
Peter Hardersd892a582020-02-12 15:45:22 +01001122 }
Peter Hardersd892a582020-02-12 15:45:22 +01001123
Peter Harders6f526a32020-06-29 21:44:41 +02001124 # this consistency check is already done in 'retr_info()'
1125 #elsif( ${$ref}[$idx] > ${$ref}[$idx+1] ){ # consistency check: abort, if this doesn't hold
1126 # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):"
1127 # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n";
1128 # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):"
1129 # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n\n$output" }
Peter Hardersd892a582020-02-12 15:45:22 +01001130
Peter Harders6f526a32020-06-29 21:44:41 +02001131 # at least 'POS' should always be there => remove constraint '$_TOKENS_PROC'
1132 #if( $_TOKENS_PROC && ${$ref}[0] ne $_TOKENS_TAG )
Peter Hardersd892a582020-02-12 15:45:22 +01001133
Peter Harders6f526a32020-06-29 21:44:41 +02001134 if( ${$ref}[0] ne $_TOKENS_TAG ){ # $_TOKENS_TAG is already written in 'write_tokens'
Peter Hardersd892a582020-02-12 15:45:22 +01001135
Peter Harders6f526a32020-06-29 21:44:41 +02001136 # l (level): insert information about depth of element in XML-tree (top element = level 1)
1137 $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n"
1138 ." <fs type=\"struct\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
1139 ." <f name=\"name\">${$ref}[ 0 ]</f>\n";
1140
1141 if ( $idx > 2 ) # attributes
1142 {
1143 $output .= " <f name=\"attr\">\n <fs type=\"attr\">\n";
1144
1145 for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){
1146
1147 ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # see explanation in func. 'write_tokens'
1148
1149 # attribute (at index $att_idx) with value (at index $att_idx+1)
1150 $output .= " <f name=\"${$ref}[ $att_idx ]\">${$ref}[ $att_idx+1 ]</f>\n";
1151 }
1152
1153 $output .= " </fs>\n </f>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001154 }
1155
Peter Harders6f526a32020-06-29 21:44:41 +02001156 $output .= " </fs>\n </span>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001157
Peter Harders6f526a32020-06-29 21:44:41 +02001158 } # fi: ... ne $_TOKENS_TAG
Peter Hardersd892a582020-02-12 15:45:22 +01001159
1160 $c++;
1161
Peter Harders6f526a32020-06-29 21:44:41 +02001162 } # end: foreach
Peter Hardersd892a582020-02-12 15:45:22 +01001163
1164 $output .= " </spanList>\n</layer>";
1165
Peter Harders6f526a32020-06-29 21:44:41 +02001166 $output = encode_utf8( $output );
Peter Hardersd892a582020-02-12 15:45:22 +01001167
Akron85717512020-07-08 11:19:19 +02001168 $zipper->new_stream("$_root_dir$dir/$_structure_dir/$_structure_file")
1169 ->print($output);
Peter Hardersd892a582020-02-12 15:45:22 +01001170
1171 #print STDERR "$0: write_structures(): DONE\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001172
Peter Hardersd892a582020-02-12 15:45:22 +01001173} # end: sub write_structures
1174
1175
Peter Harders6f526a32020-06-29 21:44:41 +02001176sub write_tokens { # called from process()
1177
1178 # ~ write @tokens ~
1179
1180 #print STDERR "$0: write_tokens(): ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001181
1182 if( $dir eq "" ){
Peter Harders6f526a32020-06-29 21:44:41 +02001183
1184 print STDERR "WARNING ($0): write_tokens(): empty textSigle => nothing to do ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001185 return;
1186 }
1187
Peter Harders6f526a32020-06-29 21:44:41 +02001188 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1189 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\""
1190 .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 +01001191
1192 $c = 0;
1193
Peter Harders6f526a32020-06-29 21:44:41 +02001194 foreach $ref ( @tokens ){
Peter Hardersd892a582020-02-12 15:45:22 +01001195
Peter Harders6f526a32020-06-29 21:44:41 +02001196 # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4 (name, from, to, rec_level), otherwise >4
1197 ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 );
Peter Hardersd892a582020-02-12 15:45:22 +01001198
Peter Harders6f526a32020-06-29 21:44:41 +02001199 # 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())
1200 if ( $#tokens == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){
1201
1202 ${$ref}[ $idx ] = ${$ref}[ $idx+1 ]; # TODO: check
Peter Hardersd892a582020-02-12 15:45:22 +01001203 }
Peter Hardersd892a582020-02-12 15:45:22 +01001204
Peter Harders6f526a32020-06-29 21:44:41 +02001205 # l (level): insert information about depth of element in XML-tree (top element = level 1)
1206 $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n"
1207 ." <fs type=\"lex\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
1208 ." <f name=\"lex\">\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001209
Peter Harders6f526a32020-06-29 21:44:41 +02001210 if ( $idx > 2 ){ # attributes
1211
Peter Hardersd892a582020-02-12 15:45:22 +01001212 $output .= " <fs>\n";
1213
Peter Harders6f526a32020-06-29 21:44:41 +02001214 for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){
Peter Hardersd892a582020-02-12 15:45:22 +01001215
Peter Harders6f526a32020-06-29 21:44:41 +02001216 ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # ... <w lemma="&gt;" ana="PUNCTUATION">&gt;</w> ...
1217 # the '&gt;' is translated to '>' and hence the result would be '<f name="lemma">></f>'
Peter Hardersd892a582020-02-12 15:45:22 +01001218
Peter Harders6f526a32020-06-29 21:44:41 +02001219 if ( $_INLINE_ANNOT && ${$ref}[ $att_idx ] eq "$_INLINE_ATT_RD" ){
Peter Hardersd892a582020-02-12 15:45:22 +01001220
Peter Harders6f526a32020-06-29 21:44:41 +02001221 ${$ref}[ $att_idx+1 ] =~ /^([^ ]+)(?: (.+))?$/;
1222
1223 die "ERROR (write_tokens()): unexpected format! => Aborting ... (att: ${$ref}[ $att_idx+1 ])\n"
1224 if ( $_INLINE_POS_WR && not defined $1 ) || ( $_INLINE_MSD_WR && not defined $2 );
1225
1226 if ( "$_INLINE_POS_WR" ){
1227
1228 $output .= " <f name=\"$_INLINE_POS_WR\">";
Peter Hardersd892a582020-02-12 15:45:22 +01001229 $output .= "$1" if defined $1;
1230 $output .= "</f>\n";
1231 }
Peter Harders6f526a32020-06-29 21:44:41 +02001232
1233 if ( "$_INLINE_MSD_WR" ){
1234
1235 $output .= " <f name=\"$_INLINE_MSD_WR\">";
Peter Hardersd892a582020-02-12 15:45:22 +01001236 $output .= "$2" if defined $2;
1237 $output .= "</f>\n";
1238 }
1239
Peter Harders6f526a32020-06-29 21:44:41 +02001240 } elsif ( $_INLINE_ANNOT && "$_INLINE_LEM_RD" && ${$ref}[ $att_idx ] eq "$_INLINE_LEM_RD" ){
Peter Hardersd892a582020-02-12 15:45:22 +01001241
Peter Harders6f526a32020-06-29 21:44:41 +02001242 $output .= " <f name=\"$_INLINE_LEM_WR\">${$ref}[ $att_idx+1 ]</f>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001243
Peter Harders6f526a32020-06-29 21:44:41 +02001244 } else { # all other attributes
Peter Hardersd892a582020-02-12 15:45:22 +01001245
Peter Harders6f526a32020-06-29 21:44:41 +02001246 $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 +01001247 }
1248
Peter Harders6f526a32020-06-29 21:44:41 +02001249 } # end: for
Peter Hardersd892a582020-02-12 15:45:22 +01001250
1251 $output .= " </fs>\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001252
1253 } # fi: attributes
Peter Hardersd892a582020-02-12 15:45:22 +01001254
1255 $output .= " </f>\n </fs>\n </span>\n";
1256
1257 $c++;
1258
Peter Harders6f526a32020-06-29 21:44:41 +02001259 } # end: foreach
Peter Hardersd892a582020-02-12 15:45:22 +01001260
1261 $output .= " </spanList>\n</layer>";
1262
Peter Harders6f526a32020-06-29 21:44:41 +02001263 $output = encode_utf8( $output );
Peter Hardersd892a582020-02-12 15:45:22 +01001264
Akron85717512020-07-08 11:19:19 +02001265 $zipper->new_stream("$_root_dir$dir/$_tokens_dir/$_tokens_file")
1266 ->print($output);
Peter Hardersd892a582020-02-12 15:45:22 +01001267
Peter Harders6f526a32020-06-29 21:44:41 +02001268 #print STDERR "$0: write_tokens(): DONE\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001269
Peter Harders6f526a32020-06-29 21:44:41 +02001270} # end: sub write_tokens
Peter Hardersd892a582020-02-12 15:45:22 +01001271
Peter Hardersd892a582020-02-12 15:45:22 +01001272
Peter Harders6f526a32020-06-29 21:44:41 +02001273## DEPRECATED ($_GEN_TOK_BAS: only IDS-intern)
Peter Hardersd892a582020-02-12 15:45:22 +01001274sub startTokenizer {
1275 $pid = open2($chld_out, $chld_in, 'java -cp '. join(":", ".", glob(&dirname(__FILE__)."/../target/*.jar"))." de.ids_mannheim.korap.tokenizer.KorAPTokenizerImpl");
1276 $select = IO::Select->new();
1277 $select->add(*$chld_out);
1278}
Peter Harders6f526a32020-06-29 21:44:41 +02001279##
Akrond949e182020-02-14 12:23:57 +01001280
1281__END__
1282
1283=pod
1284
1285=encoding utf8
1286
1287=head1 NAME
1288
1289tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
1290
1291=head1 SYNOPSIS
1292
1293 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
1294
1295=head1 DESCRIPTION
1296
Akronee434b12020-07-08 12:53:01 +02001297C<tei2korapxml> is a script to convert TEI P5 and
1298L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html>
1299based documents to the
1300L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
1301If no specific input is defined, data is
Akrond949e182020-02-14 12:23:57 +01001302read from C<STDIN>. If no specific output is defined, data is written
1303to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +02001304
Akrond949e182020-02-14 12:23:57 +01001305This program is usually called from inside another script.
1306
Akronee434b12020-07-08 12:53:01 +02001307=head1 FORMATS
1308
1309=head2 Input restrictions
1310
1311=over 2
1312
1313=item
1314
1315utf8 encoded
1316
1317=item
1318
1319TEI P5 formatted input with certain restrictions:
1320
1321=over 4
1322
1323=item
1324
1325B<mandatory>: text-header with integrated textsigle, text-body
1326
1327=item
1328
1329B<optional>: corp-header with integrated corpsigle,
1330doc-header with integrated docsigle
1331
1332=back
1333
1334=item
1335
1336all tokens inside the primary text (inside $data) may not be
1337newline seperated, because newlines are removed
1338(see code section C<~ inside text body ~>) and a conversion of newlines
1339into blanks between 2 tokens could lead to additional blanks,
1340where there should be none (e.g.: punctuation characters like C<,> or
1341C<.> should not be seperated from their predecessor token).
1342(see also code section C<~ whitespace handling ~>).
1343
1344=back
1345
1346=head2 Notes on the output
1347
1348=over 2
1349
1350=item
1351
1352zip file output (default on C<stdout>) with utf8 encoded entries
1353(which together form the KorAP-XML format)
1354
1355=back
1356
Akrond949e182020-02-14 12:23:57 +01001357=head1 INSTALLATION
1358
1359C<tei2korapxml> requires L<libxml2-dev> bindings to build. When
1360these bindings are available, the preferred way to install the script is
1361to use L<cpanm|App::cpanminus>.
1362
1363 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
1364
1365In case everything went well, the C<tei2korapxml> tool will
1366be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +02001367
Akrond949e182020-02-14 12:23:57 +01001368Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
1369
1370=head1 OPTIONS
1371
1372=over 2
1373
1374=item B<--base|-b>
1375
1376The base directory for output. Defaults to C<.>.
1377
1378=item B<--help|-h>
1379
1380Print help information.
1381
1382=item B<--version|-v>
1383
1384Print version information.
1385
1386=back
1387
1388=head1 COPYRIGHT AND LICENSE
1389
1390Copyright (C) 2020, L<IDS Mannheim|https://www.ids-mannheim.de/>
1391
1392Author: Peter Harders
1393
1394Contributors: Marc Kupietz, Carsten Schnober, Nils Diewald
1395
1396L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
1397Corpus Analysis Platform at the
1398L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
1399member of the
1400L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
1401
1402This program is free software published under the
1403L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-TEI/master/LICENSE>.
1404
1405=cut