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