blob: d4ccb5b272c4a9659b7f48241b4a91f6faf11934 [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
Marc Kupietz8a954e52021-02-16 22:03:07 +010022use KorAP::XML::TEI qw!remove_xml_comments replace_entities!;
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
Akron0c41ab32020-09-29 07:33:33 +020085
Peter Harders41c35622020-07-12 01:16:22 +020086## extern tokenization
Marc Kupietz1e882fb2020-09-09 00:05:46 +020087my $_GEN_TOK_EXT = $tokenizer_call || $tokenizer_korap ? 1 : 0;
88
Marc Kupietz985da0c2021-02-15 19:29:50 +010089if ($use_tokenizer_sentence_splits && !$tokenizer_korap) {
90 die $log->fatal("Sentence splitting is currently only supported by KorAP tokenizer (use -tk to activate it");
91}
92
Akron0c41ab32020-09-29 07:33:33 +020093my $ext_tok;
94if ($tokenizer_call) {
95 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
96}
Marc Kupietz1e882fb2020-09-09 00:05:46 +020097
Akron0c41ab32020-09-29 07:33:33 +020098elsif ($tokenizer_korap) {
Marc Kupietz985da0c2021-02-15 19:29:50 +010099 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron0c41ab32020-09-29 07:33:33 +0200100};
101my $_tok_file_ext = "tokens.xml";
Peter Harders6f526a32020-06-29 21:44:41 +0200102##
103
Akron0c41ab32020-09-29 07:33:33 +0200104
Akron4e3c7e32021-02-18 15:19:53 +0100105#
106# ~~~ constants ~~~
107#
108
109
Akron8b511f92020-07-09 17:28:08 +0200110## intern tokenization
Peter Hardersf9c51242020-07-21 02:37:44 +0200111my $_GEN_TOK_INT = $tokenizer_intern; # simple tokenization (recommended for testing)
Akron0c41ab32020-09-29 07:33:33 +0200112my $_tok_file_con = "tokens_conservative.xml";
113my $_tok_file_agg = "tokens_aggressive.xml";
114my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
115my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Peter Harders41c35622020-07-12 01:16:22 +0200116##
117
Akron0c41ab32020-09-29 07:33:33 +0200118
Peter Harders41c35622020-07-12 01:16:22 +0200119my $_tok_dir = "base"; # name of directory for storing tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200120
Peter Harders6f526a32020-06-29 21:44:41 +0200121my $_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
Akron4e3c7e32021-02-18 15:19:53 +0100133# Handling inline annotations (inside $_TOKENS_TAG)
134my $_INLINE_ANNOT = $ENV{KORAPXMLTEI_INLINE} ? 1 : 0;
Akron09e0b2c2020-07-28 15:57:01 +0200135
Peter Harders6f526a32020-06-29 21:44:41 +0200136
137#
138# ~~~ variables ~~~
139#
140
Akron7501ca02020-08-01 21:05:25 +0200141# Initialize Token- and Structure-Collector
142my $tokens = KorAP::XML::TEI::Annotations::Collector->new;
143my $structures = KorAP::XML::TEI::Annotations::Collector->new;
Akron09e0b2c2020-07-28 15:57:01 +0200144
145
Akrona10ad592020-08-03 11:20:23 +0200146# Initialize Data-Collector
147my $data = KorAP::XML::TEI::Data->new;
148
149
Akron85717512020-07-08 11:19:19 +0200150# Initialize zipper
Akron3bdc0a32020-08-03 12:12:56 +0200151my $zipper = KorAP::XML::TEI::Zipper->new($_root_dir);
Peter Harders6f526a32020-06-29 21:44:41 +0200152my $input_fh; # input file handle (default: stdin)
153
Peter Harders6f526a32020-06-29 21:44:41 +0200154my $dir; # text directory (below $_root_dir)
Peter Harders6f526a32020-06-29 21:44:41 +0200155
Akron0c41ab32020-09-29 07:33:33 +0200156my ( $text_id,
157 $text_id_esc ); # '$text_id_esc' = escaped version of $text_id
Peter Harders6f526a32020-06-29 21:44:41 +0200158
Peter Harders6f526a32020-06-29 21:44:41 +0200159my ( $reader, # instance of 'XML::LibXML::Reader->new' (on input '$buf_in')
160 $tree_data ); # instance of 'XML::CompactTree::XS::readSubtreeToPerl' (on input '$reader')
161
162# these are only used inside recursive function 'retr_info'
Akron4e3c7e32021-02-18 15:19:53 +0100163my ( $_IDX, # value is set dependent on DEBUG - for extracting array of child elements from element in $tree_data
Peter Harders6f526a32020-06-29 21:44:41 +0200164 $e, # element from $tree_data
Peter Harders6f526a32020-06-29 21:44:41 +0200165 ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags)
166 $add_one, # ...
Akron7501ca02020-08-01 21:05:25 +0200167 $fval, # ...
Peter Harders41c35622020-07-12 01:16:22 +0200168 %ws); # hash for indices of whitespace-nodes (needed to recorrect from-values)
169 # idea: when closing element, check if it's from-index minus 1 refers to a whitespace-node
Peter Harders6f526a32020-06-29 21:44:41 +0200170 # (means: 'from-index - 1' is a key in %ws).
171 # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1
172
Akron7501ca02020-08-01 21:05:25 +0200173my $c; # index variables used in loops
Peter Harders6f526a32020-06-29 21:44:41 +0200174
Peter Harders6f526a32020-06-29 21:44:41 +0200175
176#
177# ~~~ main ~~~
178#
179
180# ~ initializations ~
181
Akron4e3c7e32021-02-18 15:19:53 +0100182# Include line numbers in elements of $tree_data for debugging
183DEBUG ? ($_IDX = 5) : ($_IDX = 4);
Peter Harders6f526a32020-06-29 21:44:41 +0200184
Akron7501ca02020-08-01 21:05:25 +0200185$fval = 0;
Peter Harders6f526a32020-06-29 21:44:41 +0200186
Akronec2cef22020-07-31 10:00:15 +0200187# Normalize regex for header parsing
188for ($_CORP_HEADER_BEG,
189 $_DOC_HEADER_BEG,
190 $_TEXT_HEADER_BEG) {
191 s!^([^\s]+)(.*)$!$1\[\^>\]*$2!;
192};
Peter Hardersd892a582020-02-12 15:45:22 +0100193
Peter Hardersd892a582020-02-12 15:45:22 +0100194
Peter Harders6f526a32020-06-29 21:44:41 +0200195# ~ read input and write output (text by text) ~
Peter Hardersd892a582020-02-12 15:45:22 +0100196
Akron347be812020-09-29 07:52:52 +0200197my ( $pfx, $sfx );
Peter Hardersd892a582020-02-12 15:45:22 +0100198
Akron347be812020-09-29 07:52:52 +0200199my $tl = 0; # text line (needed for whitespace handling)
Peter Hardersd892a582020-02-12 15:45:22 +0100200
Akron347be812020-09-29 07:52:52 +0200201$input_fh = *STDIN; # input file handle (default: stdin)
Peter Hardersd892a582020-02-12 15:45:22 +0100202
Akron347be812020-09-29 07:52:52 +0200203# Maybe not necessary
204$data->reset;
Peter Hardersd892a582020-02-12 15:45:22 +0100205
Akron347be812020-09-29 07:52:52 +0200206$dir = "";
Peter Hardersd892a582020-02-12 15:45:22 +0100207
Akron347be812020-09-29 07:52:52 +0200208if ( $input_fname ne '' ){
209 unless (open($input_fh, '<', $input_fname)) {
210 die $log->fatal("File '$input_fname' could not be opened.");
211 };
212}
Peter Harders6f526a32020-06-29 21:44:41 +0200213
Akron347be812020-09-29 07:52:52 +0200214# prevents segfaulting of 'XML::LibXML::Reader' inside 'main()' - see notes on 'PerlIO layers' in 'man XML::LibXML')
215# removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
216# see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
217# see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.
218binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200219
Akron347be812020-09-29 07:52:52 +0200220my $pos;
Akroneaa96232020-10-15 17:06:15 +0200221my $input_enc = 'UTF-8';
Akron347be812020-09-29 07:52:52 +0200222my $l = length('</' . $_TEXT_BODY) + 1;
Peter Harders6f526a32020-06-29 21:44:41 +0200223
Akron347be812020-09-29 07:52:52 +0200224# ~ loop (reading input document) ~
Peter Harders6f526a32020-06-29 21:44:41 +0200225
Akron347be812020-09-29 07:52:52 +0200226MAIN: while ( <$input_fh> ){
227
228 $_ = remove_xml_comments( $input_fh, $_ ); # remove HTML (multi-line) comments (<!--...-->)
229
Akroneaa96232020-10-15 17:06:15 +0200230 # Set input encoding
231 if ( index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
232 $input_enc = $2;
233 next;
234 };
235
236 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100237 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200238
Akron347be812020-09-29 07:52:52 +0200239 if ( index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){
240
241 # ~ start of text body ~
242
243 $pfx = $1;
244 $sfx = $2;
245
246 if ($pfx !~ /^\s*$/ || $sfx !~ /^\s*$/) {
247 die $log->fatal("input line number $.: " .
248 "line with opening text-body tag '${_TEXT_BODY}' " .
249 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200250 };
Peter Harders6f526a32020-06-29 21:44:41 +0200251
Akron347be812020-09-29 07:52:52 +0200252 # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader
253 my $buf_in = '';
Peter Harders90157342020-07-01 21:05:14 +0200254
Akron347be812020-09-29 07:52:52 +0200255 # Iterate over all lines in the text body
256 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200257
Akron347be812020-09-29 07:52:52 +0200258 $_ = remove_xml_comments( $input_fh, $_ );
Akroneaa96232020-10-15 17:06:15 +0200259 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100260 $_ = replace_entities($_);
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
Akron4e3c7e32021-02-18 15:19:53 +0100292 # XCT_LINE_NUMBERS is only needed for debugging
293 # (see XML::CompactTree::XS)
294 $param |= XCT_LINE_NUMBERS if DEBUG;
Akron347be812020-09-29 07:52:52 +0200295 $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, $param);
Akron598d1a72020-08-02 17:33:31 +0200296
Akron347be812020-09-29 07:52:52 +0200297 $structures->reset;
Akron598d1a72020-08-02 17:33:31 +0200298
Akron347be812020-09-29 07:52:52 +0200299 $tokens->reset if $_TOKENS_PROC;
Akron598d1a72020-08-02 17:33:31 +0200300
Akron347be812020-09-29 07:52:52 +0200301 # ~ whitespace related issue ~
302 $add_one = 0;
303 %ws = ();
Akron598d1a72020-08-02 17:33:31 +0200304
Akron347be812020-09-29 07:52:52 +0200305 # ~ recursion ~
306 retr_info(1, \$tree_data->[2] ); # parse input data
Akron598d1a72020-08-02 17:33:31 +0200307
Akronb3649472020-09-29 08:24:46 +0200308 if (DEBUG) {
Akron347be812020-09-29 07:52:52 +0200309 $log->debug("Writing (utf8-formatted) xml file $dir/$_data_file");
Akron0bb7e722020-09-29 07:48:33 +0200310 };
Akron598d1a72020-08-02 17:33:31 +0200311
Akron347be812020-09-29 07:52:52 +0200312 # ~ write data.xml ~
313 $data->to_zip(
314 $zipper->new_stream("$dir/${_data_file}"),
315 $text_id_esc
316 );
Akron598d1a72020-08-02 17:33:31 +0200317
Akron347be812020-09-29 07:52:52 +0200318 # ~ tokenization ~
319 if ($_GEN_TOK_EXT) {
Akron598d1a72020-08-02 17:33:31 +0200320
Akron347be812020-09-29 07:52:52 +0200321 # Tokenize and output
322 $ext_tok->tokenize($data->data)->to_zip(
323 $zipper->new_stream("$dir/$_tok_dir/$_tok_file_ext"),
324 $text_id_esc
325 );
326 };
Akrona10ad592020-08-03 11:20:23 +0200327
Akron347be812020-09-29 07:52:52 +0200328 if ($_GEN_TOK_INT) {
Akrona10ad592020-08-03 11:20:23 +0200329
Akron347be812020-09-29 07:52:52 +0200330 # Tokenize and output
331 $cons_tok->tokenize($data->data)->to_zip(
332 $zipper->new_stream("$dir/$_tok_dir/$_tok_file_con"),
Akrona10ad592020-08-03 11:20:23 +0200333 $text_id_esc
334 );
Marc Kupietz74ed7f32020-09-09 18:22:07 +0200335
Akron347be812020-09-29 07:52:52 +0200336 $aggr_tok->tokenize($data->data)->to_zip(
337 $zipper->new_stream("$dir/$_tok_dir/$_tok_file_agg"),
338 $text_id_esc
339 );
Akron598d1a72020-08-02 17:33:31 +0200340
Akron347be812020-09-29 07:52:52 +0200341 $aggr_tok->reset;
342 $cons_tok->reset;
343 };
Akron598d1a72020-08-02 17:33:31 +0200344
Marc Kupietz985da0c2021-02-15 19:29:50 +0100345 if ($use_tokenizer_sentence_splits) {
346 $ext_tok->sentencize_from_previous_input($structures);
347 }
348
Akron347be812020-09-29 07:52:52 +0200349 # ~ write structures ~
350 if (!$structures->empty) {
351 $structures->to_zip(
352 $zipper->new_stream("$dir/$_structure_dir/$_structure_file"),
353 $text_id_esc,
354 2 # = structure serialization
355 );
356 };
Akron598d1a72020-08-02 17:33:31 +0200357
Akron347be812020-09-29 07:52:52 +0200358 # ~ write tokens ~
359 if ($_TOKENS_PROC && !$tokens->empty) {
360 $tokens->to_zip(
361 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}"),
362 $text_id_esc,
363 $_INLINE_ANNOT # Either 0 = tokens without inline or 1 = tokens with inline
364 );
365 };
Akron598d1a72020-08-02 17:33:31 +0200366
Akron347be812020-09-29 07:52:52 +0200367 $dir = ""; # reinit.
Akron598d1a72020-08-02 17:33:31 +0200368
Akron347be812020-09-29 07:52:52 +0200369 # Maybe not necessary
370 $data->reset;
Akron598d1a72020-08-02 17:33:31 +0200371
Akron347be812020-09-29 07:52:52 +0200372 } else { # $dir eq ""
Akron598d1a72020-08-02 17:33:31 +0200373
Akron347be812020-09-29 07:52:52 +0200374 $log->warn("Maybe empty textSigle => skipping this text ...\ndata=$data");
Akron598d1a72020-08-02 17:33:31 +0200375 }
Akron598d1a72020-08-02 17:33:31 +0200376
Akron347be812020-09-29 07:52:52 +0200377 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200378 };
379
Akron347be812020-09-29 07:52:52 +0200380 # ~ inside text body ~
Peter Harders6f526a32020-06-29 21:44:41 +0200381
Akron347be812020-09-29 07:52:52 +0200382 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200383
Akron347be812020-09-29 07:52:52 +0200384 # The idea for the below code fragment was to fix (recreate) missing whitespace in a poorly created corpus, in which linebreaks where inserted
385 # into the text with the addition that maybe (or not) whitespace before those linebreaks was unintenionally stripped.
386 #
387 # It soon turned out, that it was best to suggest considering just avoiding linebreaks and putting all primary text tokens into one line (see
388 # example further down and notes on 'Input restrictions' in the manpage).
389 #
390 # Somehow an old first very poor approach remained, which is not stringent, but also doesn't affect one-line text.
391 #
392 # 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
393 # an option on how newlines in primary text should be handled (stripped or replaced by a whitespace)).
394 #
395 # Examples (how primary text with linebreaks would be converted by below code):
396 #
397 # '...<w>end</w>\n<w>.</w>...' -> '...<w>end</w> <w>.</w>...'
398 # '...<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 +0100399
Akron347be812020-09-29 07:52:52 +0200400 s/^\s+//; s/\s+$//; # remove consecutive whitespace at beginning and end (mostly one newline)
Akronf57ed812020-07-27 10:37:52 +0200401
Akron347be812020-09-29 07:52:52 +0200402 ### NOTE: this is only relevant, if a text consists of more than one line
403 ### TODO: find a better solution, or create a warning, if a text has more than one line ($tl > 1)
404 ### do testing with 2 different corpora (one with only one-line texts, the other with several lines per text)
405 if (m/<[^>]+>[^<]/) { # line contains at least one tag with at least one character contents
Akronf57ed812020-07-27 10:37:52 +0200406
Akron347be812020-09-29 07:52:52 +0200407 # NOTE: not stringent ('...' stands for text):
408 #
409 # beg1............................end1 => no blank before 'beg1'
410 # beg2....<pb/>...................end2 => no blank before 'beg2'
411 # beg3....<info attr1="val1"/>....end3 => no blank before 'beg3'
412 # beg4....<test>ok</test>.........end4 => blank before 'beg4'
413 #
414 # => beg1....end1beg2...<pb/>...end2beg3....<info attr1="val1"/>....end3 beg4...<test>ok</test>....end4
415 # ^
416 # |_blank between 'end3' and 'beg4'
Akronf57ed812020-07-27 10:37:52 +0200417
Akron347be812020-09-29 07:52:52 +0200418 $tl++; # counter for text lines
Akronf57ed812020-07-27 10:37:52 +0200419
Akron347be812020-09-29 07:52:52 +0200420 s/^(.)/ $1/ if $tl > 1; # insert blank before 1st character (for 2nd line and consecutive lines)
421 }
422 ###
Akronf57ed812020-07-27 10:37:52 +0200423
Akron347be812020-09-29 07:52:52 +0200424 # add line to buffer
425 $buf_in .= $_;
426 };
Akronf57ed812020-07-27 10:37:52 +0200427
Akron347be812020-09-29 07:52:52 +0200428 } elsif (m#^(.*)(<(?:${_TEXT_HEADER_BEG}|${_DOC_HEADER_BEG}|${_CORP_HEADER_BEG}).*)$#) {
Akronf57ed812020-07-27 10:37:52 +0200429
Akron347be812020-09-29 07:52:52 +0200430 # ~ start of header ~
431 $pfx = $1;
432 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200433
Akron347be812020-09-29 07:52:52 +0200434 if ($pfx !~ /^\s*$/) {
435 die $log->fatal("input line number $.: " .
436 "line with opening header tag" .
437 " is not in expected format ... => Aborting (line=$_)");
438 };
439
440 # Parse header
Akroneaa96232020-10-15 17:06:15 +0200441 my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200442
443 # Header was parseable
444 if ($header) {
445
446 # Write header to zip
447 my $file = $header->dir . '/' . $_header_file;
448
Akronb3649472020-09-29 08:24:46 +0200449 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200450
451 $header->to_zip($zipper->new_stream($file));
452
453 # Header is for text level
454 if ($header->type eq 'text') {
455
456 # Remember dir and sigles
457 $dir = $header->dir;
458 $text_id = $header->id;
459 $text_id_esc = $header->id_esc;
460
461 # log output for seeing progression
Marc Kupietz44b1f252020-11-26 16:31:40 +0100462 $log->notice("$0: main(): text_id=$text_id");
Akron347be812020-09-29 07:52:52 +0200463
464 $tl = 0; # reset (needed for ~ whitespace handling ~)
Akronf57ed812020-07-27 10:37:52 +0200465 }
466 }
Akron347be812020-09-29 07:52:52 +0200467 }
468} #end: while
Peter Hardersd892a582020-02-12 15:45:22 +0100469
Akron347be812020-09-29 07:52:52 +0200470$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200471
Akron347be812020-09-29 07:52:52 +0200472$ext_tok->close if $_GEN_TOK_EXT;
Peter Hardersd892a582020-02-12 15:45:22 +0100473
Akron347be812020-09-29 07:52:52 +0200474exit(0);
Peter Hardersd892a582020-02-12 15:45:22 +0100475
Peter Hardersd892a582020-02-12 15:45:22 +0100476
Peter Harders41c35622020-07-12 01:16:22 +0200477sub retr_info { # called from main()
Akron1c4f2202020-07-30 09:28:22 +0200478 # recursion level
479 # (1 = topmost level inside retr_info() = should always be level of tag $_TEXT_BODY)
480 my $rl = shift;
Peter Hardersd892a582020-02-12 15:45:22 +0100481
Marc Kupietz985da0c2021-02-15 19:29:50 +0100482 my $dummy_anno;
483 if ($use_tokenizer_sentence_splits) {
484 $dummy_anno = $structures->new_dummy_annotation();
485 }
486
Peter Harders41c35622020-07-12 01:16:22 +0200487 # Notes on how 'XML::CompactTree::XS' works
Peter Harders6f526a32020-06-29 21:44:41 +0200488 #
Peter Harders41c35622020-07-12 01:16:22 +0200489 # Example: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>
Peter Harders6f526a32020-06-29 21:44:41 +0200490 #
Peter Harders41c35622020-07-12 01:16:22 +0200491 # Print out name of 'node2' for the above example:
492 #
493 # 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"'
494 #
495 # Exploring the structure of $data ( = reference to below array ):
Peter Harders6f526a32020-06-29 21:44:41 +0200496 #
497 # [ 0: XML_READER_TYPE_DOCUMENT,
498 # 1: ?
Peter Harders41c35622020-07-12 01:16:22 +0200499 # 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 +0200500 # 1: 'node'
501 # 2: ?
502 # 3: HASH (attributes)
503 # 4: 1 (line number)
504 # 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT
505 # 1: 'node1'
506 # 2: ?
507 # 3: undefined (no attributes)
508 # 4: 1 (line number)
509 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
510 # 1: 'some '
511 # ]
512 # 1: [ 0: XML_READER_TYPE_ELEMENT
513 # 1: 'n'
514 # 2: ?
515 # 3: undefined (no attributes)
516 # 4: 1 (line number)
Peter Harders41c35622020-07-12 01:16:22 +0200517 # 5: undefined (no child-nodes)
Peter Harders6f526a32020-06-29 21:44:41 +0200518 # ]
519 # 2: [ 0: XML_READER_TYPE_TEXT
520 # 1: ' text'
521 # ]
522 # ]
523 # ]
524 # 1: [ 0: XML_READER_TYPE_ELEMENT
525 # 1: 'node2'
526 # 2: ?
527 # 3: undefined (not attributes)
528 # 4: 1 (line number)
529 # 5: [ 0: [ 0: XML_READER_TYPE_TEXT
530 # 1: 'more-text'
531 # ]
532 # ]
533 # ]
534 # ]
535 # ]
536 # ]
537 # ]
538 #
539 # $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT)
540 #
541 # ref($data->[2]) == ARRAY (with 1 element for 'node')
542 # ref($data->[2]->[0]) == ARRAY (with 6 elements)
543 #
544 # $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
545 # $data->[2]->[0]->[1] == 'node'
546 # ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v')
547 # $data->[2]->[0]->[4] == 1 (line number)
548 # ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2')
Peter Harders41c35622020-07-12 01:16:22 +0200549 # # child-nodes of actual node (see $_IDX)
Peter Harders6f526a32020-06-29 21:44:41 +0200550 #
551 # ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements)
552 # $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
553 # $data->[2]->[0]->[5]->[0]->[1] == 'node1'
554 # $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute)
555 # $data->[2]->[0]->[5]->[0]->[4] == 1 (line number)
556 # ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text')
557 #
558 # ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements)
559 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
560 # $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some '
561 #
562 # ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements)
563 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT)
564 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n'
565 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute)
566 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number)
Peter Harders41c35622020-07-12 01:16:22 +0200567 # $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child-nodes)
Peter Harders6f526a32020-06-29 21:44:41 +0200568 #
569 # ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements)
570 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT)
571 # $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text'
Akron0c41ab32020-09-29 07:33:33 +0200572 #
Peter Harders6f526a32020-06-29 21:44:41 +0200573 #
574 # retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example.
575 # Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to
576 # ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]).
Peter Hardersd892a582020-02-12 15:45:22 +0100577
Akron0c41ab32020-09-29 07:33:33 +0200578 foreach $e (@{${$_[0]}}) { # iteration through all array elements ($_[0] is a reference to an array reference)
Peter Harders41c35622020-07-12 01:16:22 +0200579
Akron0c41ab32020-09-29 07:33:33 +0200580 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 +0100581
Peter Harders6f526a32020-06-29 21:44:41 +0200582 #~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200583 # from here: tag-node (opening)
Peter Harders6f526a32020-06-29 21:44:41 +0200584 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100585
Peter Harders6f526a32020-06-29 21:44:41 +0200586 # ~ handle structures ~
Peter Hardersd892a582020-02-12 15:45:22 +0100587
Marc Kupietz985da0c2021-02-15 19:29:50 +0100588 my $anno;
589
Akron7501ca02020-08-01 21:05:25 +0200590 # $e->[1] represents the tag name
Marc Kupietz985da0c2021-02-15 19:29:50 +0100591 if ($use_tokenizer_sentence_splits && $e->[1] eq "s") {
592 $anno = $dummy_anno;
593 } else {
594 $anno = $structures->add_new_annotation($e->[1]);
595 }
Peter Hardersd892a582020-02-12 15:45:22 +0100596
Peter Harders6f526a32020-06-29 21:44:41 +0200597 # ~ handle tokens ~
Peter Hardersd892a582020-02-12 15:45:22 +0100598
Akron7501ca02020-08-01 21:05:25 +0200599 # Add element also to token list
600 if ($_TOKENS_PROC && $e->[1] eq $_TOKENS_TAG) {
601 $tokens->add_annotation($anno);
602 };
Peter Hardersd892a582020-02-12 15:45:22 +0100603
Peter Harders6f526a32020-06-29 21:44:41 +0200604 # ~ handle attributes ~
Peter Hardersd892a582020-02-12 15:45:22 +0100605
Akron0c41ab32020-09-29 07:33:33 +0200606 if (defined $e->[3]) { # only if attributes exist
Peter Hardersd892a582020-02-12 15:45:22 +0100607
Akron0c41ab32020-09-29 07:33:33 +0200608 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 +0200609 # [ name1, value1, name2, value2, ....] of attribute names and corresponding values.
610 # note: arrays are faster (see: http://makepp.sourceforge.net/2.0/perl_performance.html)
Peter Hardersd892a582020-02-12 15:45:22 +0100611
Peter Harders6f526a32020-06-29 21:44:41 +0200612 # '$c' references the 'key' and '$c+1' the 'value'
Akron7501ca02020-08-01 21:05:25 +0200613 $anno->add_attribute(
614 @{$e->[3]}[$c, $c + 1]
615 );
Peter Harders6f526a32020-06-29 21:44:41 +0200616 }
617 }
618
619
620 # ~ index 'from' ~
621
622 # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts
Akrona10ad592020-08-03 11:20:23 +0200623 $anno->set_from($data->position + $add_one);
Peter Harders6f526a32020-06-29 21:44:41 +0200624
625 #~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200626 # until here: tag-node (opening)
Peter Harders6f526a32020-06-29 21:44:41 +0200627 #~~~~
628
629
630 # ~~ RECURSION ~~
631
Akron0c41ab32020-09-29 07:33:33 +0200632 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 +0200633
Akron1c4f2202020-07-30 09:28:22 +0200634 retr_info($rl+1, \$e->[$_IDX]); # recursion with array of child-nodes
Peter Harders6f526a32020-06-29 21:44:41 +0200635 }
636
637
638 #~~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200639 # from here: tag-node (closing)
Peter Harders6f526a32020-06-29 21:44:41 +0200640 #~~~~~
641
Akrona10ad592020-08-03 11:20:23 +0200642 my $pos = $data->position;
Peter Harders6f526a32020-06-29 21:44:41 +0200643
Akron7501ca02020-08-01 21:05:25 +0200644 # ~ handle structures and tokens ~
Peter Harders6f526a32020-06-29 21:44:41 +0200645
646 {
Akron7501ca02020-08-01 21:05:25 +0200647 $fval = $anno->from;
Peter Harders6f526a32020-06-29 21:44:41 +0200648
Akron0c41ab32020-09-29 07:33:33 +0200649 if ($fval > 0 && not exists $ws{$fval - 1}) { # ~ whitespace related issue ~
Peter Harders6f526a32020-06-29 21:44:41 +0200650
Peter Harders41c35622020-07-12 01:16:22 +0200651 # ~ previous node was a text-node ~
Peter Harders6f526a32020-06-29 21:44:41 +0200652
Akron7501ca02020-08-01 21:05:25 +0200653 $anno->set_from($fval - 1);
Peter Harders6f526a32020-06-29 21:44:41 +0200654 }
655
656 # in case this fails, check input
Akron0bb7e722020-09-29 07:48:33 +0200657 if (($fval - 1) > $pos) {
658 die $log->fatal("text_id='$text_id', " .
659 "processing of structures: " .
660 "from-value ($fval) is 2 or more greater " .
661 "than to-value ($pos) => please check. Aborting");
662 };
Peter Harders6f526a32020-06-29 21:44:41 +0200663
Peter Harders41c35622020-07-12 01:16:22 +0200664 # TODO: find example for which this case applies
Peter Harders6f526a32020-06-29 21:44:41 +0200665 # maybe this is not necessary anymore, because the above recorrection of the from-value suffices
Akron0c41ab32020-09-29 07:33:33 +0200666 #
Akrona10ad592020-08-03 11:20:23 +0200667 # 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 +0200668 # do testing with bigger corpus excerpt (wikipedia?)
Akrona10ad592020-08-03 11:20:23 +0200669 $anno->set_from($pos) if $fval == $pos + 1;
670 $anno->set_to($pos);
Akron7501ca02020-08-01 21:05:25 +0200671 $anno->set_level($rl);
Peter Harders6f526a32020-06-29 21:44:41 +0200672
Akrona10ad592020-08-03 11:20:23 +0200673 # 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 +0200674 }
675
Peter Harders6f526a32020-06-29 21:44:41 +0200676 # ~ whitespace related issue ~
Peter Hardersd892a582020-02-12 15:45:22 +0100677 # clean up
Akron0c41ab32020-09-29 07:33:33 +0200678 delete $ws{$fval - 1} if $fval > 0 && exists $ws{$fval - 1};
Peter Hardersd892a582020-02-12 15:45:22 +0100679
680
Peter Harders41c35622020-07-12 01:16:22 +0200681 #~~~~
682 # until here: tag-node (closing)
683 #~~~~
Peter Hardersd892a582020-02-12 15:45:22 +0100684
685
Akron0c41ab32020-09-29 07:33:33 +0200686 #~~~~~
687 # from here: text- and whitespace-nodes
688 #~~~~~
Peter Harders41c35622020-07-12 01:16:22 +0200689
Akron0c41ab32020-09-29 07:33:33 +0200690 # The 3rd form of nodes, besides text- (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes of the type
691 # 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'.
692 #
693 # When modifiying the previous example (see: Notes on how 'XML::CompactTree::XS' works) by inserting an additional blank between
694 # '</node1>' and '<node2>', the output for '$data->[2]->[0]->[5]->[1]->[1]' is a blank (' ') and it's type is '14'
695 # (XML_READER_TYPE_SIGNIFICANT_WHITESPACE, see 'man XML::LibXML::Reader'):
696 #
697 # 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 +0100698
Akron0c41ab32020-09-29 07:33:33 +0200699 } elsif ($e->[0] == XML_READER_TYPE_TEXT || $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE){
Peter Hardersd892a582020-02-12 15:45:22 +0100700
Peter Harders41c35622020-07-12 01:16:22 +0200701 # Notes on ~ whitespace related issue ~ (referred to the code fragment below)
Peter Harders6f526a32020-06-29 21:44:41 +0200702 #
Peter Harders41c35622020-07-12 01:16:22 +0200703 # Example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...'
Peter Harders6f526a32020-06-29 21:44:41 +0200704 #
705 # 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 +0200706 # 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' ' (see [2]).
Akron0c41ab32020-09-29 07:33:33 +0200707 #
Peter Harders41c35622020-07-12 01:16:22 +0200708 # The text-node 'Campagne in Frankreich' leads to the setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag,
709 # 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 +0200710 #
Peter Harders41c35622020-07-12 01:16:22 +0200711 # The assumption here is, that in most cases there _is_ a whitespace node between 2 text-nodes. The below code fragment
712 # 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 +0200713 #
Peter Harders41c35622020-07-12 01:16:22 +0200714 # 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.
715 # So when closing a tag, it can be checked, if the previous 'non-tag'-node (text or whitespace), which is the one before
716 # 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
717 # the last read 'non-tag'-node has to be corrected (see [1]),
Peter Harders6f526a32020-06-29 21:44:41 +0200718 #
Peter Harders41c35622020-07-12 01:16:22 +0200719 # 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 +0200720 # 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 +0200721 #
Peter Harders41c35622020-07-12 01:16:22 +0200722 # [1]
723 # Now, what happens, when 2 text-nodes are _not_ seperated by a whitespace-node (e.g.: <w>Augen<c>,</c></w>)?
724 # In this case, the falsely increased from-value has to be decreased again by 1 when closing the enclosing tag
725 # (see above code fragment '... not exists $ws{ $fval - 1 } ...').
Peter Harders6f526a32020-06-29 21:44:41 +0200726 #
Peter Harders41c35622020-07-12 01:16:22 +0200727 # [2]
728 # 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
729 # whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE).
Peter Harders6f526a32020-06-29 21:44:41 +0200730 #
Peter Harders41c35622020-07-12 01:16:22 +0200731 # The from-index of the 2nd w-tag in the second example refers to 'bar', which may not have been the intention
732 # (even though '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug?
Peter Harders6f526a32020-06-29 21:44:41 +0200733 #
Peter Harders41c35622020-07-12 01:16:22 +0200734 # 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-
735 # 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 +0200736
Akron0c41ab32020-09-29 07:33:33 +0200737 if ($e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE) {
Peter Harders6f526a32020-06-29 21:44:41 +0200738
Peter Harders41c35622020-07-12 01:16:22 +0200739 # ~ whitespace-node ~
740
741 # ~ whitespace related issue ~
Peter Harders6f526a32020-06-29 21:44:41 +0200742
743 $add_one = 0;
744
Akrona10ad592020-08-03 11:20:23 +0200745 # state, that this from-index belongs to a whitespace-node
746 # ('++' doesn't mean a thing here - maybe it could be used for a consistency check)
747 $ws{$data->position}++;
Peter Harders6f526a32020-06-29 21:44:41 +0200748
Akron0c41ab32020-09-29 07:33:33 +0200749 } else {
Peter Harders6f526a32020-06-29 21:44:41 +0200750
751 # ~ text-node ~
752
753 $add_one = 1;
Akron0c41ab32020-09-29 07:33:33 +0200754 };
Peter Harders6f526a32020-06-29 21:44:41 +0200755
756
Akrona10ad592020-08-03 11:20:23 +0200757 # ~ update $data ~
Peter Harders6f526a32020-06-29 21:44:41 +0200758
Akrona10ad592020-08-03 11:20:23 +0200759 $data->append($e->[1]);
Peter Harders6f526a32020-06-29 21:44:41 +0200760
Peter Harders41c35622020-07-12 01:16:22 +0200761 #~~~~~
762 # until here: text- and whitespace-nodes
763 #~~~~~
764
765
Akron0c41ab32020-09-29 07:33:33 +0200766 # elsif ( $e->[0] == XML_READER_TYPE_ATTRIBUTE ) # attribute-node
767 # note: attributes cannot be processed like this ( => use 'XCT_ATTRIBUTE_ARRAY' - see above )
Peter Harders6f526a32020-06-29 21:44:41 +0200768
769
Akron0c41ab32020-09-29 07:33:33 +0200770 } else { # not yet handled type
Peter Harders6f526a32020-06-29 21:44:41 +0200771
Akron0bb7e722020-09-29 07:48:33 +0200772 die $log->fatal('Not yet handled type ($e->[0]=' . $e->[0] . ') ... => Aborting');
Peter Harders6f526a32020-06-29 21:44:41 +0200773 }
774
775 } # end: foreach iteration
776
777} # end: sub retr_info
778
Akrond949e182020-02-14 12:23:57 +0100779__END__
780
781=pod
782
783=encoding utf8
784
785=head1 NAME
786
787tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
788
789=head1 SYNOPSIS
790
791 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
792
793=head1 DESCRIPTION
794
Akronee434b12020-07-08 12:53:01 +0200795C<tei2korapxml> is a script to convert TEI P5 and
796L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html>
797based documents to the
798L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
799If no specific input is defined, data is
Akrond949e182020-02-14 12:23:57 +0100800read from C<STDIN>. If no specific output is defined, data is written
801to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +0200802
Akrond949e182020-02-14 12:23:57 +0100803This program is usually called from inside another script.
804
Akronee434b12020-07-08 12:53:01 +0200805=head1 FORMATS
806
807=head2 Input restrictions
808
809=over 2
810
811=item
812
Akronee434b12020-07-08 12:53:01 +0200813TEI P5 formatted input with certain restrictions:
814
815=over 4
816
817=item
818
819B<mandatory>: text-header with integrated textsigle, text-body
820
821=item
822
823B<optional>: corp-header with integrated corpsigle,
824doc-header with integrated docsigle
825
826=back
827
828=item
829
Akron0c41ab32020-09-29 07:33:33 +0200830All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200831newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200832(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200833into blanks between 2 tokens could lead to additional blanks,
834where there should be none (e.g.: punctuation characters like C<,> or
835C<.> should not be seperated from their predecessor token).
836(see also code section C<~ whitespace handling ~>).
837
838=back
839
840=head2 Notes on the output
841
842=over 2
843
844=item
845
846zip file output (default on C<stdout>) with utf8 encoded entries
847(which together form the KorAP-XML format)
848
849=back
850
Akrond949e182020-02-14 12:23:57 +0100851=head1 INSTALLATION
852
853C<tei2korapxml> requires L<libxml2-dev> bindings to build. When
854these bindings are available, the preferred way to install the script is
855to use L<cpanm|App::cpanminus>.
856
857 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
858
859In case everything went well, the C<tei2korapxml> tool will
860be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200861
Akrond949e182020-02-14 12:23:57 +0100862Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
863
864=head1 OPTIONS
865
866=over 2
867
Akron4e603a52020-07-27 14:23:49 +0200868=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100869
Akron4e603a52020-07-27 14:23:49 +0200870The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100871
872=item B<--help|-h>
873
874Print help information.
875
876=item B<--version|-v>
877
878Print version information.
879
Akron4e603a52020-07-27 14:23:49 +0200880=item B<--tokenizer-call|-tc>
881
882Call an external tokenizer process, that will tokenize
883a single line from STDIN and outputs one token per line.
884
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200885=item B<--tokenizer-korap|-tk>
886
887Use the standard KorAP/DeReKo tokenizer.
888
Akron6d7b8e42020-09-29 07:37:41 +0200889=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200890
891Tokenize the data using two embedded tokenizers,
892that will take an I<Aggressive> and a I<conservative>
893approach.
894
Marc Kupietz985da0c2021-02-15 19:29:50 +0100895=item B<--use-tokenizer-sentence-splits|-s>
896
897Replace existing with, or add new, sentence boundary information
898provided by the KorAP tokenizer (currently supported only).
899
Akron3378dfd2020-08-01 15:01:36 +0200900=item B<--log|-l>
901
902Loglevel for I<Log::Any>. Defaults to C<notice>.
903
Akrond949e182020-02-14 12:23:57 +0100904=back
905
Akronb3649472020-09-29 08:24:46 +0200906=head1 ENVIRONMENT VARIABLES
907
908=over 2
909
910=item B<KORAPXMLTEI_DEBUG>
911
912Activate minimal debugging.
913Defaults to C<false>.
914
915=item B<KORAPXMLTEI_INLINE>
916
917Process inline annotations, if present.
918Defaults to C<false>.
919
920=back
921
Akrond949e182020-02-14 12:23:57 +0100922=head1 COPYRIGHT AND LICENSE
923
Marc Kupietze955ecc2021-02-17 17:42:01 +0100924Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100925
926Author: Peter Harders
927
Akronaabd0952020-09-29 07:35:08 +0200928Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100929
930L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
931Corpus Analysis Platform at the
932L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
933member of the
934L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
935
936This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100937L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100938
939=cut