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