blob: 6136d4703a3cf9e57a4860d40faf89b34333faf0 [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;
Peter Harders6f526a32020-06-29 21:44:41 +020044use IPC::Open2 qw(open2);
Peter Hardersd892a582020-02-12 15:45:22 +010045
Akron4f67cd42020-07-02 12:27:58 +020046use FindBin;
47BEGIN {
48 unshift @INC, "$FindBin::Bin/../lib";
49};
50
51use KorAP::XML::TEI;
Akroneac374d2020-07-07 09:00:44 +020052use KorAP::XML::TEI::Tokenization;
Akron85717512020-07-08 11:19:19 +020053use KorAP::XML::TEI::Zipper;
Peter Hardersd892a582020-02-12 15:45:22 +010054
Akrond949e182020-02-14 12:23:57 +010055our $VERSION = '0.01';
Peter Harders6f526a32020-06-29 21:44:41 +020056
Akrond949e182020-02-14 12:23:57 +010057our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n";
58
Peter Hardersd892a582020-02-12 15:45:22 +010059
Peter Harders6f526a32020-06-29 21:44:41 +020060# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010061GetOptions(
Peter Harders6f526a32020-06-29 21:44:41 +020062 "root|r=s" => \(my $_root_dir = '.'), # name of root directory inside zip file
63 "input|i=s" => \(my $input_fname = ''), # input file (yet only TEI I5 Format accepted)
Akrond949e182020-02-14 12:23:57 +010064 'help|h' => sub {
65 pod2usage(
66 -verbose => 99,
67 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
68 -msg => $VERSION_MSG,
69 -output => '-'
70 )
71 },
72 'version|v' => sub {
73 pod2usage(
74 -verbose => 0,
75 -msg => $VERSION_MSG,
76 -output => '-'
77 )
78 }
Peter Hardersd892a582020-02-12 15:45:22 +010079);
80
Peter Harders6f526a32020-06-29 21:44:41 +020081#
82# ~~~ parameter (mandatory) ~~~
83#
Peter Hardersd892a582020-02-12 15:45:22 +010084
Peter Harders6f526a32020-06-29 21:44:41 +020085 # optional
86my $_CORP_SIGLE = "korpusSigle"; # opening and closing tags (without attributes) have to be in one line
87 # (e.g.: <korpusSigle>GOE</korpusSigle>)
88 # optional
89my $_DOC_SIGLE = "dokumentSigle"; # analog
90 # mandatory
91my $_TEXT_SIGLE = "textSigle"; # analog
92 # mandatory
93my $_TEXT_BODY = "text"; # tag (without attributes), which contains the primary text
94 # optional
95my $_CORP_HEADER_BEG = "idsHeader type=\"corpus\""; # just keep the correct order of the attributes and evtl. add an '.*' between them
96 # optional
97my $_DOC_HEADER_BEG = "idsHeader type=\"document\""; # analog
98 # mandatory
99my $_TEXT_HEADER_BEG = "idsHeader type=\"text\""; # analog
100
101#
102# ~~~ constants ~~~
103#
Peter Hardersd892a582020-02-12 15:45:22 +0100104
Peter Harders6f526a32020-06-29 21:44:41 +0200105## DEPRECATED (only IDS-intern - the tokenization is normally done by external tools)
106my $_GEN_TOK_BAS = 0; # IDS internal tokenization
107 my( $chld_out, $chld_in, $pid, $select );
108##
109
110## dummy tokenization (only for testing)
Akroneac374d2020-07-07 09:00:44 +0200111my $_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 +0200112 my $_tok_file_con = "tokens_conservative.xml";
113 my $_tok_file_agg = "tokens_aggressive.xml";
Akron510a88c2020-07-07 10:16:50 +0200114 my ( @tok_tokens_con, @tok_tokens_agg, $txt, $offset );
Peter Harders6f526a32020-06-29 21:44:41 +0200115my $_base_tokenization_dir = "base"; # name of directory for storing files of dummy tokenization (only used in func. select_tokenization)
116
Peter Harders6f526a32020-06-29 21:44:41 +0200117my $_DEBUG = 0; # set to 1 for minimal more debug output (no need to be parametrized)
118my $_XCT_LN = 0; # only for debugging: include line numbers in elements of $tree_data
119 # (see also manpage of XML::CompactTree::XS)
120
121my $_header_file = "header.xml"; # name of files containing the text, document and corpus header
122my $_data_file = "data.xml"; # name of file containing the primary text data (tokens)
123my $_structure_dir = "struct"; # name of directory containing the $_structure_file
124my $_structure_file = "structure.xml"; # name of file containing all tags (except ${_TOKEN_TAG}'s) related information
125 # (= their names and byte offsets in $_data)
126## TODO: optional (different annotation tools can produce more zip-files for feeding into KorAP-XML-Krill)
127my $_TOKENS_PROC = 1; # on/off: processing of ${_TOKEN_TAG}'s (default: 1)
128my $_tokens_dir = "tokens"; # name of directory containing the $_tokens_file
129my $_tokens_file = "morpho.xml"; # name of file containing all ${_TOKEN_TAG}'s related information (=their byte offsets in $_data)
130 # - evtl. with additional inline annotations
131my $_TOKENS_TAG = "w"; # name of tag containing all information stored in $_tokens_file
132
133## TODO: optional
134# handling inline annotations (inside $_TOKENS_TAG)
135my $_INLINE_ANNOT = 0; # on/off: set to 1 if inline annotations are present and should be processed (default: 0)
136my $_INLINE_LEM_RD = "lemma"; # from which attribute to read LEMMA information
137my $_INLINE_ATT_RD = "ana"; # from which attribute to read POS information (and evtl. additional MSD - Morphosyntactic Descriptions)
138 # TODO: The format for the POS and MSD information has to suffice the regular expression ([^ ]+)( (.+))?
139 # - which means, that the POS information can be followed by an optional blank with additional
140 # MSD information; unlike the MSD part, the POS part may not contain any blanks.
141my $_INLINE_POS_WR = "pos"; # name (inside $_tokens_file) referring to POS information
142my $_INLINE_MSD_WR = "msd"; # name (inside $_tokens_file) referring to MSD information
143my $_INLINE_LEM_WR = "lemma"; # name (inside $_tokens_file) referring to LEMMA information
144##
145
146
147#
148# ~~~ variables ~~~
149#
150
Akron85717512020-07-08 11:19:19 +0200151# Initialize zipper
152my $zipper = KorAP::XML::TEI::Zipper->new;
Peter Harders6f526a32020-06-29 21:44:41 +0200153my $input_fh; # input file handle (default: stdin)
154
155my $buf_in; # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader
156my $data; # contains the primary text (created by func. 'retr_info' from $buf_in), which is written to '$data_file'
157
158my $dir; # text directory (below $_root_dir)
159my $dir_crp; # corpus directory (below $_root_dir)
160my $dir_doc; # document directory (below $_root_dir)
161
162my ( $text_id, $text_id_esc ); # '$text_id_esc' = escaped version of $text_id (see %ent)
163
164my %ent = ('"', '&quot;', '&','&amp;', # convert '&', '<' and '>' into their corresponding sgml-entities
165 '<','&lt;','>','&gt;');
166 # note: the index still refers to the 'single character'-versions, which are counted as 1
167 # (search for '&amp;' in data.xml and see corresponding indices in $_tokens_file)
168
169my $header_txt; # raw text header (written to '$_root_dir$dir/$_header_file')
170my $header_doc; # raw document header (written to '$_root_dir$dir_doc/$_header_file')
171my $header_crp; # raw corpus header (written to '$_root_dir$dir_crp/$_header_file')
172
173my ( $header_fl_crp, $header_fl_doc, # flags for tracking where we are in the input document
174 $header_fl_txt, $data_fl );
175
176my ( $header_prfx, $data_prfx1, # $header_prfx is written to $_header_file, $data_* are written to $_data_file
177 $data_prfx2, $data_sfx );
178
179my @structures; # list of arrays, where each array represents a TEI I5 tag (except $_TOKENS_TAG) from the input document
180 # - the input of this array is written in func. 'write_structures' into the file '$_structure_file'
181
182my @tokens; # list of arrays, where each array represents a $_TOKENS_TAG from the input document
183 # - the input of this array is written in func. 'write_tokens' into the file '$_tokens_file'
184
185my ( $ref, $idx, $att_idx ); # needed in func. 'write_structures' and 'write_tokens'
186
187my ( $reader, # instance of 'XML::LibXML::Reader->new' (on input '$buf_in')
188 $tree_data ); # instance of 'XML::CompactTree::XS::readSubtreeToPerl' (on input '$reader')
189
190# these are only used inside recursive function 'retr_info'
191my ( $_IDX, # value is set dependent on $_XCT_LN - for extracting array of child elements from element in $tree_data
192 $e, # element from $tree_data
193 $n, # tag name of actual processed element $e
194 $rl, # recursion level
195 $dl, # actual length of string $data
196 @oti, # oti='open tags indizes' - a stack of indizes into @structures, where the top index in @oti
197 # represents the actual processed element from @structures
198 @oti2, # analogously to @oti, but with reference to array @tokens
199 $inside_tokens_tag, # flag is set, when inside $_TOKENS_TAG
200 ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags)
201 $add_one, # ...
202 $fval, $fval2, # ...
203 %ws); # hash for indices of whitespace nodes (needed to recorrect from-values)
204 # idea: when closing element, check if it's from-index minus 1 refers to a whitespace node
205 # (means: 'from-index - 1' is a key in %ws).
206 # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1
207
208my $output; # temporary variable needed in 'write_*'-functions for writing output to zip-stream $zip)
209
210my ( $i, $c ); # index variables used in loops
211
212## DEPRECATED (only IDS-intern)
213my $_tok_file_bas = "tokens.xml";
214##
215
216my ( $_CORP_HEADER_END, $_DOC_HEADER_END, $_TEXT_HEADER_END );
217
218
219#
220# ~~~ main ~~~
221#
222
223# ~ initializations ~
224
225($_XCT_LN)?($_IDX=5):($_IDX=4);
226
227$header_prfx = $data_prfx1 = $data_prfx2 = $data_sfx = "";
228
229$header_fl_txt = $header_fl_doc = $header_fl_crp = 0;
230
231$inside_tokens_tag = -1;
232
233$fval = $fval2 = 0;
234
235$_root_dir .= '/'; # base dir must always end with a slash
236$_root_dir =~ s/^\.?\///; # remove leading / (only relative paths allowed in IO::Compress::Zip) and redundant ./
237
238$_CORP_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_CORP_HEADER_END = $1;
239$_DOC_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_DOC_HEADER_END = $1;
240$_TEXT_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_TEXT_HEADER_END = $1;
241
242## TODO: remove this, because it's IDS-specific
Peter Hardersd892a582020-02-12 15:45:22 +0100243$header_prfx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
244$header_prfx .= "<?xml-model href=\"header.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n";
245$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 +0200246##
Peter Hardersd892a582020-02-12 15:45:22 +0100247
248$data_prfx1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
249$data_prfx1 .= "<?xml-model href=\"text.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n";
250$data_prfx1 .= "<raw_text docid=\"";
251$data_prfx2 .= "\" xmlns=\"http://ids-mannheim.de/ns/KorAP\">\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200252## TODO: can 'metadata.xml' change or is it constant?
Peter Hardersd892a582020-02-12 15:45:22 +0100253$data_prfx2 .= " <metadata file=\"metadata.xml\" />\n";
Peter Harders6f526a32020-06-29 21:44:41 +0200254##
Peter Hardersd892a582020-02-12 15:45:22 +0100255$data_prfx2 .= " <text>";
256$data_sfx = "</text>\n</raw_text>";
257
Peter Hardersd892a582020-02-12 15:45:22 +0100258
Peter Harders6f526a32020-06-29 21:44:41 +0200259## DEPRECATED (only IDS-intern)
260startTokenizer() if $_GEN_TOK_BAS;
261##
Peter Hardersd892a582020-02-12 15:45:22 +0100262
Peter Harders6f526a32020-06-29 21:44:41 +0200263# ~ read input and write output (text by text) ~
Peter Harders90157342020-07-01 21:05:14 +0200264process();
Peter Hardersd892a582020-02-12 15:45:22 +0100265
Peter Hardersd892a582020-02-12 15:45:22 +0100266
Peter Harders6f526a32020-06-29 21:44:41 +0200267#
268# ~~~ subs ~~~
269#
Peter Hardersd892a582020-02-12 15:45:22 +0100270
Peter Hardersd892a582020-02-12 15:45:22 +0100271
Peter Harders90157342020-07-01 21:05:14 +0200272sub process {
Peter Hardersd892a582020-02-12 15:45:22 +0100273
Peter Harders6f526a32020-06-29 21:44:41 +0200274 my ( $pfx, $sfx );
Peter Hardersd892a582020-02-12 15:45:22 +0100275
Peter Harders6f526a32020-06-29 21:44:41 +0200276 my $lc = 0; # line counter
277
278 my $tc = 0; # text counter
279
280 $input_fh = *STDIN; # input file handle (default: stdin)
281
Akron85717512020-07-08 11:19:19 +0200282 $data_fl = 0;
Peter Harders6f526a32020-06-29 21:44:41 +0200283
284 $buf_in = $data = $dir = $dir_doc = $dir_crp = "";
285 $header_txt = $header_doc = $header_crp = "";
286
287
288 if ( $input_fname ne '' ){
289
290 open ( $input_fh, "<", "$input_fname") || die "File \'$input_fname\' could not be opened.\n";
291
292 }
293
294
Peter Harders90157342020-07-01 21:05:14 +0200295 # prevents segfaulting of 'XML::LibXML::Reader' inside 'process()' - see notes on 'PerlIO layers' in 'man XML::LibXML')
296 # removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
297 binmode $input_fh;
298
299
Peter Harders6f526a32020-06-29 21:44:41 +0200300 # ~ loop (reading input document) ~
301
302 while ( <$input_fh> ){
303
304 $lc++; # line counter
305
306 # TODO: yet not tested fo big amounts of data
307 # must-have, otherwise comments in input could be fatal (e.g.: ...<!--\n<idsHeader...\n-->...)
Akron4f67cd42020-07-02 12:27:58 +0200308 KorAP::XML::TEI::delHTMLcom ( $input_fh, $_ ); # remove HTML comments (<!--...-->)
Peter Harders6f526a32020-06-29 21:44:41 +0200309
310 if ( $data_fl && m#^(.*)</${_TEXT_BODY}>(.*)$# ){
311
312
313 # ~ end of text body ~
314
315
316 # write data.xml, structure.xml and evtl. morpho.xml and/or the dummy tokenization files (s.a.: $_tok_file_con and $_tok_file_agg)
317
318 $pfx = $1; $sfx = $2;
319
320 die "ERROR ($0): main(): input line number $lc: line with closing text-body tag '${_TEXT_BODY}'"
321 ." contains additional information ... => Aborting\n\tline=$_"
322 if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/;
323
324 if ( $dir ne "" ){
325
Peter Harders6f526a32020-06-29 21:44:41 +0200326 $reader = XML::LibXML::Reader->new( string => "<text>$buf_in</text>", huge => 1 );
Peter Harders6f526a32020-06-29 21:44:41 +0200327
328 if ( $_XCT_LN ){ # _XCT_LINE_NUMBERS is only for debugging
329 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY | XCT_LINE_NUMBERS );
330 } else {
331 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY );
332 }
333
334 @structures = (); @oti = ();
335
336 if ( $_TOKENS_PROC ){
337 @tokens = (); @oti2 = ()
338 }
339
340 $dl = $rl = 0;
341
342 # ~ whitespace related issue ~
343 $add_one = 0;
344 %ws = ();
345
346
347 # ~ recursion ~
348
349 retr_info( \$tree_data->[2] ); # parse input data
350
351 $rl--;
352
353
354 # ~ write data.xml ~
355
356 $data =~ tr/\n\r/ /; # note: 2 blanks - otherwise offset data would become corrupt
357
358 $data = encode_utf8( $data );
359
360 ## DEPRECATED (only IDS-intern)
361 # first write it to tokenization pipe to give it some time
362 if ( $_GEN_TOK_BAS ){
363 print $chld_in "$data\n\x03\n";
364 }
365 ##
366
367 print STDERR "DEBUG ($0): main(): Writing (utf8-formatted) xml file $_root_dir$dir/$_data_file\n" if $_DEBUG;
368
Peter Harders6f526a32020-06-29 21:44:41 +0200369
370 $data =~ s/(&|<|>)/$ent{$1}/g;
371
Akron85717512020-07-08 11:19:19 +0200372 $zipper->new_stream("$_root_dir$dir/$_data_file")
373 ->print("$data_prfx1$text_id_esc$data_prfx2$data$data_sfx");
Peter Harders6f526a32020-06-29 21:44:41 +0200374
375 # ~ write structures ~
376
377 write_structures() if @structures;
378
379
380 # ~ write tokens ~
381
382 write_tokens() if $_TOKENS_PROC && @tokens;
383
384
385 # ~ dummy tokenization ~
386
387 if ( $_GEN_TOK_BAS || $_GEN_TOK_DUMMY ){ ## DEPRECATED ($_GEN_TOK_BAS: only IDS-intern)
388
389 select_tokenization();
390
391 if ( $_GEN_TOK_DUMMY ){
392 $offset = 0; @tok_tokens_con=(); @tok_tokens_agg=();
393 }
394 }
395
396 $data_fl = 0; $buf_in = $data = $dir = ""; # reinit.
397
398 } else { # $dir eq ""
399
400 print STDERR "WARNING ($0): main(): maybe empty textSigle => skipping this text ...\n";
401 print STDERR "WARNING ($0): main(): text header=$header_txt\n";
402 print STDERR "WARNING ($0): main(): data=$data\n";
Peter Hardersd892a582020-02-12 15:45:22 +0100403 }
404
Peter Harders6f526a32020-06-29 21:44:41 +0200405 } elsif ( $data_fl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100406
Peter Hardersd892a582020-02-12 15:45:22 +0100407
Peter Harders6f526a32020-06-29 21:44:41 +0200408 # ~ inside text body ~
Peter Hardersd892a582020-02-12 15:45:22 +0100409
Peter Hardersd892a582020-02-12 15:45:22 +0100410
Peter Harders6f526a32020-06-29 21:44:41 +0200411 #print STDERR "inside text body (\$data_fl set)\n";
412
413 # ~ whitespace handling ~
414
415 # remove consecutive whitespace at beginning and end (mostly one newline)
416 # to let 'XML::CompactTree::XS' recognize these blanks as 'text-nodes', the option 'XCT_IGNORE_WS' may not be used (see above).
417 s/^\s+//; s/\s+$//;
418
419 # 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),
420 # because it helps for better readability of the text in the '$_data_file' (e.g.: assure blanks between sentences).
421 # Furthermore, the input lines should avoid primary text tokens, which span across several lines, unless the line breaks doesn't lead
422 # to a situation which produces unwanted blanks - e.g.: '...<w>end</w>\n<w>.</w>...' would lead to '...<w>end</w> <w>.</w>...', or
423 # '...<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
424 # to correct those unwanted effects, there would be lots of examples aside punctuation, where there would not exist an easy way or unarbitrary
425 # solution regarding the elimination of the false blanks.
Peter Hardersd892a582020-02-12 15:45:22 +0100426 #
Peter Harders6f526a32020-06-29 21:44:41 +0200427 # 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
428 # (see also comments on 'input restrictions' at the top of this script).
Peter Hardersd892a582020-02-12 15:45:22 +0100429
Peter Harders6f526a32020-06-29 21:44:41 +0200430 if ( m/<[^>]+>[^<]/ ){ # line contains at least one tag with at least one character contents
Peter Hardersd892a582020-02-12 15:45:22 +0100431
Peter Harders6f526a32020-06-29 21:44:41 +0200432 $tc++; # text counter
Peter Hardersd892a582020-02-12 15:45:22 +0100433
Peter Harders6f526a32020-06-29 21:44:41 +0200434 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 +0100435 }
436
Peter Harders6f526a32020-06-29 21:44:41 +0200437 # add line to buffer
438 $buf_in .= $_;
439
440 } elsif ( $header_fl_txt && m#^(.*</${_TEXT_HEADER_END}>)(.*)$# ){
441
442
443 # ~ end of text header ~
444
445
446 #print STDERR "end of text header\n";
447
448 # write it to header.xml
449
450 $sfx = $2;
451
452 $header_txt .= $1; $header_fl_txt = 0;
453
454
455 die "ERROR ($0): main(): input line number $lc: line with closing text-header tag '${_TEXT_HEADER_END}'"
456 ." contains additional information ... => Aborting\n\tline=$_"
457 if $sfx !~ /^\s*$/;
458
459 if ( $dir eq "" ){
460
461 print STDERR "WARNING ($0): main(): input line number $lc: empty textSigle in text header => nothing to do ...\ntext header=$header_txt\n";
462
463 } else {
464
465 print STDERR "DEBUG ($0): Writing file $_root_dir$dir/$_header_file\n" if $_DEBUG;
466
Peter Harders6f526a32020-06-29 21:44:41 +0200467 $header_txt = encode_utf8( $header_txt );
468
Akron85717512020-07-08 11:19:19 +0200469 $zipper->new_stream("$_root_dir$dir/$_header_file")
470 ->print("$header_prfx$header_txt");
Peter Harders6f526a32020-06-29 21:44:41 +0200471
472 $header_txt = "";
Peter Hardersd892a582020-02-12 15:45:22 +0100473 }
Peter Hardersd892a582020-02-12 15:45:22 +0100474
Peter Harders6f526a32020-06-29 21:44:41 +0200475 } elsif ( $header_fl_txt ){
Peter Hardersd892a582020-02-12 15:45:22 +0100476
Peter Harders6f526a32020-06-29 21:44:41 +0200477 # ~ inside text header ~
Peter Hardersd892a582020-02-12 15:45:22 +0100478
Peter Harders6f526a32020-06-29 21:44:41 +0200479
480 #print STDERR "inside text header\n";
481
482 if( m#^(.*)<${_TEXT_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
483
484 $pfx = $1; $sfx = $3;
485
486 $dir = $2; $text_id = $dir;
487
488 $text_id =~ tr/\//_/; $dir =~ s/("|&|<|>)/$ent{$1}/g;
489
490 $text_id = encode_utf8( $text_id );
491
492 die "ERROR ($0): main(): input line number $lc: line with text-sigle tag '$_TEXT_SIGLE' is not in expected format ... => Aborting\n\tline=$_"
493 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_TEXT_SIGLE}>\s*$# || $dir =~ /^\s*$/;
494
495 # log output for seeing progression
496 print STDERR "$0: main(): text_id=".decode_utf8( $text_id )."\n";
497
498 $text_id_esc = $text_id;
499
500 s#(<${_TEXT_SIGLE}(?: [^>]*)?>)[^<]+(</${_TEXT_SIGLE}>)#$1$dir$2# # to be consistent with escaping, escape also textSigle in text-header
501 if $text_id_esc =~ s/("|&|<|>)/$ent{$1}/g;
502
503 $dir =~ tr/\./\//;
504 }
505
506 $header_txt .= $_;
507
508 } elsif ( $header_fl_doc && m#^(.*</${_DOC_HEADER_END}>)(.*)$# ){
509
510
511 # ~ end of document header ~
512
Peter Harders6f526a32020-06-29 21:44:41 +0200513 #print STDERR "end of doc header\n";
514
515 # write it to header.xml
516
517 $sfx = $2;
518
519 $header_doc .= $1; $header_fl_doc = 0;
520
521 die "ERROR ($0): main(): input line number $lc: line with closing document-header tag '${_DOC_HEADER_END}'"
522 ." contains additional information ... => Aborting\n\tline=$_"
523 if $sfx !~ /^\s*$/;
524
525 if( $dir_doc eq "" ){
526
527 print STDERR "WARNING ($0): main(): input line number $lc: empty document sigle in document header"
528 ." => nothing to do ...\ndocument header=$header_doc\n";
529
530 } else {
531
532 print STDERR "DEBUG ($0): Writing file $_root_dir$dir_doc/$_header_file\n" if $_DEBUG;
533
Peter Harders6f526a32020-06-29 21:44:41 +0200534 $header_doc = encode_utf8( $header_doc );
535
Akron85717512020-07-08 11:19:19 +0200536 $zipper->new_stream("$_root_dir$dir_doc/$_header_file")
537 ->print("$header_prfx$header_doc");
Peter Harders6f526a32020-06-29 21:44:41 +0200538
539 $header_doc = $dir_doc = "";
540 }
541
542 } elsif ( $header_fl_doc ){
543
544
545 # ~ inside document header ~
546
547
548 #print STDERR "inside doc header\n";
549
550 if ( m#^(.*)<${_DOC_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
551
552 $pfx = $1; $sfx = $3;
553
554 $dir_doc = $2;
555
556 die "ERROR ($0): main(): input line number $lc: line with document-sigle tag '$_DOC_SIGLE' is not in expected format ... => Aborting\n\tline=$_"
557 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_DOC_SIGLE}>\s*$# || $dir_doc =~ /^\s*$/;
558
559 s#(<${_DOC_SIGLE}(?: [^>]*)?>)[^<]+(</${_DOC_SIGLE}>)#$1$dir_doc$2# # to be consistent with escaping, escape also textSigle in Document-Header
560 if $dir_doc =~ s/("|&|<|>)/$ent{$1}/g;
561 }
562
563 $header_doc .= $_;
564
565 } elsif ( m#^(.*)(<${_TEXT_HEADER_BEG}.*)$# ){
566
567 # ~ start of text header ~
568
569
570 #print STDERR "begin of text header\n";
571
572 $header_txt = $_; $header_fl_txt = 1; $pfx = $1;
573
574 $tc = 0; # reset (needed for ~ whitespace handling ~)
575
576 die "ERROR ($0): main(): input line number $lc: line with opening text-header tag '${_TEXT_HEADER_BEG}'"
577 ." is not in expected format ... => Aborting\n\tline=$_"
578 if $pfx !~ /^\s*$/;
579
580 } elsif ( m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){
581
582
583 # ~ start of text body ~
584
585
586 #print STDERR "inside text body\n";
587
588 $pfx = $1; $sfx = $2;
589
590 $data_fl = 1;
591
592 die "ERROR ($0): main(): input line number $lc: line with opening text-body tag '${_TEXT_BODY}'"
593 ." contains additional information ... => Aborting\n\tline=$_"
594 if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/;
595
596 } elsif ( m#^(.*)(<${_DOC_HEADER_BEG}.*)$# ){
597
598
599 # ~ start of document header ~
600
601
602 #print STDERR "begin of doc header\n";
603
604 $header_doc = "$2\n"; $header_fl_doc = 1; $pfx = $1;
605
606 die "ERROR ($0): main(): input line number $lc: line with opening document-header tag '${_DOC_HEADER_BEG}'"
607 ."is not in expected format ... => Aborting\n\tline=$_"
608 if $pfx !~ /^\s*$/;
609
610 } elsif ( $header_fl_crp && m#^(.*</${_CORP_HEADER_END}>)(.*)$# ){
611
612
613 # ~ end of corpus header ~
614
615
616 #print STDERR "end of corp header\n";
617
618 $sfx = $2;
619
620 $header_crp .= $1; $header_fl_crp = 0;
621
622 die "ERROR ($0): main(): input line number $lc: line with closing corpus-header tag '${_CORP_HEADER_END}'"
623 ." contains additional information ... => Aborting\n\tline=$_"
624 if $sfx !~ /^\s*$/;
625
626 if ( $dir_crp eq "" ){
627
628 print STDERR "WARNING ($0): main(): input line number $lc: empty corpus sigle in corpus header => nothing to do ...\ncorpus header=$header_crp\n";
629
630 } else {
631
632 print STDERR "DEBUG ($0): Writing file $_root_dir$dir_crp/$_header_file\n" if $_DEBUG;
633
Peter Harders6f526a32020-06-29 21:44:41 +0200634 $header_crp = encode_utf8( $header_crp );
635
Akron85717512020-07-08 11:19:19 +0200636 $zipper->new_stream("$_root_dir$dir_crp/$_header_file")
637 ->print("$header_prfx$header_crp");
Peter Harders6f526a32020-06-29 21:44:41 +0200638
639 $header_crp = $dir_crp = "";
640 }
641
642 } elsif ( $header_fl_crp ){
643
644
645 # ~ inside corpus header ~
646
647
648 #print STDERR "inside corp header\n";
649
650 if ( m#^(.*)<${_CORP_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){
651
652 $pfx = $1; $sfx = $3;
653
654 $dir_crp = $2;
655
656 die "ERROR ($0): main(): input line number $lc: line with korpusSigle-tag is not in expected format ... => Aborting\n\tline=$_"
657 if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_CORP_SIGLE}>\s*$# || $dir_crp =~ /^\s*$/;
658
659 if ( $dir_crp =~ s/("|&|<|>)/$ent{$1}/g ){
660
661 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 +0100662 }
663 }
664
Peter Harders6f526a32020-06-29 21:44:41 +0200665 $header_crp .= $_;
Peter Hardersd892a582020-02-12 15:45:22 +0100666
Peter Harders6f526a32020-06-29 21:44:41 +0200667 } elsif ( m#^(.*)(<${_CORP_HEADER_BEG}.*)$# ){
Peter Hardersd892a582020-02-12 15:45:22 +0100668
Peter Hardersd892a582020-02-12 15:45:22 +0100669
Peter Harders6f526a32020-06-29 21:44:41 +0200670 # ~ start of corpus header ~
Peter Hardersd892a582020-02-12 15:45:22 +0100671
Peter Harders6f526a32020-06-29 21:44:41 +0200672
673 #print STDERR "begin of corp header\n";
674
675 $header_crp = $2; $header_fl_crp = 1; $pfx = $1;
676
677 die "ERROR ($0): main(): input line number $lc: line with opening corpus-header tag '${_CORP_HEADER_BEG}'"
678 ." is not in expected format ... => Aborting\n\tline=$_"
679 if $pfx !~ /^\s*$/;
Peter Hardersd892a582020-02-12 15:45:22 +0100680 }
681
Peter Harders6f526a32020-06-29 21:44:41 +0200682 } #end: while
Peter Hardersd892a582020-02-12 15:45:22 +0100683
Akron85717512020-07-08 11:19:19 +0200684 $zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200685
686 ## DEPRECATED (only IDS-intern)
687 if( $_GEN_TOK_BAS ){
688 close($chld_in);
689 close($chld_out);
Peter Hardersd892a582020-02-12 15:45:22 +0100690 }
Peter Harders6f526a32020-06-29 21:44:41 +0200691 ##
Peter Hardersd892a582020-02-12 15:45:22 +0100692
Peter Harders90157342020-07-01 21:05:14 +0200693} # end: sub process
Peter Hardersd892a582020-02-12 15:45:22 +0100694
Peter Hardersd892a582020-02-12 15:45:22 +0100695
Peter Harders6f526a32020-06-29 21:44:41 +0200696sub retr_info { # called from process()
Peter Hardersd892a582020-02-12 15:45:22 +0100697
Peter Harders6f526a32020-06-29 21:44:41 +0200698 # EXAMPLE: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>
699 #
700 # print out values of above example:
701 # 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]'
702 #
703 # $data = reference to below array
704 #
705 # [ 0: XML_READER_TYPE_DOCUMENT,
706 # 1: ?
707 # 2: [ 0: [ 0: XML_READER_TYPE_ELEMENT <- start recursion with array '$data->[2]' (see process(): retr_info( \$tree_data->[2] ))
708 # 1: 'node'
709 # 2: ?
710 # 3: HASH (attributes)
711 # 4: 1 (line number)
712 # 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT
713 # 1: 'node1'
714 # 2: ?
715 # 3: undefined (no attributes)
716 # 4: 1 (line number)
717 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
718 # 1: 'some '
719 # ]
720 # 1: [ 0: XML_READER_TYPE_ELEMENT
721 # 1: 'n'
722 # 2: ?
723 # 3: undefined (no attributes)
724 # 4: 1 (line number)
725 # 5: undefined (no child nodes)
726 # ]
727 # 2: [ 0: XML_READER_TYPE_TEXT
728 # 1: ' text'
729 # ]
730 # ]
731 # ]
732 # 1: [ 0: XML_READER_TYPE_ELEMENT
733 # 1: 'node2'
734 # 2: ?
735 # 3: undefined (not attributes)
736 # 4: 1 (line number)
737 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
738 # 1: 'more-text'
739 # ]
740 # ]
741 # ]
742 # ]
743 # ]
744 # ]
745 # ]
746 #
747 # $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT)
748 #
749 # ref($data->[2]) == ARRAY (with 1 element for 'node')
750 # ref($data->[2]->[0]) == ARRAY (with 6 elements)
751 #
752 # $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
753 # $data->[2]->[0]->[1] == 'node'
754 # ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v')
755 # $data->[2]->[0]->[4] == 1 (line number)
756 # ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2')
757 # # child nodes of actual node (see $_IDX)
758 #
759 # ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements)
760 # $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
761 # $data->[2]->[0]->[5]->[0]->[1] == 'node1'
762 # $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute)
763 # $data->[2]->[0]->[5]->[0]->[4] == 1 (line number)
764 # ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text')
765 #
766 # ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements)
767 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
768 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some '
769 #
770 # ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements)
771 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
772 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n'
773 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute)
774 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number)
775 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child nodes)
776 #
777 # ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements)
778 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
779 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text'
780 #
781 #
782 # retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example.
783 # Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to
784 # ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]).
Peter Hardersd892a582020-02-12 15:45:22 +0100785
Peter Harders6f526a32020-06-29 21:44:41 +0200786 $rl++; # recursion level (1 = topmost level inside retr_info() = should always be level of tag $_TEXT_BODY)
Peter Hardersd892a582020-02-12 15:45:22 +0100787
Peter Hardersd892a582020-02-12 15:45:22 +0100788
Peter Harders6f526a32020-06-29 21:44:41 +0200789 foreach $e ( @{${$_[0]}} ){ # iteration through all array elements ($_[0] is a reference to an array reference)
Peter Hardersd892a582020-02-12 15:45:22 +0100790
Peter Hardersd892a582020-02-12 15:45:22 +0100791
Peter Harders6f526a32020-06-29 21:44:41 +0200792 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 +0100793
Peter Hardersd892a582020-02-12 15:45:22 +0100794
Peter Harders6f526a32020-06-29 21:44:41 +0200795 #~~~~
796 # from here: opening tag
797 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100798
Peter Hardersd892a582020-02-12 15:45:22 +0100799
Peter Harders6f526a32020-06-29 21:44:41 +0200800 # insert new array (for new tag) into @structures with tag-name and tag-attributes (if present)
801 # 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 +0100802
Peter Harders6f526a32020-06-29 21:44:41 +0200803 # ~ tag name ~
Peter Hardersd892a582020-02-12 15:45:22 +0100804
Peter Harders6f526a32020-06-29 21:44:41 +0200805 $n = $e->[1];
Peter Hardersd892a582020-02-12 15:45:22 +0100806
Peter Hardersd892a582020-02-12 15:45:22 +0100807
Peter Harders6f526a32020-06-29 21:44:41 +0200808 # ~ handle structures ~
Peter Hardersd892a582020-02-12 15:45:22 +0100809
Peter Harders6f526a32020-06-29 21:44:41 +0200810 my @array;
811 push @array, $n;
812 push @structures, \@array;
813 push @oti, $#structures; # add highest index of @structures to @oti
Peter Hardersd892a582020-02-12 15:45:22 +0100814
Peter Hardersd892a582020-02-12 15:45:22 +0100815
Peter Harders6f526a32020-06-29 21:44:41 +0200816 # ~ handle tokens ~
Peter Hardersd892a582020-02-12 15:45:22 +0100817
Peter Harders6f526a32020-06-29 21:44:41 +0200818 $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 +0100819
Peter Harders6f526a32020-06-29 21:44:41 +0200820 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100821
Peter Harders6f526a32020-06-29 21:44:41 +0200822 my @array2;
823 push @array2, $n;
824 push @tokens, \@array2;
825 push @oti2, $#tokens;
826 }
Peter Hardersd892a582020-02-12 15:45:22 +0100827
Peter Hardersd892a582020-02-12 15:45:22 +0100828
Peter Harders6f526a32020-06-29 21:44:41 +0200829 # ~ handle attributes ~
Peter Hardersd892a582020-02-12 15:45:22 +0100830
Peter Harders6f526a32020-06-29 21:44:41 +0200831 if ( defined $e->[3] ){ # only if attributes exist
Peter Hardersd892a582020-02-12 15:45:22 +0100832
Peter Harders6f526a32020-06-29 21:44:41 +0200833 for ( $c = 0; $c < @{$e->[3]}; $c += 2 ){ # with 'XCT_ATTRIBUTE_ARRAY', $node->[3] is an array reference of the form
834 # [ name1, value1, name2, value2, ....] of attribute names and corresponding values.
835 # note: arrays are faster (see: http://makepp.sourceforge.net/2.0/perl_performance.html)
Peter Hardersd892a582020-02-12 15:45:22 +0100836
Peter Harders6f526a32020-06-29 21:44:41 +0200837 # '$c' references the 'key' and '$c+1' the 'value'
838 push @{$structures[$#structures]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1];
Peter Hardersd892a582020-02-12 15:45:22 +0100839
Peter Harders6f526a32020-06-29 21:44:41 +0200840 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
Peter Hardersd892a582020-02-12 15:45:22 +0100841
Peter Harders6f526a32020-06-29 21:44:41 +0200842 push @{$tokens[$#tokens]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1];
843 }
844
845 }
846 }
847
848
849 # ~ index 'from' ~
850
851 # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts
852
853 push @{$structures[$#structures]}, ( $dl + $add_one ); # see below (text and whitespace nodes) for explanation on '$add_one'
854
855 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
856
857 push @{$tokens[$#tokens]}, ( $dl + $add_one );
858 }
859
860
861 #~~~~
862 # until here: opening tag
863 #~~~~
864
865
866 # ~~ RECURSION ~~
867
868 if ( defined $e->[$_IDX] ){ # do no recursion, if $e->[$_IDX] is not defined (because we have no array of child nodes, e.g.: <back/>)
869
870 retr_info( \$e->[$_IDX] ); # recursion with array of child nodes
871
872 $rl--; # return from recursion
873 }
874
875
876 #~~~~~
877 # from here: closing tag
878 #~~~~~
879
880
881 # ~ handle structures ~
882
883 {
884 my $ix = pop @oti; # index of just closed tag
885
886 my $aix = $#{$structures[$ix]}; # determine highest index from 'array referring to last closed tag' ...
887
888 $fval = ${$structures[$ix]}[ $aix ]; # ... and get it's from-value
889
890 if ( $fval > 0 && not exists $ws{ $fval - 1 } ){ # ~ whitespace related issue ~
891
892 # previous node was a text-node
893
894 ${$structures[$ix]}[ $aix ] = $fval - 1; # recorrect from-value (see below: notes on ~ whitespace related issue ~)
895 }
896
897 # in case this fails, check input
898 die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@structures: from-value ($fval) is 2 or more greater"
899 ." than to-value ($dl) => please check. aborting ...\n"
900 if ( $fval - 1 ) > $dl;
901
902 # TODO: construct example for which this case applies
903 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
904 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval - 1) >= $dl;
905 ${$structures[$ix]}[ $aix ] = $dl if $fval == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl)
906
907 push @{$structures[$ix]}, $dl, $rl; # to-value and recursion-level
908
909 # note: use $dl, because the offsets are _between_ the characters (e.g.: word = 'Hello' => from = 0 (before 'H'), to = 5 (after 'o'))
910 }
911
912
913 # ~ handle tokens ~
914
915
916 if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){
917
918 my $ix = pop @oti2;
919
920 my $aix = $#{$tokens[$ix]};
921
922 $fval2 = ${$tokens[$ix]}[ $aix ]; # from-value
923
924 if( $fval2 > 0 && not exists $ws{ $fval2 - 1 } ){ # ~ whitespace related issue ~
925
926 # previous node was a text-node
927
928 ${$tokens[$ix]}[ $aix ] = $fval2 - 1; # recorrect from-value
929 }
930
931 # in case this fails, check input
932 die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@tokens: from-value ($fval2) is 2 or more greater"
933 ." than to-value ($dl) => please check. aborting ...\n"
934 if ( $fval2 - 1 ) > $dl;
935
936 # TODO: construct example for which this case applies
937 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
938 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval2 - 1) >= $dl;
939 ${$tokens[$ix]}[ $aix ] = $dl if $fval2 == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl)
940
941 push @{$tokens[$ix]}, $dl, $rl; # to-value and recursion-level
942
943 $inside_tokens_tag = -1; # reset
944 }
945
946 # ~ whitespace related issue ~
Peter Hardersd892a582020-02-12 15:45:22 +0100947 # clean up
Peter Harders6f526a32020-06-29 21:44:41 +0200948 delete $ws{ $fval - 1 } if $fval > 0 && exists $ws{ $fval - 1 };
949 delete $ws{ $fval2 - 1 } if $_TOKENS_PROC && $fval2 > 0 && exists $ws{ $fval2 - 1 };
Peter Hardersd892a582020-02-12 15:45:22 +0100950
951
Peter Harders6f526a32020-06-29 21:44:41 +0200952 #~~~~~
953 # from here: text (and whitespace) nodes
954 #~~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100955
956
Peter Harders6f526a32020-06-29 21:44:41 +0200957 # the 3rd form of nodes, next to text-nodes (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes
958 # of the type 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'
959 #
960 # when modifiying the above example (at the top of this sub) by inserting an additional blank between '</node1>' and '<node2>',
961 # the output for '$data->[2]->[0]->[5]->[1]->[1]' becomes a blank (' ') and it's type is '14' (see manpage of XML::LibXML::Reader):
962 #
963 # 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 +0100964
Peter Harders6f526a32020-06-29 21:44:41 +0200965 } elsif ( $e->[0] == XML_READER_TYPE_TEXT || $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){
Peter Hardersd892a582020-02-12 15:45:22 +0100966
Peter Harders6f526a32020-06-29 21:44:41 +0200967 # notes on ~ whitespace related issue ~ (see below source code)
968 #
969 # example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...'
970 #
971 # Two text-nodes should normally be separated by a blank. In the above example, that would be the 2 text-nodes
972 # 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' '.
973 #
974 # Assumed, that the above example marks the general case, then the text-node 'Campagne in Frankreich' leads to the
975 # setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag and setting it's from-index, it gets the right
976 # offset, which is the start-index of '1792'.
977 #
978 # To check, that the above consideration holds, we save the from-index of a read whitespace-node into the hash %ws.
979 # By this, it can be checked when closing a tag, if the 'non-tag'-node (text or whitespace) before the last 'non-tag'-
980 # node was actually a whitespace-node ($ws{ $fval - 1 }).
981 #
982 # For whitespace-nodes, also $add_one has to be set to 0, so when opening the next tag (in the above example the 2nd
983 # 's'-tag), no additional 1 is added, because this was already done by the whitespace-node itself (by incrementing the
984 # variable $dl).
985 #
986 # Now, what happens, when 2 text-nodes are not seperated by a whitespace-node (blank)? (e.g.: <w>Augen<c>,</c></w>)
987 #
988 # In this case, the falsely increased from-value has to be decreased again by 1 when closing the referring tag
989 # (...$fval - 1; # recorrect).
990 #
991 # 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
992 # sense, because of '<w> </w>'), in both the ' ' is handled as a whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE).
993 #
994 # So the from-index of the 2nd w-tag (in the second example) would refer to 'bar', which may not have been the intention
995 # (even, if '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug, which needs to be fixed?
996 #
997 # 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-
998 # and to-indizes for the tags 'a' and 'b' are both 9, which is the start-index of the token 'tok3'.
999
1000 if( $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){
1001
1002 # ~ whitespace related issue ~
1003
1004 $add_one = 0;
1005
1006 $ws{ $dl }++; # '++' does not mean a thing here (could be used for consistency checking)
1007
1008 }else{
1009
1010 # ~ text-node ~
1011
1012 $add_one = 1;
1013 }
1014
1015
1016 # ~ update $data and $dl ~
1017
1018 $data .= $e->[1];
1019
1020 $dl += length( $e->[1] ); # update length of $data
1021
1022
1023
1024 #~~~~~
1025 # from here (until end): dummy tokenization
1026 #~~~~~
1027
1028 if ( $_GEN_TOK_DUMMY ){
1029
1030 $txt = $e->[1];
1031
Akroneac374d2020-07-07 09:00:44 +02001032
Peter Harders6f526a32020-06-29 21:44:41 +02001033 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 ' '
1034
Akroneac374d2020-07-07 09:00:44 +02001035 my $tok = KorAP::XML::TEI::Tokenization::conservative($txt, $offset);
1036 push @tok_tokens_con, @$tok;
Peter Harders6f526a32020-06-29 21:44:41 +02001037
Akroneac374d2020-07-07 09:00:44 +02001038 $tok = KorAP::XML::TEI::Tokenization::aggressive($txt, $offset);
1039 push @tok_tokens_agg, @$tok;
Peter Harders6f526a32020-06-29 21:44:41 +02001040
1041 ##$offset = $dl+1;
1042
1043 $offset = $dl;
1044
1045 } # fi
1046
1047 } # fi: $_GEN_TOK_DUMMY
1048
1049
1050 #elsif ( $e->[0] == XML_READER_TYPE_ATTRIBUTE ) # attribute node
1051 # note: attributes cannot be processed like this ( => use 'XCT_ATTRIBUTE_ARRAY' - see above )
1052
1053
1054 }else{ # not yet handled type
1055
1056 die "ERROR ($0): Not yet handled type (\$e->[0]=".$e->[0].") ... => Aborting\n";
1057 }
1058
1059 } # end: foreach iteration
1060
1061} # end: sub retr_info
1062
1063
1064sub select_tokenization { # called from process()
1065
1066 #print STDERR "$0: select_tokenization() ...\n";
1067
1068 ## DEPRECATED (only IDS-intern)
Peter Hardersd892a582020-02-12 15:45:22 +01001069 if( $_GEN_TOK_BAS ) {
1070 if( $select->can_read(3600) ){ # wait 60m for external tokenizer
1071 $_ = <$chld_out>;
1072 my @bounds = split;
Peter Harders6f526a32020-06-29 21:44:41 +02001073 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_bas", $text_id_esc, \@bounds);
Peter Hardersd892a582020-02-12 15:45:22 +01001074 while($select->can_read(0)) {
1075 $_ = <$chld_out>;
1076 if (defined $_ && $_ ne '') {
1077 print STDERR "WARNING: extra output: $_\n"
1078 } else {
1079 print STDERR "WARNING: tokenizer seems to have crashed, restarting.\n";
1080 startTokenizer();
1081 }
1082 }
1083 }else{
Akron85717512020-07-08 11:19:19 +02001084 $zipper->close;
Peter Hardersd892a582020-02-12 15:45:22 +01001085 die "ERROR ($0): cannot retrieve token bounds from external tokenizer for text '$text_id' => Aborting ...\n";
1086 }
Peter Harders6f526a32020-06-29 21:44:41 +02001087 ##
Peter Hardersd892a582020-02-12 15:45:22 +01001088 }elsif( $_GEN_TOK_DUMMY ){
Peter Harders6f526a32020-06-29 21:44:41 +02001089 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_con", $text_id_esc, \@tok_tokens_con);
1090 write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_agg", $text_id_esc, \@tok_tokens_agg);
Peter Hardersd892a582020-02-12 15:45:22 +01001091 }
Peter Hardersd892a582020-02-12 15:45:22 +01001092
Peter Harders6f526a32020-06-29 21:44:41 +02001093 #print STDERR "$0: write_tokenization(): DONE\n";
1094
1095} # end: select_tokenization
1096
1097sub write_tokenization { # called from select_tokenization()
1098
1099 my ( $fname, $textid_esc, $bounds ) = @_;
Peter Hardersd892a582020-02-12 15:45:22 +01001100
Peter Harders6f526a32020-06-29 21:44:41 +02001101 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1102 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\"$text_id_esc\" xmlns=\"http://ids-mannheim.de/ns/KorAP\""
1103 ." version=\"KorAP-0.4\">\n <spanList>\n";
1104
1105 $c = 0;
1106
1107 for( $i = 0; $i < ($#$bounds + 1); $i += 2 ){
1108
Peter Hardersd892a582020-02-12 15:45:22 +01001109 $output .= " <span id=\"t_$c\" from=\"".$bounds->[$i]."\" to=\"".$bounds->[$i+1]."\" />\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001110
Peter Hardersd892a582020-02-12 15:45:22 +01001111 $c++;
1112 }
Peter Hardersd892a582020-02-12 15:45:22 +01001113
Peter Harders6f526a32020-06-29 21:44:41 +02001114 $output .= " </spanList>\n</layer>";
1115
Akron85717512020-07-08 11:19:19 +02001116 $zipper->new_stream($fname)->print($output);
Peter Harders6f526a32020-06-29 21:44:41 +02001117
1118} # end: sub write_tokenization
1119
1120
1121sub write_structures { # called from process()
1122
1123 # ~ write @structures ~
Peter Hardersd892a582020-02-12 15:45:22 +01001124
1125 #print STDERR "$0: write_structures(): ...\n";
1126
Peter Harders6f526a32020-06-29 21:44:41 +02001127 if ( $dir eq "" ){
1128
1129 print STDERR "WARNING ($0): write_structures(): empty textSigle => nothing to do ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001130 return;
1131 }
1132
Peter Harders6f526a32020-06-29 21:44:41 +02001133 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1134 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\""
1135 .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 +01001136
1137 $c = 0;
1138
Peter Harders6f526a32020-06-29 21:44:41 +02001139 foreach $ref ( @structures ){
Peter Hardersd892a582020-02-12 15:45:22 +01001140
Peter Harders6f526a32020-06-29 21:44:41 +02001141 ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 ); # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4
1142 # (name, from, to, rec_level), otherwise >4
Peter Hardersd892a582020-02-12 15:45:22 +01001143
Peter Harders6f526a32020-06-29 21:44:41 +02001144 # 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 )
1145
1146 if( $#structures == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){
1147
1148 ${$ref}[$idx] = ${$ref}[ $idx+1 ];
Peter Hardersd892a582020-02-12 15:45:22 +01001149 }
Peter Hardersd892a582020-02-12 15:45:22 +01001150
Peter Harders6f526a32020-06-29 21:44:41 +02001151 # this consistency check is already done in 'retr_info()'
1152 #elsif( ${$ref}[$idx] > ${$ref}[$idx+1] ){ # consistency check: abort, if this doesn't hold
1153 # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):"
1154 # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n";
1155 # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):"
1156 # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n\n$output" }
Peter Hardersd892a582020-02-12 15:45:22 +01001157
Peter Harders6f526a32020-06-29 21:44:41 +02001158 # at least 'POS' should always be there => remove constraint '$_TOKENS_PROC'
1159 #if( $_TOKENS_PROC && ${$ref}[0] ne $_TOKENS_TAG )
Peter Hardersd892a582020-02-12 15:45:22 +01001160
Peter Harders6f526a32020-06-29 21:44:41 +02001161 if( ${$ref}[0] ne $_TOKENS_TAG ){ # $_TOKENS_TAG is already written in 'write_tokens'
Peter Hardersd892a582020-02-12 15:45:22 +01001162
Peter Harders6f526a32020-06-29 21:44:41 +02001163 # l (level): insert information about depth of element in XML-tree (top element = level 1)
1164 $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n"
1165 ." <fs type=\"struct\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
1166 ." <f name=\"name\">${$ref}[ 0 ]</f>\n";
1167
1168 if ( $idx > 2 ) # attributes
1169 {
1170 $output .= " <f name=\"attr\">\n <fs type=\"attr\">\n";
1171
1172 for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){
1173
1174 ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # see explanation in func. 'write_tokens'
1175
1176 # attribute (at index $att_idx) with value (at index $att_idx+1)
1177 $output .= " <f name=\"${$ref}[ $att_idx ]\">${$ref}[ $att_idx+1 ]</f>\n";
1178 }
1179
1180 $output .= " </fs>\n </f>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001181 }
1182
Peter Harders6f526a32020-06-29 21:44:41 +02001183 $output .= " </fs>\n </span>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001184
Peter Harders6f526a32020-06-29 21:44:41 +02001185 } # fi: ... ne $_TOKENS_TAG
Peter Hardersd892a582020-02-12 15:45:22 +01001186
1187 $c++;
1188
Peter Harders6f526a32020-06-29 21:44:41 +02001189 } # end: foreach
Peter Hardersd892a582020-02-12 15:45:22 +01001190
1191 $output .= " </spanList>\n</layer>";
1192
Peter Harders6f526a32020-06-29 21:44:41 +02001193 $output = encode_utf8( $output );
Peter Hardersd892a582020-02-12 15:45:22 +01001194
Akron85717512020-07-08 11:19:19 +02001195 $zipper->new_stream("$_root_dir$dir/$_structure_dir/$_structure_file")
1196 ->print($output);
Peter Hardersd892a582020-02-12 15:45:22 +01001197
1198 #print STDERR "$0: write_structures(): DONE\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001199
Peter Hardersd892a582020-02-12 15:45:22 +01001200} # end: sub write_structures
1201
1202
Peter Harders6f526a32020-06-29 21:44:41 +02001203sub write_tokens { # called from process()
1204
1205 # ~ write @tokens ~
1206
1207 #print STDERR "$0: write_tokens(): ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001208
1209 if( $dir eq "" ){
Peter Harders6f526a32020-06-29 21:44:41 +02001210
1211 print STDERR "WARNING ($0): write_tokens(): empty textSigle => nothing to do ...\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001212 return;
1213 }
1214
Peter Harders6f526a32020-06-29 21:44:41 +02001215 $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\""
1216 ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\""
1217 .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 +01001218
1219 $c = 0;
1220
Peter Harders6f526a32020-06-29 21:44:41 +02001221 foreach $ref ( @tokens ){
Peter Hardersd892a582020-02-12 15:45:22 +01001222
Peter Harders6f526a32020-06-29 21:44:41 +02001223 # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4 (name, from, to, rec_level), otherwise >4
1224 ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 );
Peter Hardersd892a582020-02-12 15:45:22 +01001225
Peter Harders6f526a32020-06-29 21:44:41 +02001226 # 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())
1227 if ( $#tokens == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){
1228
1229 ${$ref}[ $idx ] = ${$ref}[ $idx+1 ]; # TODO: check
Peter Hardersd892a582020-02-12 15:45:22 +01001230 }
Peter Hardersd892a582020-02-12 15:45:22 +01001231
Peter Harders6f526a32020-06-29 21:44:41 +02001232 # l (level): insert information about depth of element in XML-tree (top element = level 1)
1233 $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n"
1234 ." <fs type=\"lex\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
1235 ." <f name=\"lex\">\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001236
Peter Harders6f526a32020-06-29 21:44:41 +02001237 if ( $idx > 2 ){ # attributes
1238
Peter Hardersd892a582020-02-12 15:45:22 +01001239 $output .= " <fs>\n";
1240
Peter Harders6f526a32020-06-29 21:44:41 +02001241 for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){
Peter Hardersd892a582020-02-12 15:45:22 +01001242
Peter Harders6f526a32020-06-29 21:44:41 +02001243 ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # ... <w lemma="&gt;" ana="PUNCTUATION">&gt;</w> ...
1244 # the '&gt;' is translated to '>' and hence the result would be '<f name="lemma">></f>'
Peter Hardersd892a582020-02-12 15:45:22 +01001245
Peter Harders6f526a32020-06-29 21:44:41 +02001246 if ( $_INLINE_ANNOT && ${$ref}[ $att_idx ] eq "$_INLINE_ATT_RD" ){
Peter Hardersd892a582020-02-12 15:45:22 +01001247
Peter Harders6f526a32020-06-29 21:44:41 +02001248 ${$ref}[ $att_idx+1 ] =~ /^([^ ]+)(?: (.+))?$/;
1249
1250 die "ERROR (write_tokens()): unexpected format! => Aborting ... (att: ${$ref}[ $att_idx+1 ])\n"
1251 if ( $_INLINE_POS_WR && not defined $1 ) || ( $_INLINE_MSD_WR && not defined $2 );
1252
1253 if ( "$_INLINE_POS_WR" ){
1254
1255 $output .= " <f name=\"$_INLINE_POS_WR\">";
Peter Hardersd892a582020-02-12 15:45:22 +01001256 $output .= "$1" if defined $1;
1257 $output .= "</f>\n";
1258 }
Peter Harders6f526a32020-06-29 21:44:41 +02001259
1260 if ( "$_INLINE_MSD_WR" ){
1261
1262 $output .= " <f name=\"$_INLINE_MSD_WR\">";
Peter Hardersd892a582020-02-12 15:45:22 +01001263 $output .= "$2" if defined $2;
1264 $output .= "</f>\n";
1265 }
1266
Peter Harders6f526a32020-06-29 21:44:41 +02001267 } elsif ( $_INLINE_ANNOT && "$_INLINE_LEM_RD" && ${$ref}[ $att_idx ] eq "$_INLINE_LEM_RD" ){
Peter Hardersd892a582020-02-12 15:45:22 +01001268
Peter Harders6f526a32020-06-29 21:44:41 +02001269 $output .= " <f name=\"$_INLINE_LEM_WR\">${$ref}[ $att_idx+1 ]</f>\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001270
Peter Harders6f526a32020-06-29 21:44:41 +02001271 } else { # all other attributes
Peter Hardersd892a582020-02-12 15:45:22 +01001272
Peter Harders6f526a32020-06-29 21:44:41 +02001273 $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 +01001274 }
1275
Peter Harders6f526a32020-06-29 21:44:41 +02001276 } # end: for
Peter Hardersd892a582020-02-12 15:45:22 +01001277
1278 $output .= " </fs>\n";
Peter Harders6f526a32020-06-29 21:44:41 +02001279
1280 } # fi: attributes
Peter Hardersd892a582020-02-12 15:45:22 +01001281
1282 $output .= " </f>\n </fs>\n </span>\n";
1283
1284 $c++;
1285
Peter Harders6f526a32020-06-29 21:44:41 +02001286 } # end: foreach
Peter Hardersd892a582020-02-12 15:45:22 +01001287
1288 $output .= " </spanList>\n</layer>";
1289
Peter Harders6f526a32020-06-29 21:44:41 +02001290 $output = encode_utf8( $output );
Peter Hardersd892a582020-02-12 15:45:22 +01001291
Akron85717512020-07-08 11:19:19 +02001292 $zipper->new_stream("$_root_dir$dir/$_tokens_dir/$_tokens_file")
1293 ->print($output);
Peter Hardersd892a582020-02-12 15:45:22 +01001294
Peter Harders6f526a32020-06-29 21:44:41 +02001295 #print STDERR "$0: write_tokens(): DONE\n";
Peter Hardersd892a582020-02-12 15:45:22 +01001296
Peter Harders6f526a32020-06-29 21:44:41 +02001297} # end: sub write_tokens
Peter Hardersd892a582020-02-12 15:45:22 +01001298
Peter Hardersd892a582020-02-12 15:45:22 +01001299
Peter Harders6f526a32020-06-29 21:44:41 +02001300## DEPRECATED ($_GEN_TOK_BAS: only IDS-intern)
Peter Hardersd892a582020-02-12 15:45:22 +01001301sub startTokenizer {
1302 $pid = open2($chld_out, $chld_in, 'java -cp '. join(":", ".", glob(&dirname(__FILE__)."/../target/*.jar"))." de.ids_mannheim.korap.tokenizer.KorAPTokenizerImpl");
1303 $select = IO::Select->new();
1304 $select->add(*$chld_out);
1305}
Peter Harders6f526a32020-06-29 21:44:41 +02001306##
Akrond949e182020-02-14 12:23:57 +01001307
1308__END__
1309
1310=pod
1311
1312=encoding utf8
1313
1314=head1 NAME
1315
1316tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
1317
1318=head1 SYNOPSIS
1319
1320 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
1321
1322=head1 DESCRIPTION
1323
1324C<tei2korapxml> is a script to convert TEI P5 and I5 based documents
Peter Harders6f526a32020-06-29 21:44:41 +02001325
Akrond949e182020-02-14 12:23:57 +01001326to the KorAP-XML format. If no specific input is defined, data is
Peter Harders6f526a32020-06-29 21:44:41 +02001327
Akrond949e182020-02-14 12:23:57 +01001328read from C<STDIN>. If no specific output is defined, data is written
Peter Harders6f526a32020-06-29 21:44:41 +02001329
Akrond949e182020-02-14 12:23:57 +01001330to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +02001331
Akrond949e182020-02-14 12:23:57 +01001332This program is usually called from inside another script.
1333
1334=head1 INSTALLATION
1335
1336C<tei2korapxml> requires L<libxml2-dev> bindings to build. When
Peter Harders6f526a32020-06-29 21:44:41 +02001337
Akrond949e182020-02-14 12:23:57 +01001338these bindings are available, the preferred way to install the script is
Peter Harders6f526a32020-06-29 21:44:41 +02001339
Akrond949e182020-02-14 12:23:57 +01001340to use L<cpanm|App::cpanminus>.
1341
1342 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
1343
1344In case everything went well, the C<tei2korapxml> tool will
Peter Harders6f526a32020-06-29 21:44:41 +02001345
Akrond949e182020-02-14 12:23:57 +01001346be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +02001347
Akrond949e182020-02-14 12:23:57 +01001348Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
1349
1350=head1 OPTIONS
1351
1352=over 2
1353
1354=item B<--base|-b>
1355
1356The base directory for output. Defaults to C<.>.
1357
1358=item B<--help|-h>
1359
1360Print help information.
1361
1362=item B<--version|-v>
1363
1364Print version information.
1365
1366=back
1367
1368=head1 COPYRIGHT AND LICENSE
1369
1370Copyright (C) 2020, L<IDS Mannheim|https://www.ids-mannheim.de/>
1371
1372Author: Peter Harders
1373
1374Contributors: Marc Kupietz, Carsten Schnober, Nils Diewald
1375
1376L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
Peter Harders6f526a32020-06-29 21:44:41 +02001377
Akrond949e182020-02-14 12:23:57 +01001378Corpus Analysis Platform at the
Peter Harders6f526a32020-06-29 21:44:41 +02001379
Akrond949e182020-02-14 12:23:57 +01001380L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
Peter Harders6f526a32020-06-29 21:44:41 +02001381
Akrond949e182020-02-14 12:23:57 +01001382member of the
Peter Harders6f526a32020-06-29 21:44:41 +02001383
Akrond949e182020-02-14 12:23:57 +01001384L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
1385
1386This program is free software published under the
Peter Harders6f526a32020-06-29 21:44:41 +02001387
Akrond949e182020-02-14 12:23:57 +01001388L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-TEI/master/LICENSE>.
1389
1390=cut