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 | |
| 5 | use Pod::Usage; |
| 6 | use Getopt::Long qw(GetOptions :config no_auto_abbrev); |
| 7 | |
| 8 | use File::Basename qw(dirname); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 9 | |
| 10 | use open qw(:std :utf8); # assume utf-8 encoding |
| 11 | use Encode qw(encode_utf8 decode_utf8); |
| 12 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 13 | use XML::CompactTree::XS; |
| 14 | use XML::LibXML::Reader; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 15 | |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 16 | use FindBin; |
| 17 | BEGIN { |
| 18 | unshift @INC, "$FindBin::Bin/../lib"; |
| 19 | }; |
| 20 | |
Akron | 95bc98a | 2020-07-11 12:00:12 +0200 | [diff] [blame] | 21 | use KorAP::XML::TEI qw'remove_xml_comments'; |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 22 | use KorAP::XML::TEI::Tokenizer::External; |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 23 | use KorAP::XML::TEI::Tokenizer::Conservative; |
| 24 | use KorAP::XML::TEI::Tokenizer::Aggressive; |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 25 | use KorAP::XML::TEI::Zipper; |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 26 | use KorAP::XML::TEI::Header; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 27 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 28 | our $VERSION = '0.01'; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 29 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 30 | our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n"; |
| 31 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 32 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 33 | # Parse options from the command line |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 34 | GetOptions( |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 35 | "root|r=s" => \(my $_root_dir = '.'), # name of root directory inside zip file |
| 36 | "input|i=s" => \(my $input_fname = ''), # input file (yet only TEI I5 Format accepted) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 37 | 'tokenizer-call|tc=s' => \(my $tokenizer_call), # Temporary argument for testing purposes |
Peter Harders | f9c5124 | 2020-07-21 02:37:44 +0200 | [diff] [blame] | 38 | 'use-intern-tokenization|ti' => \(my $tokenizer_intern), # use intern tokenization (default = no) |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 39 | 'help|h' => sub { |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 40 | pod2usage( |
| 41 | -verbose => 99, |
| 42 | -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS', |
| 43 | -msg => $VERSION_MSG, |
| 44 | -output => '-' |
| 45 | ) |
| 46 | }, |
| 47 | 'version|v' => sub { |
| 48 | pod2usage( |
| 49 | -verbose => 0, |
| 50 | -msg => $VERSION_MSG, |
| 51 | -output => '-' |
| 52 | ) |
| 53 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 54 | ); |
| 55 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 56 | # |
| 57 | # ~~~ parameter (mandatory) ~~~ |
| 58 | # |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 59 | my $_TEXT_BODY = "text"; # tag (without attributes), which contains the primary text |
| 60 | # optional |
| 61 | my $_CORP_HEADER_BEG = "idsHeader type=\"corpus\""; # just keep the correct order of the attributes and evtl. add an '.*' between them |
| 62 | # optional |
| 63 | my $_DOC_HEADER_BEG = "idsHeader type=\"document\""; # analog |
| 64 | # mandatory |
| 65 | my $_TEXT_HEADER_BEG = "idsHeader type=\"text\""; # analog |
| 66 | |
| 67 | # |
| 68 | # ~~~ constants ~~~ |
| 69 | # |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 70 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 71 | ## extern tokenization |
Peter Harders | f9c5124 | 2020-07-21 02:37:44 +0200 | [diff] [blame] | 72 | my $_GEN_TOK_EXT = $tokenizer_call ? 1 : 0; |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 73 | # TODO: |
| 74 | # Read tokenizer call from configuration file. |
| 75 | # was 'java -cp '. join(":", ".", glob(&dirname(__FILE__)."/../target/*.jar")). " de.ids_mannheim.korap.tokenizer.KorAPTokenizerImpl"; |
| 76 | my $ext_tok; |
| 77 | if ($tokenizer_call) { |
| 78 | $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call); |
| 79 | }; |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 80 | my $_tok_file_ext = "tokens.xml"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 81 | ## |
| 82 | |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 83 | ## intern tokenization |
Peter Harders | f9c5124 | 2020-07-21 02:37:44 +0200 | [diff] [blame] | 84 | my $_GEN_TOK_INT = $tokenizer_intern; # simple tokenization (recommended for testing) |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 85 | my $_tok_file_con = "tokens_conservative.xml"; |
| 86 | my $_tok_file_agg = "tokens_aggressive.xml"; |
| 87 | my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new; |
| 88 | my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new; |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 89 | ## |
| 90 | |
| 91 | my $_tok_dir = "base"; # name of directory for storing tokenization files |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 92 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 93 | my $_DEBUG = 0; # set to 1 for minimal more debug output (no need to be parametrized) |
| 94 | my $_XCT_LN = 0; # only for debugging: include line numbers in elements of $tree_data |
| 95 | # (see also manpage of XML::CompactTree::XS) |
| 96 | |
| 97 | my $_header_file = "header.xml"; # name of files containing the text, document and corpus header |
| 98 | my $_data_file = "data.xml"; # name of file containing the primary text data (tokens) |
| 99 | my $_structure_dir = "struct"; # name of directory containing the $_structure_file |
| 100 | my $_structure_file = "structure.xml"; # name of file containing all tags (except ${_TOKEN_TAG}'s) related information |
| 101 | # (= their names and byte offsets in $_data) |
| 102 | ## TODO: optional (different annotation tools can produce more zip-files for feeding into KorAP-XML-Krill) |
| 103 | my $_TOKENS_PROC = 1; # on/off: processing of ${_TOKEN_TAG}'s (default: 1) |
| 104 | my $_tokens_dir = "tokens"; # name of directory containing the $_tokens_file |
| 105 | my $_tokens_file = "morpho.xml"; # name of file containing all ${_TOKEN_TAG}'s related information (=their byte offsets in $_data) |
| 106 | # - evtl. with additional inline annotations |
| 107 | my $_TOKENS_TAG = "w"; # name of tag containing all information stored in $_tokens_file |
| 108 | |
| 109 | ## TODO: optional |
| 110 | # handling inline annotations (inside $_TOKENS_TAG) |
| 111 | my $_INLINE_ANNOT = 0; # on/off: set to 1 if inline annotations are present and should be processed (default: 0) |
| 112 | my $_INLINE_LEM_RD = "lemma"; # from which attribute to read LEMMA information |
| 113 | my $_INLINE_ATT_RD = "ana"; # from which attribute to read POS information (and evtl. additional MSD - Morphosyntactic Descriptions) |
| 114 | # TODO: The format for the POS and MSD information has to suffice the regular expression ([^ ]+)( (.+))? |
| 115 | # - which means, that the POS information can be followed by an optional blank with additional |
| 116 | # MSD information; unlike the MSD part, the POS part may not contain any blanks. |
| 117 | my $_INLINE_POS_WR = "pos"; # name (inside $_tokens_file) referring to POS information |
| 118 | my $_INLINE_MSD_WR = "msd"; # name (inside $_tokens_file) referring to MSD information |
| 119 | my $_INLINE_LEM_WR = "lemma"; # name (inside $_tokens_file) referring to LEMMA information |
| 120 | ## |
| 121 | |
| 122 | |
| 123 | # |
| 124 | # ~~~ variables ~~~ |
| 125 | # |
| 126 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 127 | # Initialize zipper |
| 128 | my $zipper = KorAP::XML::TEI::Zipper->new; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 129 | my $input_fh; # input file handle (default: stdin) |
| 130 | |
| 131 | my $buf_in; # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader |
| 132 | my $data; # contains the primary text (created by func. 'retr_info' from $buf_in), which is written to '$data_file' |
| 133 | |
| 134 | my $dir; # text directory (below $_root_dir) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 135 | |
| 136 | my ( $text_id, $text_id_esc ); # '$text_id_esc' = escaped version of $text_id (see %ent) |
| 137 | |
| 138 | my %ent = ('"', '"', '&','&', # convert '&', '<' and '>' into their corresponding sgml-entities |
| 139 | '<','<','>','>'); |
| 140 | # note: the index still refers to the 'single character'-versions, which are counted as 1 |
| 141 | # (search for '&' in data.xml and see corresponding indices in $_tokens_file) |
| 142 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 143 | my ( $data_fl ); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 144 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 145 | my ( $data_prfx1, $data_prfx2, $data_sfx ); # $data_* are written to $_data_file |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 146 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 147 | |
| 148 | my @structures; # list of arrays, where each array represents a TEI I5 tag (except $_TOKENS_TAG) from the input document |
| 149 | # - the input of this array is written in func. 'write_structures' into the file '$_structure_file' |
| 150 | |
| 151 | my @tokens; # list of arrays, where each array represents a $_TOKENS_TAG from the input document |
| 152 | # - the input of this array is written in func. 'write_tokens' into the file '$_tokens_file' |
| 153 | |
| 154 | my ( $ref, $idx, $att_idx ); # needed in func. 'write_structures' and 'write_tokens' |
| 155 | |
| 156 | my ( $reader, # instance of 'XML::LibXML::Reader->new' (on input '$buf_in') |
| 157 | $tree_data ); # instance of 'XML::CompactTree::XS::readSubtreeToPerl' (on input '$reader') |
| 158 | |
| 159 | # these are only used inside recursive function 'retr_info' |
| 160 | my ( $_IDX, # value is set dependent on $_XCT_LN - for extracting array of child elements from element in $tree_data |
| 161 | $e, # element from $tree_data |
| 162 | $n, # tag name of actual processed element $e |
| 163 | $rl, # recursion level |
| 164 | $dl, # actual length of string $data |
| 165 | @oti, # oti='open tags indizes' - a stack of indizes into @structures, where the top index in @oti |
| 166 | # represents the actual processed element from @structures |
| 167 | @oti2, # analogously to @oti, but with reference to array @tokens |
| 168 | $inside_tokens_tag, # flag is set, when inside $_TOKENS_TAG |
| 169 | ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags) |
| 170 | $add_one, # ... |
| 171 | $fval, $fval2, # ... |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 172 | %ws); # hash for indices of whitespace-nodes (needed to recorrect from-values) |
| 173 | # 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] | 174 | # (means: 'from-index - 1' is a key in %ws). |
| 175 | # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1 |
| 176 | |
| 177 | my $output; # temporary variable needed in 'write_*'-functions for writing output to zip-stream $zip) |
| 178 | |
| 179 | my ( $i, $c ); # index variables used in loops |
| 180 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 181 | |
| 182 | # |
| 183 | # ~~~ main ~~~ |
| 184 | # |
| 185 | |
| 186 | # ~ initializations ~ |
| 187 | |
| 188 | ($_XCT_LN)?($_IDX=5):($_IDX=4); |
| 189 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 190 | $data_prfx1 = $data_prfx2 = $data_sfx = ""; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 191 | |
| 192 | $inside_tokens_tag = -1; |
| 193 | |
| 194 | $fval = $fval2 = 0; |
| 195 | |
| 196 | $_root_dir .= '/'; # base dir must always end with a slash |
| 197 | $_root_dir =~ s/^\.?\///; # remove leading / (only relative paths allowed in IO::Compress::Zip) and redundant ./ |
| 198 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 199 | $_CORP_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; |
| 200 | $_DOC_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; |
| 201 | $_TEXT_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 202 | |
| 203 | $data_prfx1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 204 | $data_prfx1 .= "<?xml-model href=\"text.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n"; |
| 205 | $data_prfx1 .= "<raw_text docid=\""; |
| 206 | $data_prfx2 .= "\" xmlns=\"http://ids-mannheim.de/ns/KorAP\">\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 207 | ## TODO: can 'metadata.xml' change or is it constant? |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 208 | $data_prfx2 .= " <metadata file=\"metadata.xml\" />\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 209 | ## |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 210 | $data_prfx2 .= " <text>"; |
| 211 | $data_sfx = "</text>\n</raw_text>"; |
| 212 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 213 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 214 | # ~ read input and write output (text by text) ~ |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 215 | main(); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 216 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 217 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 218 | # |
| 219 | # ~~~ subs ~~~ |
| 220 | # |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 221 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 222 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 223 | sub main { |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 224 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 225 | my ( $pfx, $sfx ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 226 | |
Akron | 95bc98a | 2020-07-11 12:00:12 +0200 | [diff] [blame] | 227 | # TODO: |
| 228 | # Replace all calls of $lc with $. or $input_fh->input_line_number, |
| 229 | # because otherwise remove_html_comments will |
| 230 | # move the lines forward without incrementing. |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 231 | my $lc = 0; # line counter (only for error handling and debugging) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 232 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 233 | my $tl = 0; # text line (needed for whitespace handling) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 234 | |
| 235 | $input_fh = *STDIN; # input file handle (default: stdin) |
| 236 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 237 | $data_fl = 0; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 238 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 239 | $buf_in = $data = $dir = ""; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 240 | |
| 241 | |
| 242 | if ( $input_fname ne '' ){ |
| 243 | |
| 244 | open ( $input_fh, "<", "$input_fname") || die "File \'$input_fname\' could not be opened.\n"; |
| 245 | |
| 246 | } |
| 247 | |
| 248 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 249 | # prevents segfaulting of 'XML::LibXML::Reader' inside 'main()' - see notes on 'PerlIO layers' in 'man XML::LibXML') |
Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 250 | # removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular |
| 251 | binmode $input_fh; |
| 252 | |
| 253 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 254 | # ~ loop (reading input document) ~ |
| 255 | |
| 256 | while ( <$input_fh> ){ |
| 257 | |
| 258 | $lc++; # line counter |
| 259 | |
| 260 | # TODO: yet not tested fo big amounts of data |
| 261 | # must-have, otherwise comments in input could be fatal (e.g.: ...<!--\n<idsHeader...\n-->...) |
Akron | 95bc98a | 2020-07-11 12:00:12 +0200 | [diff] [blame] | 262 | remove_xml_comments( $input_fh, $_ ); # remove HTML comments (<!--...-->) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 263 | |
| 264 | if ( $data_fl && m#^(.*)</${_TEXT_BODY}>(.*)$# ){ |
| 265 | |
| 266 | |
| 267 | # ~ end of text body ~ |
| 268 | |
| 269 | |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 270 | # 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 Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 271 | |
| 272 | $pfx = $1; $sfx = $2; |
| 273 | |
| 274 | die "ERROR ($0): main(): input line number $lc: line with closing text-body tag '${_TEXT_BODY}'" |
| 275 | ." contains additional information ... => Aborting\n\tline=$_" |
| 276 | if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/; |
| 277 | |
| 278 | if ( $dir ne "" ){ |
| 279 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 280 | $reader = XML::LibXML::Reader->new( string => "<text>$buf_in</text>", huge => 1 ); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 281 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 282 | # ~ whitespace handling ~ |
| 283 | # |
| 284 | # Every whitespace inside the processed text is 'significant' and recognized as a node of type 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE' |
| 285 | # (see function 'retr_info()'). |
| 286 | # |
| 287 | # Definition of significant and insignificant whitespace |
| 288 | # (source: https://www.oracle.com/technical-resources/articles/wang-whitespace.html): |
| 289 | # |
| 290 | # Significant whitespace is part of the document content and should be preserved. |
| 291 | # Insignificant whitespace is used when editing XML documents for readability. |
| 292 | # These whitespaces are typically not intended for inclusion in the delivery of the document. |
| 293 | # |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 294 | if ( $_XCT_LN ){ # _XCT_LINE_NUMBERS is only for debugging |
| 295 | $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY | XCT_LINE_NUMBERS ); |
| 296 | } else { |
| 297 | $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY ); |
| 298 | } |
| 299 | |
| 300 | @structures = (); @oti = (); |
| 301 | |
| 302 | if ( $_TOKENS_PROC ){ |
| 303 | @tokens = (); @oti2 = () |
| 304 | } |
| 305 | |
| 306 | $dl = $rl = 0; |
| 307 | |
| 308 | # ~ whitespace related issue ~ |
| 309 | $add_one = 0; |
| 310 | %ws = (); |
| 311 | |
| 312 | |
| 313 | # ~ recursion ~ |
| 314 | |
| 315 | retr_info( \$tree_data->[2] ); # parse input data |
| 316 | |
| 317 | $rl--; |
| 318 | |
| 319 | |
| 320 | # ~ write data.xml ~ |
| 321 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 322 | # TODO: should not be necessary, because whitespace at the end of every input line is removed: see 'whitespace handling' inside text body |
| 323 | # (...elsif ( $data_fl )....) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 324 | $data =~ tr/\n\r/ /; # note: 2 blanks - otherwise offset data would become corrupt |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 325 | # |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 326 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 327 | |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 328 | if ( $_GEN_TOK_EXT ){ |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 329 | |
| 330 | $ext_tok->tokenize($data); |
| 331 | |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if ( $_GEN_TOK_INT ){ |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 335 | |
| 336 | $cons_tok->tokenize($data); |
| 337 | $aggr_tok->tokenize($data); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 338 | } |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 339 | |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 340 | $data = encode_utf8( $data ); |
| 341 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 342 | print STDERR "DEBUG ($0): main(): Writing (utf8-formatted) xml file $_root_dir$dir/$_data_file\n" if $_DEBUG; |
| 343 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 344 | |
| 345 | $data =~ s/(&|<|>)/$ent{$1}/g; |
| 346 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 347 | $zipper->new_stream("$_root_dir$dir/$_data_file") |
| 348 | ->print("$data_prfx1$text_id_esc$data_prfx2$data$data_sfx"); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 349 | |
| 350 | # ~ write structures ~ |
| 351 | |
| 352 | write_structures() if @structures; |
| 353 | |
| 354 | |
| 355 | # ~ write tokens ~ |
| 356 | |
| 357 | write_tokens() if $_TOKENS_PROC && @tokens; |
| 358 | |
| 359 | |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 360 | # ~ tokenization ~ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 361 | |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 362 | if ( $_GEN_TOK_EXT ) { |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 363 | |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 364 | $ext_tok->to_zip( |
| 365 | $zipper->new_stream("$_root_dir$dir/$_tok_dir/$_tok_file_ext"), |
| 366 | $text_id_esc |
| 367 | ) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 368 | |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if ( $_GEN_TOK_INT ){ |
Akron | 997e940 | 2020-07-11 12:06:13 +0200 | [diff] [blame] | 372 | |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 373 | # Output token streams to zip streams |
| 374 | $cons_tok->to_zip( |
| 375 | $zipper->new_stream("$_root_dir$dir/$_tok_dir/$_tok_file_con"), |
| 376 | $text_id_esc |
| 377 | ); |
| 378 | $aggr_tok->to_zip( |
| 379 | $zipper->new_stream("$_root_dir$dir/$_tok_dir/$_tok_file_agg"), |
| 380 | $text_id_esc |
| 381 | ); |
| 382 | $aggr_tok->reset; |
| 383 | $cons_tok->reset; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 386 | #print STDERR "$0: write_tokenization(): DONE\n"; |
| 387 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 388 | $data_fl = 0; $buf_in = $data = $dir = ""; # reinit. |
| 389 | |
| 390 | } else { # $dir eq "" |
| 391 | |
| 392 | print STDERR "WARNING ($0): main(): maybe empty textSigle => skipping this text ...\n"; |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 393 | # print STDERR "WARNING ($0): main(): text header=$header_txt\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 394 | print STDERR "WARNING ($0): main(): data=$data\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 397 | } elsif ( $data_fl ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 398 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 399 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 400 | # ~ inside text body ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 401 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 402 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 403 | #print STDERR "inside text body (\$data_fl set)\n"; |
| 404 | |
| 405 | # ~ whitespace handling ~ |
| 406 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 407 | # The idea for the below code fragment was to fix (recreate) missing whitespace in a poorly created corpus, in which linebreaks where inserted |
| 408 | # into the text with the addition that maybe (or not) whitespace before those linebreaks was unintenionally stripped. |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 409 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 410 | # It soon turned out, that it was best to suggest considering just avoiding linebreaks and putting all primary text tokens into one line (see |
| 411 | # example further down and notes on 'Input restrictions' in the manpage). |
| 412 | # |
| 413 | # Somehow an old first very poor approach remained, which is not stringent, but also doesn't affect one-line text. |
| 414 | # |
| 415 | # 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 |
| 416 | # an option on how newlines in primary text should be handled (stripped or replaced by a whitespace)). |
| 417 | # |
| 418 | # Examples (how primary text with linebreaks would be converted by below code): |
| 419 | # |
| 420 | # '...<w>end</w>\n<w>.</w>...' -> '...<w>end</w> <w>.</w>...' |
| 421 | # '...<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 Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 422 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 423 | s/^\s+//; s/\s+$//; # remove consecutive whitespace at beginning and end (mostly one newline) |
| 424 | |
| 425 | ### NOTE: this is only relevant, if a text consists of more than one line |
| 426 | ### TODO: find a better solution, or create a warning, if a text has more than one line ($tl > 1) |
| 427 | ### do testing with 2 different corpora (one with only one-line texts, the other with several lines per text) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 428 | if ( m/<[^>]+>[^<]/ ){ # line contains at least one tag with at least one character contents |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 429 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 430 | # NOTE: not stringent ('...' stands for text): |
| 431 | # |
| 432 | # beg1............................end1 => no blank before 'beg1' |
| 433 | # beg2....<pb/>...................end2 => no blank before 'beg2' |
| 434 | # beg3....<info attr1="val1"/>....end3 => no blank before 'beg3' |
| 435 | # beg4....<test>ok</test>.........end4 => blank before 'beg4' |
| 436 | # |
| 437 | # => beg1....end1beg2...<pb/>...end2beg3....<info attr1="val1"/>....end3 beg4...<test>ok</test>....end4 |
| 438 | # ^ |
| 439 | # |_blank between 'end3' and 'beg4' |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 440 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 441 | $tl++; # counter for text lines |
| 442 | |
| 443 | s/^(.)/ $1/ if $tl > 1; # insert blank before 1st character (for 2nd line and consecutive lines) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 444 | } |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 445 | ### |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 446 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 447 | # add line to buffer |
| 448 | $buf_in .= $_; |
| 449 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 450 | } elsif ( m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){ |
| 451 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 452 | # ~ start of text body ~ |
| 453 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 454 | #print STDERR "inside text body\n"; |
| 455 | |
| 456 | $pfx = $1; $sfx = $2; |
| 457 | |
| 458 | $data_fl = 1; |
| 459 | |
| 460 | die "ERROR ($0): main(): input line number $lc: line with opening text-body tag '${_TEXT_BODY}'" |
| 461 | ." contains additional information ... => Aborting\n\tline=$_" |
| 462 | if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/; |
| 463 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 464 | } elsif ( m#^(.*)(<(?:${_TEXT_HEADER_BEG}|${_DOC_HEADER_BEG}|${_CORP_HEADER_BEG}).*)$# ){ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 465 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 466 | # ~ start of header ~ |
| 467 | $pfx = $1; |
| 468 | my $content = "$2\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 469 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 470 | die "ERROR ($0): main(): input line number $lc: line with opening header tag" |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 471 | ." is not in expected format ... => Aborting\n\tline=$_" |
| 472 | if $pfx !~ /^\s*$/; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 473 | |
Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 474 | # Parse header |
| 475 | my $header = KorAP::XML::TEI::Header->new($content)->parse($input_fh); |
| 476 | |
| 477 | # Header was parseable |
| 478 | if ($header) { |
| 479 | |
| 480 | # Write header to zip |
| 481 | my $file = $_root_dir . $header->dir . '/' . $_header_file; |
| 482 | |
| 483 | print STDERR "DEBUG ($0): Writing file $file\n" if $_DEBUG; |
| 484 | |
| 485 | $header->to_zip($zipper->new_stream($file)); |
| 486 | |
| 487 | # Header is for text level |
| 488 | if ($header->type eq 'text') { |
| 489 | |
| 490 | # Remember dir and sigles |
| 491 | $dir = $header->dir; |
| 492 | $text_id = $header->id; |
| 493 | $text_id_esc = $header->id_esc; |
| 494 | |
| 495 | # log output for seeing progression |
| 496 | print STDERR "$0: main(): text_id=".decode_utf8( $text_id )."\n"; |
| 497 | |
| 498 | $tl = 0; # reset (needed for ~ whitespace handling ~) |
| 499 | }; |
| 500 | } |
| 501 | } |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 502 | } #end: while |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 503 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 504 | $zipper->close; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 505 | |
Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 506 | if( $_GEN_TOK_EXT ){ |
| 507 | $ext_tok->close; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 508 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 509 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 510 | } # end: sub main |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 511 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 512 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 513 | sub retr_info { # called from main() |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 514 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 515 | # Notes on how 'XML::CompactTree::XS' works |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 516 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 517 | # Example: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node> |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 518 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 519 | # Print out name of 'node2' for the above example: |
| 520 | # |
| 521 | # 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"' |
| 522 | # |
| 523 | # Exploring the structure of $data ( = reference to below array ): |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 524 | # |
| 525 | # [ 0: XML_READER_TYPE_DOCUMENT, |
| 526 | # 1: ? |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 527 | # 2: [ 0: [ 0: XML_READER_TYPE_ELEMENT <- start recursion with array '$data->[2]' (see main(): retr_info( \$tree_data->[2] )) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 528 | # 1: 'node' |
| 529 | # 2: ? |
| 530 | # 3: HASH (attributes) |
| 531 | # 4: 1 (line number) |
| 532 | # 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT |
| 533 | # 1: 'node1' |
| 534 | # 2: ? |
| 535 | # 3: undefined (no attributes) |
| 536 | # 4: 1 (line number) |
| 537 | # 5: [ 0: [ 0: XML_READER_TYPE_TEXT |
| 538 | # 1: 'some ' |
| 539 | # ] |
| 540 | # 1: [ 0: XML_READER_TYPE_ELEMENT |
| 541 | # 1: 'n' |
| 542 | # 2: ? |
| 543 | # 3: undefined (no attributes) |
| 544 | # 4: 1 (line number) |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 545 | # 5: undefined (no child-nodes) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 546 | # ] |
| 547 | # 2: [ 0: XML_READER_TYPE_TEXT |
| 548 | # 1: ' text' |
| 549 | # ] |
| 550 | # ] |
| 551 | # ] |
| 552 | # 1: [ 0: XML_READER_TYPE_ELEMENT |
| 553 | # 1: 'node2' |
| 554 | # 2: ? |
| 555 | # 3: undefined (not attributes) |
| 556 | # 4: 1 (line number) |
| 557 | # 5: [ 0: [ 0: XML_READER_TYPE_TEXT |
| 558 | # 1: 'more-text' |
| 559 | # ] |
| 560 | # ] |
| 561 | # ] |
| 562 | # ] |
| 563 | # ] |
| 564 | # ] |
| 565 | # ] |
| 566 | # |
| 567 | # $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT) |
| 568 | # |
| 569 | # ref($data->[2]) == ARRAY (with 1 element for 'node') |
| 570 | # ref($data->[2]->[0]) == ARRAY (with 6 elements) |
| 571 | # |
| 572 | # $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 573 | # $data->[2]->[0]->[1] == 'node' |
| 574 | # ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v') |
| 575 | # $data->[2]->[0]->[4] == 1 (line number) |
| 576 | # ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2') |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 577 | # # child-nodes of actual node (see $_IDX) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 578 | # |
| 579 | # ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements) |
| 580 | # $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 581 | # $data->[2]->[0]->[5]->[0]->[1] == 'node1' |
| 582 | # $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute) |
| 583 | # $data->[2]->[0]->[5]->[0]->[4] == 1 (line number) |
| 584 | # ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text') |
| 585 | # |
| 586 | # ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements) |
| 587 | # $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT) |
| 588 | # $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some ' |
| 589 | # |
| 590 | # ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements) |
| 591 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 592 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n' |
| 593 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute) |
| 594 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number) |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 595 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child-nodes) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 596 | # |
| 597 | # ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements) |
| 598 | # $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT) |
| 599 | # $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text' |
| 600 | # |
| 601 | # |
| 602 | # retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example. |
| 603 | # Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to |
| 604 | # ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]). |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 605 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 606 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 607 | $rl++; # recursion level (1 = topmost level inside retr_info() = should always be level of tag $_TEXT_BODY) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 608 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 609 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 610 | foreach $e ( @{${$_[0]}} ){ # iteration through all array elements ($_[0] is a reference to an array reference) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 611 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 612 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 613 | if ( $e->[0] == XML_READER_TYPE_ELEMENT ){ # element-node (see 'NODE TYPES' in manpage of XML::LibXML::Reader) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 614 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 615 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 616 | #~~~~ |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 617 | # from here: tag-node (opening) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 618 | #~~~~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 619 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 620 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 621 | # insert new array (for new tag) into @structures with tag-name and tag-attributes (if present) |
| 622 | # update @oti (open tags indizes) with @structures highest index (= $#structures); e.g.: @a=(1,2,3) => $#a = 2 |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 623 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 624 | # ~ tag name ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 625 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 626 | $n = $e->[1]; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 627 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 628 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 629 | # ~ handle structures ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 630 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 631 | my @array; |
| 632 | push @array, $n; |
| 633 | push @structures, \@array; |
| 634 | push @oti, $#structures; # add highest index of @structures to @oti |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 635 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 636 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 637 | # ~ handle tokens ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 638 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 639 | $inside_tokens_tag = $rl if $_TOKENS_PROC && $n eq $_TOKENS_TAG; # wether to push entry also into @tokens array |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 640 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 641 | if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 642 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 643 | my @array2; |
| 644 | push @array2, $n; |
| 645 | push @tokens, \@array2; |
| 646 | push @oti2, $#tokens; |
| 647 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 648 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 649 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 650 | # ~ handle attributes ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 651 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 652 | if ( defined $e->[3] ){ # only if attributes exist |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 653 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 654 | for ( $c = 0; $c < @{$e->[3]}; $c += 2 ){ # with 'XCT_ATTRIBUTE_ARRAY', $node->[3] is an array reference of the form |
| 655 | # [ name1, value1, name2, value2, ....] of attribute names and corresponding values. |
| 656 | # note: arrays are faster (see: http://makepp.sourceforge.net/2.0/perl_performance.html) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 657 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 658 | # '$c' references the 'key' and '$c+1' the 'value' |
| 659 | push @{$structures[$#structures]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1]; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 660 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 661 | if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 662 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 663 | push @{$tokens[$#tokens]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1]; |
| 664 | } |
| 665 | |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | |
| 670 | # ~ index 'from' ~ |
| 671 | |
| 672 | # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts |
| 673 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 674 | push @{$structures[$#structures]}, ( $dl + $add_one ); # see below (text- and whitespace-nodes) for explanation on '$add_one' |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 675 | |
| 676 | if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){ |
| 677 | |
| 678 | push @{$tokens[$#tokens]}, ( $dl + $add_one ); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | #~~~~ |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 683 | # until here: tag-node (opening) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 684 | #~~~~ |
| 685 | |
| 686 | |
| 687 | # ~~ RECURSION ~~ |
| 688 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 689 | 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 Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 690 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 691 | retr_info( \$e->[$_IDX] ); # recursion with array of child-nodes |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 692 | |
| 693 | $rl--; # return from recursion |
| 694 | } |
| 695 | |
| 696 | |
| 697 | #~~~~~ |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 698 | # from here: tag-node (closing) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 699 | #~~~~~ |
| 700 | |
| 701 | |
| 702 | # ~ handle structures ~ |
| 703 | |
| 704 | { |
| 705 | my $ix = pop @oti; # index of just closed tag |
| 706 | |
| 707 | my $aix = $#{$structures[$ix]}; # determine highest index from 'array referring to last closed tag' ... |
| 708 | |
| 709 | $fval = ${$structures[$ix]}[ $aix ]; # ... and get it's from-value |
| 710 | |
| 711 | if ( $fval > 0 && not exists $ws{ $fval - 1 } ){ # ~ whitespace related issue ~ |
| 712 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 713 | # ~ previous node was a text-node ~ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 714 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 715 | ${$structures[$ix]}[ $aix ] = $fval - 1; # recorrect from-value (see below: Notes on ~ whitespace related issue ~) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | # in case this fails, check input |
| 719 | die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@structures: from-value ($fval) is 2 or more greater" |
| 720 | ." than to-value ($dl) => please check. aborting ...\n" |
| 721 | if ( $fval - 1 ) > $dl; |
| 722 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 723 | # TODO: find example for which this case applies |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 724 | # maybe this is not necessary anymore, because the above recorrection of the from-value suffices |
| 725 | # TODO: check, if it's better to remove this line and change above check to 'if ( $fval - 1) >= $dl; |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 726 | # do testing with bigger corpus excerpt (wikipedia?) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 727 | ${$structures[$ix]}[ $aix ] = $dl if $fval == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl) |
| 728 | |
| 729 | push @{$structures[$ix]}, $dl, $rl; # to-value and recursion-level |
| 730 | |
| 731 | # note: use $dl, because the offsets are _between_ the characters (e.g.: word = 'Hello' => from = 0 (before 'H'), to = 5 (after 'o')) |
| 732 | } |
| 733 | |
| 734 | |
| 735 | # ~ handle tokens ~ |
| 736 | |
| 737 | |
| 738 | if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){ |
| 739 | |
| 740 | my $ix = pop @oti2; |
| 741 | |
| 742 | my $aix = $#{$tokens[$ix]}; |
| 743 | |
| 744 | $fval2 = ${$tokens[$ix]}[ $aix ]; # from-value |
| 745 | |
| 746 | if( $fval2 > 0 && not exists $ws{ $fval2 - 1 } ){ # ~ whitespace related issue ~ |
| 747 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 748 | # ~ previous node was a text-node ~ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 749 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 750 | ${$tokens[$ix]}[ $aix ] = $fval2 - 1; # recorrect from-value (see below: Notes on ~ whitespace related issue ~) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | # in case this fails, check input |
| 754 | die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@tokens: from-value ($fval2) is 2 or more greater" |
| 755 | ." than to-value ($dl) => please check. aborting ...\n" |
| 756 | if ( $fval2 - 1 ) > $dl; |
| 757 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 758 | # TODO: find example for which this case applies |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 759 | # maybe this is not necessary anymore, because the above recorrection of the from-value suffices |
| 760 | # TODO: check, if it's better to remove this line and change above check to 'if ( $fval2 - 1) >= $dl; |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 761 | # do testing with bigger corpus excerpt (wikipedia?) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 762 | ${$tokens[$ix]}[ $aix ] = $dl if $fval2 == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl) |
| 763 | |
| 764 | push @{$tokens[$ix]}, $dl, $rl; # to-value and recursion-level |
| 765 | |
| 766 | $inside_tokens_tag = -1; # reset |
| 767 | } |
| 768 | |
| 769 | # ~ whitespace related issue ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 770 | # clean up |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 771 | delete $ws{ $fval - 1 } if $fval > 0 && exists $ws{ $fval - 1 }; |
| 772 | delete $ws{ $fval2 - 1 } if $_TOKENS_PROC && $fval2 > 0 && exists $ws{ $fval2 - 1 }; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 773 | |
| 774 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 775 | #~~~~ |
| 776 | # until here: tag-node (closing) |
| 777 | #~~~~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 778 | |
| 779 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 780 | #~~~~~ |
| 781 | # from here: text- and whitespace-nodes |
| 782 | #~~~~~ |
| 783 | |
| 784 | # The 3rd form of nodes, besides text- (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes of the type |
| 785 | # 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE'. |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 786 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 787 | # When modifiying the previous example (see: Notes on how 'XML::CompactTree::XS' works) by inserting an additional blank between |
| 788 | # '</node1>' and '<node2>', the output for '$data->[2]->[0]->[5]->[1]->[1]' is a blank (' ') and it's type is '14' |
| 789 | # (XML_READER_TYPE_SIGNIFICANT_WHITESPACE, see 'man XML::LibXML::Reader'): |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 790 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 791 | # 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 Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 792 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 793 | } elsif ( $e->[0] == XML_READER_TYPE_TEXT || $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 794 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 795 | # Notes on ~ whitespace related issue ~ (referred to the code fragment below) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 796 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 797 | # Example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...' |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 798 | # |
| 799 | # Two text-nodes should normally be separated by a blank. In the above example, that would be the 2 text-nodes |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 800 | # 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' ' (see [2]). |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 801 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 802 | # The text-node 'Campagne in Frankreich' leads to the setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag, |
| 803 | # it's from-index gets set to the correct start-index of '1792' (and not to the start-index of the whitespace-node ' '). |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 804 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 805 | # The assumption here is, that in most cases there _is_ a whitespace node between 2 text-nodes. The below code fragment |
| 806 | # enables a way, to check, if this really _was_ the case for the last 2 'non-tag'-nodes, when closing a tag: |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 807 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 808 | # 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. |
| 809 | # So when closing a tag, it can be checked, if the previous 'non-tag'-node (text or whitespace), which is the one before |
| 810 | # 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 |
| 811 | # the last read 'non-tag'-node has to be corrected (see [1]), |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 812 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 813 | # For whitespace-nodes $add_one is set to 0, so when opening the next tag (in the above example the 2nd 's'-tag), no |
| 814 | # additional 1 is added (because this was already done by the whitespace-node itself when incrementing the variable $dl). |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 815 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 816 | # [1] |
| 817 | # Now, what happens, when 2 text-nodes are _not_ seperated by a whitespace-node (e.g.: <w>Augen<c>,</c></w>)? |
| 818 | # In this case, the falsely increased from-value has to be decreased again by 1 when closing the enclosing tag |
| 819 | # (see above code fragment '... not exists $ws{ $fval - 1 } ...'). |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 820 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 821 | # [2] |
| 822 | # 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 |
| 823 | # whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE). |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 824 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 825 | # The from-index of the 2nd w-tag in the second example refers to 'bar', which may not have been the intention |
| 826 | # (even though '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug? |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 827 | # |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 828 | # 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- |
| 829 | # and to-indizes for the tags 'a' and 'b' both 12, which is the start-index of the token 'tok3'. |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 830 | |
| 831 | if( $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){ |
| 832 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 833 | # ~ whitespace-node ~ |
| 834 | |
| 835 | # ~ whitespace related issue ~ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 836 | |
| 837 | $add_one = 0; |
| 838 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 839 | $ws{ $dl }++; # state, that this from-index belongs to a whitespace-node |
| 840 | # ('++' doesn't mean a thing here - maybe it could be used for a consistency check) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 841 | |
| 842 | }else{ |
| 843 | |
| 844 | # ~ text-node ~ |
| 845 | |
| 846 | $add_one = 1; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | # ~ update $data and $dl ~ |
| 851 | |
| 852 | $data .= $e->[1]; |
| 853 | |
| 854 | $dl += length( $e->[1] ); # update length of $data |
| 855 | |
| 856 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 857 | #~~~~~ |
| 858 | # until here: text- and whitespace-nodes |
| 859 | #~~~~~ |
| 860 | |
| 861 | |
| 862 | #elsif ( $e->[0] == XML_READER_TYPE_ATTRIBUTE ) # attribute-node |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 863 | # note: attributes cannot be processed like this ( => use 'XCT_ATTRIBUTE_ARRAY' - see above ) |
| 864 | |
| 865 | |
| 866 | }else{ # not yet handled type |
| 867 | |
| 868 | die "ERROR ($0): Not yet handled type (\$e->[0]=".$e->[0].") ... => Aborting\n"; |
| 869 | } |
| 870 | |
| 871 | } # end: foreach iteration |
| 872 | |
| 873 | } # end: sub retr_info |
| 874 | |
| 875 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 876 | sub write_structures { # called from main() |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 877 | |
| 878 | # ~ write @structures ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 879 | |
| 880 | #print STDERR "$0: write_structures(): ...\n"; |
| 881 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 882 | if ( $dir eq "" ){ |
| 883 | |
| 884 | print STDERR "WARNING ($0): write_structures(): empty textSigle => nothing to do ...\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 885 | return; |
| 886 | } |
| 887 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 888 | $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\"" |
| 889 | ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\"" |
| 890 | .decode_utf8($text_id_esc)."\" xmlns=\"http://ids-mannheim.de/ns/KorAP\" version=\"KorAP-0.4\">\n <spanList>\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 891 | |
| 892 | $c = 0; |
| 893 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 894 | foreach $ref ( @structures ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 895 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 896 | ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 ); # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4 |
| 897 | # (name, from, to, rec_level), otherwise >4 |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 898 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 899 | # correct last from-value ( if the 'second to last' from-value refers to an s-tag, then the last from-value is one to big - see retr_info ) |
| 900 | |
| 901 | if( $#structures == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){ |
| 902 | |
| 903 | ${$ref}[$idx] = ${$ref}[ $idx+1 ]; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 904 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 905 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 906 | # this consistency check is already done in 'retr_info()' |
| 907 | #elsif( ${$ref}[$idx] > ${$ref}[$idx+1] ){ # consistency check: abort, if this doesn't hold |
| 908 | # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):" |
| 909 | # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n"; |
| 910 | # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):" |
| 911 | # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n\n$output" } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 912 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 913 | # at least 'POS' should always be there => remove constraint '$_TOKENS_PROC' |
| 914 | #if( $_TOKENS_PROC && ${$ref}[0] ne $_TOKENS_TAG ) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 915 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 916 | if( ${$ref}[0] ne $_TOKENS_TAG ){ # $_TOKENS_TAG is already written in 'write_tokens' |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 917 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 918 | # l (level): insert information about depth of element in XML-tree (top element = level 1) |
| 919 | $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n" |
| 920 | ." <fs type=\"struct\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n" |
| 921 | ." <f name=\"name\">${$ref}[ 0 ]</f>\n"; |
| 922 | |
| 923 | if ( $idx > 2 ) # attributes |
| 924 | { |
| 925 | $output .= " <f name=\"attr\">\n <fs type=\"attr\">\n"; |
| 926 | |
| 927 | for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){ |
| 928 | |
| 929 | ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # see explanation in func. 'write_tokens' |
| 930 | |
| 931 | # attribute (at index $att_idx) with value (at index $att_idx+1) |
| 932 | $output .= " <f name=\"${$ref}[ $att_idx ]\">${$ref}[ $att_idx+1 ]</f>\n"; |
| 933 | } |
| 934 | |
| 935 | $output .= " </fs>\n </f>\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 938 | $output .= " </fs>\n </span>\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 939 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 940 | } # fi: ... ne $_TOKENS_TAG |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 941 | |
| 942 | $c++; |
| 943 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 944 | } # end: foreach |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 945 | |
| 946 | $output .= " </spanList>\n</layer>"; |
| 947 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 948 | $output = encode_utf8( $output ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 949 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 950 | $zipper->new_stream("$_root_dir$dir/$_structure_dir/$_structure_file") |
| 951 | ->print($output); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 952 | |
| 953 | #print STDERR "$0: write_structures(): DONE\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 954 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 955 | } # end: sub write_structures |
| 956 | |
| 957 | |
Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 958 | sub write_tokens { # called from main() |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 959 | |
| 960 | # ~ write @tokens ~ |
| 961 | |
| 962 | #print STDERR "$0: write_tokens(): ...\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 963 | |
| 964 | if( $dir eq "" ){ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 965 | |
| 966 | print STDERR "WARNING ($0): write_tokens(): empty textSigle => nothing to do ...\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 967 | return; |
| 968 | } |
| 969 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 970 | $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\"" |
| 971 | ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\"" |
| 972 | .decode_utf8($text_id_esc)."\" xmlns=\"http://ids-mannheim.de/ns/KorAP\" version=\"KorAP-0.4\">\n <spanList>\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 973 | |
| 974 | $c = 0; |
| 975 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 976 | foreach $ref ( @tokens ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 977 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 978 | # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4 (name, from, to, rec_level), otherwise >4 |
| 979 | ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 980 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 981 | # correct last from-value (if the 'second to last' from-value refers to an s-tag, then the last from-value is one to big - see retr_info()) |
| 982 | if ( $#tokens == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){ |
| 983 | |
| 984 | ${$ref}[ $idx ] = ${$ref}[ $idx+1 ]; # TODO: check |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 985 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 986 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 987 | # l (level): insert information about depth of element in XML-tree (top element = level 1) |
| 988 | $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n" |
| 989 | ." <fs type=\"lex\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n" |
| 990 | ." <f name=\"lex\">\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 991 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 992 | if ( $idx > 2 ){ # attributes |
| 993 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 994 | $output .= " <fs>\n"; |
| 995 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 996 | for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 997 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 998 | ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # ... <w lemma=">" ana="PUNCTUATION">></w> ... |
| 999 | # the '>' is translated to '>' and hence the result would be '<f name="lemma">></f>' |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1000 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1001 | if ( $_INLINE_ANNOT && ${$ref}[ $att_idx ] eq "$_INLINE_ATT_RD" ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1002 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1003 | ${$ref}[ $att_idx+1 ] =~ /^([^ ]+)(?: (.+))?$/; |
| 1004 | |
| 1005 | die "ERROR (write_tokens()): unexpected format! => Aborting ... (att: ${$ref}[ $att_idx+1 ])\n" |
| 1006 | if ( $_INLINE_POS_WR && not defined $1 ) || ( $_INLINE_MSD_WR && not defined $2 ); |
| 1007 | |
| 1008 | if ( "$_INLINE_POS_WR" ){ |
| 1009 | |
| 1010 | $output .= " <f name=\"$_INLINE_POS_WR\">"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1011 | $output .= "$1" if defined $1; |
| 1012 | $output .= "</f>\n"; |
| 1013 | } |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1014 | |
| 1015 | if ( "$_INLINE_MSD_WR" ){ |
| 1016 | |
| 1017 | $output .= " <f name=\"$_INLINE_MSD_WR\">"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1018 | $output .= "$2" if defined $2; |
| 1019 | $output .= "</f>\n"; |
| 1020 | } |
| 1021 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1022 | } elsif ( $_INLINE_ANNOT && "$_INLINE_LEM_RD" && ${$ref}[ $att_idx ] eq "$_INLINE_LEM_RD" ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1023 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1024 | $output .= " <f name=\"$_INLINE_LEM_WR\">${$ref}[ $att_idx+1 ]</f>\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1025 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1026 | } else { # all other attributes |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1027 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1028 | $output .= " <f name=\"${$ref}[$att_idx]\">${$ref}[ $att_idx+1 ]</f>\n"; # attribute (at index $att_idx) with value (at index $att_idx+1) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1029 | } |
| 1030 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1031 | } # end: for |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1032 | |
| 1033 | $output .= " </fs>\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1034 | |
| 1035 | } # fi: attributes |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1036 | |
| 1037 | $output .= " </f>\n </fs>\n </span>\n"; |
| 1038 | |
| 1039 | $c++; |
| 1040 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1041 | } # end: foreach |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1042 | |
| 1043 | $output .= " </spanList>\n</layer>"; |
| 1044 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1045 | $output = encode_utf8( $output ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1046 | |
Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 1047 | $zipper->new_stream("$_root_dir$dir/$_tokens_dir/$_tokens_file") |
| 1048 | ->print($output); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1049 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1050 | #print STDERR "$0: write_tokens(): DONE\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1051 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1052 | } # end: sub write_tokens |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1053 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1054 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1055 | __END__ |
| 1056 | |
| 1057 | =pod |
| 1058 | |
| 1059 | =encoding utf8 |
| 1060 | |
| 1061 | =head1 NAME |
| 1062 | |
| 1063 | tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML |
| 1064 | |
| 1065 | =head1 SYNOPSIS |
| 1066 | |
| 1067 | cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip |
| 1068 | |
| 1069 | =head1 DESCRIPTION |
| 1070 | |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 1071 | C<tei2korapxml> is a script to convert TEI P5 and |
| 1072 | L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html> |
| 1073 | based documents to the |
| 1074 | L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>. |
| 1075 | If no specific input is defined, data is |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1076 | read from C<STDIN>. If no specific output is defined, data is written |
| 1077 | to C<STDOUT>. |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1078 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1079 | This program is usually called from inside another script. |
| 1080 | |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 1081 | =head1 FORMATS |
| 1082 | |
| 1083 | =head2 Input restrictions |
| 1084 | |
| 1085 | =over 2 |
| 1086 | |
| 1087 | =item |
| 1088 | |
| 1089 | utf8 encoded |
| 1090 | |
| 1091 | =item |
| 1092 | |
| 1093 | TEI P5 formatted input with certain restrictions: |
| 1094 | |
| 1095 | =over 4 |
| 1096 | |
| 1097 | =item |
| 1098 | |
| 1099 | B<mandatory>: text-header with integrated textsigle, text-body |
| 1100 | |
| 1101 | =item |
| 1102 | |
| 1103 | B<optional>: corp-header with integrated corpsigle, |
| 1104 | doc-header with integrated docsigle |
| 1105 | |
| 1106 | =back |
| 1107 | |
| 1108 | =item |
| 1109 | |
| 1110 | all tokens inside the primary text (inside $data) may not be |
| 1111 | newline seperated, because newlines are removed |
| 1112 | (see code section C<~ inside text body ~>) and a conversion of newlines |
| 1113 | into blanks between 2 tokens could lead to additional blanks, |
| 1114 | where there should be none (e.g.: punctuation characters like C<,> or |
| 1115 | C<.> should not be seperated from their predecessor token). |
| 1116 | (see also code section C<~ whitespace handling ~>). |
| 1117 | |
| 1118 | =back |
| 1119 | |
| 1120 | =head2 Notes on the output |
| 1121 | |
| 1122 | =over 2 |
| 1123 | |
| 1124 | =item |
| 1125 | |
| 1126 | zip file output (default on C<stdout>) with utf8 encoded entries |
| 1127 | (which together form the KorAP-XML format) |
| 1128 | |
| 1129 | =back |
| 1130 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1131 | =head1 INSTALLATION |
| 1132 | |
| 1133 | C<tei2korapxml> requires L<libxml2-dev> bindings to build. When |
| 1134 | these bindings are available, the preferred way to install the script is |
| 1135 | to use L<cpanm|App::cpanminus>. |
| 1136 | |
| 1137 | $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git |
| 1138 | |
| 1139 | In case everything went well, the C<tei2korapxml> tool will |
| 1140 | be available on your command line immediately. |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1141 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1142 | Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16. |
| 1143 | |
| 1144 | =head1 OPTIONS |
| 1145 | |
| 1146 | =over 2 |
| 1147 | |
| 1148 | =item B<--base|-b> |
| 1149 | |
| 1150 | The base directory for output. Defaults to C<.>. |
| 1151 | |
| 1152 | =item B<--help|-h> |
| 1153 | |
| 1154 | Print help information. |
| 1155 | |
| 1156 | =item B<--version|-v> |
| 1157 | |
| 1158 | Print version information. |
| 1159 | |
| 1160 | =back |
| 1161 | |
| 1162 | =head1 COPYRIGHT AND LICENSE |
| 1163 | |
| 1164 | Copyright (C) 2020, L<IDS Mannheim|https://www.ids-mannheim.de/> |
| 1165 | |
| 1166 | Author: Peter Harders |
| 1167 | |
| 1168 | Contributors: Marc Kupietz, Carsten Schnober, Nils Diewald |
| 1169 | |
| 1170 | L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/> |
| 1171 | Corpus Analysis Platform at the |
| 1172 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 1173 | member of the |
| 1174 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>. |
| 1175 | |
| 1176 | This program is free software published under the |
| 1177 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-TEI/master/LICENSE>. |
| 1178 | |
| 1179 | =cut |