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