blob: 5c74b91e43fb8fd2f9d0d9a2dd31c3a358ad2ebe [file] [log] [blame]
Akron9cb13942020-02-14 07:39:54 +01001#!/usr/bin/env perl
Peter Hardersd892a582020-02-12 15:45:22 +01002use strict;
3use warnings;
Peter Harders6f526a32020-06-29 21:44:41 +02004
Akron3378dfd2020-08-01 15:01:36 +02005use Log::Any '$log';
6use Log::Any::Adapter;
Peter Harders6f526a32020-06-29 21:44:41 +02007use Pod::Usage;
8use Getopt::Long qw(GetOptions :config no_auto_abbrev);
9
10use File::Basename qw(dirname);
Peter Hardersd892a582020-02-12 15:45:22 +010011
Akroneaa96232020-10-15 17:06:15 +020012use Encode qw(decode);
Peter Hardersd892a582020-02-12 15:45:22 +010013
Akron4f67cd42020-07-02 12:27:58 +020014use FindBin;
15BEGIN {
16 unshift @INC, "$FindBin::Bin/../lib";
17};
18
Marc Kupietz8a954e52021-02-16 22:03:07 +010019use KorAP::XML::TEI qw!remove_xml_comments replace_entities!;
Akron8b511f92020-07-09 17:28:08 +020020use KorAP::XML::TEI::Tokenizer::External;
Akrond9627472020-07-09 16:53:09 +020021use KorAP::XML::TEI::Tokenizer::Conservative;
22use KorAP::XML::TEI::Tokenizer::Aggressive;
Akron85717512020-07-08 11:19:19 +020023use KorAP::XML::TEI::Zipper;
Akronf57ed812020-07-27 10:37:52 +020024use KorAP::XML::TEI::Header;
Akroneb12e232021-02-25 13:49:50 +010025use KorAP::XML::TEI::Inline;
Peter Hardersd892a582020-02-12 15:45:22 +010026
Akron85269c02022-11-07 14:03:31 +010027our $VERSION = '2.3.4';
Peter Harders6f526a32020-06-29 21:44:41 +020028
Akrond949e182020-02-14 12:23:57 +010029our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n";
30
Akron33db4ec2021-02-24 12:52:21 +010031use constant {
32 # Set to 1 for minimal more debug output (no need to be parametrized)
Akroneb12e232021-02-25 13:49:50 +010033 DEBUG => $ENV{KORAPXMLTEI_DEBUG} // 0
Akron33db4ec2021-02-24 12:52:21 +010034};
Peter Hardersd892a582020-02-12 15:45:22 +010035
Akron692d17d2021-03-05 13:21:03 +010036if ($ENV{KORAPXMLTEI_INLINE}) {
37 warn 'KORAPXMLTEI_INLINE is deprecated in favor of --skip-inline-token-annotations';
38};
39
Akrone2819a12021-10-12 15:52:55 +020040# Inline tokens won't be stored in the structure file
41my $inline_tokens_exclusive = 0;
42
Peter Harders6f526a32020-06-29 21:44:41 +020043# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010044GetOptions(
Akrond3e1d282021-02-24 14:51:27 +010045 'root|r=s' => \(my $root_dir = '.'),
46 'input|i=s' => \(my $input_fname = ''),
Akron75d63142021-02-23 18:40:56 +010047 'tokenizer-call|tc=s' => \(my $tokenizer_call),
48 'tokenizer-korap|tk' => \(my $tokenizer_korap),
Akrond53913c2021-02-24 09:50:13 +010049 'tokenizer-internal|ti' => \(my $tokenizer_intern),
Akron75d63142021-02-23 18:40:56 +010050 'use-tokenizer-sentence-splits|s' => \(my $use_tokenizer_sentence_splits),
51 'inline-tokens=s' => \(my $inline_tokens = 'tokens#morpho'),
52 'inline-structures=s' => \(my $inline_structures = 'struct#structure'),
53 'skip-inline-tokens' => \(my $skip_inline_tokens = 0),
Akron692d17d2021-03-05 13:21:03 +010054 'skip-inline-token-annotations' => \(
55 my $skip_inline_token_annotations = ($ENV{KORAPXMLTEI_INLINE} ? 0 : 1)),
Akron54c3ff12021-02-25 11:33:37 +010056 'skip-inline-tags=s' => \(my $skip_inline_tags_str = ''),
Akrond3e1d282021-02-24 14:51:27 +010057 'base-foundry=s' => \(my $base_dir = 'base'),
58 'data-file=s' => \(my $data_file = 'data'),
Akrond53913c2021-02-24 09:50:13 +010059 'header-file=s' => \(my $header_file = 'header'),
60 'tokens-file=s' => \(my $tokens_file = 'tokens'),
Akrond3e1d282021-02-24 14:51:27 +010061 'log|l=s' => \(my $log_level = 'notice'),
Akron2520a342022-03-29 18:18:05 +020062 'required-version|rv=s' => \(my $required_version),
Akrona2cb2812021-10-30 10:29:08 +020063 '' => \(my $stdio),
Akron75d63142021-02-23 18:40:56 +010064 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010065 pod2usage(
66 -verbose => 99,
67 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
68 -msg => $VERSION_MSG,
69 -output => '-'
70 )
71 },
72 'version|v' => sub {
73 pod2usage(
74 -verbose => 0,
75 -msg => $VERSION_MSG,
76 -output => '-'
Akrond3e1d282021-02-24 14:51:27 +010077 );
Akrond949e182020-02-14 12:23:57 +010078 }
Peter Hardersd892a582020-02-12 15:45:22 +010079);
80
Akrond3e1d282021-02-24 14:51:27 +010081
Akronb87c58d2021-02-23 17:23:30 +010082# Establish logger
Akron33db4ec2021-02-24 12:52:21 +010083binmode(STDERR, ':encoding(UTF-8)');
Akron3378dfd2020-08-01 15:01:36 +020084Log::Any::Adapter->set('Stderr', log_level => $log_level);
Akronb3649472020-09-29 08:24:46 +020085$log->notice('Debugging is activated') if DEBUG;
86
Akrond3e1d282021-02-24 14:51:27 +010087
Akron2520a342022-03-29 18:18:05 +020088if ($required_version) {
89 $required_version =~ /^\s*(\d+\.\d+\.\d+)\s*$/;
90 if (!$1 || $1 ne $VERSION) {
91 $log->error("Required version $required_version mismatches version $VERSION");
92 exit(1);
93 };
94};
95
96
Akron0529e512021-02-22 09:55:35 +010097# tag (without attributes), which contains the primary text
98my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +020099# optional
Akron09e0b2c2020-07-28 15:57:01 +0200100
Akron0529e512021-02-22 09:55:35 +0100101# TODO: IDS-specific (and redundant)
102my $_HEADER_TAG = 'idsHeader';
Akron0c41ab32020-09-29 07:33:33 +0200103
Akron54c3ff12021-02-25 11:33:37 +0100104# Remember to skip certain inline tags
105my %skip_inline_tags = ();
106if ($skip_inline_tags_str) {
107 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
108 $skip_inline_tags{$_} = 1;
109 };
110};
111
Akrond3e1d282021-02-24 14:51:27 +0100112# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200113my $ext_tok;
114if ($tokenizer_call) {
115 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
Akron11484782021-11-03 20:12:14 +0100116 $ext_tok->sentence_splits(1) if $use_tokenizer_sentence_splits;
Akron0c41ab32020-09-29 07:33:33 +0200117}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200118
Akron0c41ab32020-09-29 07:33:33 +0200119elsif ($tokenizer_korap) {
Akronbd4281e2022-03-28 08:31:40 +0200120 eval {
121 require KorAP::XML::TEI::Tokenizer::KorAP;
122 1;
123 };
Akron2520a342022-03-29 18:18:05 +0200124
125 my $korap_tok_ver = $KorAP::XML::TEI::Tokenizer::KorAP::VERSION;
126 if ($korap_tok_ver ne $VERSION) {
127 $log->error("KorAP-Tokenizer version ($korap_tok_ver) differs from the expected version ($VERSION)");
128 exit(1);
129 };
130
Marc Kupietz985da0c2021-02-15 19:29:50 +0100131 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron0c41ab32020-09-29 07:33:33 +0200132};
Peter Harders6f526a32020-06-29 21:44:41 +0200133
Akron11484782021-11-03 20:12:14 +0100134if ($use_tokenizer_sentence_splits) {
135 $skip_inline_tags{s} = 1;
136};
Akron0c41ab32020-09-29 07:33:33 +0200137
Akrond3e1d282021-02-24 14:51:27 +0100138# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100139my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
140my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100141
Peter Harders41c35622020-07-12 01:16:22 +0200142
Akrondd0be8f2021-02-18 19:29:41 +0100143# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100144# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100145my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100146
Akron1a5271a2021-02-18 13:18:15 +0100147# Name of the directory and the file containing all inline token informations
148# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
149my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100150
Akrone2819a12021-10-12 15:52:55 +0200151if (index($_tokens_dir, '!') == 0) {
152 $_tokens_dir = substr($_tokens_dir, 1);
153 $inline_tokens_exclusive = 1;
154};
155
Akronb87c58d2021-02-23 17:23:30 +0100156# Initialize zipper
Akrond53913c2021-02-24 09:50:13 +0100157my $zipper = KorAP::XML::TEI::Zipper->new($root_dir);
Akron09e0b2c2020-07-28 15:57:01 +0200158
Akronbc899192021-02-24 12:14:47 +0100159# text directory (below $root_dir)
160my $dir = '';
Akron09e0b2c2020-07-28 15:57:01 +0200161
Akronbc899192021-02-24 12:14:47 +0100162# Escaped version of text id
163my $text_id_esc;
Peter Harders6f526a32020-06-29 21:44:41 +0200164
Akrond53913c2021-02-24 09:50:13 +0100165# Default encoding of the text
166my $input_enc = 'UTF-8';
167
Akrond53913c2021-02-24 09:50:13 +0100168# text line (needed for whitespace handling)
169my $text_line = 0;
170
Peter Harders6f526a32020-06-29 21:44:41 +0200171
Akrond53913c2021-02-24 09:50:13 +0100172# Input file handle (default: stdin)
Akrona2cb2812021-10-30 10:29:08 +0200173my $input_fh;
Peter Hardersd892a582020-02-12 15:45:22 +0100174
Akrona2cb2812021-10-30 10:29:08 +0200175# Single dash was set
176if ($stdio) {
177 $input_fh = *STDIN;
178}
179
180# Input flag was passed
181elsif ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200182 unless (open($input_fh, '<', $input_fname)) {
183 die $log->fatal("File '$input_fname' could not be opened.");
184 };
Akrona2cb2812021-10-30 10:29:08 +0200185}
186
187# No input to process
188else {
189 pod2usage(
190 -verbose => 99,
191 -sections => 'NAME|SYNOPSIS',
192 -msg => $VERSION_MSG,
193 -output => '-'
194 );
195 exit;
Akrond53913c2021-02-24 09:50:13 +0100196};
Peter Harders6f526a32020-06-29 21:44:41 +0200197
Akronf8088e62021-02-18 16:18:59 +0100198# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200199binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200200
Peter Harders6f526a32020-06-29 21:44:41 +0200201
Akroneb12e232021-02-25 13:49:50 +0100202# Create inline parser object
203my $inline = KorAP::XML::TEI::Inline->new(
204 $skip_inline_tokens,
Akrone2819a12021-10-12 15:52:55 +0200205 \%skip_inline_tags,
206 $inline_tokens_exclusive
Akroneb12e232021-02-25 13:49:50 +0100207);
208
209
Akrond53913c2021-02-24 09:50:13 +0100210# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100211MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200212
Akrond53913c2021-02-24 09:50:13 +0100213 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100214 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200215
Akroneaa96232020-10-15 17:06:15 +0200216 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100217 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200218 $input_enc = $2;
219 next;
220 };
221
222 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100223 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200224
Akrond3e1d282021-02-24 14:51:27 +0100225 # Start of text body
226 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100227 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200228
Akrond53913c2021-02-24 09:50:13 +0100229 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200230 die $log->fatal("input line number $.: " .
231 "line with opening text-body tag '${_TEXT_BODY}' " .
232 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200233 };
Peter Harders6f526a32020-06-29 21:44:41 +0200234
Akrond53913c2021-02-24 09:50:13 +0100235 # Text body data extracted from input document ($input_fh),
236 # further processed by XML::LibXML::Reader
237 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200238
Akron347be812020-09-29 07:52:52 +0200239 # Iterate over all lines in the text body
240 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200241
Akrond3e1d282021-02-24 14:51:27 +0100242 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200243 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100244 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200245
Akrond53913c2021-02-24 09:50:13 +0100246 # End of text body
Akronb43b4912021-02-25 10:31:11 +0100247 if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200248
Akron91705d72021-02-19 10:59:45 +0100249 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200250
Akrond53913c2021-02-24 09:50:13 +0100251 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200252 die $log->fatal("input line number $.: " .
253 "line with closing text-body tag '${_TEXT_BODY}'".
254 " contains additional information ... => Aborting (line=$_)");
255 };
Peter Harders6f526a32020-06-29 21:44:41 +0200256
Akrondafaa7a2021-02-19 15:17:58 +0100257 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100258 $log->warn(
259 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100260 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100261 );
Akrondafaa7a2021-02-19 15:17:58 +0100262 next MAIN;
263 };
Peter Harders6f526a32020-06-29 21:44:41 +0200264
Akroneb12e232021-02-25 13:49:50 +0100265 # Parse inline structure
266 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100267
268 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100269 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100270 };
271
Akroneb12e232021-02-25 13:49:50 +0100272 my $data = $inline->data;
273
Akrond53913c2021-02-24 09:50:13 +0100274 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100275 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100276 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100277 $text_id_esc
278 );
279
Akrond53913c2021-02-24 09:50:13 +0100280 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100281 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100282
283 # Tokenize and output
284 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100285 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100286 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100287 );
Akrond53ab4b2021-02-24 09:56:12 +0100288
289 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100290 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100291 };
Akrondafaa7a2021-02-19 15:17:58 +0100292 };
Peter Harders6f526a32020-06-29 21:44:41 +0200293
Akrond53913c2021-02-24 09:50:13 +0100294 # Tokenize with internal tokenizer
295 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200296
Akrondafaa7a2021-02-19 15:17:58 +0100297 # Tokenize and output
298 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100299 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200300 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100301 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200302
Akrondafaa7a2021-02-19 15:17:58 +0100303 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100304 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100305 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100306 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100307 };
Akrona10ad592020-08-03 11:20:23 +0200308
Akrondafaa7a2021-02-19 15:17:58 +0100309 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100310 if (!$inline->structures->empty) {
311 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100312 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100313 $text_id_esc,
314 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100315 );
Akrondafaa7a2021-02-19 15:17:58 +0100316 };
317
318 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100319 unless ($skip_inline_tokens || $inline->tokens->empty) {
320 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100321 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100322 $text_id_esc,
Akron692d17d2021-03-05 13:21:03 +0100323 # Either 0 = tokens without inline or 1 = tokens with inline
324 !$skip_inline_token_annotations
Akroneb12e232021-02-25 13:49:50 +0100325 );
Akrondafaa7a2021-02-19 15:17:58 +0100326 };
327
328 # reinit.
329 $dir = '';
330
Akron347be812020-09-29 07:52:52 +0200331 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200332 };
333
Peter Harders6f526a32020-06-29 21:44:41 +0200334
Akron347be812020-09-29 07:52:52 +0200335 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200336
Akronf8088e62021-02-18 16:18:59 +0100337 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100338
Akrond53913c2021-02-24 09:50:13 +0100339 # TODO:
340 # Maybe it's best, to keep the stripping of whitespace and
341 # to just remove the if-clause and to insert a blank by default
342 # (with possibly an option on how newlines in primary text should
343 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100344
345 # Remove consecutive whitespace at beginning and end (mostly one newline)
346 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200347
Akrond53913c2021-02-24 09:50:13 +0100348 # NOTE:
349 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200350
Akrond53913c2021-02-24 09:50:13 +0100351 # TODO:
352 # find a better solution, or create a warning, if a text has more
353 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200354
Akrond53913c2021-02-24 09:50:13 +0100355 # TODO:
356 # do testing with 2 different corpora
357 # (one with only one-line texts, the other with several lines per text)
358
359 # line contains at least one tag with at least one character contents
360 if (m/<[^>]+>[^<]/) {
361
362 # Increment counter for text lines
363 $text_line++;
364
365 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100366 # (for 2nd line and consecutive lines)
367 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200368 }
Akronf57ed812020-07-27 10:37:52 +0200369
Akron347be812020-09-29 07:52:52 +0200370 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100371 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200372 };
Akrond3e1d282021-02-24 14:51:27 +0100373 }
Akronf57ed812020-07-27 10:37:52 +0200374
Akrond3e1d282021-02-24 14:51:27 +0100375 # Start of header section
376 elsif (m#^(.*)(\<${_HEADER_TAG}[^>]*?type=["'].*)$#) {
Akronf57ed812020-07-27 10:37:52 +0200377
Akron347be812020-09-29 07:52:52 +0200378 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200379
Akrond20898f2021-02-19 15:52:17 +0100380 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100381 die $log->fatal(
382 "input line number $.: " .
383 'line with opening header tag is not in expected format ... ' .
384 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200385 };
386
387 # Parse header
Akroneaa96232020-10-15 17:06:15 +0200388 my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200389
390 # Header was parseable
391 if ($header) {
392
393 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100394 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200395
Akronb3649472020-09-29 08:24:46 +0200396 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200397
398 $header->to_zip($zipper->new_stream($file));
399
400 # Header is for text level
401 if ($header->type eq 'text') {
402
403 # Remember dir and sigles
404 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200405 $text_id_esc = $header->id_esc;
406
407 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100408 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200409
Akrond53913c2021-02-24 09:50:13 +0100410 # Reset counter for text lines
411 # (needed for whitespace handling)
412 $text_line = 0;
413 };
414 };
415 };
416};
Peter Hardersd892a582020-02-12 15:45:22 +0100417
Akron347be812020-09-29 07:52:52 +0200418$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200419
Akron9df4a242021-02-19 15:31:16 +0100420$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100421
Akrond53913c2021-02-24 09:50:13 +0100422close $input_fh;
423
Peter Harders6f526a32020-06-29 21:44:41 +0200424
Akrond949e182020-02-14 12:23:57 +0100425__END__
426
427=pod
428
429=encoding utf8
430
431=head1 NAME
432
433tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
434
435=head1 SYNOPSIS
436
Akrona2cb2812021-10-30 10:29:08 +0200437 cat corpus.i5.xml | tei2korapxml - > corpus.korapxml.zip
Akrond949e182020-02-14 12:23:57 +0100438
439=head1 DESCRIPTION
440
Akronee434b12020-07-08 12:53:01 +0200441C<tei2korapxml> is a script to convert TEI P5 and
Akrond72baca2021-07-23 13:25:32 +0200442L<I5|https://www.ids-mannheim.de/digspra/kl/projekte/korpora/textmodell>
Akronee434b12020-07-08 12:53:01 +0200443based documents to the
444L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
Peter Harders6f526a32020-06-29 21:44:41 +0200445
Akrond949e182020-02-14 12:23:57 +0100446This program is usually called from inside another script.
447
Akronee434b12020-07-08 12:53:01 +0200448=head1 FORMATS
449
450=head2 Input restrictions
451
452=over 2
453
454=item
455
Akronee434b12020-07-08 12:53:01 +0200456TEI P5 formatted input with certain restrictions:
457
458=over 4
459
460=item
461
462B<mandatory>: text-header with integrated textsigle, text-body
463
464=item
465
466B<optional>: corp-header with integrated corpsigle,
467doc-header with integrated docsigle
468
469=back
470
471=item
472
Akron0c41ab32020-09-29 07:33:33 +0200473All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200474newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200475(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200476into blanks between 2 tokens could lead to additional blanks,
477where there should be none (e.g.: punctuation characters like C<,> or
478C<.> should not be seperated from their predecessor token).
Akron8a0c4bf2021-03-16 16:51:21 +0100479(see also code section C<~ whitespace handling ~> in C<script/tei2korapxml>).
Akronee434b12020-07-08 12:53:01 +0200480
Akron940ca6f2021-10-11 12:38:39 +0200481=item
482
483Header types, like C<E<lt>idsHeader [...] type="document" [...] E<gt>>
484need to be defined in the same line as the header tag.
485
Akronee434b12020-07-08 12:53:01 +0200486=back
487
488=head2 Notes on the output
489
490=over 2
491
492=item
493
494zip file output (default on C<stdout>) with utf8 encoded entries
495(which together form the KorAP-XML format)
496
497=back
498
Akrond949e182020-02-14 12:23:57 +0100499=head1 INSTALLATION
500
Marc Kupietze83a4e92021-03-16 20:51:26 +0100501C<tei2korapxml> requires L<libxml2-dev> bindings and L<File::ShareDir::Install> to be installed.
502When these requirements are met, the preferred way to install the script is
Akrond949e182020-02-14 12:23:57 +0100503to use L<cpanm|App::cpanminus>.
504
505 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
506
507In case everything went well, the C<tei2korapxml> tool will
508be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200509
Akrond949e182020-02-14 12:23:57 +0100510Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
511
512=head1 OPTIONS
513
514=over 2
515
Akrona2cb2812021-10-30 10:29:08 +0200516=item B<--input|-i>
517
518The input file to process. If no specific input is defined and a single
519dash C<-> is passed as an argument, data is read from C<STDIN>.
520
521
Akron4e603a52020-07-27 14:23:49 +0200522=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100523
Akron4e603a52020-07-27 14:23:49 +0200524The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100525
526=item B<--help|-h>
527
528Print help information.
529
530=item B<--version|-v>
531
532Print version information.
533
Akron2520a342022-03-29 18:18:05 +0200534=item B<--required-version|-rv>
535
536Version required for conversion. If the script doesn't match
537the version passed, it will exit immediately.
538
Akron4e603a52020-07-27 14:23:49 +0200539=item B<--tokenizer-call|-tc>
540
541Call an external tokenizer process, that will tokenize
Akron11484782021-11-03 20:12:14 +0100542from STDIN and outputs the offsets of all tokens.
543
544Texts are separated using C<\x04\n>. The external process
545should add a new line per text.
546
547If the L</--use-tokenizer-sentence-splits> option is activated,
548sentences are marked by offset as well in new lines.
549
550To use L<Datok|https://github.com/KorAP/Datok> including sentence
551splitting, call C<tei2korap> as follows:
552
553 $ cat corpus.i5.xml | tei2korapxml -s \
554 $ -tc 'datok tokenize \
555 $ -t ./tokenizer.matok \
556 $ -p --newline-after-eot --no-sentences \
557 $ --no-tokens --sentence-positions -' - \
558 $ > corpus.korapxml.zip
Akron4e603a52020-07-27 14:23:49 +0200559
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200560=item B<--tokenizer-korap|-tk>
561
562Use the standard KorAP/DeReKo tokenizer.
563
Akron6d7b8e42020-09-29 07:37:41 +0200564=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200565
566Tokenize the data using two embedded tokenizers,
567that will take an I<Aggressive> and a I<conservative>
568approach.
569
Akron75d63142021-02-23 18:40:56 +0100570=item B<--skip-inline-tokens>
571
572Boolean flag indicating that inline tokens should not
573be processed. Defaults to false (meaning inline tokens will be processed).
574
Akron692d17d2021-03-05 13:21:03 +0100575=item B<--skip-inline-token-annotations>
576
577Boolean flag indicating that inline token annotations should not
578be processed. Defaults to true (meaning inline token annotations
579won't be processed).
580
Akronca70a1d2021-02-25 16:21:31 +0100581=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100582
583Expects a comma-separated list of tags to be ignored when the structure
584is parsed. Content of these tags however will be processed.
585
Akron1a5271a2021-02-18 13:18:15 +0100586=item B<--inline-tokens> <foundry>#[<file>]
587
588Define the foundry and file (without extension)
589to store inline token information in.
Akron8a0c4bf2021-03-16 16:51:21 +0100590Unless C<--skip-inline-token-annotations> is set,
591this will contain annotations as well.
Akron1a5271a2021-02-18 13:18:15 +0100592Defaults to C<tokens> and C<morpho>.
593
Akrone2819a12021-10-12 15:52:55 +0200594The inline token data will also be stored in the
595inline structures file (see I<--inline-structures>),
596unless the inline token foundry is prepended
597by an B<!> exclamation mark, indicating that inline
598tokens are stored exclusively in the inline tokens
599file.
600
601Example:
602
603 tei2korapxml --inline-tokens '!gingko#morpho' < data.i5.xml > korapxml.zip
604
Akrondd0be8f2021-02-18 19:29:41 +0100605=item B<--inline-structures> <foundry>#[<file>]
606
607Define the foundry and file (without extension)
608to store inline structure information in.
609Defaults to C<struct> and C<structures>.
610
Akron26a71522021-02-19 10:27:37 +0100611=item B<--base-foundry> <foundry>
612
613Define the base foundry to store newly generated
614token information in.
615Defaults to C<base>.
616
617=item B<--data-file> <file>
618
619Define the file (without extension)
620to store primary data information in.
621Defaults to C<data>.
622
623=item B<--header-file> <file>
624
625Define the file name (without extension)
626to store header information on
627the corpus, document, and text level in.
628Defaults to C<header>.
629
Marc Kupietz985da0c2021-02-15 19:29:50 +0100630=item B<--use-tokenizer-sentence-splits|-s>
631
632Replace existing with, or add new, sentence boundary information
Akron11484782021-11-03 20:12:14 +0100633provided by the tokenizer.
634Currently KorAP-tokenizer and certain external tokenizers support
635these boundaries.
Marc Kupietz985da0c2021-02-15 19:29:50 +0100636
Akron91705d72021-02-19 10:59:45 +0100637=item B<--tokens-file> <file>
638
639Define the file (without extension)
640to store generated token information in
641(either from the KorAP tokenizer or an externally called tokenizer).
642Defaults to C<tokens>.
643
Akron3378dfd2020-08-01 15:01:36 +0200644=item B<--log|-l>
645
646Loglevel for I<Log::Any>. Defaults to C<notice>.
647
Akrond949e182020-02-14 12:23:57 +0100648=back
649
Akronb3649472020-09-29 08:24:46 +0200650=head1 ENVIRONMENT VARIABLES
651
652=over 2
653
654=item B<KORAPXMLTEI_DEBUG>
655
656Activate minimal debugging.
657Defaults to C<false>.
658
Akronb3649472020-09-29 08:24:46 +0200659=back
660
Akrond949e182020-02-14 12:23:57 +0100661=head1 COPYRIGHT AND LICENSE
662
Marc Kupietze955ecc2021-02-17 17:42:01 +0100663Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100664
665Author: Peter Harders
666
Akronaabd0952020-09-29 07:35:08 +0200667Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100668
669L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
670Corpus Analysis Platform at the
Akrond72baca2021-07-23 13:25:32 +0200671L<Leibniz Institute for the German Language (IDS)|https://www.ids-mannheim.de/>,
Akrond949e182020-02-14 12:23:57 +0100672member of the
673L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
674
675This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100676L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100677
678=cut
Akronf8088e62021-02-18 16:18:59 +0100679
680# NOTES
681
Akronf8088e62021-02-18 16:18:59 +0100682## Notes on segfault prevention
683
Akron91577922021-02-19 10:32:54 +0100684binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100685(see notes on 'PerlIO layers' in 'man XML::LibXML'),
686removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
687see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
688see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.