Akron | 9cb1394 | 2020-02-14 07:39:54 +0100 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 2 | use strict; |
| 3 | use warnings; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 4 | |
Akron | 3378dfd | 2020-08-01 15:01:36 +0200 | [diff] [blame] | 5 | use Log::Any '$log'; |
| 6 | use Log::Any::Adapter; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 7 | use Pod::Usage; |
| 8 | use Getopt::Long qw(GetOptions :config no_auto_abbrev); |
| 9 | |
| 10 | use File::Basename qw(dirname); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 11 | |
Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 12 | use Encode qw(decode); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 13 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 14 | use XML::CompactTree::XS; |
| 15 | use XML::LibXML::Reader; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 16 | |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 17 | use FindBin; |
| 18 | BEGIN { |
| 19 | unshift @INC, "$FindBin::Bin/../lib"; |
| 20 | }; |
| 21 | |
Marc Kupietz | 8a954e5 | 2021-02-16 22:03:07 +0100 | [diff] [blame] | 22 | use KorAP::XML::TEI qw!remove_xml_comments replace_entities!; |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 23 | use KorAP::XML::TEI::Tokenizer::External; |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 24 | use KorAP::XML::TEI::Tokenizer::Conservative; |
| 25 | use KorAP::XML::TEI::Tokenizer::Aggressive; |
Akron | 7501ca0 | 2020-08-01 21:05:25 +0200 | [diff] [blame] | 26 | use KorAP::XML::TEI::Annotations::Collector; |
Akron | a10ad59 | 2020-08-03 11:20:23 +0200 | [diff] [blame] | 27 | use KorAP::XML::TEI::Data; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 28 | use KorAP::XML::TEI::Zipper; |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 29 | use KorAP::XML::TEI::Header; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 30 | |
Marc Kupietz | 1e882fb | 2020-09-09 00:05:46 +0200 | [diff] [blame] | 31 | eval { |
| 32 | require KorAP::XML::TEI::Tokenizer::KorAP; |
| 33 | 1; |
| 34 | }; |
Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 35 | |
Marc Kupietz | a1421f0 | 2021-02-18 15:32:38 +0100 | [diff] [blame] | 36 | our $VERSION = '1.00'; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 37 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 38 | our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n"; |
| 39 | |
Akron | b364947 | 2020-09-29 08:24:46 +0200 | [diff] [blame] | 40 | # Set to 1 for minimal more debug output (no need to be parametrized) |
| 41 | use constant DEBUG => $ENV{KORAPXMLTEI_DEBUG} // 0; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 42 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 43 | # Parse options from the command line |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 44 | GetOptions( |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 45 | "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) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 47 | 'tokenizer-call|tc=s' => \(my $tokenizer_call), # Temporary argument for testing purposes |
Marc Kupietz | 1e882fb | 2020-09-09 00:05:46 +0200 | [diff] [blame] | 48 | 'tokenizer-korap|tk' => \(my $tokenizer_korap), # use KorAP-tokenizer |
Akron | 91705d7 | 2021-02-19 10:59:45 +0100 | [diff] [blame] | 49 | 'tokenizer-internal|ti' => \(my $_GEN_TOK_INT), # use intern tokenization (default = no) |
Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 50 | 'use-tokenizer-sentence-splits|s' => (\my $use_tokenizer_sentence_splits), # use KorAP tokenizer to split s (default=no) |
Akron | 1a5271a | 2021-02-18 13:18:15 +0100 | [diff] [blame] | 51 | 'inline-tokens=s' => \(my $inline_tokens = 'tokens#morpho'), |
Akron | dd0be8f | 2021-02-18 19:29:41 +0100 | [diff] [blame] | 52 | 'inline-structures=s' => \(my $inline_structures = 'struct#structure'), |
Akron | 26a7152 | 2021-02-19 10:27:37 +0100 | [diff] [blame] | 53 | 'base-foundry=s' => \(my $_tok_dir = 'base'), |
| 54 | 'data-file=s' => \(my $_data_file = 'data'), |
| 55 | 'header-file=s' => \(my $_header_file = 'header'), |
Akron | 91705d7 | 2021-02-19 10:59:45 +0100 | [diff] [blame] | 56 | 'tokens-file=s' => \(my $_tok_file_ext = 'tokens'), |
Akron | 3378dfd | 2020-08-01 15:01:36 +0200 | [diff] [blame] | 57 | 'log|l=s' => \(my $log_level = 'notice'), |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 58 | 'help|h' => sub { |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 59 | pod2usage( |
| 60 | -verbose => 99, |
| 61 | -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS', |
| 62 | -msg => $VERSION_MSG, |
| 63 | -output => '-' |
| 64 | ) |
| 65 | }, |
| 66 | 'version|v' => sub { |
| 67 | pod2usage( |
| 68 | -verbose => 0, |
| 69 | -msg => $VERSION_MSG, |
| 70 | -output => '-' |
| 71 | ) |
| 72 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 73 | ); |
| 74 | |
Marc Kupietz | 44b1f25 | 2020-11-26 16:31:40 +0100 | [diff] [blame] | 75 | binmode(STDERR, ":encoding(UTF-8)"); |
Akron | 3378dfd | 2020-08-01 15:01:36 +0200 | [diff] [blame] | 76 | Log::Any::Adapter->set('Stderr', log_level => $log_level); |
| 77 | |
Akron | b364947 | 2020-09-29 08:24:46 +0200 | [diff] [blame] | 78 | $log->notice('Debugging is activated') if DEBUG; |
| 79 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 80 | # |
| 81 | # ~~~ parameter (mandatory) ~~~ |
| 82 | # |
Akron | 0529e51 | 2021-02-22 09:55:35 +0100 | [diff] [blame] | 83 | # tag (without attributes), which contains the primary text |
| 84 | my $_TEXT_BODY = 'text'; |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 85 | # optional |
Akron | 09e0b2c | 2020-07-28 15:57:01 +0200 | [diff] [blame] | 86 | |
Akron | 0529e51 | 2021-02-22 09:55:35 +0100 | [diff] [blame] | 87 | # TODO: IDS-specific (and redundant) |
| 88 | my $_HEADER_TAG = 'idsHeader'; |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 89 | |
Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 90 | if ($use_tokenizer_sentence_splits && !$tokenizer_korap) { |
| 91 | die $log->fatal("Sentence splitting is currently only supported by KorAP tokenizer (use -tk to activate it"); |
| 92 | } |
| 93 | |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 94 | my $ext_tok; |
| 95 | if ($tokenizer_call) { |
| 96 | $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call); |
| 97 | } |
Marc Kupietz | 1e882fb | 2020-09-09 00:05:46 +0200 | [diff] [blame] | 98 | |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 99 | elsif ($tokenizer_korap) { |
Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 100 | $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits); |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 101 | }; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 102 | ## |
| 103 | |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 104 | |
Akron | 4e3c7e3 | 2021-02-18 15:19:53 +0100 | [diff] [blame] | 105 | # |
| 106 | # ~~~ constants ~~~ |
| 107 | # |
| 108 | |
| 109 | |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 110 | ## intern tokenization |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 111 | my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new; |
| 112 | my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new; |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 113 | ## |
| 114 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 115 | ## TODO: optional (different annotation tools can produce more zip-files for feeding into KorAP-XML-Krill) |
| 116 | my $_TOKENS_PROC = 1; # on/off: processing of ${_TOKEN_TAG}'s (default: 1) |
Akron | 1a5271a | 2021-02-18 13:18:15 +0100 | [diff] [blame] | 117 | |
| 118 | |
Akron | dd0be8f | 2021-02-18 19:29:41 +0100 | [diff] [blame] | 119 | # Name of the directory and the file containing all inline structure informations |
| 120 | # except for $_TOKEN_TAG information |
| 121 | my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure'; |
| 122 | $_structure_file .= '.xml'; |
| 123 | |
| 124 | |
Akron | 1a5271a | 2021-02-18 13:18:15 +0100 | [diff] [blame] | 125 | # Name of the directory and the file containing all inline token informations |
| 126 | # i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set |
| 127 | my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho'; |
| 128 | $_tokens_file .= '.xml'; |
| 129 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 130 | my $_TOKENS_TAG = "w"; # name of tag containing all information stored in $_tokens_file |
| 131 | |
Akron | 4e3c7e3 | 2021-02-18 15:19:53 +0100 | [diff] [blame] | 132 | # Handling inline annotations (inside $_TOKENS_TAG) |
| 133 | my $_INLINE_ANNOT = $ENV{KORAPXMLTEI_INLINE} ? 1 : 0; |
Akron | 09e0b2c | 2020-07-28 15:57:01 +0200 | [diff] [blame] | 134 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 135 | |
| 136 | # |
| 137 | # ~~~ variables ~~~ |
| 138 | # |
| 139 | |
Akron | 7501ca0 | 2020-08-01 21:05:25 +0200 | [diff] [blame] | 140 | # Initialize Token- and Structure-Collector |
| 141 | my $tokens = KorAP::XML::TEI::Annotations::Collector->new; |
| 142 | my $structures = KorAP::XML::TEI::Annotations::Collector->new; |
Akron | 09e0b2c | 2020-07-28 15:57:01 +0200 | [diff] [blame] | 143 | |
| 144 | |
Akron | a10ad59 | 2020-08-03 11:20:23 +0200 | [diff] [blame] | 145 | # Initialize Data-Collector |
| 146 | my $data = KorAP::XML::TEI::Data->new; |
| 147 | |
| 148 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 149 | # Initialize zipper |
Akron | 3bdc0a3 | 2020-08-03 12:12:56 +0200 | [diff] [blame] | 150 | my $zipper = KorAP::XML::TEI::Zipper->new($_root_dir); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 151 | my $input_fh; # input file handle (default: stdin) |
| 152 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 153 | my $dir; # text directory (below $_root_dir) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 154 | |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 155 | my ( $text_id, |
| 156 | $text_id_esc ); # '$text_id_esc' = escaped version of $text_id |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 157 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 158 | # these are only used inside recursive function 'retr_info' |
Akron | 4e3c7e3 | 2021-02-18 15:19:53 +0100 | [diff] [blame] | 159 | my ( $_IDX, # value is set dependent on DEBUG - for extracting array of child elements from element in $tree_data |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 160 | $e, # element from $tree_data |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 161 | ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags) |
| 162 | $add_one, # ... |
Akron | 7501ca0 | 2020-08-01 21:05:25 +0200 | [diff] [blame] | 163 | $fval, # ... |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 164 | %ws); # hash for indices of whitespace-nodes (needed to recorrect from-values) |
| 165 | # idea: when closing element, check if it's from-index minus 1 refers to a whitespace-node |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 166 | # (means: 'from-index - 1' is a key in %ws). |
| 167 | # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1 |
| 168 | |
Akron | 7501ca0 | 2020-08-01 21:05:25 +0200 | [diff] [blame] | 169 | my $c; # index variables used in loops |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 170 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 171 | |
| 172 | # |
| 173 | # ~~~ main ~~~ |
| 174 | # |
| 175 | |
| 176 | # ~ initializations ~ |
| 177 | |
Akron | 4e3c7e3 | 2021-02-18 15:19:53 +0100 | [diff] [blame] | 178 | # Include line numbers in elements of $tree_data for debugging |
| 179 | DEBUG ? ($_IDX = 5) : ($_IDX = 4); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 180 | |
Akron | 7501ca0 | 2020-08-01 21:05:25 +0200 | [diff] [blame] | 181 | $fval = 0; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 182 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 183 | # ~ read input and write output (text by text) ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 184 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 185 | my $tl = 0; # text line (needed for whitespace handling) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 186 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 187 | $input_fh = *STDIN; # input file handle (default: stdin) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 188 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 189 | # Maybe not necessary |
| 190 | $data->reset; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 191 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 192 | $dir = ''; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 193 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 194 | if ( $input_fname ne '' ){ |
| 195 | unless (open($input_fh, '<', $input_fname)) { |
| 196 | die $log->fatal("File '$input_fname' could not be opened."); |
| 197 | }; |
| 198 | } |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 199 | |
Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 200 | # Prevents segfaulting (see notes on segfault prevention) |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 201 | binmode $input_fh; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 202 | |
Akron | d20898f | 2021-02-19 15:52:17 +0100 | [diff] [blame] | 203 | my $sfx; |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 204 | my $pos; |
Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 205 | my $input_enc = 'UTF-8'; |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 206 | my $l = length('</' . $_TEXT_BODY) + 1; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 207 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 208 | # ~ loop (reading input document) ~ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 209 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 210 | MAIN: while ( <$input_fh> ){ |
| 211 | |
| 212 | $_ = remove_xml_comments( $input_fh, $_ ); # remove HTML (multi-line) comments (<!--...-->) |
| 213 | |
Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 214 | # Set input encoding |
| 215 | if ( index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) { |
| 216 | $input_enc = $2; |
| 217 | next; |
| 218 | }; |
| 219 | |
| 220 | $_ = decode($input_enc, $_); |
Marc Kupietz | 8a954e5 | 2021-02-16 22:03:07 +0100 | [diff] [blame] | 221 | $_ = replace_entities($_); |
Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 222 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 223 | if ( index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){ |
| 224 | |
| 225 | # ~ start of text body ~ |
| 226 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 227 | $sfx = $2; |
| 228 | |
Akron | d20898f | 2021-02-19 15:52:17 +0100 | [diff] [blame] | 229 | if ($1 !~ /^\s*$/ || $sfx !~ /^\s*$/) { |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 230 | die $log->fatal("input line number $.: " . |
| 231 | "line with opening text-body tag '${_TEXT_BODY}' " . |
| 232 | "contains additional information ... => Aborting (line=$_)"); |
Akron | 0bb7e72 | 2020-09-29 07:48:33 +0200 | [diff] [blame] | 233 | }; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 234 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 235 | # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader |
| 236 | my $buf_in = ''; |
Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 237 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 238 | # Iterate over all lines in the text body |
| 239 | while (<$input_fh>) { |
Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 240 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 241 | $_ = remove_xml_comments( $input_fh, $_ ); |
Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 242 | $_ = decode($input_enc, $_); |
Marc Kupietz | 8a954e5 | 2021-02-16 22:03:07 +0100 | [diff] [blame] | 243 | $_ = replace_entities($_); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 244 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 245 | # ~ end of text body ~ |
| 246 | if (($pos = index($_, '</' . $_TEXT_BODY)) >= 0) { |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 247 | |
Akron | 91705d7 | 2021-02-19 10:59:45 +0100 | [diff] [blame] | 248 | # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 249 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 250 | if ((substr($_, 0, $pos) . substr($_, $l + $pos)) !~ /^\s*$/) { |
| 251 | die $log->fatal("input line number $.: " . |
| 252 | "line with closing text-body tag '${_TEXT_BODY}'". |
| 253 | " contains additional information ... => Aborting (line=$_)"); |
| 254 | }; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 255 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 256 | if ($dir eq '') { |
| 257 | $log->warn("Maybe empty textSigle => skipping this text ...\ndata=" . substr($data->data, 0, 200)); |
| 258 | next MAIN; |
| 259 | }; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 260 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 261 | my $reader = XML::LibXML::Reader->new( |
| 262 | string => "<text>$buf_in</text>", |
| 263 | huge => 1 |
| 264 | ); |
| 265 | |
| 266 | # See notes on whitespace handling |
| 267 | my $param = XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY; |
| 268 | |
| 269 | # XCT_LINE_NUMBERS is only needed for debugging |
| 270 | # (see XML::CompactTree::XS) |
| 271 | $param |= XCT_LINE_NUMBERS if DEBUG; |
| 272 | my $tree_data = XML::CompactTree::XS::readSubtreeToPerl($reader, $param); |
| 273 | |
| 274 | $structures->reset; |
| 275 | |
| 276 | $tokens->reset if $_TOKENS_PROC; |
| 277 | |
| 278 | # ~ whitespace related issue ~ |
| 279 | $add_one = 0; |
| 280 | %ws = (); |
| 281 | |
| 282 | # ~ recursion ~ |
| 283 | retr_info(1, \$tree_data->[2] ); # parse input data |
| 284 | |
| 285 | if (DEBUG) { |
| 286 | $log->debug("Writing (utf8-formatted) xml file $dir/${_data_file}.xml"); |
| 287 | }; |
| 288 | |
| 289 | # ~ write data.xml ~ |
| 290 | $data->to_zip( |
| 291 | $zipper->new_stream("$dir/${_data_file}.xml"), |
| 292 | $text_id_esc |
| 293 | ); |
| 294 | |
| 295 | # ~ tokenization ~ |
Akron | 9df4a24 | 2021-02-19 15:31:16 +0100 | [diff] [blame] | 296 | if ($ext_tok) { |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 297 | |
| 298 | # Tokenize and output |
| 299 | $ext_tok->tokenize($data->data)->to_zip( |
| 300 | $zipper->new_stream("$dir/$_tok_dir/${_tok_file_ext}.xml"), |
| 301 | $text_id_esc |
Akron | d20898f | 2021-02-19 15:52:17 +0100 | [diff] [blame] | 302 | ); |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 303 | }; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 304 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 305 | if ($_GEN_TOK_INT) { |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 306 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 307 | # Tokenize and output |
| 308 | $cons_tok->tokenize($data->data)->to_zip( |
| 309 | $zipper->new_stream("$dir/$_tok_dir/" . $cons_tok->name . '.xml'), |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 310 | $text_id_esc |
| 311 | ); |
Akron | 598d1a7 | 2020-08-02 17:33:31 +0200 | [diff] [blame] | 312 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 313 | $aggr_tok->tokenize($data->data)->to_zip( |
| 314 | $zipper->new_stream("$dir/$_tok_dir/" . $aggr_tok->name . '.xml'), |
| 315 | $text_id_esc |
| 316 | ); |
Akron | 598d1a7 | 2020-08-02 17:33:31 +0200 | [diff] [blame] | 317 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 318 | $aggr_tok->reset; |
| 319 | $cons_tok->reset; |
| 320 | }; |
Akron | a10ad59 | 2020-08-03 11:20:23 +0200 | [diff] [blame] | 321 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 322 | if ($use_tokenizer_sentence_splits) { |
| 323 | $ext_tok->sentencize_from_previous_input($structures); |
Akron | 9df4a24 | 2021-02-19 15:31:16 +0100 | [diff] [blame] | 324 | }; |
Akron | 598d1a7 | 2020-08-02 17:33:31 +0200 | [diff] [blame] | 325 | |
Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 326 | # ~ write structures ~ |
| 327 | if (!$structures->empty) { |
| 328 | $structures->to_zip( |
| 329 | $zipper->new_stream("$dir/$_structure_dir/$_structure_file"), |
| 330 | $text_id_esc, |
| 331 | 2 # = structure serialization |
| 332 | ); |
| 333 | }; |
| 334 | |
| 335 | # ~ write tokens ~ |
| 336 | if ($_TOKENS_PROC && !$tokens->empty) { |
| 337 | $tokens->to_zip( |
| 338 | $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}"), |
| 339 | $text_id_esc, |
| 340 | $_INLINE_ANNOT # Either 0 = tokens without inline or 1 = tokens with inline |
| 341 | ); |
| 342 | }; |
| 343 | |
| 344 | # reinit. |
| 345 | $dir = ''; |
| 346 | |
| 347 | # Maybe not necessary |
| 348 | $data->reset; |
| 349 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 350 | next MAIN; |
Akron | 598d1a7 | 2020-08-02 17:33:31 +0200 | [diff] [blame] | 351 | }; |
| 352 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 353 | # ~ inside text body ~ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 354 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 355 | # ~ whitespace handling ~ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 356 | |
Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 357 | # Fix whitespaces (see notes on whitespace fixing) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 358 | |
Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 359 | # 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 |
| 360 | # an option on how newlines in primary text should be handled (stripped or replaced by a whitespace)). |
| 361 | |
| 362 | # Remove consecutive whitespace at beginning and end (mostly one newline) |
| 363 | s/^\s+//; s/\s+$//; |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 364 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 365 | ### NOTE: this is only relevant, if a text consists of more than one line |
| 366 | ### TODO: find a better solution, or create a warning, if a text has more than one line ($tl > 1) |
| 367 | ### do testing with 2 different corpora (one with only one-line texts, the other with several lines per text) |
| 368 | if (m/<[^>]+>[^<]/) { # line contains at least one tag with at least one character contents |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 369 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 370 | $tl++; # counter for text lines |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 371 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 372 | s/^(.)/ $1/ if $tl > 1; # insert blank before 1st character (for 2nd line and consecutive lines) |
| 373 | } |
| 374 | ### |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 375 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 376 | # add line to buffer |
| 377 | $buf_in .= $_; |
| 378 | }; |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 379 | |
Akron | 0529e51 | 2021-02-22 09:55:35 +0100 | [diff] [blame] | 380 | } elsif (m#^(.*)(\<${_HEADER_TAG}[^>]*?type=["'].*)$#) { |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 381 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 382 | # ~ start of header ~ |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 383 | my $content = "$2\n"; |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 384 | |
Akron | d20898f | 2021-02-19 15:52:17 +0100 | [diff] [blame] | 385 | if ($1 !~ /^\s*$/) { |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 386 | die $log->fatal("input line number $.: " . |
| 387 | "line with opening header tag" . |
| 388 | " is not in expected format ... => Aborting (line=$_)"); |
| 389 | }; |
| 390 | |
| 391 | # Parse header |
Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 392 | my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh); |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 393 | |
| 394 | # Header was parseable |
| 395 | if ($header) { |
| 396 | |
| 397 | # Write header to zip |
Akron | 26a7152 | 2021-02-19 10:27:37 +0100 | [diff] [blame] | 398 | my $file = $header->dir . '/' . $_header_file . '.xml'; |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 399 | |
Akron | b364947 | 2020-09-29 08:24:46 +0200 | [diff] [blame] | 400 | $log->debug("Writing file $file") if DEBUG; |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 401 | |
| 402 | $header->to_zip($zipper->new_stream($file)); |
| 403 | |
| 404 | # Header is for text level |
| 405 | if ($header->type eq 'text') { |
| 406 | |
| 407 | # Remember dir and sigles |
| 408 | $dir = $header->dir; |
| 409 | $text_id = $header->id; |
| 410 | $text_id_esc = $header->id_esc; |
| 411 | |
| 412 | # log output for seeing progression |
Akron | 9157792 | 2021-02-19 10:32:54 +0100 | [diff] [blame] | 413 | $log->notice("$0: text_id=$text_id"); |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 414 | |
| 415 | $tl = 0; # reset (needed for ~ whitespace handling ~) |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 416 | } |
| 417 | } |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 418 | } |
| 419 | } #end: while |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 420 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 421 | $zipper->close; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 422 | |
Akron | 9df4a24 | 2021-02-19 15:31:16 +0100 | [diff] [blame] | 423 | $ext_tok->close if $ext_tok; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 424 | |
Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 425 | exit(0); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 426 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 427 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 428 | # Recursively called function to handle XML tree data |
| 429 | sub retr_info { |
Akron | 1c4f220 | 2020-07-30 09:28:22 +0200 | [diff] [blame] | 430 | # recursion level |
| 431 | # (1 = topmost level inside retr_info() = should always be level of tag $_TEXT_BODY) |
| 432 | my $rl = shift; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 433 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 434 | # Iteration through all array elements |
| 435 | # ($_[0] is a reference to an array reference) |
| 436 | # See notes on how 'XML::CompactTree::XS' works and |
| 437 | # see 'NODE TYPES' in manpage of XML::LibXML::Reader |
| 438 | foreach $e (@{${$_[0]}}) { |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 439 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 440 | # Element node |
| 441 | if ($e->[0] == XML_READER_TYPE_ELEMENT) { |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 442 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 443 | #~~~~ |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 444 | # from here: tag-node (opening) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 445 | #~~~~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 446 | |
Akron | 7501ca0 | 2020-08-01 21:05:25 +0200 | [diff] [blame] | 447 | # $e->[1] represents the tag name |
Akron | ace1277 | 2021-02-19 13:16:26 +0100 | [diff] [blame^] | 448 | # Skip sentences |
Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 449 | if ($use_tokenizer_sentence_splits && $e->[1] eq "s") { |
Akron | ace1277 | 2021-02-19 13:16:26 +0100 | [diff] [blame^] | 450 | if (defined $e->[$_IDX]) { |
| 451 | retr_info($rl+1, \$e->[$_IDX]); |
| 452 | } |
| 453 | next; |
Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 454 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 455 | |
Akron | ace1277 | 2021-02-19 13:16:26 +0100 | [diff] [blame^] | 456 | my $anno = $structures->add_new_annotation($e->[1]); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 457 | |
Akron | 7501ca0 | 2020-08-01 21:05:25 +0200 | [diff] [blame] | 458 | # Add element also to token list |
| 459 | if ($_TOKENS_PROC && $e->[1] eq $_TOKENS_TAG) { |
| 460 | $tokens->add_annotation($anno); |
| 461 | }; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 462 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 463 | # Handle attributes (if attributes exist) |
| 464 | if (defined $e->[3]) { |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 465 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 466 | # with 'XCT_ATTRIBUTE_ARRAY', $node->[3] is an array reference of the form |
| 467 | # [ name1, value1, name2, value2, ....] of attribute names and corresponding values. |
| 468 | # note: arrays are faster (see: http://makepp.sourceforge.net/2.0/perl_performance.html) |
| 469 | for ($c = 0; $c < @{$e->[3]}; $c += 2) { |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 470 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 471 | # '$c' references the 'key' and '$c+1' the 'value' |
Akron | 7501ca0 | 2020-08-01 21:05:25 +0200 | [diff] [blame] | 472 | $anno->add_attribute( |
| 473 | @{$e->[3]}[$c, $c + 1] |
| 474 | ); |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 475 | }; |
| 476 | }; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 477 | |
| 478 | # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts |
Akron | a10ad59 | 2020-08-03 11:20:23 +0200 | [diff] [blame] | 479 | $anno->set_from($data->position + $add_one); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 480 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 481 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 482 | #~~~~ |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 483 | # until here: tag-node (opening) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 484 | #~~~~ |
| 485 | |
| 486 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 487 | # Call function recursively |
| 488 | # do no recursion, if $e->[$_IDX] is not defined |
| 489 | # (because we have no array of child-nodes, e.g.: <back/>) |
| 490 | if (defined $e->[$_IDX]) { |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 491 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 492 | # Recursion with array of child-nodes |
| 493 | retr_info($rl+1, \$e->[$_IDX]); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
| 497 | #~~~~~ |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 498 | # from here: tag-node (closing) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 499 | #~~~~~ |
| 500 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 501 | # NOTE: use $pos, because the offsets are _between_ the characters |
| 502 | # (e.g.: word = 'Hello' => from = 0 (before 'H'), to = 5 (after 'o')) |
Akron | a10ad59 | 2020-08-03 11:20:23 +0200 | [diff] [blame] | 503 | my $pos = $data->position; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 504 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 505 | # Handle structures and tokens |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 506 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 507 | $fval = $anno->from; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 508 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 509 | # ~ whitespace related issue ~ |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 510 | if ($fval > 0 && not exists $ws{$fval - 1}) { |
| 511 | |
| 512 | # ~ previous node was a text-node ~ |
| 513 | $anno->set_from($fval - 1); |
| 514 | } |
| 515 | |
| 516 | # in case this fails, check input |
| 517 | if (($fval - 1) > $pos) { |
| 518 | die $log->fatal("text_id='$text_id', " . |
| 519 | "processing of structures: " . |
| 520 | "from-value ($fval) is 2 or more greater " . |
| 521 | "than to-value ($pos) => please check. Aborting"); |
| 522 | }; |
| 523 | |
| 524 | # TODO: find example for which this case applies |
| 525 | # maybe this is not necessary anymore, because the above recorrection of the from-value suffices |
| 526 | # |
| 527 | # TODO: check, if it's better to remove this line and change above check to 'if ($fval - 1) >= $pos; |
| 528 | # do testing with bigger corpus excerpt (wikipedia?) |
| 529 | $anno->set_from($pos) if $fval == $pos + 1; |
| 530 | $anno->set_to($pos); |
| 531 | $anno->set_level($rl); |
| 532 | |
| 533 | # Clean up whitespace |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 534 | delete $ws{$fval - 1} if $fval > 0 && exists $ws{$fval - 1}; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 535 | |
| 536 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 537 | #~~~~ |
| 538 | # until here: tag-node (closing) |
| 539 | #~~~~ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 542 | # Text node |
| 543 | elsif ($e->[0] == XML_READER_TYPE_TEXT){ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 544 | |
Akron | d658df7 | 2021-02-18 18:58:56 +0100 | [diff] [blame] | 545 | $add_one = 1; |
| 546 | $data->append($e->[1]); |
| 547 | } |
| 548 | |
| 549 | # Whitespace node |
| 550 | # (See notes on whitespace handling - regarding XML_READER_TYPE_SIGNIFICANT_WHITESPACE) |
| 551 | elsif ($e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE) { |
| 552 | |
| 553 | # state, that this from-index belongs to a whitespace-node |
| 554 | # ('++' doesn't mean a thing here - maybe it could be used for a consistency check) |
| 555 | $ws{$data->position}++; |
| 556 | |
| 557 | $add_one = 0; |
| 558 | $data->append($e->[1]); |
| 559 | } |
| 560 | |
| 561 | # not yet handled type |
| 562 | else { |
| 563 | |
| 564 | die $log->fatal('Not yet handled type ($e->[0]=' . $e->[0] . ') ... => Aborting'); |
| 565 | }; |
| 566 | }; |
| 567 | }; |
| 568 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 569 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 570 | __END__ |
| 571 | |
| 572 | =pod |
| 573 | |
| 574 | =encoding utf8 |
| 575 | |
| 576 | =head1 NAME |
| 577 | |
| 578 | tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML |
| 579 | |
| 580 | =head1 SYNOPSIS |
| 581 | |
| 582 | cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip |
| 583 | |
| 584 | =head1 DESCRIPTION |
| 585 | |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 586 | C<tei2korapxml> is a script to convert TEI P5 and |
| 587 | L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html> |
| 588 | based documents to the |
| 589 | L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>. |
| 590 | If no specific input is defined, data is |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 591 | read from C<STDIN>. If no specific output is defined, data is written |
| 592 | to C<STDOUT>. |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 593 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 594 | This program is usually called from inside another script. |
| 595 | |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 596 | =head1 FORMATS |
| 597 | |
| 598 | =head2 Input restrictions |
| 599 | |
| 600 | =over 2 |
| 601 | |
| 602 | =item |
| 603 | |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 604 | TEI P5 formatted input with certain restrictions: |
| 605 | |
| 606 | =over 4 |
| 607 | |
| 608 | =item |
| 609 | |
| 610 | B<mandatory>: text-header with integrated textsigle, text-body |
| 611 | |
| 612 | =item |
| 613 | |
| 614 | B<optional>: corp-header with integrated corpsigle, |
| 615 | doc-header with integrated docsigle |
| 616 | |
| 617 | =back |
| 618 | |
| 619 | =item |
| 620 | |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 621 | All tokens inside the primary text may not be |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 622 | newline seperated, because newlines are removed |
Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 623 | (see L<KorAP::XML::TEI::Data>) and a conversion of newlines |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 624 | into blanks between 2 tokens could lead to additional blanks, |
| 625 | where there should be none (e.g.: punctuation characters like C<,> or |
| 626 | C<.> should not be seperated from their predecessor token). |
| 627 | (see also code section C<~ whitespace handling ~>). |
| 628 | |
| 629 | =back |
| 630 | |
| 631 | =head2 Notes on the output |
| 632 | |
| 633 | =over 2 |
| 634 | |
| 635 | =item |
| 636 | |
| 637 | zip file output (default on C<stdout>) with utf8 encoded entries |
| 638 | (which together form the KorAP-XML format) |
| 639 | |
| 640 | =back |
| 641 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 642 | =head1 INSTALLATION |
| 643 | |
| 644 | C<tei2korapxml> requires L<libxml2-dev> bindings to build. When |
| 645 | these bindings are available, the preferred way to install the script is |
| 646 | to use L<cpanm|App::cpanminus>. |
| 647 | |
| 648 | $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git |
| 649 | |
| 650 | In case everything went well, the C<tei2korapxml> tool will |
| 651 | be available on your command line immediately. |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 652 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 653 | Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16. |
| 654 | |
| 655 | =head1 OPTIONS |
| 656 | |
| 657 | =over 2 |
| 658 | |
Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 659 | =item B<--root|-r> |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 660 | |
Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 661 | The root directory for output. Defaults to C<.>. |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 662 | |
| 663 | =item B<--help|-h> |
| 664 | |
| 665 | Print help information. |
| 666 | |
| 667 | =item B<--version|-v> |
| 668 | |
| 669 | Print version information. |
| 670 | |
Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 671 | =item B<--tokenizer-call|-tc> |
| 672 | |
| 673 | Call an external tokenizer process, that will tokenize |
| 674 | a single line from STDIN and outputs one token per line. |
| 675 | |
Marc Kupietz | 1e882fb | 2020-09-09 00:05:46 +0200 | [diff] [blame] | 676 | =item B<--tokenizer-korap|-tk> |
| 677 | |
| 678 | Use the standard KorAP/DeReKo tokenizer. |
| 679 | |
Akron | 6d7b8e4 | 2020-09-29 07:37:41 +0200 | [diff] [blame] | 680 | =item B<--tokenizer-internal|-ti> |
Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 681 | |
| 682 | Tokenize the data using two embedded tokenizers, |
| 683 | that will take an I<Aggressive> and a I<conservative> |
| 684 | approach. |
| 685 | |
Akron | 1a5271a | 2021-02-18 13:18:15 +0100 | [diff] [blame] | 686 | =item B<--inline-tokens> <foundry>#[<file>] |
| 687 | |
| 688 | Define the foundry and file (without extension) |
| 689 | to store inline token information in. |
| 690 | If L</KORAPXMLTEI_INLINE> is set, this will contain |
| 691 | annotations as well. |
| 692 | Defaults to C<tokens> and C<morpho>. |
| 693 | |
Akron | dd0be8f | 2021-02-18 19:29:41 +0100 | [diff] [blame] | 694 | =item B<--inline-structures> <foundry>#[<file>] |
| 695 | |
| 696 | Define the foundry and file (without extension) |
| 697 | to store inline structure information in. |
| 698 | Defaults to C<struct> and C<structures>. |
| 699 | |
Akron | 26a7152 | 2021-02-19 10:27:37 +0100 | [diff] [blame] | 700 | =item B<--base-foundry> <foundry> |
| 701 | |
| 702 | Define the base foundry to store newly generated |
| 703 | token information in. |
| 704 | Defaults to C<base>. |
| 705 | |
| 706 | =item B<--data-file> <file> |
| 707 | |
| 708 | Define the file (without extension) |
| 709 | to store primary data information in. |
| 710 | Defaults to C<data>. |
| 711 | |
| 712 | =item B<--header-file> <file> |
| 713 | |
| 714 | Define the file name (without extension) |
| 715 | to store header information on |
| 716 | the corpus, document, and text level in. |
| 717 | Defaults to C<header>. |
| 718 | |
Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 719 | =item B<--use-tokenizer-sentence-splits|-s> |
| 720 | |
| 721 | Replace existing with, or add new, sentence boundary information |
| 722 | provided by the KorAP tokenizer (currently supported only). |
| 723 | |
Akron | 91705d7 | 2021-02-19 10:59:45 +0100 | [diff] [blame] | 724 | =item B<--tokens-file> <file> |
| 725 | |
| 726 | Define the file (without extension) |
| 727 | to store generated token information in |
| 728 | (either from the KorAP tokenizer or an externally called tokenizer). |
| 729 | Defaults to C<tokens>. |
| 730 | |
Akron | 3378dfd | 2020-08-01 15:01:36 +0200 | [diff] [blame] | 731 | =item B<--log|-l> |
| 732 | |
| 733 | Loglevel for I<Log::Any>. Defaults to C<notice>. |
| 734 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 735 | =back |
| 736 | |
Akron | b364947 | 2020-09-29 08:24:46 +0200 | [diff] [blame] | 737 | =head1 ENVIRONMENT VARIABLES |
| 738 | |
| 739 | =over 2 |
| 740 | |
| 741 | =item B<KORAPXMLTEI_DEBUG> |
| 742 | |
| 743 | Activate minimal debugging. |
| 744 | Defaults to C<false>. |
| 745 | |
| 746 | =item B<KORAPXMLTEI_INLINE> |
| 747 | |
| 748 | Process inline annotations, if present. |
| 749 | Defaults to C<false>. |
| 750 | |
| 751 | =back |
| 752 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 753 | =head1 COPYRIGHT AND LICENSE |
| 754 | |
Marc Kupietz | e955ecc | 2021-02-17 17:42:01 +0100 | [diff] [blame] | 755 | Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/> |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 756 | |
| 757 | Author: Peter Harders |
| 758 | |
Akron | aabd095 | 2020-09-29 07:35:08 +0200 | [diff] [blame] | 759 | Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 760 | |
| 761 | L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/> |
| 762 | Corpus Analysis Platform at the |
| 763 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 764 | member of the |
| 765 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>. |
| 766 | |
| 767 | This program is free software published under the |
Marc Kupietz | e955ecc | 2021-02-17 17:42:01 +0100 | [diff] [blame] | 768 | L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>. |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 769 | |
| 770 | =cut |
Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 771 | |
| 772 | # NOTES |
| 773 | |
| 774 | ## Notes on how 'XML::CompactTree::XS' works |
| 775 | |
| 776 | Example: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node> |
| 777 | |
| 778 | Print out name of 'node2' for the above example: |
| 779 | |
| 780 | 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"' |
| 781 | |
| 782 | Exploring the structure of $data ( = reference to below array ): |
| 783 | |
| 784 | [ 0: XML_READER_TYPE_DOCUMENT, |
| 785 | 1: ? |
Akron | 9157792 | 2021-02-19 10:32:54 +0100 | [diff] [blame] | 786 | 2: [ 0: [ 0: XML_READER_TYPE_ELEMENT <- start recursion with array '$data->[2]' (see retr_info( \$tree_data->[2] )) |
Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 787 | 1: 'node' |
| 788 | 2: ? |
| 789 | 3: HASH (attributes) |
| 790 | 4: 1 (line number) |
| 791 | 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT |
| 792 | 1: 'node1' |
| 793 | 2: ? |
| 794 | 3: undefined (no attributes) |
| 795 | 4: 1 (line number) |
| 796 | 5: [ 0: [ 0: XML_READER_TYPE_TEXT |
| 797 | 1: 'some ' |
| 798 | ] |
| 799 | 1: [ 0: XML_READER_TYPE_ELEMENT |
| 800 | 1: 'n' |
| 801 | 2: ? |
| 802 | 3: undefined (no attributes) |
| 803 | 4: 1 (line number) |
| 804 | 5: undefined (no child-nodes) |
| 805 | ] |
| 806 | 2: [ 0: XML_READER_TYPE_TEXT |
| 807 | 1: ' text' |
| 808 | ] |
| 809 | ] |
| 810 | ] |
| 811 | 1: [ 0: XML_READER_TYPE_ELEMENT |
| 812 | 1: 'node2' |
| 813 | 2: ? |
| 814 | 3: undefined (not attributes) |
| 815 | 4: 1 (line number) |
| 816 | 5: [ 0: [ 0: XML_READER_TYPE_TEXT |
| 817 | 1: 'more-text' |
| 818 | ] |
| 819 | ] |
| 820 | ] |
| 821 | ] |
| 822 | ] |
| 823 | ] |
| 824 | ] |
| 825 | |
| 826 | $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT) |
| 827 | |
| 828 | ref($data->[2]) == ARRAY (with 1 element for 'node') |
| 829 | ref($data->[2]->[0]) == ARRAY (with 6 elements) |
| 830 | |
| 831 | $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 832 | $data->[2]->[0]->[1] == 'node' |
| 833 | ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v') |
| 834 | $data->[2]->[0]->[4] == 1 (line number) |
| 835 | ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2') |
| 836 | # child-nodes of actual node (see $_IDX) |
| 837 | |
| 838 | ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements) |
| 839 | $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 840 | $data->[2]->[0]->[5]->[0]->[1] == 'node1' |
| 841 | $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute) |
| 842 | $data->[2]->[0]->[5]->[0]->[4] == 1 (line number) |
| 843 | ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text') |
| 844 | |
| 845 | ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements) |
| 846 | $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT) |
| 847 | $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some ' |
| 848 | |
| 849 | ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements) |
| 850 | $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 851 | $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n' |
| 852 | $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute) |
| 853 | $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number) |
| 854 | $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child-nodes) |
| 855 | |
| 856 | ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements) |
| 857 | $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT) |
| 858 | $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text' |
| 859 | |
| 860 | |
| 861 | retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example. |
| 862 | Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to |
| 863 | ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]). |
| 864 | |
| 865 | |
| 866 | ## Notes on whitespace handling |
| 867 | |
| 868 | Every whitespace inside the processed text is 'significant' and recognized as a node of type 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE' |
| 869 | (see function 'retr_info()'). |
| 870 | |
| 871 | Definition of significant and insignificant whitespace |
| 872 | (source: https://www.oracle.com/technical-resources/articles/wang-whitespace.html): |
| 873 | |
| 874 | Significant whitespace is part of the document content and should be preserved. |
| 875 | Insignificant whitespace is used when editing XML documents for readability. |
| 876 | These whitespaces are typically not intended for inclusion in the delivery of the document. |
| 877 | |
| 878 | ### Regarding XML_READER_TYPE_SIGNIFICANT_WHITESPACE |
| 879 | |
| 880 | The 3rd form of nodes, besides text- (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes of the type |
| 881 | 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'. |
| 882 | |
| 883 | When modifiying the previous example (see: Notes on how 'XML::CompactTree::XS' works) by inserting an additional blank between |
| 884 | '</node1>' and '<node2>', the output for '$data->[2]->[0]->[5]->[1]->[1]' is a blank (' ') and it's type is '14' |
| 885 | (XML_READER_TYPE_SIGNIFICANT_WHITESPACE, see 'man XML::LibXML::Reader'): |
| 886 | |
| 887 | 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"' |
| 888 | |
| 889 | |
| 890 | Example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...' |
| 891 | |
| 892 | Two text-nodes should normally be separated by a blank. In the above example, that would be the 2 text-nodes |
| 893 | 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' ' (see [2]). |
| 894 | |
| 895 | The text-node 'Campagne in Frankreich' leads to the setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag, |
| 896 | it's from-index gets set to the correct start-index of '1792' (and not to the start-index of the whitespace-node ' '). |
| 897 | |
| 898 | The assumption here is, that in most cases there _is_ a whitespace node between 2 text-nodes. The below code fragment |
| 899 | enables a way, to check, if this really _was_ the case for the last 2 'non-tag'-nodes, when closing a tag: |
| 900 | |
| 901 | 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. |
| 902 | So when closing a tag, it can be checked, if the previous 'non-tag'-node (text or whitespace), which is the one before |
| 903 | 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 |
| 904 | the last read 'non-tag'-node has to be corrected (see [1]), |
| 905 | |
| 906 | For whitespace-nodes $add_one is set to 0, so when opening the next tag (in the above example the 2nd 's'-tag), no |
| 907 | additional 1 is added (because this was already done by the whitespace-node itself when incrementing the variable $pos). |
| 908 | |
| 909 | [1] |
| 910 | Now, what happens, when 2 text-nodes are _not_ seperated by a whitespace-node (e.g.: <w>Augen<c>,</c></w>)? |
| 911 | In this case, the falsely increased from-value has to be decreased again by 1 when closing the enclosing tag |
| 912 | (see above code fragment '... not exists $ws{ $fval - 1 } ...'). |
| 913 | |
| 914 | [2] |
| 915 | 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 |
| 916 | whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE). |
| 917 | |
| 918 | The from-index of the 2nd w-tag in the second example refers to 'bar', which may not have been the intention |
| 919 | (even though '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug? |
| 920 | |
| 921 | 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- |
| 922 | and to-indizes for the tags 'a' and 'b' both 12, which is the start-index of the token 'tok3'. |
| 923 | |
| 924 | |
| 925 | ## Notes on whitespace fixing |
| 926 | |
| 927 | The idea for the below code fragment was to fix (recreate) missing whitespace in a poorly created corpus, in which linebreaks where inserted |
| 928 | into the text with the addition that maybe (or not) whitespace before those linebreaks was unintenionally stripped. |
| 929 | |
| 930 | It soon turned out, that it was best to suggest considering just avoiding linebreaks and putting all primary text tokens into one line (see |
| 931 | example further down and notes on 'Input restrictions' in the manpage). |
| 932 | |
| 933 | Somehow an old first very poor approach remained, which is not stringent, but also doesn't affect one-line text. |
| 934 | |
| 935 | Examples (how primary text with linebreaks would be converted by below code): |
| 936 | |
| 937 | '...<w>end</w>\n<w>.</w>...' -> '...<w>end</w> <w>.</w>...' |
| 938 | '...<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>'. |
| 939 | |
| 940 | Blanks are inserted before the 1st character: |
| 941 | |
| 942 | NOTE: not stringent ('...' stands for text): |
| 943 | |
| 944 | beg1............................end1 => no blank before 'beg1' |
| 945 | beg2....<pb/>...................end2 => no blank before 'beg2' |
| 946 | beg3....<info attr1="val1"/>....end3 => no blank before 'beg3' |
| 947 | beg4....<test>ok</test>.........end4 => blank before 'beg4' |
| 948 | |
| 949 | => beg1....end1beg2...<pb/>...end2beg3....<info attr1="val1"/>....end3 beg4...<test>ok</test>....end4 |
| 950 | ^ |
| 951 | |_blank between 'end3' and 'beg4' |
| 952 | |
| 953 | |
| 954 | ## Notes on segfault prevention |
| 955 | |
Akron | 9157792 | 2021-02-19 10:32:54 +0100 | [diff] [blame] | 956 | binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop |
Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 957 | (see notes on 'PerlIO layers' in 'man XML::LibXML'), |
| 958 | removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular |
| 959 | see in perluniintro: You can switch encodings on an already opened stream by using "binmode() |
| 960 | see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data. |