blob: deee2cc7b3eccd373d80e8809b5af105415cf7ed [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'),
Marc Kupietza671ae52022-12-22 16:28:14 +010061 'xmlid-to-textsigle|x=s'=> \(my $xmlid_to_textsigle = ''),
Akrond3e1d282021-02-24 14:51:27 +010062 'log|l=s' => \(my $log_level = 'notice'),
Akron2520a342022-03-29 18:18:05 +020063 'required-version|rv=s' => \(my $required_version),
Akrona2cb2812021-10-30 10:29:08 +020064 '' => \(my $stdio),
Akron75d63142021-02-23 18:40:56 +010065 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010066 pod2usage(
67 -verbose => 99,
68 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
69 -msg => $VERSION_MSG,
70 -output => '-'
71 )
72 },
73 'version|v' => sub {
74 pod2usage(
75 -verbose => 0,
76 -msg => $VERSION_MSG,
77 -output => '-'
Akrond3e1d282021-02-24 14:51:27 +010078 );
Akrond949e182020-02-14 12:23:57 +010079 }
Peter Hardersd892a582020-02-12 15:45:22 +010080);
81
Akrond3e1d282021-02-24 14:51:27 +010082
Akronb87c58d2021-02-23 17:23:30 +010083# Establish logger
Akron33db4ec2021-02-24 12:52:21 +010084binmode(STDERR, ':encoding(UTF-8)');
Akron3378dfd2020-08-01 15:01:36 +020085Log::Any::Adapter->set('Stderr', log_level => $log_level);
Akronb3649472020-09-29 08:24:46 +020086$log->notice('Debugging is activated') if DEBUG;
87
Akrond3e1d282021-02-24 14:51:27 +010088
Akron2520a342022-03-29 18:18:05 +020089if ($required_version) {
90 $required_version =~ /^\s*(\d+\.\d+\.\d+)\s*$/;
91 if (!$1 || $1 ne $VERSION) {
92 $log->error("Required version $required_version mismatches version $VERSION");
93 exit(1);
94 };
95};
96
97
Marc Kupietza671ae52022-12-22 16:28:14 +010098my ($what, $with);
99if ($xmlid_to_textsigle ne '') {
100 ($what, $with) = split('@', $xmlid_to_textsigle);
101 $what = qr!$what!;
102};
103
Akron0529e512021-02-22 09:55:35 +0100104# tag (without attributes), which contains the primary text
105my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +0200106# optional
Akron09e0b2c2020-07-28 15:57:01 +0200107
Akron54c3ff12021-02-25 11:33:37 +0100108# Remember to skip certain inline tags
109my %skip_inline_tags = ();
110if ($skip_inline_tags_str) {
111 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
112 $skip_inline_tags{$_} = 1;
113 };
114};
115
Akrond3e1d282021-02-24 14:51:27 +0100116# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200117my $ext_tok;
118if ($tokenizer_call) {
119 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
Akron11484782021-11-03 20:12:14 +0100120 $ext_tok->sentence_splits(1) if $use_tokenizer_sentence_splits;
Akron0c41ab32020-09-29 07:33:33 +0200121}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200122
Akron0c41ab32020-09-29 07:33:33 +0200123elsif ($tokenizer_korap) {
Akronbd4281e2022-03-28 08:31:40 +0200124 eval {
125 require KorAP::XML::TEI::Tokenizer::KorAP;
126 1;
127 };
Akron2520a342022-03-29 18:18:05 +0200128
129 my $korap_tok_ver = $KorAP::XML::TEI::Tokenizer::KorAP::VERSION;
130 if ($korap_tok_ver ne $VERSION) {
131 $log->error("KorAP-Tokenizer version ($korap_tok_ver) differs from the expected version ($VERSION)");
132 exit(1);
133 };
134
Marc Kupietz985da0c2021-02-15 19:29:50 +0100135 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron0c41ab32020-09-29 07:33:33 +0200136};
Peter Harders6f526a32020-06-29 21:44:41 +0200137
Akron11484782021-11-03 20:12:14 +0100138if ($use_tokenizer_sentence_splits) {
139 $skip_inline_tags{s} = 1;
140};
Akron0c41ab32020-09-29 07:33:33 +0200141
Akrond3e1d282021-02-24 14:51:27 +0100142# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100143my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
144my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100145
Peter Harders41c35622020-07-12 01:16:22 +0200146
Akrondd0be8f2021-02-18 19:29:41 +0100147# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100148# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100149my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100150
Akron1a5271a2021-02-18 13:18:15 +0100151# Name of the directory and the file containing all inline token informations
152# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
153my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100154
Akrone2819a12021-10-12 15:52:55 +0200155if (index($_tokens_dir, '!') == 0) {
156 $_tokens_dir = substr($_tokens_dir, 1);
157 $inline_tokens_exclusive = 1;
158};
159
Akronb87c58d2021-02-23 17:23:30 +0100160# Initialize zipper
Akrond53913c2021-02-24 09:50:13 +0100161my $zipper = KorAP::XML::TEI::Zipper->new($root_dir);
Akron09e0b2c2020-07-28 15:57:01 +0200162
Akronbc899192021-02-24 12:14:47 +0100163# text directory (below $root_dir)
164my $dir = '';
Akron09e0b2c2020-07-28 15:57:01 +0200165
Akronbc899192021-02-24 12:14:47 +0100166# Escaped version of text id
167my $text_id_esc;
Peter Harders6f526a32020-06-29 21:44:41 +0200168
Akrond53913c2021-02-24 09:50:13 +0100169# Default encoding of the text
170my $input_enc = 'UTF-8';
171
Akrond53913c2021-02-24 09:50:13 +0100172# text line (needed for whitespace handling)
173my $text_line = 0;
174
Peter Harders6f526a32020-06-29 21:44:41 +0200175
Akrond53913c2021-02-24 09:50:13 +0100176# Input file handle (default: stdin)
Akrona2cb2812021-10-30 10:29:08 +0200177my $input_fh;
Peter Hardersd892a582020-02-12 15:45:22 +0100178
Akrona2cb2812021-10-30 10:29:08 +0200179# Single dash was set
180if ($stdio) {
181 $input_fh = *STDIN;
182}
183
184# Input flag was passed
185elsif ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200186 unless (open($input_fh, '<', $input_fname)) {
187 die $log->fatal("File '$input_fname' could not be opened.");
188 };
Akrona2cb2812021-10-30 10:29:08 +0200189}
190
191# No input to process
192else {
193 pod2usage(
194 -verbose => 99,
195 -sections => 'NAME|SYNOPSIS',
196 -msg => $VERSION_MSG,
197 -output => '-'
198 );
199 exit;
Akrond53913c2021-02-24 09:50:13 +0100200};
Peter Harders6f526a32020-06-29 21:44:41 +0200201
Akronf8088e62021-02-18 16:18:59 +0100202# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200203binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200204
Peter Harders6f526a32020-06-29 21:44:41 +0200205
Akroneb12e232021-02-25 13:49:50 +0100206# Create inline parser object
207my $inline = KorAP::XML::TEI::Inline->new(
208 $skip_inline_tokens,
Akrone2819a12021-10-12 15:52:55 +0200209 \%skip_inline_tags,
210 $inline_tokens_exclusive
Akroneb12e232021-02-25 13:49:50 +0100211);
212
213
Akrond53913c2021-02-24 09:50:13 +0100214# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100215MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200216
Akrond53913c2021-02-24 09:50:13 +0100217 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100218 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200219
Akroneaa96232020-10-15 17:06:15 +0200220 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100221 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200222 $input_enc = $2;
223 next;
224 };
225
226 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100227 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200228
Akrond3e1d282021-02-24 14:51:27 +0100229 # Start of text body
230 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100231 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200232
Akrond53913c2021-02-24 09:50:13 +0100233 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200234 die $log->fatal("input line number $.: " .
235 "line with opening text-body tag '${_TEXT_BODY}' " .
236 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200237 };
Peter Harders6f526a32020-06-29 21:44:41 +0200238
Akrond53913c2021-02-24 09:50:13 +0100239 # Text body data extracted from input document ($input_fh),
240 # further processed by XML::LibXML::Reader
241 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200242
Akron347be812020-09-29 07:52:52 +0200243 # Iterate over all lines in the text body
244 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200245
Akrond3e1d282021-02-24 14:51:27 +0100246 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200247 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100248 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200249
Akrond53913c2021-02-24 09:50:13 +0100250 # End of text body
Akronb43b4912021-02-25 10:31:11 +0100251 if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200252
Akron91705d72021-02-19 10:59:45 +0100253 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200254
Akrond53913c2021-02-24 09:50:13 +0100255 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200256 die $log->fatal("input line number $.: " .
257 "line with closing text-body tag '${_TEXT_BODY}'".
258 " contains additional information ... => Aborting (line=$_)");
259 };
Peter Harders6f526a32020-06-29 21:44:41 +0200260
Akrondafaa7a2021-02-19 15:17:58 +0100261 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100262 $log->warn(
263 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100264 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100265 );
Akrondafaa7a2021-02-19 15:17:58 +0100266 next MAIN;
267 };
Peter Harders6f526a32020-06-29 21:44:41 +0200268
Akroneb12e232021-02-25 13:49:50 +0100269 # Parse inline structure
270 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100271
272 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100273 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100274 };
275
Akroneb12e232021-02-25 13:49:50 +0100276 my $data = $inline->data;
277
Akrond53913c2021-02-24 09:50:13 +0100278 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100279 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100280 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100281 $text_id_esc
282 );
283
Akrond53913c2021-02-24 09:50:13 +0100284 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100285 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100286
287 # Tokenize and output
288 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100289 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100290 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100291 );
Akrond53ab4b2021-02-24 09:56:12 +0100292
293 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100294 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100295 };
Akrondafaa7a2021-02-19 15:17:58 +0100296 };
Peter Harders6f526a32020-06-29 21:44:41 +0200297
Akrond53913c2021-02-24 09:50:13 +0100298 # Tokenize with internal tokenizer
299 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200300
Akrondafaa7a2021-02-19 15:17:58 +0100301 # Tokenize and output
302 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100303 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200304 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100305 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200306
Akrondafaa7a2021-02-19 15:17:58 +0100307 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100308 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100309 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100310 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100311 };
Akrona10ad592020-08-03 11:20:23 +0200312
Akrondafaa7a2021-02-19 15:17:58 +0100313 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100314 if (!$inline->structures->empty) {
315 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100316 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100317 $text_id_esc,
318 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100319 );
Akrondafaa7a2021-02-19 15:17:58 +0100320 };
321
322 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100323 unless ($skip_inline_tokens || $inline->tokens->empty) {
324 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100325 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100326 $text_id_esc,
Akron692d17d2021-03-05 13:21:03 +0100327 # Either 0 = tokens without inline or 1 = tokens with inline
328 !$skip_inline_token_annotations
Akroneb12e232021-02-25 13:49:50 +0100329 );
Akrondafaa7a2021-02-19 15:17:58 +0100330 };
331
332 # reinit.
333 $dir = '';
334
Akron347be812020-09-29 07:52:52 +0200335 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200336 };
337
Peter Harders6f526a32020-06-29 21:44:41 +0200338
Akron347be812020-09-29 07:52:52 +0200339 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200340
Akronf8088e62021-02-18 16:18:59 +0100341 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100342
Akrond53913c2021-02-24 09:50:13 +0100343 # TODO:
344 # Maybe it's best, to keep the stripping of whitespace and
345 # to just remove the if-clause and to insert a blank by default
346 # (with possibly an option on how newlines in primary text should
347 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100348
349 # Remove consecutive whitespace at beginning and end (mostly one newline)
350 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200351
Akrond53913c2021-02-24 09:50:13 +0100352 # NOTE:
353 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200354
Akrond53913c2021-02-24 09:50:13 +0100355 # TODO:
356 # find a better solution, or create a warning, if a text has more
357 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200358
Akrond53913c2021-02-24 09:50:13 +0100359 # TODO:
360 # do testing with 2 different corpora
361 # (one with only one-line texts, the other with several lines per text)
362
363 # line contains at least one tag with at least one character contents
364 if (m/<[^>]+>[^<]/) {
365
366 # Increment counter for text lines
367 $text_line++;
368
369 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100370 # (for 2nd line and consecutive lines)
371 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200372 }
Akronf57ed812020-07-27 10:37:52 +0200373
Akron347be812020-09-29 07:52:52 +0200374 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100375 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200376 };
Akrond3e1d282021-02-24 14:51:27 +0100377 }
Akronf57ed812020-07-27 10:37:52 +0200378
Marc Kupietza671ae52022-12-22 16:28:14 +0100379 elsif (m#^(.*)\<TEI\s+[^>]*?xml:id=(["'])(.+?)\2#) {
380 my $leadin = $1;
381 my $id = $3;
382 my $sigle = $3;
Akronf57ed812020-07-27 10:37:52 +0200383
Marc Kupietza671ae52022-12-22 16:28:14 +0100384 if ($what) {
385 $_ = $id;
386 eval "s|$what|$with|"; # s@ICC.German\.([^.]+\.[^.]+)\.(.+)@ICCGER/$1/$2@;
387 $sigle = $_;
388 $log->debug("Converted text id `$id' to sigle `$sigle'");
389 };
390 $sigle =~ s/\./-/g;
391
392 my @parts = split(/[\/_]/, $sigle);
393 if (@parts != 3) {
394 die $log->fatal(
395 "input line number $.: " .
396 "ids must have exactly three parts split by '/', but `$id` only has " . scalar(@parts) . " ".
397 "=> Aborting (line=$_)");
398 };
399
400 $dir = join("/", @parts);
401 $text_id_esc = "$parts[0]/$parts[1].$parts[2]";
402 $log->notice("$0: text_id=$text_id_esc");
403
404 if ($leadin !~ /^\s*$/) {
405 die $log->fatal(
406 "input line number $.: " .
407 'line with opening header tag is not in expected format ... ' .
408 "=> Aborting (line=$_)");
409 };
410 }
411
412 # Start of header section
413 elsif (m#^(.*)(\<(?:ids|tei)Header.*)$#) {
Akron347be812020-09-29 07:52:52 +0200414 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200415
Akrond20898f2021-02-19 15:52:17 +0100416 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100417 die $log->fatal(
418 "input line number $.: " .
419 'line with opening header tag is not in expected format ... ' .
420 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200421 };
422
423 # Parse header
Marc Kupietza671ae52022-12-22 16:28:14 +0100424 my $header = KorAP::XML::TEI::Header->new($content, $input_enc, $text_id_esc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200425
426 # Header was parseable
427 if ($header) {
428
429 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100430 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200431
Akronb3649472020-09-29 08:24:46 +0200432 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200433
434 $header->to_zip($zipper->new_stream($file));
435
436 # Header is for text level
437 if ($header->type eq 'text') {
438
439 # Remember dir and sigles
440 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200441 $text_id_esc = $header->id_esc;
442
443 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100444 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200445
Akrond53913c2021-02-24 09:50:13 +0100446 # Reset counter for text lines
447 # (needed for whitespace handling)
448 $text_line = 0;
449 };
450 };
451 };
452};
Peter Hardersd892a582020-02-12 15:45:22 +0100453
Akron347be812020-09-29 07:52:52 +0200454$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200455
Akron9df4a242021-02-19 15:31:16 +0100456$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100457
Akrond53913c2021-02-24 09:50:13 +0100458close $input_fh;
459
Peter Harders6f526a32020-06-29 21:44:41 +0200460
Akrond949e182020-02-14 12:23:57 +0100461__END__
462
463=pod
464
465=encoding utf8
466
467=head1 NAME
468
469tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
470
471=head1 SYNOPSIS
472
Akrona2cb2812021-10-30 10:29:08 +0200473 cat corpus.i5.xml | tei2korapxml - > corpus.korapxml.zip
Akrond949e182020-02-14 12:23:57 +0100474
475=head1 DESCRIPTION
476
Akronee434b12020-07-08 12:53:01 +0200477C<tei2korapxml> is a script to convert TEI P5 and
Akrond72baca2021-07-23 13:25:32 +0200478L<I5|https://www.ids-mannheim.de/digspra/kl/projekte/korpora/textmodell>
Akronee434b12020-07-08 12:53:01 +0200479based documents to the
480L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
Peter Harders6f526a32020-06-29 21:44:41 +0200481
Akrond949e182020-02-14 12:23:57 +0100482This program is usually called from inside another script.
483
Akronee434b12020-07-08 12:53:01 +0200484=head1 FORMATS
485
486=head2 Input restrictions
487
488=over 2
489
490=item
491
Akronee434b12020-07-08 12:53:01 +0200492TEI P5 formatted input with certain restrictions:
493
494=over 4
495
496=item
497
498B<mandatory>: text-header with integrated textsigle, text-body
499
500=item
501
502B<optional>: corp-header with integrated corpsigle,
503doc-header with integrated docsigle
504
505=back
506
507=item
508
Akron0c41ab32020-09-29 07:33:33 +0200509All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200510newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200511(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200512into blanks between 2 tokens could lead to additional blanks,
513where there should be none (e.g.: punctuation characters like C<,> or
514C<.> should not be seperated from their predecessor token).
Akron8a0c4bf2021-03-16 16:51:21 +0100515(see also code section C<~ whitespace handling ~> in C<script/tei2korapxml>).
Akronee434b12020-07-08 12:53:01 +0200516
Akron940ca6f2021-10-11 12:38:39 +0200517=item
518
519Header types, like C<E<lt>idsHeader [...] type="document" [...] E<gt>>
520need to be defined in the same line as the header tag.
521
Akronee434b12020-07-08 12:53:01 +0200522=back
523
524=head2 Notes on the output
525
526=over 2
527
528=item
529
530zip file output (default on C<stdout>) with utf8 encoded entries
531(which together form the KorAP-XML format)
532
533=back
534
Akrond949e182020-02-14 12:23:57 +0100535=head1 INSTALLATION
536
Marc Kupietze83a4e92021-03-16 20:51:26 +0100537C<tei2korapxml> requires L<libxml2-dev> bindings and L<File::ShareDir::Install> to be installed.
538When these requirements are met, the preferred way to install the script is
Akrond949e182020-02-14 12:23:57 +0100539to use L<cpanm|App::cpanminus>.
540
541 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
542
543In case everything went well, the C<tei2korapxml> tool will
544be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200545
Akrond949e182020-02-14 12:23:57 +0100546Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
547
548=head1 OPTIONS
549
550=over 2
551
Akrona2cb2812021-10-30 10:29:08 +0200552=item B<--input|-i>
553
554The input file to process. If no specific input is defined and a single
555dash C<-> is passed as an argument, data is read from C<STDIN>.
556
557
Akron4e603a52020-07-27 14:23:49 +0200558=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100559
Akron4e603a52020-07-27 14:23:49 +0200560The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100561
562=item B<--help|-h>
563
564Print help information.
565
566=item B<--version|-v>
567
568Print version information.
569
Akron2520a342022-03-29 18:18:05 +0200570=item B<--required-version|-rv>
571
572Version required for conversion. If the script doesn't match
573the version passed, it will exit immediately.
574
Akron4e603a52020-07-27 14:23:49 +0200575=item B<--tokenizer-call|-tc>
576
577Call an external tokenizer process, that will tokenize
Akron11484782021-11-03 20:12:14 +0100578from STDIN and outputs the offsets of all tokens.
579
580Texts are separated using C<\x04\n>. The external process
581should add a new line per text.
582
583If the L</--use-tokenizer-sentence-splits> option is activated,
584sentences are marked by offset as well in new lines.
585
586To use L<Datok|https://github.com/KorAP/Datok> including sentence
587splitting, call C<tei2korap> as follows:
588
589 $ cat corpus.i5.xml | tei2korapxml -s \
590 $ -tc 'datok tokenize \
591 $ -t ./tokenizer.matok \
592 $ -p --newline-after-eot --no-sentences \
593 $ --no-tokens --sentence-positions -' - \
594 $ > corpus.korapxml.zip
Akron4e603a52020-07-27 14:23:49 +0200595
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200596=item B<--tokenizer-korap|-tk>
597
598Use the standard KorAP/DeReKo tokenizer.
599
Akron6d7b8e42020-09-29 07:37:41 +0200600=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200601
602Tokenize the data using two embedded tokenizers,
603that will take an I<Aggressive> and a I<conservative>
604approach.
605
Akron75d63142021-02-23 18:40:56 +0100606=item B<--skip-inline-tokens>
607
608Boolean flag indicating that inline tokens should not
609be processed. Defaults to false (meaning inline tokens will be processed).
610
Akron692d17d2021-03-05 13:21:03 +0100611=item B<--skip-inline-token-annotations>
612
613Boolean flag indicating that inline token annotations should not
614be processed. Defaults to true (meaning inline token annotations
615won't be processed).
616
Akronca70a1d2021-02-25 16:21:31 +0100617=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100618
619Expects a comma-separated list of tags to be ignored when the structure
620is parsed. Content of these tags however will be processed.
621
Marc Kupietza671ae52022-12-22 16:28:14 +0100622=item B<--xmlid-to-textsigle> <from-regex>@<to-c/to-d/to-t>
623
624Expects a regular replacement expression (separated by a B<@> between the
625search and the replacement) to convert text id attributes to text sigles
626with three parts (separated by B</>).
627
628Example:
629
630 tei2korapxml \
631 --xmlid-to-textsigle 'ICC.German\.([^.]+\.[^.]+)\.(.+)@ICCGER/$1/$2' \
632 -tk - < t/data/icc_german_sample.p5.xml
633
634Converts text id `ICC.German.DeReKo.WPD17.G11.00238' to
635sigle `ICCGER/DeReKo.WPD17/G11.00238'.
636
Akron1a5271a2021-02-18 13:18:15 +0100637=item B<--inline-tokens> <foundry>#[<file>]
638
639Define the foundry and file (without extension)
640to store inline token information in.
Akron8a0c4bf2021-03-16 16:51:21 +0100641Unless C<--skip-inline-token-annotations> is set,
642this will contain annotations as well.
Akron1a5271a2021-02-18 13:18:15 +0100643Defaults to C<tokens> and C<morpho>.
644
Akrone2819a12021-10-12 15:52:55 +0200645The inline token data will also be stored in the
646inline structures file (see I<--inline-structures>),
647unless the inline token foundry is prepended
648by an B<!> exclamation mark, indicating that inline
649tokens are stored exclusively in the inline tokens
650file.
651
652Example:
653
654 tei2korapxml --inline-tokens '!gingko#morpho' < data.i5.xml > korapxml.zip
655
Akrondd0be8f2021-02-18 19:29:41 +0100656=item B<--inline-structures> <foundry>#[<file>]
657
658Define the foundry and file (without extension)
659to store inline structure information in.
660Defaults to C<struct> and C<structures>.
661
Akron26a71522021-02-19 10:27:37 +0100662=item B<--base-foundry> <foundry>
663
664Define the base foundry to store newly generated
665token information in.
666Defaults to C<base>.
667
668=item B<--data-file> <file>
669
670Define the file (without extension)
671to store primary data information in.
672Defaults to C<data>.
673
674=item B<--header-file> <file>
675
676Define the file name (without extension)
677to store header information on
678the corpus, document, and text level in.
679Defaults to C<header>.
680
Marc Kupietz985da0c2021-02-15 19:29:50 +0100681=item B<--use-tokenizer-sentence-splits|-s>
682
683Replace existing with, or add new, sentence boundary information
Akron11484782021-11-03 20:12:14 +0100684provided by the tokenizer.
685Currently KorAP-tokenizer and certain external tokenizers support
686these boundaries.
Marc Kupietz985da0c2021-02-15 19:29:50 +0100687
Akron91705d72021-02-19 10:59:45 +0100688=item B<--tokens-file> <file>
689
690Define the file (without extension)
691to store generated token information in
692(either from the KorAP tokenizer or an externally called tokenizer).
693Defaults to C<tokens>.
694
Akron3378dfd2020-08-01 15:01:36 +0200695=item B<--log|-l>
696
697Loglevel for I<Log::Any>. Defaults to C<notice>.
698
Akrond949e182020-02-14 12:23:57 +0100699=back
700
Akronb3649472020-09-29 08:24:46 +0200701=head1 ENVIRONMENT VARIABLES
702
703=over 2
704
705=item B<KORAPXMLTEI_DEBUG>
706
707Activate minimal debugging.
708Defaults to C<false>.
709
Akronb3649472020-09-29 08:24:46 +0200710=back
711
Akrond949e182020-02-14 12:23:57 +0100712=head1 COPYRIGHT AND LICENSE
713
Marc Kupietze955ecc2021-02-17 17:42:01 +0100714Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100715
716Author: Peter Harders
717
Akronaabd0952020-09-29 07:35:08 +0200718Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100719
720L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
721Corpus Analysis Platform at the
Akrond72baca2021-07-23 13:25:32 +0200722L<Leibniz Institute for the German Language (IDS)|https://www.ids-mannheim.de/>,
Akrond949e182020-02-14 12:23:57 +0100723member of the
724L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
725
726This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100727L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100728
729=cut
Akronf8088e62021-02-18 16:18:59 +0100730
731# NOTES
732
Akronf8088e62021-02-18 16:18:59 +0100733## Notes on segfault prevention
734
Akron91577922021-02-19 10:32:54 +0100735binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100736(see notes on 'PerlIO layers' in 'man XML::LibXML'),
737removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
738see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
739see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.