blob: f15376f31303f8a53299b4c632fc9fd86d63bddc [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
Akron3378dfd2020-08-01 15:01:36 +02005use Log::Any '$log';
6use Log::Any::Adapter;
Peter Harders6f526a32020-06-29 21:44:41 +02007use Pod::Usage;
8use Getopt::Long qw(GetOptions :config no_auto_abbrev);
9
10use File::Basename qw(dirname);
Peter Hardersd892a582020-02-12 15:45:22 +010011
Akroneaa96232020-10-15 17:06:15 +020012use Encode qw(decode);
Peter Hardersd892a582020-02-12 15:45:22 +010013
Peter Hardersd892a582020-02-12 15:45:22 +010014use XML::CompactTree::XS;
15use XML::LibXML::Reader;
Peter Hardersd892a582020-02-12 15:45:22 +010016
Akron4f67cd42020-07-02 12:27:58 +020017use FindBin;
18BEGIN {
19 unshift @INC, "$FindBin::Bin/../lib";
20};
21
Akrona10ad592020-08-03 11:20:23 +020022use KorAP::XML::TEI qw!remove_xml_comments!;
Akron8b511f92020-07-09 17:28:08 +020023use KorAP::XML::TEI::Tokenizer::External;
Akrond9627472020-07-09 16:53:09 +020024use KorAP::XML::TEI::Tokenizer::Conservative;
25use KorAP::XML::TEI::Tokenizer::Aggressive;
Akron7501ca02020-08-01 21:05:25 +020026use KorAP::XML::TEI::Annotations::Collector;
Akrona10ad592020-08-03 11:20:23 +020027use KorAP::XML::TEI::Data;
Akron85717512020-07-08 11:19:19 +020028use KorAP::XML::TEI::Zipper;
Akronf57ed812020-07-27 10:37:52 +020029use KorAP::XML::TEI::Header;
Peter Hardersd892a582020-02-12 15:45:22 +010030
Marc Kupietz1e882fb2020-09-09 00:05:46 +020031eval {
32 require KorAP::XML::TEI::Tokenizer::KorAP;
33 1;
34};
Peter Harders1c5ce152020-07-22 18:02:50 +020035
Akronf7084c42021-01-07 10:25:22 +010036our $VERSION = '0.03';
Peter Harders6f526a32020-06-29 21:44:41 +020037
Akrond949e182020-02-14 12:23:57 +010038our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n";
39
Akronb3649472020-09-29 08:24:46 +020040# Set to 1 for minimal more debug output (no need to be parametrized)
41use constant DEBUG => $ENV{KORAPXMLTEI_DEBUG} // 0;
Peter Hardersd892a582020-02-12 15:45:22 +010042
Peter Harders6f526a32020-06-29 21:44:41 +020043# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010044GetOptions(
Peter Harders6f526a32020-06-29 21:44:41 +020045 "root|r=s" => \(my $_root_dir = '.'), # name of root directory inside zip file
46 "input|i=s" => \(my $input_fname = ''), # input file (yet only TEI I5 Format accepted)
Akron8b511f92020-07-09 17:28:08 +020047 'tokenizer-call|tc=s' => \(my $tokenizer_call), # Temporary argument for testing purposes
Marc Kupietz1e882fb2020-09-09 00:05:46 +020048 'tokenizer-korap|tk' => \(my $tokenizer_korap), # use KorAP-tokenizer
Akron6d7b8e42020-09-29 07:37:41 +020049 'tokenizer-internal|ti' => \(my $tokenizer_intern), # use intern tokenization (default = no)
Marc Kupietz985da0c2021-02-15 19:29:50 +010050 'use-tokenizer-sentence-splits|s' => (\my $use_tokenizer_sentence_splits), # use KorAP tokenizer to split s (default=no)
Akron3378dfd2020-08-01 15:01:36 +020051 'log|l=s' => \(my $log_level = 'notice'),
Akron8b511f92020-07-09 17:28:08 +020052 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010053 pod2usage(
54 -verbose => 99,
55 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
56 -msg => $VERSION_MSG,
57 -output => '-'
58 )
59 },
60 'version|v' => sub {
61 pod2usage(
62 -verbose => 0,
63 -msg => $VERSION_MSG,
64 -output => '-'
65 )
66 }
Peter Hardersd892a582020-02-12 15:45:22 +010067);
68
Marc Kupietz44b1f252020-11-26 16:31:40 +010069binmode(STDERR, ":encoding(UTF-8)");
Akron3378dfd2020-08-01 15:01:36 +020070Log::Any::Adapter->set('Stderr', log_level => $log_level);
71
Akronb3649472020-09-29 08:24:46 +020072$log->notice('Debugging is activated') if DEBUG;
73
Peter Harders6f526a32020-06-29 21:44:41 +020074#
75# ~~~ parameter (mandatory) ~~~
76#
Peter Harders6f526a32020-06-29 21:44:41 +020077my $_TEXT_BODY = "text"; # tag (without attributes), which contains the primary text
Akron0c41ab32020-09-29 07:33:33 +020078# optional
Peter Harders6f526a32020-06-29 21:44:41 +020079my $_CORP_HEADER_BEG = "idsHeader type=\"corpus\""; # just keep the correct order of the attributes and evtl. add an '.*' between them
Akron0c41ab32020-09-29 07:33:33 +020080# optional
Peter Harders6f526a32020-06-29 21:44:41 +020081my $_DOC_HEADER_BEG = "idsHeader type=\"document\""; # analog
Akron0c41ab32020-09-29 07:33:33 +020082# mandatory
Peter Harders6f526a32020-06-29 21:44:41 +020083my $_TEXT_HEADER_BEG = "idsHeader type=\"text\""; # analog
Akron09e0b2c2020-07-28 15:57:01 +020084
Peter Harders6f526a32020-06-29 21:44:41 +020085#
86# ~~~ constants ~~~
87#
Peter Hardersd892a582020-02-12 15:45:22 +010088
Akron0c41ab32020-09-29 07:33:33 +020089
Peter Harders41c35622020-07-12 01:16:22 +020090## extern tokenization
Marc Kupietz1e882fb2020-09-09 00:05:46 +020091my $_GEN_TOK_EXT = $tokenizer_call || $tokenizer_korap ? 1 : 0;
92
Marc Kupietz985da0c2021-02-15 19:29:50 +010093if ($use_tokenizer_sentence_splits && !$tokenizer_korap) {
94 die $log->fatal("Sentence splitting is currently only supported by KorAP tokenizer (use -tk to activate it");
95}
96
Akron0c41ab32020-09-29 07:33:33 +020097my $ext_tok;
98if ($tokenizer_call) {
99 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
100}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200101
Akron0c41ab32020-09-29 07:33:33 +0200102elsif ($tokenizer_korap) {
Marc Kupietz985da0c2021-02-15 19:29:50 +0100103 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron0c41ab32020-09-29 07:33:33 +0200104};
105my $_tok_file_ext = "tokens.xml";
Peter Harders6f526a32020-06-29 21:44:41 +0200106##
107
Akron0c41ab32020-09-29 07:33:33 +0200108
Akron8b511f92020-07-09 17:28:08 +0200109## intern tokenization
Peter Hardersf9c51242020-07-21 02:37:44 +0200110my $_GEN_TOK_INT = $tokenizer_intern; # simple tokenization (recommended for testing)
Akron0c41ab32020-09-29 07:33:33 +0200111my $_tok_file_con = "tokens_conservative.xml";
112my $_tok_file_agg = "tokens_aggressive.xml";
113my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
114my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Peter Harders41c35622020-07-12 01:16:22 +0200115##
116
Akron0c41ab32020-09-29 07:33:33 +0200117
Peter Harders41c35622020-07-12 01:16:22 +0200118my $_tok_dir = "base"; # name of directory for storing tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200119
Peter Harders6f526a32020-06-29 21:44:41 +0200120my $_XCT_LN = 0; # only for debugging: include line numbers in elements of $tree_data
121 # (see also manpage of XML::CompactTree::XS)
122
123my $_header_file = "header.xml"; # name of files containing the text, document and corpus header
124my $_data_file = "data.xml"; # name of file containing the primary text data (tokens)
125my $_structure_dir = "struct"; # name of directory containing the $_structure_file
126my $_structure_file = "structure.xml"; # name of file containing all tags (except ${_TOKEN_TAG}'s) related information
127 # (= their names and byte offsets in $_data)
128## TODO: optional (different annotation tools can produce more zip-files for feeding into KorAP-XML-Krill)
129my $_TOKENS_PROC = 1; # on/off: processing of ${_TOKEN_TAG}'s (default: 1)
130my $_tokens_dir = "tokens"; # name of directory containing the $_tokens_file
131my $_tokens_file = "morpho.xml"; # name of file containing all ${_TOKEN_TAG}'s related information (=their byte offsets in $_data)
132 # - evtl. with additional inline annotations
133my $_TOKENS_TAG = "w"; # name of tag containing all information stored in $_tokens_file
134
135## TODO: optional
136# handling inline annotations (inside $_TOKENS_TAG)
Akrone68ec0c2020-07-28 18:06:19 +0200137my $_INLINE_ANNOT = $ENV{KORAPXMLTEI_INLINE}?1:0; # on/off: set to 1 if inline annotations are present and should be processed (default: 0)
Akron09e0b2c2020-07-28 15:57:01 +0200138
Peter Harders6f526a32020-06-29 21:44:41 +0200139
140#
141# ~~~ variables ~~~
142#
143
Akron7501ca02020-08-01 21:05:25 +0200144# Initialize Token- and Structure-Collector
145my $tokens = KorAP::XML::TEI::Annotations::Collector->new;
146my $structures = KorAP::XML::TEI::Annotations::Collector->new;
Akron09e0b2c2020-07-28 15:57:01 +0200147
148
Akrona10ad592020-08-03 11:20:23 +0200149# Initialize Data-Collector
150my $data = KorAP::XML::TEI::Data->new;
151
152
Akron85717512020-07-08 11:19:19 +0200153# Initialize zipper
Akron3bdc0a32020-08-03 12:12:56 +0200154my $zipper = KorAP::XML::TEI::Zipper->new($_root_dir);
Peter Harders6f526a32020-06-29 21:44:41 +0200155my $input_fh; # input file handle (default: stdin)
156
Peter Harders6f526a32020-06-29 21:44:41 +0200157my $dir; # text directory (below $_root_dir)
Peter Harders6f526a32020-06-29 21:44:41 +0200158
Akron0c41ab32020-09-29 07:33:33 +0200159my ( $text_id,
160 $text_id_esc ); # '$text_id_esc' = escaped version of $text_id
Peter Harders6f526a32020-06-29 21:44:41 +0200161
Peter Harders6f526a32020-06-29 21:44:41 +0200162my ( $reader, # instance of 'XML::LibXML::Reader->new' (on input '$buf_in')
163 $tree_data ); # instance of 'XML::CompactTree::XS::readSubtreeToPerl' (on input '$reader')
164
165# these are only used inside recursive function 'retr_info'
166my ( $_IDX, # value is set dependent on $_XCT_LN - for extracting array of child elements from element in $tree_data
167 $e, # element from $tree_data
Peter Harders6f526a32020-06-29 21:44:41 +0200168 ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags)
169 $add_one, # ...
Akron7501ca02020-08-01 21:05:25 +0200170 $fval, # ...
Peter Harders41c35622020-07-12 01:16:22 +0200171 %ws); # hash for indices of whitespace-nodes (needed to recorrect from-values)
172 # idea: when closing element, check if it's from-index minus 1 refers to a whitespace-node
Peter Harders6f526a32020-06-29 21:44:41 +0200173 # (means: 'from-index - 1' is a key in %ws).
174 # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1
175
Akron7501ca02020-08-01 21:05:25 +0200176my $c; # index variables used in loops
Peter Harders6f526a32020-06-29 21:44:41 +0200177
Peter Harders6f526a32020-06-29 21:44:41 +0200178
179#
180# ~~~ main ~~~
181#
182
183# ~ initializations ~
184
185($_XCT_LN)?($_IDX=5):($_IDX=4);
186
Akron7501ca02020-08-01 21:05:25 +0200187$fval = 0;
Peter Harders6f526a32020-06-29 21:44:41 +0200188
Akronec2cef22020-07-31 10:00:15 +0200189# Normalize regex for header parsing
190for ($_CORP_HEADER_BEG,
191 $_DOC_HEADER_BEG,
192 $_TEXT_HEADER_BEG) {
193 s!^([^\s]+)(.*)$!$1\[\^>\]*$2!;
194};
Peter Hardersd892a582020-02-12 15:45:22 +0100195
Peter Hardersd892a582020-02-12 15:45:22 +0100196
Peter Harders6f526a32020-06-29 21:44:41 +0200197# ~ read input and write output (text by text) ~
Peter Hardersd892a582020-02-12 15:45:22 +0100198
Akron347be812020-09-29 07:52:52 +0200199my ( $pfx, $sfx );
Peter Hardersd892a582020-02-12 15:45:22 +0100200
Akron347be812020-09-29 07:52:52 +0200201my $tl = 0; # text line (needed for whitespace handling)
Peter Hardersd892a582020-02-12 15:45:22 +0100202
Akron347be812020-09-29 07:52:52 +0200203$input_fh = *STDIN; # input file handle (default: stdin)
Peter Hardersd892a582020-02-12 15:45:22 +0100204
Akron347be812020-09-29 07:52:52 +0200205# Maybe not necessary
206$data->reset;
Peter Hardersd892a582020-02-12 15:45:22 +0100207
Akron347be812020-09-29 07:52:52 +0200208$dir = "";
Peter Hardersd892a582020-02-12 15:45:22 +0100209
Akron347be812020-09-29 07:52:52 +0200210if ( $input_fname ne '' ){
211 unless (open($input_fh, '<', $input_fname)) {
212 die $log->fatal("File '$input_fname' could not be opened.");
213 };
214}
Peter Harders6f526a32020-06-29 21:44:41 +0200215
Akron347be812020-09-29 07:52:52 +0200216# prevents segfaulting of 'XML::LibXML::Reader' inside 'main()' - see notes on 'PerlIO layers' in 'man XML::LibXML')
217# removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
218# see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
219# see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.
220binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200221
Akron347be812020-09-29 07:52:52 +0200222my $pos;
Akroneaa96232020-10-15 17:06:15 +0200223my $input_enc = 'UTF-8';
Akron347be812020-09-29 07:52:52 +0200224my $l = length('</' . $_TEXT_BODY) + 1;
Peter Harders6f526a32020-06-29 21:44:41 +0200225
Akron347be812020-09-29 07:52:52 +0200226# ~ loop (reading input document) ~
Peter Harders6f526a32020-06-29 21:44:41 +0200227
Akron347be812020-09-29 07:52:52 +0200228MAIN: while ( <$input_fh> ){
229
230 $_ = remove_xml_comments( $input_fh, $_ ); # remove HTML (multi-line) comments (<!--...-->)
231
Akroneaa96232020-10-15 17:06:15 +0200232 # Set input encoding
233 if ( index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
234 $input_enc = $2;
235 next;
236 };
237
238 $_ = decode($input_enc, $_);
239
Akron347be812020-09-29 07:52:52 +0200240 if ( index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){
241
242 # ~ start of text body ~
243
244 $pfx = $1;
245 $sfx = $2;
246
247 if ($pfx !~ /^\s*$/ || $sfx !~ /^\s*$/) {
248 die $log->fatal("input line number $.: " .
249 "line with opening text-body tag '${_TEXT_BODY}' " .
250 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200251 };
Peter Harders6f526a32020-06-29 21:44:41 +0200252
Akron347be812020-09-29 07:52:52 +0200253 # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader
254 my $buf_in = '';
Peter Harders90157342020-07-01 21:05:14 +0200255
Akron347be812020-09-29 07:52:52 +0200256 # Iterate over all lines in the text body
257 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200258
Akron347be812020-09-29 07:52:52 +0200259 $_ = remove_xml_comments( $input_fh, $_ );
Akroneaa96232020-10-15 17:06:15 +0200260 $_ = decode($input_enc, $_);
Peter Harders6f526a32020-06-29 21:44:41 +0200261
Akron347be812020-09-29 07:52:52 +0200262 # ~ end of text body ~
263 if (($pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200264
Akron347be812020-09-29 07:52:52 +0200265 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files (s.a.: $_tok_file_ext, $_tok_file_con, $_tok_file_agg)
Peter Harders6f526a32020-06-29 21:44:41 +0200266
Akron347be812020-09-29 07:52:52 +0200267 if ((substr($_, 0, $pos) . substr($_, $l + $pos)) !~ /^\s*$/) {
268 die $log->fatal("input line number $.: " .
269 "line with closing text-body tag '${_TEXT_BODY}'".
270 " contains additional information ... => Aborting (line=$_)");
271 };
Peter Harders6f526a32020-06-29 21:44:41 +0200272
Akron347be812020-09-29 07:52:52 +0200273 if ($dir ne "") {
Peter Harders6f526a32020-06-29 21:44:41 +0200274
Akron347be812020-09-29 07:52:52 +0200275 $reader = XML::LibXML::Reader->new( string => "<text>$buf_in</text>", huge => 1 );
Peter Harders6f526a32020-06-29 21:44:41 +0200276
Akron347be812020-09-29 07:52:52 +0200277 # ~ whitespace handling ~
278 #
279 # Every whitespace inside the processed text is 'significant' and recognized as a node of type 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'
280 # (see function 'retr_info()').
281 #
282 # Definition of significant and insignificant whitespace
283 # (source: https://www.oracle.com/technical-resources/articles/wang-whitespace.html):
284 #
285 # Significant whitespace is part of the document content and should be preserved.
286 # Insignificant whitespace is used when editing XML documents for readability.
287 # These whitespaces are typically not intended for inclusion in the delivery of the document.
288 #
Peter Harders6f526a32020-06-29 21:44:41 +0200289
Akron347be812020-09-29 07:52:52 +0200290 my $param = XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY;
Peter Harders6f526a32020-06-29 21:44:41 +0200291
Akron347be812020-09-29 07:52:52 +0200292 # _XCT_LINE_NUMBERS is only for debugging
293 $param |= XCT_LINE_NUMBERS if $_XCT_LN;
294 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, $param);
Akron598d1a72020-08-02 17:33:31 +0200295
Akron347be812020-09-29 07:52:52 +0200296 $structures->reset;
Akron598d1a72020-08-02 17:33:31 +0200297
Akron347be812020-09-29 07:52:52 +0200298 $tokens->reset if $_TOKENS_PROC;
Akron598d1a72020-08-02 17:33:31 +0200299
Akron347be812020-09-29 07:52:52 +0200300 # ~ whitespace related issue ~
301 $add_one = 0;
302 %ws = ();
Akron598d1a72020-08-02 17:33:31 +0200303
Akron347be812020-09-29 07:52:52 +0200304 # ~ recursion ~
305 retr_info(1, \$tree_data->[2] ); # parse input data
Akron598d1a72020-08-02 17:33:31 +0200306
Akronb3649472020-09-29 08:24:46 +0200307 if (DEBUG) {
Akron347be812020-09-29 07:52:52 +0200308 $log->debug("Writing (utf8-formatted) xml file $dir/$_data_file");
Akron0bb7e722020-09-29 07:48:33 +0200309 };
Akron598d1a72020-08-02 17:33:31 +0200310
Akron347be812020-09-29 07:52:52 +0200311 # ~ write data.xml ~
312 $data->to_zip(
313 $zipper->new_stream("$dir/${_data_file}"),
314 $text_id_esc
315 );
Akron598d1a72020-08-02 17:33:31 +0200316
Akron347be812020-09-29 07:52:52 +0200317 # ~ tokenization ~
318 if ($_GEN_TOK_EXT) {
Akron598d1a72020-08-02 17:33:31 +0200319
Akron347be812020-09-29 07:52:52 +0200320 # Tokenize and output
321 $ext_tok->tokenize($data->data)->to_zip(
322 $zipper->new_stream("$dir/$_tok_dir/$_tok_file_ext"),
323 $text_id_esc
324 );
325 };
Akrona10ad592020-08-03 11:20:23 +0200326
Akron347be812020-09-29 07:52:52 +0200327 if ($_GEN_TOK_INT) {
Akrona10ad592020-08-03 11:20:23 +0200328
Akron347be812020-09-29 07:52:52 +0200329 # Tokenize and output
330 $cons_tok->tokenize($data->data)->to_zip(
331 $zipper->new_stream("$dir/$_tok_dir/$_tok_file_con"),
Akrona10ad592020-08-03 11:20:23 +0200332 $text_id_esc
333 );
Marc Kupietz74ed7f32020-09-09 18:22:07 +0200334
Akron347be812020-09-29 07:52:52 +0200335 $aggr_tok->tokenize($data->data)->to_zip(
336 $zipper->new_stream("$dir/$_tok_dir/$_tok_file_agg"),
337 $text_id_esc
338 );
Akron598d1a72020-08-02 17:33:31 +0200339
Akron347be812020-09-29 07:52:52 +0200340 $aggr_tok->reset;
341 $cons_tok->reset;
342 };
Akron598d1a72020-08-02 17:33:31 +0200343
Marc Kupietz985da0c2021-02-15 19:29:50 +0100344 if ($use_tokenizer_sentence_splits) {
345 $ext_tok->sentencize_from_previous_input($structures);
346 }
347
Akron347be812020-09-29 07:52:52 +0200348 # ~ write structures ~
349 if (!$structures->empty) {
350 $structures->to_zip(
351 $zipper->new_stream("$dir/$_structure_dir/$_structure_file"),
352 $text_id_esc,
353 2 # = structure serialization
354 );
355 };
Akron598d1a72020-08-02 17:33:31 +0200356
Akron347be812020-09-29 07:52:52 +0200357 # ~ write tokens ~
358 if ($_TOKENS_PROC && !$tokens->empty) {
359 $tokens->to_zip(
360 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}"),
361 $text_id_esc,
362 $_INLINE_ANNOT # Either 0 = tokens without inline or 1 = tokens with inline
363 );
364 };
Akron598d1a72020-08-02 17:33:31 +0200365
Akron347be812020-09-29 07:52:52 +0200366 $dir = ""; # reinit.
Akron598d1a72020-08-02 17:33:31 +0200367
Akron347be812020-09-29 07:52:52 +0200368 # Maybe not necessary
369 $data->reset;
Akron598d1a72020-08-02 17:33:31 +0200370
Akron347be812020-09-29 07:52:52 +0200371 } else { # $dir eq ""
Akron598d1a72020-08-02 17:33:31 +0200372
Akron347be812020-09-29 07:52:52 +0200373 $log->warn("Maybe empty textSigle => skipping this text ...\ndata=$data");
Akron598d1a72020-08-02 17:33:31 +0200374 }
Akron598d1a72020-08-02 17:33:31 +0200375
Akron347be812020-09-29 07:52:52 +0200376 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200377 };
378
Akron347be812020-09-29 07:52:52 +0200379 # ~ inside text body ~
Peter Harders6f526a32020-06-29 21:44:41 +0200380
Akron347be812020-09-29 07:52:52 +0200381 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200382
Akron347be812020-09-29 07:52:52 +0200383 # The idea for the below code fragment was to fix (recreate) missing whitespace in a poorly created corpus, in which linebreaks where inserted
384 # into the text with the addition that maybe (or not) whitespace before those linebreaks was unintenionally stripped.
385 #
386 # It soon turned out, that it was best to suggest considering just avoiding linebreaks and putting all primary text tokens into one line (see
387 # example further down and notes on 'Input restrictions' in the manpage).
388 #
389 # Somehow an old first very poor approach remained, which is not stringent, but also doesn't affect one-line text.
390 #
391 # TODO: Maybe it's best, to keep the stripping of whitespace and to just remove the if-clause and to insert a blank by default (with possibly
392 # an option on how newlines in primary text should be handled (stripped or replaced by a whitespace)).
393 #
394 # Examples (how primary text with linebreaks would be converted by below code):
395 #
396 # '...<w>end</w>\n<w>.</w>...' -> '...<w>end</w> <w>.</w>...'
397 # '...<w>,</w>\n<w>this</w>\n<w>is</w>\n<w>it</w>\n<w>!</w>...' -> '<w>,<w> <w>this</w> <w>is</w> <w>it</w> <w>!</w>'.
Peter Hardersd892a582020-02-12 15:45:22 +0100398
Akron347be812020-09-29 07:52:52 +0200399 s/^\s+//; s/\s+$//; # remove consecutive whitespace at beginning and end (mostly one newline)
Akronf57ed812020-07-27 10:37:52 +0200400
Akron347be812020-09-29 07:52:52 +0200401 ### NOTE: this is only relevant, if a text consists of more than one line
402 ### TODO: find a better solution, or create a warning, if a text has more than one line ($tl > 1)
403 ### do testing with 2 different corpora (one with only one-line texts, the other with several lines per text)
404 if (m/<[^>]+>[^<]/) { # line contains at least one tag with at least one character contents
Akronf57ed812020-07-27 10:37:52 +0200405
Akron347be812020-09-29 07:52:52 +0200406 # NOTE: not stringent ('...' stands for text):
407 #
408 # beg1............................end1 => no blank before 'beg1'
409 # beg2....<pb/>...................end2 => no blank before 'beg2'
410 # beg3....<info attr1="val1"/>....end3 => no blank before 'beg3'
411 # beg4....<test>ok</test>.........end4 => blank before 'beg4'
412 #
413 # => beg1....end1beg2...<pb/>...end2beg3....<info attr1="val1"/>....end3 beg4...<test>ok</test>....end4
414 # ^
415 # |_blank between 'end3' and 'beg4'
Akronf57ed812020-07-27 10:37:52 +0200416
Akron347be812020-09-29 07:52:52 +0200417 $tl++; # counter for text lines
Akronf57ed812020-07-27 10:37:52 +0200418
Akron347be812020-09-29 07:52:52 +0200419 s/^(.)/ $1/ if $tl > 1; # insert blank before 1st character (for 2nd line and consecutive lines)
420 }
421 ###
Akronf57ed812020-07-27 10:37:52 +0200422
Akron347be812020-09-29 07:52:52 +0200423 # add line to buffer
424 $buf_in .= $_;
425 };
Akronf57ed812020-07-27 10:37:52 +0200426
Akron347be812020-09-29 07:52:52 +0200427 } elsif (m#^(.*)(<(?:${_TEXT_HEADER_BEG}|${_DOC_HEADER_BEG}|${_CORP_HEADER_BEG}).*)$#) {
Akronf57ed812020-07-27 10:37:52 +0200428
Akron347be812020-09-29 07:52:52 +0200429 # ~ start of header ~
430 $pfx = $1;
431 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200432
Akron347be812020-09-29 07:52:52 +0200433 if ($pfx !~ /^\s*$/) {
434 die $log->fatal("input line number $.: " .
435 "line with opening header tag" .
436 " is not in expected format ... => Aborting (line=$_)");
437 };
438
439 # Parse header
Akroneaa96232020-10-15 17:06:15 +0200440 my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200441
442 # Header was parseable
443 if ($header) {
444
445 # Write header to zip
446 my $file = $header->dir . '/' . $_header_file;
447
Akronb3649472020-09-29 08:24:46 +0200448 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200449
450 $header->to_zip($zipper->new_stream($file));
451
452 # Header is for text level
453 if ($header->type eq 'text') {
454
455 # Remember dir and sigles
456 $dir = $header->dir;
457 $text_id = $header->id;
458 $text_id_esc = $header->id_esc;
459
460 # log output for seeing progression
Marc Kupietz44b1f252020-11-26 16:31:40 +0100461 $log->notice("$0: main(): text_id=$text_id");
Akron347be812020-09-29 07:52:52 +0200462
463 $tl = 0; # reset (needed for ~ whitespace handling ~)
Akronf57ed812020-07-27 10:37:52 +0200464 }
465 }
Akron347be812020-09-29 07:52:52 +0200466 }
467} #end: while
Peter Hardersd892a582020-02-12 15:45:22 +0100468
Akron347be812020-09-29 07:52:52 +0200469$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200470
Akron347be812020-09-29 07:52:52 +0200471$ext_tok->close if $_GEN_TOK_EXT;
Peter Hardersd892a582020-02-12 15:45:22 +0100472
Akron347be812020-09-29 07:52:52 +0200473exit(0);
Peter Hardersd892a582020-02-12 15:45:22 +0100474
Peter Hardersd892a582020-02-12 15:45:22 +0100475
Peter Harders41c35622020-07-12 01:16:22 +0200476sub retr_info { # called from main()
Akron1c4f2202020-07-30 09:28:22 +0200477 # recursion level
478 # (1 = topmost level inside retr_info() = should always be level of tag $_TEXT_BODY)
479 my $rl = shift;
Peter Hardersd892a582020-02-12 15:45:22 +0100480
Marc Kupietz985da0c2021-02-15 19:29:50 +0100481 my $dummy_anno;
482 if ($use_tokenizer_sentence_splits) {
483 $dummy_anno = $structures->new_dummy_annotation();
484 }
485
Peter Harders41c35622020-07-12 01:16:22 +0200486 # Notes on how 'XML::CompactTree::XS' works
Peter Harders6f526a32020-06-29 21:44:41 +0200487 #
Peter Harders41c35622020-07-12 01:16:22 +0200488 # Example: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>
Peter Harders6f526a32020-06-29 21:44:41 +0200489 #
Peter Harders41c35622020-07-12 01:16:22 +0200490 # Print out name of 'node2' for the above example:
491 #
492 # echo '<node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>' | perl -e 'use XML::CompactTree::XS; use XML::LibXML::Reader; $reader = XML::LibXML::Reader->new(IO => STDIN); $data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_LINE_NUMBERS ); print "\x27".$data->[2]->[0]->[5]->[1]->[1]."\x27\n"'
493 #
494 # Exploring the structure of $data ( = reference to below array ):
Peter Harders6f526a32020-06-29 21:44:41 +0200495 #
496 # [ 0: XML_READER_TYPE_DOCUMENT,
497 # 1: ?
Peter Harders41c35622020-07-12 01:16:22 +0200498 # 2: [ 0: [ 0: XML_READER_TYPE_ELEMENT <- start recursion with array '$data->[2]' (see main(): retr_info( \$tree_data->[2] ))
Peter Harders6f526a32020-06-29 21:44:41 +0200499 # 1: 'node'
500 # 2: ?
501 # 3: HASH (attributes)
502 # 4: 1 (line number)
503 # 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT
504 # 1: 'node1'
505 # 2: ?
506 # 3: undefined (no attributes)
507 # 4: 1 (line number)
508 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
509 # 1: 'some '
510 # ]
511 # 1: [ 0: XML_READER_TYPE_ELEMENT
512 # 1: 'n'
513 # 2: ?
514 # 3: undefined (no attributes)
515 # 4: 1 (line number)
Peter Harders41c35622020-07-12 01:16:22 +0200516 # 5: undefined (no child-nodes)
Peter Harders6f526a32020-06-29 21:44:41 +0200517 # ]
518 # 2: [ 0: XML_READER_TYPE_TEXT
519 # 1: ' text'
520 # ]
521 # ]
522 # ]
523 # 1: [ 0: XML_READER_TYPE_ELEMENT
524 # 1: 'node2'
525 # 2: ?
526 # 3: undefined (not attributes)
527 # 4: 1 (line number)
528 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
529 # 1: 'more-text'
530 # ]
531 # ]
532 # ]
533 # ]
534 # ]
535 # ]
536 # ]
537 #
538 # $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT)
539 #
540 # ref($data->[2]) == ARRAY (with 1 element for 'node')
541 # ref($data->[2]->[0]) == ARRAY (with 6 elements)
542 #
543 # $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
544 # $data->[2]->[0]->[1] == 'node'
545 # ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v')
546 # $data->[2]->[0]->[4] == 1 (line number)
547 # ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2')
Peter Harders41c35622020-07-12 01:16:22 +0200548 # # child-nodes of actual node (see $_IDX)
Peter Harders6f526a32020-06-29 21:44:41 +0200549 #
550 # ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements)
551 # $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
552 # $data->[2]->[0]->[5]->[0]->[1] == 'node1'
553 # $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute)
554 # $data->[2]->[0]->[5]->[0]->[4] == 1 (line number)
555 # ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text')
556 #
557 # ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements)
558 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
559 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some '
560 #
561 # ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements)
562 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
563 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n'
564 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute)
565 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number)
Peter Harders41c35622020-07-12 01:16:22 +0200566 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child-nodes)
Peter Harders6f526a32020-06-29 21:44:41 +0200567 #
568 # ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements)
569 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
570 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text'
Akron0c41ab32020-09-29 07:33:33 +0200571 #
Peter Harders6f526a32020-06-29 21:44:41 +0200572 #
573 # retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example.
574 # Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to
575 # ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]).
Peter Hardersd892a582020-02-12 15:45:22 +0100576
Akron0c41ab32020-09-29 07:33:33 +0200577 foreach $e (@{${$_[0]}}) { # iteration through all array elements ($_[0] is a reference to an array reference)
Peter Harders41c35622020-07-12 01:16:22 +0200578
Akron0c41ab32020-09-29 07:33:33 +0200579 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 +0100580
Peter Harders6f526a32020-06-29 21:44:41 +0200581 #~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200582 # from here: tag-node (opening)
Peter Harders6f526a32020-06-29 21:44:41 +0200583 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100584
Peter Harders6f526a32020-06-29 21:44:41 +0200585 # ~ handle structures ~
Peter Hardersd892a582020-02-12 15:45:22 +0100586
Marc Kupietz985da0c2021-02-15 19:29:50 +0100587 my $anno;
588
Akron7501ca02020-08-01 21:05:25 +0200589 # $e->[1] represents the tag name
Marc Kupietz985da0c2021-02-15 19:29:50 +0100590 if ($use_tokenizer_sentence_splits && $e->[1] eq "s") {
591 $anno = $dummy_anno;
592 } else {
593 $anno = $structures->add_new_annotation($e->[1]);
594 }
Peter Hardersd892a582020-02-12 15:45:22 +0100595
Peter Harders6f526a32020-06-29 21:44:41 +0200596 # ~ handle tokens ~
Peter Hardersd892a582020-02-12 15:45:22 +0100597
Akron7501ca02020-08-01 21:05:25 +0200598 # Add element also to token list
599 if ($_TOKENS_PROC && $e->[1] eq $_TOKENS_TAG) {
600 $tokens->add_annotation($anno);
601 };
Peter Hardersd892a582020-02-12 15:45:22 +0100602
Peter Harders6f526a32020-06-29 21:44:41 +0200603 # ~ handle attributes ~
Peter Hardersd892a582020-02-12 15:45:22 +0100604
Akron0c41ab32020-09-29 07:33:33 +0200605 if (defined $e->[3]) { # only if attributes exist
Peter Hardersd892a582020-02-12 15:45:22 +0100606
Akron0c41ab32020-09-29 07:33:33 +0200607 for ($c = 0; $c < @{$e->[3]}; $c += 2) { # with 'XCT_ATTRIBUTE_ARRAY', $node->[3] is an array reference of the form
Peter Harders6f526a32020-06-29 21:44:41 +0200608 # [ name1, value1, name2, value2, ....] of attribute names and corresponding values.
609 # note: arrays are faster (see: http://makepp.sourceforge.net/2.0/perl_performance.html)
Peter Hardersd892a582020-02-12 15:45:22 +0100610
Peter Harders6f526a32020-06-29 21:44:41 +0200611 # '$c' references the 'key' and '$c+1' the 'value'
Akron7501ca02020-08-01 21:05:25 +0200612 $anno->add_attribute(
613 @{$e->[3]}[$c, $c + 1]
614 );
Peter Harders6f526a32020-06-29 21:44:41 +0200615 }
616 }
617
618
619 # ~ index 'from' ~
620
621 # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts
Akrona10ad592020-08-03 11:20:23 +0200622 $anno->set_from($data->position + $add_one);
Peter Harders6f526a32020-06-29 21:44:41 +0200623
624 #~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200625 # until here: tag-node (opening)
Peter Harders6f526a32020-06-29 21:44:41 +0200626 #~~~~
627
628
629 # ~~ RECURSION ~~
630
Akron0c41ab32020-09-29 07:33:33 +0200631 if (defined $e->[$_IDX]) { # do no recursion, if $e->[$_IDX] is not defined (because we have no array of child-nodes, e.g.: <back/>)
Peter Harders6f526a32020-06-29 21:44:41 +0200632
Akron1c4f2202020-07-30 09:28:22 +0200633 retr_info($rl+1, \$e->[$_IDX]); # recursion with array of child-nodes
Peter Harders6f526a32020-06-29 21:44:41 +0200634 }
635
636
637 #~~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200638 # from here: tag-node (closing)
Peter Harders6f526a32020-06-29 21:44:41 +0200639 #~~~~~
640
Akrona10ad592020-08-03 11:20:23 +0200641 my $pos = $data->position;
Peter Harders6f526a32020-06-29 21:44:41 +0200642
Akron7501ca02020-08-01 21:05:25 +0200643 # ~ handle structures and tokens ~
Peter Harders6f526a32020-06-29 21:44:41 +0200644
645 {
Akron7501ca02020-08-01 21:05:25 +0200646 $fval = $anno->from;
Peter Harders6f526a32020-06-29 21:44:41 +0200647
Akron0c41ab32020-09-29 07:33:33 +0200648 if ($fval > 0 && not exists $ws{$fval - 1}) { # ~ whitespace related issue ~
Peter Harders6f526a32020-06-29 21:44:41 +0200649
Peter Harders41c35622020-07-12 01:16:22 +0200650 # ~ previous node was a text-node ~
Peter Harders6f526a32020-06-29 21:44:41 +0200651
Akron7501ca02020-08-01 21:05:25 +0200652 $anno->set_from($fval - 1);
Peter Harders6f526a32020-06-29 21:44:41 +0200653 }
654
655 # in case this fails, check input
Akron0bb7e722020-09-29 07:48:33 +0200656 if (($fval - 1) > $pos) {
657 die $log->fatal("text_id='$text_id', " .
658 "processing of structures: " .
659 "from-value ($fval) is 2 or more greater " .
660 "than to-value ($pos) => please check. Aborting");
661 };
Peter Harders6f526a32020-06-29 21:44:41 +0200662
Peter Harders41c35622020-07-12 01:16:22 +0200663 # TODO: find example for which this case applies
Peter Harders6f526a32020-06-29 21:44:41 +0200664 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
Akron0c41ab32020-09-29 07:33:33 +0200665 #
Akrona10ad592020-08-03 11:20:23 +0200666 # TODO: check, if it's better to remove this line and change above check to 'if ( $fval - 1) >= $pos;
Peter Harders41c35622020-07-12 01:16:22 +0200667 # do testing with bigger corpus excerpt (wikipedia?)
Akrona10ad592020-08-03 11:20:23 +0200668 $anno->set_from($pos) if $fval == $pos + 1;
669 $anno->set_to($pos);
Akron7501ca02020-08-01 21:05:25 +0200670 $anno->set_level($rl);
Peter Harders6f526a32020-06-29 21:44:41 +0200671
Akrona10ad592020-08-03 11:20:23 +0200672 # note: use $pos, because the offsets are _between_ the characters (e.g.: word = 'Hello' => from = 0 (before 'H'), to = 5 (after 'o'))
Peter Harders6f526a32020-06-29 21:44:41 +0200673 }
674
Peter Harders6f526a32020-06-29 21:44:41 +0200675 # ~ whitespace related issue ~
Peter Hardersd892a582020-02-12 15:45:22 +0100676 # clean up
Akron0c41ab32020-09-29 07:33:33 +0200677 delete $ws{$fval - 1} if $fval > 0 && exists $ws{$fval - 1};
Peter Hardersd892a582020-02-12 15:45:22 +0100678
679
Peter Harders41c35622020-07-12 01:16:22 +0200680 #~~~~
681 # until here: tag-node (closing)
682 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100683
684
Akron0c41ab32020-09-29 07:33:33 +0200685 #~~~~~
686 # from here: text- and whitespace-nodes
687 #~~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200688
Akron0c41ab32020-09-29 07:33:33 +0200689 # The 3rd form of nodes, besides text- (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes of the type
690 # 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'.
691 #
692 # When modifiying the previous example (see: Notes on how 'XML::CompactTree::XS' works) by inserting an additional blank between
693 # '</node1>' and '<node2>', the output for '$data->[2]->[0]->[5]->[1]->[1]' is a blank (' ') and it's type is '14'
694 # (XML_READER_TYPE_SIGNIFICANT_WHITESPACE, see 'man XML::LibXML::Reader'):
695 #
696 # echo '<node a="v"><node1>some <n/> text</node1> <node2>more-text</node2></node>' | perl -e 'use XML::CompactTree::XS; use XML::LibXML::Reader; $reader = XML::LibXML::Reader->new(IO => STDIN); $data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_LINE_NUMBERS ); print "node=\x27".$data->[2]->[0]->[5]->[1]->[1]."\x27, type=".$data->[2]->[0]->[5]->[1]->[0]."\n"'
Peter Hardersd892a582020-02-12 15:45:22 +0100697
Akron0c41ab32020-09-29 07:33:33 +0200698 } elsif ($e->[0] == XML_READER_TYPE_TEXT || $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE){
Peter Hardersd892a582020-02-12 15:45:22 +0100699
Peter Harders41c35622020-07-12 01:16:22 +0200700 # Notes on ~ whitespace related issue ~ (referred to the code fragment below)
Peter Harders6f526a32020-06-29 21:44:41 +0200701 #
Peter Harders41c35622020-07-12 01:16:22 +0200702 # Example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...'
Peter Harders6f526a32020-06-29 21:44:41 +0200703 #
704 # Two text-nodes should normally be separated by a blank. In the above example, that would be the 2 text-nodes
Peter Harders41c35622020-07-12 01:16:22 +0200705 # 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' ' (see [2]).
Akron0c41ab32020-09-29 07:33:33 +0200706 #
Peter Harders41c35622020-07-12 01:16:22 +0200707 # The text-node 'Campagne in Frankreich' leads to the setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag,
708 # it's from-index gets set to the correct start-index of '1792' (and not to the start-index of the whitespace-node ' ').
Peter Harders6f526a32020-06-29 21:44:41 +0200709 #
Peter Harders41c35622020-07-12 01:16:22 +0200710 # The assumption here is, that in most cases there _is_ a whitespace node between 2 text-nodes. The below code fragment
711 # enables a way, to check, if this really _was_ the case for the last 2 'non-tag'-nodes, when closing a tag:
Peter Harders6f526a32020-06-29 21:44:41 +0200712 #
Peter Harders41c35622020-07-12 01:16:22 +0200713 # When a whitespace-node is read, its from-index is stored as a hash-key (in %ws), to state that it belongs to a ws-node.
714 # So when closing a tag, it can be checked, if the previous 'non-tag'-node (text or whitespace), which is the one before
715 # the last read 'non-tag'-node, was a actually _not_ a ws-node, but instead a text-node. In that case, the from-value of
716 # the last read 'non-tag'-node has to be corrected (see [1]),
Peter Harders6f526a32020-06-29 21:44:41 +0200717 #
Peter Harders41c35622020-07-12 01:16:22 +0200718 # For whitespace-nodes $add_one is set to 0, so when opening the next tag (in the above example the 2nd 's'-tag), no
Akrona10ad592020-08-03 11:20:23 +0200719 # additional 1 is added (because this was already done by the whitespace-node itself when incrementing the variable $pos).
Peter Harders6f526a32020-06-29 21:44:41 +0200720 #
Peter Harders41c35622020-07-12 01:16:22 +0200721 # [1]
722 # Now, what happens, when 2 text-nodes are _not_ seperated by a whitespace-node (e.g.: <w>Augen<c>,</c></w>)?
723 # In this case, the falsely increased from-value has to be decreased again by 1 when closing the enclosing tag
724 # (see above code fragment '... not exists $ws{ $fval - 1 } ...').
Peter Harders6f526a32020-06-29 21:44:41 +0200725 #
Peter Harders41c35622020-07-12 01:16:22 +0200726 # [2]
727 # Comparing the 2 examples '<w>fu</w> <w>bar</w>' and '<w>fu</w><w> </w><w>bar</w>', is ' ' in both cases handled as a
728 # whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE).
Peter Harders6f526a32020-06-29 21:44:41 +0200729 #
Peter Harders41c35622020-07-12 01:16:22 +0200730 # The from-index of the 2nd w-tag in the second example refers to 'bar', which may not have been the intention
731 # (even though '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug?
Peter Harders6f526a32020-06-29 21:44:41 +0200732 #
Peter Harders41c35622020-07-12 01:16:22 +0200733 # Empty tags also cling to the next text-token - e.g. in '<w>tok1</w> <w>tok2</w><a><b/></a> <w>tok3</w>' are the from-
734 # and to-indizes for the tags 'a' and 'b' both 12, which is the start-index of the token 'tok3'.
Peter Harders6f526a32020-06-29 21:44:41 +0200735
Akron0c41ab32020-09-29 07:33:33 +0200736 if ($e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE) {
Peter Harders6f526a32020-06-29 21:44:41 +0200737
Peter Harders41c35622020-07-12 01:16:22 +0200738 # ~ whitespace-node ~
739
740 # ~ whitespace related issue ~
Peter Harders6f526a32020-06-29 21:44:41 +0200741
742 $add_one = 0;
743
Akrona10ad592020-08-03 11:20:23 +0200744 # state, that this from-index belongs to a whitespace-node
745 # ('++' doesn't mean a thing here - maybe it could be used for a consistency check)
746 $ws{$data->position}++;
Peter Harders6f526a32020-06-29 21:44:41 +0200747
Akron0c41ab32020-09-29 07:33:33 +0200748 } else {
Peter Harders6f526a32020-06-29 21:44:41 +0200749
750 # ~ text-node ~
751
752 $add_one = 1;
Akron0c41ab32020-09-29 07:33:33 +0200753 };
Peter Harders6f526a32020-06-29 21:44:41 +0200754
755
Akrona10ad592020-08-03 11:20:23 +0200756 # ~ update $data ~
Peter Harders6f526a32020-06-29 21:44:41 +0200757
Akrona10ad592020-08-03 11:20:23 +0200758 $data->append($e->[1]);
Peter Harders6f526a32020-06-29 21:44:41 +0200759
Peter Harders41c35622020-07-12 01:16:22 +0200760 #~~~~~
761 # until here: text- and whitespace-nodes
762 #~~~~~
763
764
Akron0c41ab32020-09-29 07:33:33 +0200765 # elsif ( $e->[0] == XML_READER_TYPE_ATTRIBUTE ) # attribute-node
766 # note: attributes cannot be processed like this ( => use 'XCT_ATTRIBUTE_ARRAY' - see above )
Peter Harders6f526a32020-06-29 21:44:41 +0200767
768
Akron0c41ab32020-09-29 07:33:33 +0200769 } else { # not yet handled type
Peter Harders6f526a32020-06-29 21:44:41 +0200770
Akron0bb7e722020-09-29 07:48:33 +0200771 die $log->fatal('Not yet handled type ($e->[0]=' . $e->[0] . ') ... => Aborting');
Peter Harders6f526a32020-06-29 21:44:41 +0200772 }
773
774 } # end: foreach iteration
775
776} # end: sub retr_info
777
Akrond949e182020-02-14 12:23:57 +0100778__END__
779
780=pod
781
782=encoding utf8
783
784=head1 NAME
785
786tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
787
788=head1 SYNOPSIS
789
790 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
791
792=head1 DESCRIPTION
793
Akronee434b12020-07-08 12:53:01 +0200794C<tei2korapxml> is a script to convert TEI P5 and
795L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html>
796based documents to the
797L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
798If no specific input is defined, data is
Akrond949e182020-02-14 12:23:57 +0100799read from C<STDIN>. If no specific output is defined, data is written
800to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +0200801
Akrond949e182020-02-14 12:23:57 +0100802This program is usually called from inside another script.
803
Akronee434b12020-07-08 12:53:01 +0200804=head1 FORMATS
805
806=head2 Input restrictions
807
808=over 2
809
810=item
811
812utf8 encoded
813
814=item
815
816TEI P5 formatted input with certain restrictions:
817
818=over 4
819
820=item
821
822B<mandatory>: text-header with integrated textsigle, text-body
823
824=item
825
826B<optional>: corp-header with integrated corpsigle,
827doc-header with integrated docsigle
828
829=back
830
831=item
832
Akron0c41ab32020-09-29 07:33:33 +0200833All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200834newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200835(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200836into blanks between 2 tokens could lead to additional blanks,
837where there should be none (e.g.: punctuation characters like C<,> or
838C<.> should not be seperated from their predecessor token).
839(see also code section C<~ whitespace handling ~>).
840
841=back
842
843=head2 Notes on the output
844
845=over 2
846
847=item
848
849zip file output (default on C<stdout>) with utf8 encoded entries
850(which together form the KorAP-XML format)
851
852=back
853
Akrond949e182020-02-14 12:23:57 +0100854=head1 INSTALLATION
855
856C<tei2korapxml> requires L<libxml2-dev> bindings to build. When
857these bindings are available, the preferred way to install the script is
858to use L<cpanm|App::cpanminus>.
859
860 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
861
862In case everything went well, the C<tei2korapxml> tool will
863be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200864
Akrond949e182020-02-14 12:23:57 +0100865Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
866
867=head1 OPTIONS
868
869=over 2
870
Akron4e603a52020-07-27 14:23:49 +0200871=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100872
Akron4e603a52020-07-27 14:23:49 +0200873The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100874
875=item B<--help|-h>
876
877Print help information.
878
879=item B<--version|-v>
880
881Print version information.
882
Akron4e603a52020-07-27 14:23:49 +0200883=item B<--tokenizer-call|-tc>
884
885Call an external tokenizer process, that will tokenize
886a single line from STDIN and outputs one token per line.
887
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200888=item B<--tokenizer-korap|-tk>
889
890Use the standard KorAP/DeReKo tokenizer.
891
Akron6d7b8e42020-09-29 07:37:41 +0200892=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200893
894Tokenize the data using two embedded tokenizers,
895that will take an I<Aggressive> and a I<conservative>
896approach.
897
Marc Kupietz985da0c2021-02-15 19:29:50 +0100898=item B<--use-tokenizer-sentence-splits|-s>
899
900Replace existing with, or add new, sentence boundary information
901provided by the KorAP tokenizer (currently supported only).
902
Akron3378dfd2020-08-01 15:01:36 +0200903=item B<--log|-l>
904
905Loglevel for I<Log::Any>. Defaults to C<notice>.
906
Akrond949e182020-02-14 12:23:57 +0100907=back
908
Akronb3649472020-09-29 08:24:46 +0200909=head1 ENVIRONMENT VARIABLES
910
911=over 2
912
913=item B<KORAPXMLTEI_DEBUG>
914
915Activate minimal debugging.
916Defaults to C<false>.
917
918=item B<KORAPXMLTEI_INLINE>
919
920Process inline annotations, if present.
921Defaults to C<false>.
922
923=back
924
Akrond949e182020-02-14 12:23:57 +0100925=head1 COPYRIGHT AND LICENSE
926
927Copyright (C) 2020, L<IDS Mannheim|https://www.ids-mannheim.de/>
928
929Author: Peter Harders
930
Akronaabd0952020-09-29 07:35:08 +0200931Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100932
933L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
934Corpus Analysis Platform at the
935L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
936member of the
937L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
938
939This program is free software published under the
940L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-TEI/master/LICENSE>.
941
942=cut