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); |
| 9 | use IO::Handle; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 10 | use IO::Select; |
| 11 | |
| 12 | use open qw(:std :utf8); # assume utf-8 encoding |
| 13 | use Encode qw(encode_utf8 decode_utf8); |
| 14 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 15 | use XML::CompactTree::XS; |
| 16 | use XML::LibXML::Reader; |
| 17 | use IO::Compress::Zip qw(zip $ZipError :constants); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 18 | use IPC::Open2 qw(open2); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 19 | |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 20 | use FindBin; |
| 21 | BEGIN { |
| 22 | unshift @INC, "$FindBin::Bin/../lib"; |
| 23 | }; |
| 24 | |
| 25 | use KorAP::XML::TEI; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 26 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 27 | our $VERSION = '0.01'; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 28 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 29 | our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n"; |
| 30 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 31 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 32 | # Parse options from the command line |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 33 | GetOptions( |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 34 | "root|r=s" => \(my $_root_dir = '.'), # name of root directory inside zip file |
| 35 | "input|i=s" => \(my $input_fname = ''), # input file (yet only TEI I5 Format accepted) |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 36 | 'help|h' => sub { |
| 37 | pod2usage( |
| 38 | -verbose => 99, |
| 39 | -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS', |
| 40 | -msg => $VERSION_MSG, |
| 41 | -output => '-' |
| 42 | ) |
| 43 | }, |
| 44 | 'version|v' => sub { |
| 45 | pod2usage( |
| 46 | -verbose => 0, |
| 47 | -msg => $VERSION_MSG, |
| 48 | -output => '-' |
| 49 | ) |
| 50 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 51 | ); |
| 52 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 53 | # |
| 54 | # ~~~ parameter (mandatory) ~~~ |
| 55 | # |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 56 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 57 | # optional |
| 58 | my $_CORP_SIGLE = "korpusSigle"; # opening and closing tags (without attributes) have to be in one line |
| 59 | # (e.g.: <korpusSigle>GOE</korpusSigle>) |
| 60 | # optional |
| 61 | my $_DOC_SIGLE = "dokumentSigle"; # analog |
| 62 | # mandatory |
| 63 | my $_TEXT_SIGLE = "textSigle"; # analog |
| 64 | # mandatory |
| 65 | my $_TEXT_BODY = "text"; # tag (without attributes), which contains the primary text |
| 66 | # optional |
| 67 | my $_CORP_HEADER_BEG = "idsHeader type=\"corpus\""; # just keep the correct order of the attributes and evtl. add an '.*' between them |
| 68 | # optional |
| 69 | my $_DOC_HEADER_BEG = "idsHeader type=\"document\""; # analog |
| 70 | # mandatory |
| 71 | my $_TEXT_HEADER_BEG = "idsHeader type=\"text\""; # analog |
| 72 | |
| 73 | # |
| 74 | # ~~~ constants ~~~ |
| 75 | # |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 76 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 77 | ## DEPRECATED (only IDS-intern - the tokenization is normally done by external tools) |
| 78 | my $_GEN_TOK_BAS = 0; # IDS internal tokenization |
| 79 | my( $chld_out, $chld_in, $pid, $select ); |
| 80 | ## |
| 81 | |
| 82 | ## dummy tokenization (only for testing) |
| 83 | my $_GEN_TOK_DUMMY = 0; # use dummy base tokenization for testing (base tokenization is normally done by external tools) |
| 84 | my $_tok_file_con = "tokens_conservative.xml"; |
| 85 | my $_tok_file_agg = "tokens_aggressive.xml"; |
| 86 | my ( @tok_tokens_con, @tok_tokens_agg, $m1, $m2, $m3, $m4, $tmp, $p1, $p2, $pr, $txt, $offset ); |
| 87 | my $_base_tokenization_dir = "base"; # name of directory for storing files of dummy tokenization (only used in func. select_tokenization) |
| 88 | |
| 89 | # man IO::Compress::Zip |
| 90 | # At present three compression methods are supported by IO::Compress::Zip, namely |
| 91 | # Store (no compression at all), Deflate, Bzip2 and LZMA. |
| 92 | # Note that to create Bzip2 content, the module "IO::Compress::Bzip2" must be installed. |
| 93 | # Note that to create LZMA content, the module "IO::Compress::Lzma" must be installed. |
| 94 | my $_COMPRESSION_METHOD = ZIP_CM_DEFLATE; # The symbols, ZIP_CM_STORE, ZIP_CM_DEFLATE, ZIP_CM_BZIP2 and ZIP_CM_LZMA are used to select the compression method. |
| 95 | |
| 96 | my $_DEBUG = 0; # set to 1 for minimal more debug output (no need to be parametrized) |
| 97 | my $_XCT_LN = 0; # only for debugging: include line numbers in elements of $tree_data |
| 98 | # (see also manpage of XML::CompactTree::XS) |
| 99 | |
| 100 | my $_header_file = "header.xml"; # name of files containing the text, document and corpus header |
| 101 | my $_data_file = "data.xml"; # name of file containing the primary text data (tokens) |
| 102 | my $_structure_dir = "struct"; # name of directory containing the $_structure_file |
| 103 | my $_structure_file = "structure.xml"; # name of file containing all tags (except ${_TOKEN_TAG}'s) related information |
| 104 | # (= their names and byte offsets in $_data) |
| 105 | ## TODO: optional (different annotation tools can produce more zip-files for feeding into KorAP-XML-Krill) |
| 106 | my $_TOKENS_PROC = 1; # on/off: processing of ${_TOKEN_TAG}'s (default: 1) |
| 107 | my $_tokens_dir = "tokens"; # name of directory containing the $_tokens_file |
| 108 | my $_tokens_file = "morpho.xml"; # name of file containing all ${_TOKEN_TAG}'s related information (=their byte offsets in $_data) |
| 109 | # - evtl. with additional inline annotations |
| 110 | my $_TOKENS_TAG = "w"; # name of tag containing all information stored in $_tokens_file |
| 111 | |
| 112 | ## TODO: optional |
| 113 | # handling inline annotations (inside $_TOKENS_TAG) |
| 114 | my $_INLINE_ANNOT = 0; # on/off: set to 1 if inline annotations are present and should be processed (default: 0) |
| 115 | my $_INLINE_LEM_RD = "lemma"; # from which attribute to read LEMMA information |
| 116 | my $_INLINE_ATT_RD = "ana"; # from which attribute to read POS information (and evtl. additional MSD - Morphosyntactic Descriptions) |
| 117 | # TODO: The format for the POS and MSD information has to suffice the regular expression ([^ ]+)( (.+))? |
| 118 | # - which means, that the POS information can be followed by an optional blank with additional |
| 119 | # MSD information; unlike the MSD part, the POS part may not contain any blanks. |
| 120 | my $_INLINE_POS_WR = "pos"; # name (inside $_tokens_file) referring to POS information |
| 121 | my $_INLINE_MSD_WR = "msd"; # name (inside $_tokens_file) referring to MSD information |
| 122 | my $_INLINE_LEM_WR = "lemma"; # name (inside $_tokens_file) referring to LEMMA information |
| 123 | ## |
| 124 | |
| 125 | |
| 126 | # |
| 127 | # ~~~ variables ~~~ |
| 128 | # |
| 129 | |
| 130 | my $zip; # IO::Compress::Zip object |
| 131 | my $zip_outh; # handle for zip file output (stdout) |
| 132 | my $first_write; # needed to decide wether to call '$zip->newStream' (for appending to zip file) |
| 133 | my $input_fh; # input file handle (default: stdin) |
| 134 | |
| 135 | my $buf_in; # text body data extracted from input document ($input_fh), further processed by XML::LibXML::Reader |
| 136 | my $data; # contains the primary text (created by func. 'retr_info' from $buf_in), which is written to '$data_file' |
| 137 | |
| 138 | my $dir; # text directory (below $_root_dir) |
| 139 | my $dir_crp; # corpus directory (below $_root_dir) |
| 140 | my $dir_doc; # document directory (below $_root_dir) |
| 141 | |
| 142 | my ( $text_id, $text_id_esc ); # '$text_id_esc' = escaped version of $text_id (see %ent) |
| 143 | |
| 144 | my %ent = ('"', '"', '&','&', # convert '&', '<' and '>' into their corresponding sgml-entities |
| 145 | '<','<','>','>'); |
| 146 | # note: the index still refers to the 'single character'-versions, which are counted as 1 |
| 147 | # (search for '&' in data.xml and see corresponding indices in $_tokens_file) |
| 148 | |
| 149 | my $header_txt; # raw text header (written to '$_root_dir$dir/$_header_file') |
| 150 | my $header_doc; # raw document header (written to '$_root_dir$dir_doc/$_header_file') |
| 151 | my $header_crp; # raw corpus header (written to '$_root_dir$dir_crp/$_header_file') |
| 152 | |
| 153 | my ( $header_fl_crp, $header_fl_doc, # flags for tracking where we are in the input document |
| 154 | $header_fl_txt, $data_fl ); |
| 155 | |
| 156 | my ( $header_prfx, $data_prfx1, # $header_prfx is written to $_header_file, $data_* are written to $_data_file |
| 157 | $data_prfx2, $data_sfx ); |
| 158 | |
| 159 | my @structures; # list of arrays, where each array represents a TEI I5 tag (except $_TOKENS_TAG) from the input document |
| 160 | # - the input of this array is written in func. 'write_structures' into the file '$_structure_file' |
| 161 | |
| 162 | my @tokens; # list of arrays, where each array represents a $_TOKENS_TAG from the input document |
| 163 | # - the input of this array is written in func. 'write_tokens' into the file '$_tokens_file' |
| 164 | |
| 165 | my ( $ref, $idx, $att_idx ); # needed in func. 'write_structures' and 'write_tokens' |
| 166 | |
| 167 | my ( $reader, # instance of 'XML::LibXML::Reader->new' (on input '$buf_in') |
| 168 | $tree_data ); # instance of 'XML::CompactTree::XS::readSubtreeToPerl' (on input '$reader') |
| 169 | |
| 170 | # these are only used inside recursive function 'retr_info' |
| 171 | my ( $_IDX, # value is set dependent on $_XCT_LN - for extracting array of child elements from element in $tree_data |
| 172 | $e, # element from $tree_data |
| 173 | $n, # tag name of actual processed element $e |
| 174 | $rl, # recursion level |
| 175 | $dl, # actual length of string $data |
| 176 | @oti, # oti='open tags indizes' - a stack of indizes into @structures, where the top index in @oti |
| 177 | # represents the actual processed element from @structures |
| 178 | @oti2, # analogously to @oti, but with reference to array @tokens |
| 179 | $inside_tokens_tag, # flag is set, when inside $_TOKENS_TAG |
| 180 | ## variables for handling ~ whitespace related issue ~ (it is sometimes necessary, to correct the from-values for some tags) |
| 181 | $add_one, # ... |
| 182 | $fval, $fval2, # ... |
| 183 | %ws); # hash for indices of whitespace nodes (needed to recorrect from-values) |
| 184 | # idea: when closing element, check if it's from-index minus 1 refers to a whitespace node |
| 185 | # (means: 'from-index - 1' is a key in %ws). |
| 186 | # if this is _not_ the case, then the from-value is one to high => correct it by substracting 1 |
| 187 | |
| 188 | my $output; # temporary variable needed in 'write_*'-functions for writing output to zip-stream $zip) |
| 189 | |
| 190 | my ( $i, $c ); # index variables used in loops |
| 191 | |
| 192 | ## DEPRECATED (only IDS-intern) |
| 193 | my $_tok_file_bas = "tokens.xml"; |
| 194 | ## |
| 195 | |
| 196 | my ( $_CORP_HEADER_END, $_DOC_HEADER_END, $_TEXT_HEADER_END ); |
| 197 | |
| 198 | |
| 199 | # |
| 200 | # ~~~ main ~~~ |
| 201 | # |
| 202 | |
| 203 | # ~ initializations ~ |
| 204 | |
| 205 | ($_XCT_LN)?($_IDX=5):($_IDX=4); |
| 206 | |
| 207 | $header_prfx = $data_prfx1 = $data_prfx2 = $data_sfx = ""; |
| 208 | |
| 209 | $header_fl_txt = $header_fl_doc = $header_fl_crp = 0; |
| 210 | |
| 211 | $inside_tokens_tag = -1; |
| 212 | |
| 213 | $fval = $fval2 = 0; |
| 214 | |
| 215 | $_root_dir .= '/'; # base dir must always end with a slash |
| 216 | $_root_dir =~ s/^\.?\///; # remove leading / (only relative paths allowed in IO::Compress::Zip) and redundant ./ |
| 217 | |
| 218 | $_CORP_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_CORP_HEADER_END = $1; |
| 219 | $_DOC_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_DOC_HEADER_END = $1; |
| 220 | $_TEXT_HEADER_BEG =~ s#^([^\s]+)(.*)$#$1\[\^>\]*$2#; $_TEXT_HEADER_END = $1; |
| 221 | |
| 222 | ## TODO: remove this, because it's IDS-specific |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 223 | $header_prfx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 224 | $header_prfx .= "<?xml-model href=\"header.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n"; |
| 225 | $header_prfx .= "<!DOCTYPE idsCorpus PUBLIC \"-//IDS//DTD IDS-XCES 1.0//EN\" \"http://corpora.ids-mannheim.de/idsxces1/DTD/ids.xcesdoc.dtd\">\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 226 | ## |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 227 | |
| 228 | $data_prfx1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 229 | $data_prfx1 .= "<?xml-model href=\"text.rng\" type=\"application/xml\" schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n"; |
| 230 | $data_prfx1 .= "<raw_text docid=\""; |
| 231 | $data_prfx2 .= "\" xmlns=\"http://ids-mannheim.de/ns/KorAP\">\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 232 | ## TODO: can 'metadata.xml' change or is it constant? |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 233 | $data_prfx2 .= " <metadata file=\"metadata.xml\" />\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 234 | ## |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 235 | $data_prfx2 .= " <text>"; |
| 236 | $data_sfx = "</text>\n</raw_text>"; |
| 237 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 238 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 239 | ## DEPRECATED (only IDS-intern) |
| 240 | startTokenizer() if $_GEN_TOK_BAS; |
| 241 | ## |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 242 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 243 | # ~ read input and write output (text by text) ~ |
Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 244 | process(); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 245 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 246 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 247 | # |
| 248 | # ~~~ subs ~~~ |
| 249 | # |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 250 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 251 | |
Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 252 | sub process { |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 253 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 254 | my ( $pfx, $sfx ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 255 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 256 | my $lc = 0; # line counter |
| 257 | |
| 258 | my $tc = 0; # text counter |
| 259 | |
| 260 | $input_fh = *STDIN; # input file handle (default: stdin) |
| 261 | |
| 262 | $zip_outh = *STDOUT; # output file handle (default: stdout) |
| 263 | |
| 264 | $data_fl = 0; $first_write = 1; |
| 265 | |
| 266 | $buf_in = $data = $dir = $dir_doc = $dir_crp = ""; |
| 267 | $header_txt = $header_doc = $header_crp = ""; |
| 268 | |
| 269 | |
| 270 | if ( $input_fname ne '' ){ |
| 271 | |
| 272 | open ( $input_fh, "<", "$input_fname") || die "File \'$input_fname\' could not be opened.\n"; |
| 273 | |
| 274 | } |
| 275 | |
| 276 | |
Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 277 | # prevents segfaulting of 'XML::LibXML::Reader' inside 'process()' - see notes on 'PerlIO layers' in 'man XML::LibXML') |
| 278 | # removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular |
| 279 | binmode $input_fh; |
| 280 | |
| 281 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 282 | # ~ loop (reading input document) ~ |
| 283 | |
| 284 | while ( <$input_fh> ){ |
| 285 | |
| 286 | $lc++; # line counter |
| 287 | |
| 288 | # TODO: yet not tested fo big amounts of data |
| 289 | # must-have, otherwise comments in input could be fatal (e.g.: ...<!--\n<idsHeader...\n-->...) |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 290 | KorAP::XML::TEI::delHTMLcom ( $input_fh, $_ ); # remove HTML comments (<!--...-->) |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 291 | |
| 292 | if ( $data_fl && m#^(.*)</${_TEXT_BODY}>(.*)$# ){ |
| 293 | |
| 294 | |
| 295 | # ~ end of text body ~ |
| 296 | |
| 297 | |
| 298 | # write data.xml, structure.xml and evtl. morpho.xml and/or the dummy tokenization files (s.a.: $_tok_file_con and $_tok_file_agg) |
| 299 | |
| 300 | $pfx = $1; $sfx = $2; |
| 301 | |
| 302 | die "ERROR ($0): main(): input line number $lc: line with closing text-body tag '${_TEXT_BODY}'" |
| 303 | ." contains additional information ... => Aborting\n\tline=$_" |
| 304 | if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/; |
| 305 | |
| 306 | if ( $dir ne "" ){ |
| 307 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 308 | $reader = XML::LibXML::Reader->new( string => "<text>$buf_in</text>", huge => 1 ); |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 309 | |
| 310 | if ( $_XCT_LN ){ # _XCT_LINE_NUMBERS is only for debugging |
| 311 | $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY | XCT_LINE_NUMBERS ); |
| 312 | } else { |
| 313 | $tree_data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_ATTRIBUTE_ARRAY ); |
| 314 | } |
| 315 | |
| 316 | @structures = (); @oti = (); |
| 317 | |
| 318 | if ( $_TOKENS_PROC ){ |
| 319 | @tokens = (); @oti2 = () |
| 320 | } |
| 321 | |
| 322 | $dl = $rl = 0; |
| 323 | |
| 324 | # ~ whitespace related issue ~ |
| 325 | $add_one = 0; |
| 326 | %ws = (); |
| 327 | |
| 328 | |
| 329 | # ~ recursion ~ |
| 330 | |
| 331 | retr_info( \$tree_data->[2] ); # parse input data |
| 332 | |
| 333 | $rl--; |
| 334 | |
| 335 | |
| 336 | # ~ write data.xml ~ |
| 337 | |
| 338 | $data =~ tr/\n\r/ /; # note: 2 blanks - otherwise offset data would become corrupt |
| 339 | |
| 340 | $data = encode_utf8( $data ); |
| 341 | |
| 342 | ## DEPRECATED (only IDS-intern) |
| 343 | # first write it to tokenization pipe to give it some time |
| 344 | if ( $_GEN_TOK_BAS ){ |
| 345 | print $chld_in "$data\n\x03\n"; |
| 346 | } |
| 347 | ## |
| 348 | |
| 349 | print STDERR "DEBUG ($0): main(): Writing (utf8-formatted) xml file $_root_dir$dir/$_data_file\n" if $_DEBUG; |
| 350 | |
| 351 | if ( $first_write ){ |
| 352 | |
| 353 | $first_write = 0; |
| 354 | |
| 355 | # 1st time: create instance |
| 356 | $zip = new IO::Compress::Zip $zip_outh, Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 0, Name => "$_root_dir$dir/$_data_file" |
| 357 | or die "ERROR ('$_root_dir$dir/$_data_file'): zip failed: $ZipError\n" |
| 358 | |
| 359 | } else { |
| 360 | |
| 361 | # closes the current compressed data stream and starts a new one. |
| 362 | $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir/$_data_file" ) |
| 363 | or die "ERROR ('$_root_dir$dir/$_data_file'): zip failed: $ZipError\n" |
| 364 | } |
| 365 | |
| 366 | $data =~ s/(&|<|>)/$ent{$1}/g; |
| 367 | |
| 368 | $zip->print( "$data_prfx1$text_id_esc$data_prfx2$data$data_sfx" ); |
| 369 | |
| 370 | |
| 371 | # ~ write structures ~ |
| 372 | |
| 373 | write_structures() if @structures; |
| 374 | |
| 375 | |
| 376 | # ~ write tokens ~ |
| 377 | |
| 378 | write_tokens() if $_TOKENS_PROC && @tokens; |
| 379 | |
| 380 | |
| 381 | # ~ dummy tokenization ~ |
| 382 | |
| 383 | if ( $_GEN_TOK_BAS || $_GEN_TOK_DUMMY ){ ## DEPRECATED ($_GEN_TOK_BAS: only IDS-intern) |
| 384 | |
| 385 | select_tokenization(); |
| 386 | |
| 387 | if ( $_GEN_TOK_DUMMY ){ |
| 388 | $offset = 0; @tok_tokens_con=(); @tok_tokens_agg=(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | $data_fl = 0; $buf_in = $data = $dir = ""; # reinit. |
| 393 | |
| 394 | } else { # $dir eq "" |
| 395 | |
| 396 | print STDERR "WARNING ($0): main(): maybe empty textSigle => skipping this text ...\n"; |
| 397 | print STDERR "WARNING ($0): main(): text header=$header_txt\n"; |
| 398 | print STDERR "WARNING ($0): main(): data=$data\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 401 | } elsif ( $data_fl ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 402 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 403 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 404 | # ~ inside text body ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 405 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 406 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 407 | #print STDERR "inside text body (\$data_fl set)\n"; |
| 408 | |
| 409 | # ~ whitespace handling ~ |
| 410 | |
| 411 | # remove consecutive whitespace at beginning and end (mostly one newline) |
| 412 | # to let 'XML::CompactTree::XS' recognize these blanks as 'text-nodes', the option 'XCT_IGNORE_WS' may not be used (see above). |
| 413 | s/^\s+//; s/\s+$//; |
| 414 | |
| 415 | # There's nothing wrong with inserting an additional blank at the start of the 2nd and all consecutive lines (which contain at least one tag), |
| 416 | # because it helps for better readability of the text in the '$_data_file' (e.g.: assure blanks between sentences). |
| 417 | # Furthermore, the input lines should avoid primary text tokens, which span across several lines, unless the line breaks doesn't lead |
| 418 | # to a situation which produces unwanted blanks - e.g.: '...<w>end</w>\n<w>.</w>...' would lead to '...<w>end</w> <w>.</w>...', or |
| 419 | # '...<w>,</w>\n<w>this</w>\n<w>is</w>\n<w>it</w>\n<w>!</w>...' to '<w>,<w> <w>this</w> <w>is</w> <w>it</w> <w>!</w>'. Even when considering |
| 420 | # to correct those unwanted effects, there would be lots of examples aside punctuation, where there would not exist an easy way or unarbitrary |
| 421 | # solution regarding the elimination of the false blanks. |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 422 | # |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 423 | # So, the best way to avoid having false blanks in the output, is to assure that linebreaks between word-tags doesn't occur in the input |
| 424 | # (see also comments on 'input restrictions' at the top of this script). |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 425 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 426 | 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] | 427 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 428 | $tc++; # text counter |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 429 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 430 | s/^(.)/ $1/ if $tc > 1; # add blank before 1st character for 2nd line and consecutive lines (which contain at least one tag) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 433 | # add line to buffer |
| 434 | $buf_in .= $_; |
| 435 | |
| 436 | } elsif ( $header_fl_txt && m#^(.*</${_TEXT_HEADER_END}>)(.*)$# ){ |
| 437 | |
| 438 | |
| 439 | # ~ end of text header ~ |
| 440 | |
| 441 | |
| 442 | #print STDERR "end of text header\n"; |
| 443 | |
| 444 | # write it to header.xml |
| 445 | |
| 446 | $sfx = $2; |
| 447 | |
| 448 | $header_txt .= $1; $header_fl_txt = 0; |
| 449 | |
| 450 | |
| 451 | die "ERROR ($0): main(): input line number $lc: line with closing text-header tag '${_TEXT_HEADER_END}'" |
| 452 | ." contains additional information ... => Aborting\n\tline=$_" |
| 453 | if $sfx !~ /^\s*$/; |
| 454 | |
| 455 | if ( $dir eq "" ){ |
| 456 | |
| 457 | print STDERR "WARNING ($0): main(): input line number $lc: empty textSigle in text header => nothing to do ...\ntext header=$header_txt\n"; |
| 458 | |
| 459 | } else { |
| 460 | |
| 461 | print STDERR "DEBUG ($0): Writing file $_root_dir$dir/$_header_file\n" if $_DEBUG; |
| 462 | |
| 463 | if ( $first_write ){ |
| 464 | |
| 465 | $first_write = 0; |
| 466 | |
| 467 | $zip = new IO::Compress::Zip $zip_outh, Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, |
| 468 | Append => 0, Name => "$_root_dir$dir/$_header_file" |
| 469 | or die "ERROR ('$_root_dir$dir/$_header_file'): zip failed: $ZipError\n" |
| 470 | |
| 471 | } else { |
| 472 | |
| 473 | $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir/$_header_file" ) |
| 474 | or die "ERROR ('$_root_dir$dir/$_header_file'): zip failed: $ZipError\n" |
| 475 | } |
| 476 | |
| 477 | $header_txt = encode_utf8( $header_txt ); |
| 478 | |
| 479 | $zip->print( "$header_prfx$header_txt" ); |
| 480 | |
| 481 | $header_txt = ""; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 482 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 483 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 484 | } elsif ( $header_fl_txt ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 485 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 486 | # ~ inside text header ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 487 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 488 | |
| 489 | #print STDERR "inside text header\n"; |
| 490 | |
| 491 | if( m#^(.*)<${_TEXT_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){ |
| 492 | |
| 493 | $pfx = $1; $sfx = $3; |
| 494 | |
| 495 | $dir = $2; $text_id = $dir; |
| 496 | |
| 497 | $text_id =~ tr/\//_/; $dir =~ s/("|&|<|>)/$ent{$1}/g; |
| 498 | |
| 499 | $text_id = encode_utf8( $text_id ); |
| 500 | |
| 501 | die "ERROR ($0): main(): input line number $lc: line with text-sigle tag '$_TEXT_SIGLE' is not in expected format ... => Aborting\n\tline=$_" |
| 502 | if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_TEXT_SIGLE}>\s*$# || $dir =~ /^\s*$/; |
| 503 | |
| 504 | # log output for seeing progression |
| 505 | print STDERR "$0: main(): text_id=".decode_utf8( $text_id )."\n"; |
| 506 | |
| 507 | $text_id_esc = $text_id; |
| 508 | |
| 509 | s#(<${_TEXT_SIGLE}(?: [^>]*)?>)[^<]+(</${_TEXT_SIGLE}>)#$1$dir$2# # to be consistent with escaping, escape also textSigle in text-header |
| 510 | if $text_id_esc =~ s/("|&|<|>)/$ent{$1}/g; |
| 511 | |
| 512 | $dir =~ tr/\./\//; |
| 513 | } |
| 514 | |
| 515 | $header_txt .= $_; |
| 516 | |
| 517 | } elsif ( $header_fl_doc && m#^(.*</${_DOC_HEADER_END}>)(.*)$# ){ |
| 518 | |
| 519 | |
| 520 | # ~ end of document header ~ |
| 521 | |
| 522 | |
| 523 | #print STDERR "end of doc header\n"; |
| 524 | |
| 525 | # write it to header.xml |
| 526 | |
| 527 | $sfx = $2; |
| 528 | |
| 529 | $header_doc .= $1; $header_fl_doc = 0; |
| 530 | |
| 531 | die "ERROR ($0): main(): input line number $lc: line with closing document-header tag '${_DOC_HEADER_END}'" |
| 532 | ." contains additional information ... => Aborting\n\tline=$_" |
| 533 | if $sfx !~ /^\s*$/; |
| 534 | |
| 535 | if( $dir_doc eq "" ){ |
| 536 | |
| 537 | print STDERR "WARNING ($0): main(): input line number $lc: empty document sigle in document header" |
| 538 | ." => nothing to do ...\ndocument header=$header_doc\n"; |
| 539 | |
| 540 | } else { |
| 541 | |
| 542 | print STDERR "DEBUG ($0): Writing file $_root_dir$dir_doc/$_header_file\n" if $_DEBUG; |
| 543 | |
| 544 | if ( $first_write ){ |
| 545 | |
| 546 | $first_write = 0; |
| 547 | |
| 548 | $zip = new IO::Compress::Zip $zip_outh, Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 0, |
| 549 | Name => "$_root_dir$dir_doc/$_header_file" |
| 550 | or die "ERROR ('$_root_dir$dir_doc/$_header_file'): zip failed: $ZipError\n" |
| 551 | |
| 552 | } else { |
| 553 | |
| 554 | $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir_doc/$_header_file" ) |
| 555 | or die "ERROR ('$_root_dir$dir_doc/$_header_file'): zip failed: $ZipError\n" |
| 556 | } |
| 557 | |
| 558 | $header_doc = encode_utf8( $header_doc ); |
| 559 | |
| 560 | $zip->print( "$header_prfx$header_doc" ); |
| 561 | |
| 562 | $header_doc = $dir_doc = ""; |
| 563 | } |
| 564 | |
| 565 | } elsif ( $header_fl_doc ){ |
| 566 | |
| 567 | |
| 568 | # ~ inside document header ~ |
| 569 | |
| 570 | |
| 571 | #print STDERR "inside doc header\n"; |
| 572 | |
| 573 | if ( m#^(.*)<${_DOC_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){ |
| 574 | |
| 575 | $pfx = $1; $sfx = $3; |
| 576 | |
| 577 | $dir_doc = $2; |
| 578 | |
| 579 | die "ERROR ($0): main(): input line number $lc: line with document-sigle tag '$_DOC_SIGLE' is not in expected format ... => Aborting\n\tline=$_" |
| 580 | if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_DOC_SIGLE}>\s*$# || $dir_doc =~ /^\s*$/; |
| 581 | |
| 582 | s#(<${_DOC_SIGLE}(?: [^>]*)?>)[^<]+(</${_DOC_SIGLE}>)#$1$dir_doc$2# # to be consistent with escaping, escape also textSigle in Document-Header |
| 583 | if $dir_doc =~ s/("|&|<|>)/$ent{$1}/g; |
| 584 | } |
| 585 | |
| 586 | $header_doc .= $_; |
| 587 | |
| 588 | } elsif ( m#^(.*)(<${_TEXT_HEADER_BEG}.*)$# ){ |
| 589 | |
| 590 | # ~ start of text header ~ |
| 591 | |
| 592 | |
| 593 | #print STDERR "begin of text header\n"; |
| 594 | |
| 595 | $header_txt = $_; $header_fl_txt = 1; $pfx = $1; |
| 596 | |
| 597 | $tc = 0; # reset (needed for ~ whitespace handling ~) |
| 598 | |
| 599 | die "ERROR ($0): main(): input line number $lc: line with opening text-header tag '${_TEXT_HEADER_BEG}'" |
| 600 | ." is not in expected format ... => Aborting\n\tline=$_" |
| 601 | if $pfx !~ /^\s*$/; |
| 602 | |
| 603 | } elsif ( m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$# ){ |
| 604 | |
| 605 | |
| 606 | # ~ start of text body ~ |
| 607 | |
| 608 | |
| 609 | #print STDERR "inside text body\n"; |
| 610 | |
| 611 | $pfx = $1; $sfx = $2; |
| 612 | |
| 613 | $data_fl = 1; |
| 614 | |
| 615 | die "ERROR ($0): main(): input line number $lc: line with opening text-body tag '${_TEXT_BODY}'" |
| 616 | ." contains additional information ... => Aborting\n\tline=$_" |
| 617 | if $pfx !~ /^\s*$/ || $sfx !~ /^\s*$/; |
| 618 | |
| 619 | } elsif ( m#^(.*)(<${_DOC_HEADER_BEG}.*)$# ){ |
| 620 | |
| 621 | |
| 622 | # ~ start of document header ~ |
| 623 | |
| 624 | |
| 625 | #print STDERR "begin of doc header\n"; |
| 626 | |
| 627 | $header_doc = "$2\n"; $header_fl_doc = 1; $pfx = $1; |
| 628 | |
| 629 | die "ERROR ($0): main(): input line number $lc: line with opening document-header tag '${_DOC_HEADER_BEG}'" |
| 630 | ."is not in expected format ... => Aborting\n\tline=$_" |
| 631 | if $pfx !~ /^\s*$/; |
| 632 | |
| 633 | } elsif ( $header_fl_crp && m#^(.*</${_CORP_HEADER_END}>)(.*)$# ){ |
| 634 | |
| 635 | |
| 636 | # ~ end of corpus header ~ |
| 637 | |
| 638 | |
| 639 | #print STDERR "end of corp header\n"; |
| 640 | |
| 641 | $sfx = $2; |
| 642 | |
| 643 | $header_crp .= $1; $header_fl_crp = 0; |
| 644 | |
| 645 | die "ERROR ($0): main(): input line number $lc: line with closing corpus-header tag '${_CORP_HEADER_END}'" |
| 646 | ." contains additional information ... => Aborting\n\tline=$_" |
| 647 | if $sfx !~ /^\s*$/; |
| 648 | |
| 649 | if ( $dir_crp eq "" ){ |
| 650 | |
| 651 | print STDERR "WARNING ($0): main(): input line number $lc: empty corpus sigle in corpus header => nothing to do ...\ncorpus header=$header_crp\n"; |
| 652 | |
| 653 | } else { |
| 654 | |
| 655 | print STDERR "DEBUG ($0): Writing file $_root_dir$dir_crp/$_header_file\n" if $_DEBUG; |
| 656 | |
| 657 | if ( $first_write ){ |
| 658 | |
| 659 | $first_write = 0; |
| 660 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 661 | $zip = new IO::Compress::Zip $zip_outh, Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, |
| 662 | Append => 0, Name => "$_root_dir$dir_crp/$_header_file" |
| 663 | or die "ERROR ('$_root_dir$dir_crp/$_header_file'): zip failed: $ZipError\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 664 | |
| 665 | } else { |
| 666 | |
| 667 | $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir_crp/$_header_file" ) |
| 668 | or die "ERROR ('$_root_dir$dir_crp/$_header_file'): zip failed: $ZipError\n" |
| 669 | } |
| 670 | |
| 671 | $header_crp = encode_utf8( $header_crp ); |
| 672 | |
| 673 | $zip->print( "$header_prfx$header_crp" ); |
| 674 | |
| 675 | $header_crp = $dir_crp = ""; |
| 676 | } |
| 677 | |
| 678 | } elsif ( $header_fl_crp ){ |
| 679 | |
| 680 | |
| 681 | # ~ inside corpus header ~ |
| 682 | |
| 683 | |
| 684 | #print STDERR "inside corp header\n"; |
| 685 | |
| 686 | if ( m#^(.*)<${_CORP_SIGLE}(?: [^>]*)?>([^<]*)(.*)$# ){ |
| 687 | |
| 688 | $pfx = $1; $sfx = $3; |
| 689 | |
| 690 | $dir_crp = $2; |
| 691 | |
| 692 | die "ERROR ($0): main(): input line number $lc: line with korpusSigle-tag is not in expected format ... => Aborting\n\tline=$_" |
| 693 | if $pfx !~ /^\s*$/ || $sfx !~ m#^</${_CORP_SIGLE}>\s*$# || $dir_crp =~ /^\s*$/; |
| 694 | |
| 695 | if ( $dir_crp =~ s/("|&|<|>)/$ent{$1}/g ){ |
| 696 | |
| 697 | s#(<${_CORP_SIGLE}(?: [^>]*)?>)[^<]+(</${_CORP_SIGLE}>)#$1$dir_crp$2# # to be consistent with escaping, escape also textSigle in Corpus-Header |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 701 | $header_crp .= $_; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 702 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 703 | } elsif ( m#^(.*)(<${_CORP_HEADER_BEG}.*)$# ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 704 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 705 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 706 | # ~ start of corpus header ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 707 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 708 | |
| 709 | #print STDERR "begin of corp header\n"; |
| 710 | |
| 711 | $header_crp = $2; $header_fl_crp = 1; $pfx = $1; |
| 712 | |
| 713 | die "ERROR ($0): main(): input line number $lc: line with opening corpus-header tag '${_CORP_HEADER_BEG}'" |
| 714 | ." is not in expected format ... => Aborting\n\tline=$_" |
| 715 | if $pfx !~ /^\s*$/; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 716 | } |
| 717 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 718 | } #end: while |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 719 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 720 | $zip->close(); |
| 721 | |
| 722 | ## DEPRECATED (only IDS-intern) |
| 723 | if( $_GEN_TOK_BAS ){ |
| 724 | close($chld_in); |
| 725 | close($chld_out); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 726 | } |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 727 | ## |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 728 | |
Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 729 | } # end: sub process |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 730 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 731 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 732 | sub retr_info { # called from process() |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 733 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 734 | # EXAMPLE: <node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node> |
| 735 | # |
| 736 | # print out values of above example: |
| 737 | # echo '<node a="v"><node1>some <n/> text</node1><node2>more-text</node2></node>' | perl -e 'use XML::CompactTree::XS; use XML::LibXML::Reader; $reader = XML::LibXML::Reader->new(IO => STDIN); $data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_LINE_NUMBERS ); print $data->[2]->[0]->[5]->[1]->[1]' |
| 738 | # |
| 739 | # $data = reference to below array |
| 740 | # |
| 741 | # [ 0: XML_READER_TYPE_DOCUMENT, |
| 742 | # 1: ? |
| 743 | # 2: [ 0: [ 0: XML_READER_TYPE_ELEMENT <- start recursion with array '$data->[2]' (see process(): retr_info( \$tree_data->[2] )) |
| 744 | # 1: 'node' |
| 745 | # 2: ? |
| 746 | # 3: HASH (attributes) |
| 747 | # 4: 1 (line number) |
| 748 | # 5: [ 0: [ 0: XML_READER_TYPE_ELEMENT |
| 749 | # 1: 'node1' |
| 750 | # 2: ? |
| 751 | # 3: undefined (no attributes) |
| 752 | # 4: 1 (line number) |
| 753 | # 5: [ 0: [ 0: XML_READER_TYPE_TEXT |
| 754 | # 1: 'some ' |
| 755 | # ] |
| 756 | # 1: [ 0: XML_READER_TYPE_ELEMENT |
| 757 | # 1: 'n' |
| 758 | # 2: ? |
| 759 | # 3: undefined (no attributes) |
| 760 | # 4: 1 (line number) |
| 761 | # 5: undefined (no child nodes) |
| 762 | # ] |
| 763 | # 2: [ 0: XML_READER_TYPE_TEXT |
| 764 | # 1: ' text' |
| 765 | # ] |
| 766 | # ] |
| 767 | # ] |
| 768 | # 1: [ 0: XML_READER_TYPE_ELEMENT |
| 769 | # 1: 'node2' |
| 770 | # 2: ? |
| 771 | # 3: undefined (not attributes) |
| 772 | # 4: 1 (line number) |
| 773 | # 5: [ 0: [ 0: XML_READER_TYPE_TEXT |
| 774 | # 1: 'more-text' |
| 775 | # ] |
| 776 | # ] |
| 777 | # ] |
| 778 | # ] |
| 779 | # ] |
| 780 | # ] |
| 781 | # ] |
| 782 | # |
| 783 | # $data->[0] = 9 (=> type == XML_READER_TYPE_DOCUMENT) |
| 784 | # |
| 785 | # ref($data->[2]) == ARRAY (with 1 element for 'node') |
| 786 | # ref($data->[2]->[0]) == ARRAY (with 6 elements) |
| 787 | # |
| 788 | # $data->[2]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 789 | # $data->[2]->[0]->[1] == 'node' |
| 790 | # ref($data->[2]->[0]->[3]) == HASH (=> ${$data->[2]->[0]->[3]}{a} == 'v') |
| 791 | # $data->[2]->[0]->[4] == 1 (line number) |
| 792 | # ref($data->[2]->[0]->[5]) == ARRAY (with 2 elements for 'node1' and 'node2') |
| 793 | # # child nodes of actual node (see $_IDX) |
| 794 | # |
| 795 | # ref($data->[2]->[0]->[5]->[0]) == ARRAY (with 6 elements) |
| 796 | # $data->[2]->[0]->[5]->[0]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 797 | # $data->[2]->[0]->[5]->[0]->[1] == 'node1' |
| 798 | # $data->[2]->[0]->[5]->[0]->[3] == undefined (=> no attribute) |
| 799 | # $data->[2]->[0]->[5]->[0]->[4] == 1 (line number) |
| 800 | # ref($data->[2]->[0]->[5]->[0]->[5]) == ARRAY (with 3 elements for 'some ', '<n/>' and ' text') |
| 801 | # |
| 802 | # ref($data->[2]->[0]->[5]->[0]->[5]->[0]) == ARRAY (with 2 elements) |
| 803 | # $data->[2]->[0]->[5]->[0]->[5]->[0]->[0] == 3 (=> type == XML_READER_TYPE_TEXT) |
| 804 | # $data->[2]->[0]->[5]->[0]->[5]->[0]->[1] == 'some ' |
| 805 | # |
| 806 | # ref($data->[2]->[0]->[5]->[0]->[5]->[1]) == ARRAY (with 5 elements) |
| 807 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[0] == 1 (=> type == XML_READER_TYPE_ELEMENT) |
| 808 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[1] == 'n' |
| 809 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[3] == undefined (=> no attribute) |
| 810 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[4] == 1 (line number) |
| 811 | # $data->[2]->[0]->[5]->[0]->[5]->[1]->[5] == undefined (=> no child nodes) |
| 812 | # |
| 813 | # ref($data->[2]->[0]->[5]->[0]->[5]->[2]) == ARRAY (with 2 elements) |
| 814 | # $data->[2]->[0]->[5]->[0]->[5]->[2]->[0] == 3 (=> type == XML_READER_TYPE_TEXT) |
| 815 | # $data->[2]->[0]->[5]->[0]->[5]->[2]->[1] == ' text' |
| 816 | # |
| 817 | # |
| 818 | # retr_info() starts with the array reference ${$_[0]} (= \$tree_data->[2]), which corresponds to ${\$data->[2]} in the above example. |
| 819 | # Hence, the expression @{${$_[0]}} corresponds to @{${\$data->[2]}}, $e to ${${\$data->[2]}}[0] (= $data->[2]->[0]) and $e->[0] to |
| 820 | # ${${\$data->[2]}}[0]->[0] (= $data->[2]->[0]->[0]). |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 821 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 822 | $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] | 823 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 824 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 825 | 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] | 826 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 827 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 828 | 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] | 829 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 830 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 831 | #~~~~ |
| 832 | # from here: opening tag |
| 833 | #~~~~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 834 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 835 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 836 | # insert new array (for new tag) into @structures with tag-name and tag-attributes (if present) |
| 837 | # 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] | 838 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 839 | # ~ tag name ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 840 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 841 | $n = $e->[1]; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 842 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 843 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 844 | # ~ handle structures ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 845 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 846 | my @array; |
| 847 | push @array, $n; |
| 848 | push @structures, \@array; |
| 849 | push @oti, $#structures; # add highest index of @structures to @oti |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 850 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 851 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 852 | # ~ handle tokens ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 853 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 854 | $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] | 855 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 856 | if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 857 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 858 | my @array2; |
| 859 | push @array2, $n; |
| 860 | push @tokens, \@array2; |
| 861 | push @oti2, $#tokens; |
| 862 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 863 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 864 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 865 | # ~ handle attributes ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 866 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 867 | if ( defined $e->[3] ){ # only if attributes exist |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 868 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 869 | for ( $c = 0; $c < @{$e->[3]}; $c += 2 ){ # with 'XCT_ATTRIBUTE_ARRAY', $node->[3] is an array reference of the form |
| 870 | # [ name1, value1, name2, value2, ....] of attribute names and corresponding values. |
| 871 | # 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] | 872 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 873 | # '$c' references the 'key' and '$c+1' the 'value' |
| 874 | push @{$structures[$#structures]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1]; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 875 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 876 | if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 877 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 878 | push @{$tokens[$#tokens]}, ${$e->[3]}[$c], ${$e->[3]}[$c+1]; |
| 879 | } |
| 880 | |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | |
| 885 | # ~ index 'from' ~ |
| 886 | |
| 887 | # this is, where a normal tag or tokens-tag ($_TOKENS_TAG) starts |
| 888 | |
| 889 | push @{$structures[$#structures]}, ( $dl + $add_one ); # see below (text and whitespace nodes) for explanation on '$add_one' |
| 890 | |
| 891 | if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){ |
| 892 | |
| 893 | push @{$tokens[$#tokens]}, ( $dl + $add_one ); |
| 894 | } |
| 895 | |
| 896 | |
| 897 | #~~~~ |
| 898 | # until here: opening tag |
| 899 | #~~~~ |
| 900 | |
| 901 | |
| 902 | # ~~ RECURSION ~~ |
| 903 | |
| 904 | if ( defined $e->[$_IDX] ){ # do no recursion, if $e->[$_IDX] is not defined (because we have no array of child nodes, e.g.: <back/>) |
| 905 | |
| 906 | retr_info( \$e->[$_IDX] ); # recursion with array of child nodes |
| 907 | |
| 908 | $rl--; # return from recursion |
| 909 | } |
| 910 | |
| 911 | |
| 912 | #~~~~~ |
| 913 | # from here: closing tag |
| 914 | #~~~~~ |
| 915 | |
| 916 | |
| 917 | # ~ handle structures ~ |
| 918 | |
| 919 | { |
| 920 | my $ix = pop @oti; # index of just closed tag |
| 921 | |
| 922 | my $aix = $#{$structures[$ix]}; # determine highest index from 'array referring to last closed tag' ... |
| 923 | |
| 924 | $fval = ${$structures[$ix]}[ $aix ]; # ... and get it's from-value |
| 925 | |
| 926 | if ( $fval > 0 && not exists $ws{ $fval - 1 } ){ # ~ whitespace related issue ~ |
| 927 | |
| 928 | # previous node was a text-node |
| 929 | |
| 930 | ${$structures[$ix]}[ $aix ] = $fval - 1; # recorrect from-value (see below: notes on ~ whitespace related issue ~) |
| 931 | } |
| 932 | |
| 933 | # in case this fails, check input |
| 934 | die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@structures: from-value ($fval) is 2 or more greater" |
| 935 | ." than to-value ($dl) => please check. aborting ...\n" |
| 936 | if ( $fval - 1 ) > $dl; |
| 937 | |
| 938 | # TODO: construct example for which this case applies |
| 939 | # maybe this is not necessary anymore, because the above recorrection of the from-value suffices |
| 940 | # TODO: check, if it's better to remove this line and change above check to 'if ( $fval - 1) >= $dl; |
| 941 | ${$structures[$ix]}[ $aix ] = $dl if $fval == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl) |
| 942 | |
| 943 | push @{$structures[$ix]}, $dl, $rl; # to-value and recursion-level |
| 944 | |
| 945 | # note: use $dl, because the offsets are _between_ the characters (e.g.: word = 'Hello' => from = 0 (before 'H'), to = 5 (after 'o')) |
| 946 | } |
| 947 | |
| 948 | |
| 949 | # ~ handle tokens ~ |
| 950 | |
| 951 | |
| 952 | if ( $_TOKENS_PROC && $inside_tokens_tag == $rl ){ |
| 953 | |
| 954 | my $ix = pop @oti2; |
| 955 | |
| 956 | my $aix = $#{$tokens[$ix]}; |
| 957 | |
| 958 | $fval2 = ${$tokens[$ix]}[ $aix ]; # from-value |
| 959 | |
| 960 | if( $fval2 > 0 && not exists $ws{ $fval2 - 1 } ){ # ~ whitespace related issue ~ |
| 961 | |
| 962 | # previous node was a text-node |
| 963 | |
| 964 | ${$tokens[$ix]}[ $aix ] = $fval2 - 1; # recorrect from-value |
| 965 | } |
| 966 | |
| 967 | # in case this fails, check input |
| 968 | die "ERROR ($0, retr_info()): text_id='$text_id', processing of \@tokens: from-value ($fval2) is 2 or more greater" |
| 969 | ." than to-value ($dl) => please check. aborting ...\n" |
| 970 | if ( $fval2 - 1 ) > $dl; |
| 971 | |
| 972 | # TODO: construct example for which this case applies |
| 973 | # maybe this is not necessary anymore, because the above recorrection of the from-value suffices |
| 974 | # TODO: check, if it's better to remove this line and change above check to 'if ( $fval2 - 1) >= $dl; |
| 975 | ${$tokens[$ix]}[ $aix ] = $dl if $fval2 == $dl + 1; # correct from-value (same as ... if $fval-1 == $dl) |
| 976 | |
| 977 | push @{$tokens[$ix]}, $dl, $rl; # to-value and recursion-level |
| 978 | |
| 979 | $inside_tokens_tag = -1; # reset |
| 980 | } |
| 981 | |
| 982 | # ~ whitespace related issue ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 983 | # clean up |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 984 | delete $ws{ $fval - 1 } if $fval > 0 && exists $ws{ $fval - 1 }; |
| 985 | 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] | 986 | |
| 987 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 988 | #~~~~~ |
| 989 | # from here: text (and whitespace) nodes |
| 990 | #~~~~~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 991 | |
| 992 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 993 | # the 3rd form of nodes, next to text-nodes (XML_READER_TYPE_TEXT) and tag-nodes (XML_READER_TYPE_ELEMENT) are nodes |
| 994 | # of the type 'XML_READER_TYPE_SIGNIFICANT_WHITESPACE' |
| 995 | # |
| 996 | # when modifiying the above example (at the top of this sub) by inserting an additional blank between '</node1>' and '<node2>', |
| 997 | # the output for '$data->[2]->[0]->[5]->[1]->[1]' becomes a blank (' ') and it's type is '14' (see manpage of XML::LibXML::Reader): |
| 998 | # |
| 999 | # echo '<node a="v"><node1>some <n/> text</node1> <node2>more-text</node2></node>' | perl -e 'use XML::CompactTree::XS; use XML::LibXML::Reader; $reader = XML::LibXML::Reader->new(IO => STDIN); $data = XML::CompactTree::XS::readSubtreeToPerl( $reader, XCT_DOCUMENT_ROOT | XCT_IGNORE_COMMENTS | XCT_LINE_NUMBERS ); print "node=".$data->[2]->[0]->[5]->[1]->[1].", type=".$data->[2]->[0]->[5]->[1]->[0]."\n"' |
Peter 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 | } 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] | 1002 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1003 | # notes on ~ whitespace related issue ~ (see below source code) |
| 1004 | # |
| 1005 | # example: '... <head type="main"><s>Campagne in Frankreich</s></head><head type="sub"> <s>1792</s> ...' |
| 1006 | # |
| 1007 | # Two text-nodes should normally be separated by a blank. In the above example, that would be the 2 text-nodes |
| 1008 | # 'Campagne in Frankreich' and '1792', which are separated by the whitespace-node ' '. |
| 1009 | # |
| 1010 | # Assumed, that the above example marks the general case, then the text-node 'Campagne in Frankreich' leads to the |
| 1011 | # setting of '$add_one' to 1, so that when opening the 2nd 'head'-tag and setting it's from-index, it gets the right |
| 1012 | # offset, which is the start-index of '1792'. |
| 1013 | # |
| 1014 | # To check, that the above consideration holds, we save the from-index of a read whitespace-node into the hash %ws. |
| 1015 | # By this, it can be checked when closing a tag, if the 'non-tag'-node (text or whitespace) before the last 'non-tag'- |
| 1016 | # node was actually a whitespace-node ($ws{ $fval - 1 }). |
| 1017 | # |
| 1018 | # For whitespace-nodes, also $add_one has to be set to 0, so when opening the next tag (in the above example the 2nd |
| 1019 | # 's'-tag), no additional 1 is added, because this was already done by the whitespace-node itself (by incrementing the |
| 1020 | # variable $dl). |
| 1021 | # |
| 1022 | # Now, what happens, when 2 text-nodes are not seperated by a whitespace-node (blank)? (e.g.: <w>Augen<c>,</c></w>) |
| 1023 | # |
| 1024 | # In this case, the falsely increased from-value has to be decreased again by 1 when closing the referring tag |
| 1025 | # (...$fval - 1; # recorrect). |
| 1026 | # |
| 1027 | # Comparing the 2 examples '<w>fu</w> <w>bar</w>' and '<w>fu</w><w> </w><w>bar</w>' (even though, the 2nd one makes less |
| 1028 | # sense, because of '<w> </w>'), in both the ' ' is handled as a whitespace-node (XML_READER_TYPE_SIGNIFICANT_WHITESPACE). |
| 1029 | # |
| 1030 | # So the from-index of the 2nd w-tag (in the second example) would refer to 'bar', which may not have been the intention |
| 1031 | # (even, if '<w> </w>' doesn't make a lot of sense). TODO: could this be a bug, which needs to be fixed? |
| 1032 | # |
| 1033 | # Empty tags also cling to the next text-token - e.g. in '...<w>tok1</w> <w>tok2</w><a><b/></a><w>tok3</w>...' the from- |
| 1034 | # and to-indizes for the tags 'a' and 'b' are both 9, which is the start-index of the token 'tok3'. |
| 1035 | |
| 1036 | if( $e->[0] == XML_READER_TYPE_SIGNIFICANT_WHITESPACE ){ |
| 1037 | |
| 1038 | # ~ whitespace related issue ~ |
| 1039 | |
| 1040 | $add_one = 0; |
| 1041 | |
| 1042 | $ws{ $dl }++; # '++' does not mean a thing here (could be used for consistency checking) |
| 1043 | |
| 1044 | }else{ |
| 1045 | |
| 1046 | # ~ text-node ~ |
| 1047 | |
| 1048 | $add_one = 1; |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | # ~ update $data and $dl ~ |
| 1053 | |
| 1054 | $data .= $e->[1]; |
| 1055 | |
| 1056 | $dl += length( $e->[1] ); # update length of $data |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | #~~~~~ |
| 1061 | # from here (until end): dummy tokenization |
| 1062 | #~~~~~ |
| 1063 | |
| 1064 | if ( $_GEN_TOK_DUMMY ){ |
| 1065 | |
| 1066 | $txt = $e->[1]; |
| 1067 | |
| 1068 | if ( substr( $txt, 0, 1 ) ne ' ' || substr( $txt, 1, 1) ne ' ' ){ # $txt has at least 2 chars, if it's not empty or equal to ' ' |
| 1069 | |
| 1070 | |
| 1071 | # ~ start: conservative tokenization ~ |
| 1072 | |
| 1073 | |
| 1074 | # '\p{Punct}' is equal to the character class '[-!"#%&'()*,./:;?@[\\\]_{}]' |
| 1075 | while ( $txt =~ /([\p{Punct}]*)([^\p{Punct} \x{9}\n]+(?:([\p{Punct}]+)[^\p{Punct} \x{9}\n]+)*)?([\p{Punct}]*)(?:[ \x{9}\n])?/g ){ |
| 1076 | |
| 1077 | $m1 = $1; $m2 = $2; $m3 = $3; $m4 = $4; |
| 1078 | |
| 1079 | if ( "$m1" ne "" ){ # special chars before token |
| 1080 | |
| 1081 | $p1 = $-[1]; $p2 = $+[1]; |
| 1082 | |
| 1083 | #print STDERR "A1: ".$m1." -> from $p1 to $p2\n"; |
| 1084 | |
| 1085 | if ( $p2 == $p1+1 ){ |
| 1086 | |
| 1087 | if ( $p1 != 0 ){ $tmp = substr( $txt, $p1-1, 1 ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) } else { $pr = 0 }; |
| 1088 | |
| 1089 | if ( not $pr ){ $tmp = substr( $txt, $p2, 1 ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) }; |
| 1090 | |
| 1091 | if ( $pr ){ push @tok_tokens_con, $p1+$offset; push @tok_tokens_con, $p2+$offset }; # from and to |
| 1092 | |
| 1093 | } else { |
| 1094 | |
| 1095 | for ( $i = 0; $i < ( $p2-$p1 ); $i++ ){ |
| 1096 | |
| 1097 | #print STDERR "A2: ".substr($m1,$i,1)." -> from $p1 to $p2\n"; |
| 1098 | |
| 1099 | push @tok_tokens_con, $p1+$i+$offset; push @tok_tokens_con, $p1+$i+1+$offset; # from and to |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | } # fi: "$m1" ne "" |
| 1104 | |
| 1105 | #print STDERR "B: "."$m2 -> from ".($-[2]+$offset)." to ".($+[2]+$offset)."\n" if defined $m2; # token (wordform) |
| 1106 | |
| 1107 | if ( defined $m2 ){ push @tok_tokens_con, $-[2]+$offset; push @tok_tokens_con, $+[2]+$offset }; # from and to |
| 1108 | |
| 1109 | if ( defined $m3 ){ |
| 1110 | |
| 1111 | $p1 = $-[3]; $p2 = $+[3]; |
| 1112 | |
| 1113 | #print STDERR "C: ".$m3." -> from $p1 to $p2\n"; |
| 1114 | |
| 1115 | if ( $p2 == $p1+1 ){ |
| 1116 | |
| 1117 | $tmp = substr( $txt, $p2, 1); $pr = ( $tmp =~ /^$/ ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) if not $pr; # char after match |
| 1118 | |
| 1119 | if ( not $pr ){ $tmp = substr( $txt, $p1-1, 1 ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) }; # char before match |
| 1120 | |
| 1121 | if ( $pr ){ push @tok_tokens_con, $p1+$offset; push @tok_tokens_con, $p2+$offset }; # from and to |
| 1122 | |
| 1123 | } else { # length($m3)>1 => print all chars |
| 1124 | |
| 1125 | for ( $i = 0; $i < ( $p2-$p1 ); $i++ ){ |
| 1126 | |
| 1127 | #$tmp=substr($m3,$i,1); |
| 1128 | #print STDERR "C2: $tmp -> from $p1 to $p2\n"; |
| 1129 | |
| 1130 | push @tok_tokens_con, $p1+$i+$offset; push @tok_tokens_con, $p1+$i+1+$offset; # from and to |
| 1131 | } |
| 1132 | |
| 1133 | } |
| 1134 | |
| 1135 | } # fi: defined $m3 |
| 1136 | |
| 1137 | if ( "$m4" ne "" ){ # special chars after token |
| 1138 | |
| 1139 | $p1 = $-[4]; $p2 = $+[4]; |
| 1140 | |
| 1141 | #print STDERR "D1: ".$m4." -> from ".($p1+$offset)." to ".($p2+$offset)."\n"; |
| 1142 | |
| 1143 | if ( $p2 == $p1+1 ){ |
| 1144 | |
| 1145 | $tmp = substr( $txt, $p2, 1 ); $pr = ( $tmp =~ /^$/ ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) if not $pr; # char after match |
| 1146 | |
| 1147 | if ( not $pr ){ $tmp = substr ( $txt, $p1-1, 1 ); $pr = ( $tmp =~ /^[^A-Za-z0-9]/ ) }; # char before match |
| 1148 | |
| 1149 | if ( $pr ){ push @tok_tokens_con, $p1+$offset; push @tok_tokens_con, $p2+$offset } # from and to |
| 1150 | |
| 1151 | }else{ |
| 1152 | |
| 1153 | for ( $i = 0; $i < ( $p2-$p1 ); $i++ ){ |
| 1154 | |
| 1155 | #print STDERR "D2: ".substr($m4,$i,1)." -> from ".($p1+$i+$offset)." to ".($p1+$i+1+$offset)."\n"; |
| 1156 | |
| 1157 | push @tok_tokens_con, $p1+$i+$offset; push @tok_tokens_con, $p1+$i+1+$offset; # from and to |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | }# fi: "$m4" ne "" |
| 1162 | |
| 1163 | }# end: while |
| 1164 | |
| 1165 | |
| 1166 | # ~ end: conservative tokenization ~ |
| 1167 | |
| 1168 | |
| 1169 | # ~ start: aggressive tokenization ~ |
| 1170 | |
| 1171 | |
| 1172 | while ( $txt =~ /([^\p{Punct} \x{9}\n]+)(?:([\p{Punct}])|(?:[ \x{9}\n])?)|([\p{Punct}])/g ){ |
| 1173 | |
| 1174 | if ( defined $1 ){ |
| 1175 | |
| 1176 | push @tok_tokens_agg, $-[1]+$offset; push @tok_tokens_agg, $+[1]+$offset; # from and to |
| 1177 | |
| 1178 | if ( defined $2 ){ push @tok_tokens_agg, $-[2]+$offset; push @tok_tokens_agg, $+[2]+$offset } # from and to |
| 1179 | |
| 1180 | }else{ # defined $3 |
| 1181 | |
| 1182 | push @tok_tokens_agg, $-[3]+$offset; push @tok_tokens_agg, $+[3]+$offset # from and to |
| 1183 | } |
| 1184 | |
| 1185 | } # end: while |
| 1186 | |
| 1187 | # ~ end: aggressive tokenization ~ |
| 1188 | |
| 1189 | ##$offset = $dl+1; |
| 1190 | |
| 1191 | $offset = $dl; |
| 1192 | |
| 1193 | } # fi |
| 1194 | |
| 1195 | } # fi: $_GEN_TOK_DUMMY |
| 1196 | |
| 1197 | |
| 1198 | #elsif ( $e->[0] == XML_READER_TYPE_ATTRIBUTE ) # attribute node |
| 1199 | # note: attributes cannot be processed like this ( => use 'XCT_ATTRIBUTE_ARRAY' - see above ) |
| 1200 | |
| 1201 | |
| 1202 | }else{ # not yet handled type |
| 1203 | |
| 1204 | die "ERROR ($0): Not yet handled type (\$e->[0]=".$e->[0].") ... => Aborting\n"; |
| 1205 | } |
| 1206 | |
| 1207 | } # end: foreach iteration |
| 1208 | |
| 1209 | } # end: sub retr_info |
| 1210 | |
| 1211 | |
| 1212 | sub select_tokenization { # called from process() |
| 1213 | |
| 1214 | #print STDERR "$0: select_tokenization() ...\n"; |
| 1215 | |
| 1216 | ## DEPRECATED (only IDS-intern) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1217 | if( $_GEN_TOK_BAS ) { |
| 1218 | if( $select->can_read(3600) ){ # wait 60m for external tokenizer |
| 1219 | $_ = <$chld_out>; |
| 1220 | my @bounds = split; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1221 | write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_bas", $text_id_esc, \@bounds); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1222 | while($select->can_read(0)) { |
| 1223 | $_ = <$chld_out>; |
| 1224 | if (defined $_ && $_ ne '') { |
| 1225 | print STDERR "WARNING: extra output: $_\n" |
| 1226 | } else { |
| 1227 | print STDERR "WARNING: tokenizer seems to have crashed, restarting.\n"; |
| 1228 | startTokenizer(); |
| 1229 | } |
| 1230 | } |
| 1231 | }else{ |
| 1232 | $zip->close(); |
| 1233 | die "ERROR ($0): cannot retrieve token bounds from external tokenizer for text '$text_id' => Aborting ...\n"; |
| 1234 | } |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1235 | ## |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1236 | }elsif( $_GEN_TOK_DUMMY ){ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1237 | write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_con", $text_id_esc, \@tok_tokens_con); |
| 1238 | write_tokenization("$_root_dir$dir/$_base_tokenization_dir/$_tok_file_agg", $text_id_esc, \@tok_tokens_agg); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1239 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1240 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1241 | #print STDERR "$0: write_tokenization(): DONE\n"; |
| 1242 | |
| 1243 | } # end: select_tokenization |
| 1244 | |
| 1245 | sub write_tokenization { # called from select_tokenization() |
| 1246 | |
| 1247 | my ( $fname, $textid_esc, $bounds ) = @_; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1248 | |
| 1249 | $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => $fname) |
| 1250 | or die "ERROR ('$fname'): zip failed: $ZipError\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1251 | |
| 1252 | $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\"" |
| 1253 | ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\"$text_id_esc\" xmlns=\"http://ids-mannheim.de/ns/KorAP\"" |
| 1254 | ." version=\"KorAP-0.4\">\n <spanList>\n"; |
| 1255 | |
| 1256 | $c = 0; |
| 1257 | |
| 1258 | for( $i = 0; $i < ($#$bounds + 1); $i += 2 ){ |
| 1259 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1260 | $output .= " <span id=\"t_$c\" from=\"".$bounds->[$i]."\" to=\"".$bounds->[$i+1]."\" />\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1261 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1262 | $c++; |
| 1263 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1264 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1265 | $output .= " </spanList>\n</layer>"; |
| 1266 | |
| 1267 | $zip->print ( "$output" ); |
| 1268 | |
| 1269 | } # end: sub write_tokenization |
| 1270 | |
| 1271 | |
| 1272 | sub write_structures { # called from process() |
| 1273 | |
| 1274 | # ~ write @structures ~ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1275 | |
| 1276 | #print STDERR "$0: write_structures(): ...\n"; |
| 1277 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1278 | if ( $dir eq "" ){ |
| 1279 | |
| 1280 | print STDERR "WARNING ($0): write_structures(): empty textSigle => nothing to do ...\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1281 | return; |
| 1282 | } |
| 1283 | |
| 1284 | $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir/$_structure_dir/$_structure_file" ) |
| 1285 | or die "ERROR ('$_root_dir$dir/$_structure_dir/$_structure_file'): zip failed: $ZipError\n"; |
| 1286 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1287 | $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\"" |
| 1288 | ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\"" |
| 1289 | .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] | 1290 | |
| 1291 | $c = 0; |
| 1292 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1293 | foreach $ref ( @structures ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1294 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1295 | ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 ); # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4 |
| 1296 | # (name, from, to, rec_level), otherwise >4 |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1297 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1298 | # 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 ) |
| 1299 | |
| 1300 | if( $#structures == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){ |
| 1301 | |
| 1302 | ${$ref}[$idx] = ${$ref}[ $idx+1 ]; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1303 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1304 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1305 | # this consistency check is already done in 'retr_info()' |
| 1306 | #elsif( ${$ref}[$idx] > ${$ref}[$idx+1] ){ # consistency check: abort, if this doesn't hold |
| 1307 | # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):" |
| 1308 | # ." 'from-index=${$ref}[$idx]' > 'to-index=${$ref}[$idx+1]' => please check! aborting ...\n"; |
| 1309 | # die "ERROR ($0: write_structures(): \$text_id=$text_id, \$c=$c, tag-name=${$ref}[0]):" |
| 1310 | # ." '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] | 1311 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1312 | # at least 'POS' should always be there => remove constraint '$_TOKENS_PROC' |
| 1313 | #if( $_TOKENS_PROC && ${$ref}[0] ne $_TOKENS_TAG ) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1314 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1315 | 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] | 1316 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1317 | # l (level): insert information about depth of element in XML-tree (top element = level 1) |
| 1318 | $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n" |
| 1319 | ." <fs type=\"struct\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n" |
| 1320 | ." <f name=\"name\">${$ref}[ 0 ]</f>\n"; |
| 1321 | |
| 1322 | if ( $idx > 2 ) # attributes |
| 1323 | { |
| 1324 | $output .= " <f name=\"attr\">\n <fs type=\"attr\">\n"; |
| 1325 | |
| 1326 | for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){ |
| 1327 | |
| 1328 | ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # see explanation in func. 'write_tokens' |
| 1329 | |
| 1330 | # attribute (at index $att_idx) with value (at index $att_idx+1) |
| 1331 | $output .= " <f name=\"${$ref}[ $att_idx ]\">${$ref}[ $att_idx+1 ]</f>\n"; |
| 1332 | } |
| 1333 | |
| 1334 | $output .= " </fs>\n </f>\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1335 | } |
| 1336 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1337 | $output .= " </fs>\n </span>\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1338 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1339 | } # fi: ... ne $_TOKENS_TAG |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1340 | |
| 1341 | $c++; |
| 1342 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1343 | } # end: foreach |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1344 | |
| 1345 | $output .= " </spanList>\n</layer>"; |
| 1346 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1347 | $output = encode_utf8( $output ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1348 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1349 | $zip->print( "$output" ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1350 | |
| 1351 | #print STDERR "$0: write_structures(): DONE\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1352 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1353 | } # end: sub write_structures |
| 1354 | |
| 1355 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1356 | sub write_tokens { # called from process() |
| 1357 | |
| 1358 | # ~ write @tokens ~ |
| 1359 | |
| 1360 | #print STDERR "$0: write_tokens(): ...\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1361 | |
| 1362 | if( $dir eq "" ){ |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1363 | |
| 1364 | print STDERR "WARNING ($0): write_tokens(): empty textSigle => nothing to do ...\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1365 | return; |
| 1366 | } |
| 1367 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1368 | $zip->newStream( Zip64 => 1, TextFlag => 1, Method => $_COMPRESSION_METHOD, Append => 1, Name => "$_root_dir$dir/$_tokens_dir/$_tokens_file" ) |
| 1369 | or die "ERROR ('$_root_dir$dir/$_tokens_dir/$_tokens_file'): zip failed: $ZipError\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1370 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1371 | $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-model href=\"span.rng\" type=\"application/xml\"" |
| 1372 | ." schematypens=\"http://relaxng.org/ns/structure/1.0\"?>\n\n<layer docid=\"" |
| 1373 | .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] | 1374 | |
| 1375 | $c = 0; |
| 1376 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1377 | foreach $ref ( @tokens ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1378 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1379 | # if array '@{$ref}' doesn't contain attributes, then the number of elements in this array is 4 (name, from, to, rec_level), otherwise >4 |
| 1380 | ( @{$ref} == 4 )?( $idx = 1 ):( $idx = @{$ref}-3 ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1381 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1382 | # 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()) |
| 1383 | if ( $#tokens == $c && ${$ref}[ $idx ] == ${$ref}[ $idx+1 ] + 1 ){ |
| 1384 | |
| 1385 | ${$ref}[ $idx ] = ${$ref}[ $idx+1 ]; # TODO: check |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1386 | } |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1387 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1388 | # l (level): insert information about depth of element in XML-tree (top element = level 1) |
| 1389 | $output .= " <span id=\"s$c\" from=\"${$ref}[ $idx ]\" to=\"${$ref}[ $idx+1 ]\" l=\"${$ref}[ $idx+2 ]\">\n" |
| 1390 | ." <fs type=\"lex\" xmlns=\"http://www.tei-c.org/ns/1.0\">\n" |
| 1391 | ." <f name=\"lex\">\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1392 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1393 | if ( $idx > 2 ){ # attributes |
| 1394 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1395 | $output .= " <fs>\n"; |
| 1396 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1397 | for ( $att_idx = 1; $att_idx < $idx; $att_idx += 2 ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1398 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1399 | ${$ref}[ $att_idx+1 ] =~ s/(&|<|>)/$ent{$1}/g; # ... <w lemma=">" ana="PUNCTUATION">></w> ... |
| 1400 | # 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] | 1401 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1402 | if ( $_INLINE_ANNOT && ${$ref}[ $att_idx ] eq "$_INLINE_ATT_RD" ){ |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1403 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1404 | ${$ref}[ $att_idx+1 ] =~ /^([^ ]+)(?: (.+))?$/; |
| 1405 | |
| 1406 | die "ERROR (write_tokens()): unexpected format! => Aborting ... (att: ${$ref}[ $att_idx+1 ])\n" |
| 1407 | if ( $_INLINE_POS_WR && not defined $1 ) || ( $_INLINE_MSD_WR && not defined $2 ); |
| 1408 | |
| 1409 | if ( "$_INLINE_POS_WR" ){ |
| 1410 | |
| 1411 | $output .= " <f name=\"$_INLINE_POS_WR\">"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1412 | $output .= "$1" if defined $1; |
| 1413 | $output .= "</f>\n"; |
| 1414 | } |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1415 | |
| 1416 | if ( "$_INLINE_MSD_WR" ){ |
| 1417 | |
| 1418 | $output .= " <f name=\"$_INLINE_MSD_WR\">"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1419 | $output .= "$2" if defined $2; |
| 1420 | $output .= "</f>\n"; |
| 1421 | } |
| 1422 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1423 | } 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] | 1424 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1425 | $output .= " <f name=\"$_INLINE_LEM_WR\">${$ref}[ $att_idx+1 ]</f>\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1426 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1427 | } else { # all other attributes |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1428 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1429 | $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] | 1430 | } |
| 1431 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1432 | } # end: for |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1433 | |
| 1434 | $output .= " </fs>\n"; |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1435 | |
| 1436 | } # fi: attributes |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1437 | |
| 1438 | $output .= " </f>\n </fs>\n </span>\n"; |
| 1439 | |
| 1440 | $c++; |
| 1441 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1442 | } # end: foreach |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1443 | |
| 1444 | $output .= " </spanList>\n</layer>"; |
| 1445 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1446 | $output = encode_utf8( $output ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1447 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1448 | $zip->print( "$output" ); |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1449 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1450 | #print STDERR "$0: write_tokens(): DONE\n"; |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1451 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1452 | } # end: sub write_tokens |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1453 | |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1454 | |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1455 | ## DEPRECATED ($_GEN_TOK_BAS: only IDS-intern) |
Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 1456 | sub startTokenizer { |
| 1457 | $pid = open2($chld_out, $chld_in, 'java -cp '. join(":", ".", glob(&dirname(__FILE__)."/../target/*.jar"))." de.ids_mannheim.korap.tokenizer.KorAPTokenizerImpl"); |
| 1458 | $select = IO::Select->new(); |
| 1459 | $select->add(*$chld_out); |
| 1460 | } |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1461 | ## |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1462 | |
| 1463 | __END__ |
| 1464 | |
| 1465 | =pod |
| 1466 | |
| 1467 | =encoding utf8 |
| 1468 | |
| 1469 | =head1 NAME |
| 1470 | |
| 1471 | tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML |
| 1472 | |
| 1473 | =head1 SYNOPSIS |
| 1474 | |
| 1475 | cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip |
| 1476 | |
| 1477 | =head1 DESCRIPTION |
| 1478 | |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame^] | 1479 | C<tei2korapxml> is a script to convert TEI P5 and |
| 1480 | L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html> |
| 1481 | based documents to the |
| 1482 | L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>. |
| 1483 | If no specific input is defined, data is |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1484 | read from C<STDIN>. If no specific output is defined, data is written |
| 1485 | to C<STDOUT>. |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1486 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1487 | This program is usually called from inside another script. |
| 1488 | |
Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame^] | 1489 | =head1 FORMATS |
| 1490 | |
| 1491 | =head2 Input restrictions |
| 1492 | |
| 1493 | =over 2 |
| 1494 | |
| 1495 | =item |
| 1496 | |
| 1497 | utf8 encoded |
| 1498 | |
| 1499 | =item |
| 1500 | |
| 1501 | TEI P5 formatted input with certain restrictions: |
| 1502 | |
| 1503 | =over 4 |
| 1504 | |
| 1505 | =item |
| 1506 | |
| 1507 | B<mandatory>: text-header with integrated textsigle, text-body |
| 1508 | |
| 1509 | =item |
| 1510 | |
| 1511 | B<optional>: corp-header with integrated corpsigle, |
| 1512 | doc-header with integrated docsigle |
| 1513 | |
| 1514 | =back |
| 1515 | |
| 1516 | =item |
| 1517 | |
| 1518 | all tokens inside the primary text (inside $data) may not be |
| 1519 | newline seperated, because newlines are removed |
| 1520 | (see code section C<~ inside text body ~>) and a conversion of newlines |
| 1521 | into blanks between 2 tokens could lead to additional blanks, |
| 1522 | where there should be none (e.g.: punctuation characters like C<,> or |
| 1523 | C<.> should not be seperated from their predecessor token). |
| 1524 | (see also code section C<~ whitespace handling ~>). |
| 1525 | |
| 1526 | =back |
| 1527 | |
| 1528 | =head2 Notes on the output |
| 1529 | |
| 1530 | =over 2 |
| 1531 | |
| 1532 | =item |
| 1533 | |
| 1534 | zip file output (default on C<stdout>) with utf8 encoded entries |
| 1535 | (which together form the KorAP-XML format) |
| 1536 | |
| 1537 | =back |
| 1538 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1539 | =head1 INSTALLATION |
| 1540 | |
| 1541 | C<tei2korapxml> requires L<libxml2-dev> bindings to build. When |
| 1542 | these bindings are available, the preferred way to install the script is |
| 1543 | to use L<cpanm|App::cpanminus>. |
| 1544 | |
| 1545 | $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git |
| 1546 | |
| 1547 | In case everything went well, the C<tei2korapxml> tool will |
| 1548 | be available on your command line immediately. |
Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 1549 | |
Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 1550 | Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16. |
| 1551 | |
| 1552 | =head1 OPTIONS |
| 1553 | |
| 1554 | =over 2 |
| 1555 | |
| 1556 | =item B<--base|-b> |
| 1557 | |
| 1558 | The base directory for output. Defaults to C<.>. |
| 1559 | |
| 1560 | =item B<--help|-h> |
| 1561 | |
| 1562 | Print help information. |
| 1563 | |
| 1564 | =item B<--version|-v> |
| 1565 | |
| 1566 | Print version information. |
| 1567 | |
| 1568 | =back |
| 1569 | |
| 1570 | =head1 COPYRIGHT AND LICENSE |
| 1571 | |
| 1572 | Copyright (C) 2020, L<IDS Mannheim|https://www.ids-mannheim.de/> |
| 1573 | |
| 1574 | Author: Peter Harders |
| 1575 | |
| 1576 | Contributors: Marc Kupietz, Carsten Schnober, Nils Diewald |
| 1577 | |
| 1578 | L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/> |
| 1579 | Corpus Analysis Platform at the |
| 1580 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
| 1581 | member of the |
| 1582 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>. |
| 1583 | |
| 1584 | This program is free software published under the |
| 1585 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-TEI/master/LICENSE>. |
| 1586 | |
| 1587 | =cut |