blob: c150c047060e89540d780206d25aebfccf08ffd8 [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
Akron132bdeb2024-06-06 14:28:56 +020027our $VERSION = '2.6.0';
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 = ''),
Akron132bdeb2024-06-06 14:28:56 +020047 'output|o=s' => \(my $output_fname = ''),
Akron75d63142021-02-23 18:40:56 +010048 'tokenizer-call|tc=s' => \(my $tokenizer_call),
49 'tokenizer-korap|tk' => \(my $tokenizer_korap),
Akrond53913c2021-02-24 09:50:13 +010050 'tokenizer-internal|ti' => \(my $tokenizer_intern),
Akronb93fabb2023-01-13 12:05:44 +010051 'no-tokenizer' => \(my $no_tokenizer),
Akron75d63142021-02-23 18:40:56 +010052 'use-tokenizer-sentence-splits|s' => \(my $use_tokenizer_sentence_splits),
53 'inline-tokens=s' => \(my $inline_tokens = 'tokens#morpho'),
54 'inline-structures=s' => \(my $inline_structures = 'struct#structure'),
55 'skip-inline-tokens' => \(my $skip_inline_tokens = 0),
Akron692d17d2021-03-05 13:21:03 +010056 'skip-inline-token-annotations' => \(
57 my $skip_inline_token_annotations = ($ENV{KORAPXMLTEI_INLINE} ? 0 : 1)),
Akron54c3ff12021-02-25 11:33:37 +010058 'skip-inline-tags=s' => \(my $skip_inline_tags_str = ''),
Akrond3e1d282021-02-24 14:51:27 +010059 'base-foundry=s' => \(my $base_dir = 'base'),
60 'data-file=s' => \(my $data_file = 'data'),
Akrond53913c2021-02-24 09:50:13 +010061 'header-file=s' => \(my $header_file = 'header'),
62 'tokens-file=s' => \(my $tokens_file = 'tokens'),
Marc Kupietza671ae52022-12-22 16:28:14 +010063 'xmlid-to-textsigle|x=s'=> \(my $xmlid_to_textsigle = ''),
Akrond3e1d282021-02-24 14:51:27 +010064 'log|l=s' => \(my $log_level = 'notice'),
Akron2520a342022-03-29 18:18:05 +020065 'required-version|rv=s' => \(my $required_version),
Akrona2cb2812021-10-30 10:29:08 +020066 '' => \(my $stdio),
Akron75d63142021-02-23 18:40:56 +010067 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010068 pod2usage(
69 -verbose => 99,
70 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
71 -msg => $VERSION_MSG,
72 -output => '-'
73 )
74 },
75 'version|v' => sub {
76 pod2usage(
77 -verbose => 0,
78 -msg => $VERSION_MSG,
79 -output => '-'
Akrond3e1d282021-02-24 14:51:27 +010080 );
Akrond949e182020-02-14 12:23:57 +010081 }
Peter Hardersd892a582020-02-12 15:45:22 +010082);
83
Akrond3e1d282021-02-24 14:51:27 +010084
Akronb87c58d2021-02-23 17:23:30 +010085# Establish logger
Akron33db4ec2021-02-24 12:52:21 +010086binmode(STDERR, ':encoding(UTF-8)');
Akron3378dfd2020-08-01 15:01:36 +020087Log::Any::Adapter->set('Stderr', log_level => $log_level);
Akronb3649472020-09-29 08:24:46 +020088$log->notice('Debugging is activated') if DEBUG;
89
Akrond3e1d282021-02-24 14:51:27 +010090
Akron2520a342022-03-29 18:18:05 +020091if ($required_version) {
Marc Kupietz2475c952024-01-09 10:40:04 +010092 $required_version =~ /^\s*(\d+\.\d+\.\d+(-TRIAL)?)\s*$/;
Akron2520a342022-03-29 18:18:05 +020093 if (!$1 || $1 ne $VERSION) {
94 $log->error("Required version $required_version mismatches version $VERSION");
95 exit(1);
96 };
97};
98
99
Marc Kupietza671ae52022-12-22 16:28:14 +0100100my ($what, $with);
101if ($xmlid_to_textsigle ne '') {
102 ($what, $with) = split('@', $xmlid_to_textsigle);
103 $what = qr!$what!;
104};
105
Akron0529e512021-02-22 09:55:35 +0100106# tag (without attributes), which contains the primary text
107my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +0200108# optional
Akron09e0b2c2020-07-28 15:57:01 +0200109
Akron54c3ff12021-02-25 11:33:37 +0100110# Remember to skip certain inline tags
111my %skip_inline_tags = ();
112if ($skip_inline_tags_str) {
113 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
114 $skip_inline_tags{$_} = 1;
115 };
116};
117
Akrond3e1d282021-02-24 14:51:27 +0100118# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200119my $ext_tok;
120if ($tokenizer_call) {
121 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
Akron11484782021-11-03 20:12:14 +0100122 $ext_tok->sentence_splits(1) if $use_tokenizer_sentence_splits;
Akron0c41ab32020-09-29 07:33:33 +0200123}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200124
Akronb93fabb2023-01-13 12:05:44 +0100125# KorAP tokenization
Akron0c41ab32020-09-29 07:33:33 +0200126elsif ($tokenizer_korap) {
Akronbd4281e2022-03-28 08:31:40 +0200127 eval {
128 require KorAP::XML::TEI::Tokenizer::KorAP;
129 1;
130 };
Akron2520a342022-03-29 18:18:05 +0200131
132 my $korap_tok_ver = $KorAP::XML::TEI::Tokenizer::KorAP::VERSION;
133 if ($korap_tok_ver ne $VERSION) {
134 $log->error("KorAP-Tokenizer version ($korap_tok_ver) differs from the expected version ($VERSION)");
135 exit(1);
136 };
137
Marc Kupietz985da0c2021-02-15 19:29:50 +0100138 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akronb93fabb2023-01-13 12:05:44 +0100139}
140
141# No internal tokenizer chosen
142elsif (!$tokenizer_intern && !$no_tokenizer) {
143 $log->error("No tokenizer chosen. If only internal tokens should be used, pass the --no-tokenizer flag");
144 exit(1);
Akron0c41ab32020-09-29 07:33:33 +0200145};
Peter Harders6f526a32020-06-29 21:44:41 +0200146
Akron11484782021-11-03 20:12:14 +0100147if ($use_tokenizer_sentence_splits) {
148 $skip_inline_tags{s} = 1;
149};
Akron0c41ab32020-09-29 07:33:33 +0200150
Akrond3e1d282021-02-24 14:51:27 +0100151# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100152my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
153my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100154
Peter Harders41c35622020-07-12 01:16:22 +0200155
Akrondd0be8f2021-02-18 19:29:41 +0100156# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100157# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100158my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100159
Akron1a5271a2021-02-18 13:18:15 +0100160# Name of the directory and the file containing all inline token informations
161# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
162my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100163
Akrone2819a12021-10-12 15:52:55 +0200164if (index($_tokens_dir, '!') == 0) {
165 $_tokens_dir = substr($_tokens_dir, 1);
166 $inline_tokens_exclusive = 1;
167};
168
Akronb87c58d2021-02-23 17:23:30 +0100169# Initialize zipper
Akron132bdeb2024-06-06 14:28:56 +0200170my $zipper = KorAP::XML::TEI::Zipper->new($root_dir, $output_fname);
Akron09e0b2c2020-07-28 15:57:01 +0200171
Akronbc899192021-02-24 12:14:47 +0100172# text directory (below $root_dir)
173my $dir = '';
Akron09e0b2c2020-07-28 15:57:01 +0200174
Akronbc899192021-02-24 12:14:47 +0100175# Escaped version of text id
176my $text_id_esc;
Peter Harders6f526a32020-06-29 21:44:41 +0200177
Akrond53913c2021-02-24 09:50:13 +0100178# Default encoding of the text
179my $input_enc = 'UTF-8';
180
Akrond53913c2021-02-24 09:50:13 +0100181# text line (needed for whitespace handling)
182my $text_line = 0;
183
Peter Harders6f526a32020-06-29 21:44:41 +0200184
Akrond53913c2021-02-24 09:50:13 +0100185# Input file handle (default: stdin)
Akrona2cb2812021-10-30 10:29:08 +0200186my $input_fh;
Peter Hardersd892a582020-02-12 15:45:22 +0100187
Akrona2cb2812021-10-30 10:29:08 +0200188# Single dash was set
189if ($stdio) {
190 $input_fh = *STDIN;
191}
192
193# Input flag was passed
194elsif ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200195 unless (open($input_fh, '<', $input_fname)) {
196 die $log->fatal("File '$input_fname' could not be opened.");
197 };
Akrona2cb2812021-10-30 10:29:08 +0200198}
199
200# No input to process
201else {
202 pod2usage(
203 -verbose => 99,
204 -sections => 'NAME|SYNOPSIS',
205 -msg => $VERSION_MSG,
206 -output => '-'
207 );
208 exit;
Akrond53913c2021-02-24 09:50:13 +0100209};
Peter Harders6f526a32020-06-29 21:44:41 +0200210
Akronf8088e62021-02-18 16:18:59 +0100211# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200212binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200213
Peter Harders6f526a32020-06-29 21:44:41 +0200214
Akroneb12e232021-02-25 13:49:50 +0100215# Create inline parser object
216my $inline = KorAP::XML::TEI::Inline->new(
217 $skip_inline_tokens,
Akrone2819a12021-10-12 15:52:55 +0200218 \%skip_inline_tags,
219 $inline_tokens_exclusive
Akroneb12e232021-02-25 13:49:50 +0100220);
221
222
Akrond53913c2021-02-24 09:50:13 +0100223# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100224MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200225
Akrond53913c2021-02-24 09:50:13 +0100226 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100227 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200228
Akroneaa96232020-10-15 17:06:15 +0200229 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100230 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200231 $input_enc = $2;
232 next;
233 };
234
235 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100236 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200237
Akrond3e1d282021-02-24 14:51:27 +0100238 # Start of text body
239 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100240 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200241
Akrond53913c2021-02-24 09:50:13 +0100242 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200243 die $log->fatal("input line number $.: " .
244 "line with opening text-body tag '${_TEXT_BODY}' " .
245 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200246 };
Peter Harders6f526a32020-06-29 21:44:41 +0200247
Akrond53913c2021-02-24 09:50:13 +0100248 # Text body data extracted from input document ($input_fh),
249 # further processed by XML::LibXML::Reader
250 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200251
Akron347be812020-09-29 07:52:52 +0200252 # Iterate over all lines in the text body
253 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200254
Akrond3e1d282021-02-24 14:51:27 +0100255 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200256 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100257 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200258
Akrond53913c2021-02-24 09:50:13 +0100259 # End of text body
Akron72f4a882023-03-02 09:48:14 +0100260 if ((my $pos = index($_, "</$_TEXT_BODY>")) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200261
Akron91705d72021-02-19 10:59:45 +0100262 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200263
Akrond53913c2021-02-24 09:50:13 +0100264 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200265 die $log->fatal("input line number $.: " .
266 "line with closing text-body tag '${_TEXT_BODY}'".
267 " contains additional information ... => Aborting (line=$_)");
268 };
Peter Harders6f526a32020-06-29 21:44:41 +0200269
Akrondafaa7a2021-02-19 15:17:58 +0100270 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100271 $log->warn(
272 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100273 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100274 );
Akrondafaa7a2021-02-19 15:17:58 +0100275 next MAIN;
276 };
Peter Harders6f526a32020-06-29 21:44:41 +0200277
Akroneb12e232021-02-25 13:49:50 +0100278 # Parse inline structure
279 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100280
281 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100282 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100283 };
284
Akroneb12e232021-02-25 13:49:50 +0100285 my $data = $inline->data;
286
Akrond53913c2021-02-24 09:50:13 +0100287 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100288 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100289 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100290 $text_id_esc
291 );
292
Akrond53913c2021-02-24 09:50:13 +0100293 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100294 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100295
296 # Tokenize and output
297 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100298 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100299 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100300 );
Akrond53ab4b2021-02-24 09:56:12 +0100301
302 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100303 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100304 };
Akrondafaa7a2021-02-19 15:17:58 +0100305 };
Peter Harders6f526a32020-06-29 21:44:41 +0200306
Akrond53913c2021-02-24 09:50:13 +0100307 # Tokenize with internal tokenizer
308 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200309
Akrondafaa7a2021-02-19 15:17:58 +0100310 # Tokenize and output
311 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100312 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200313 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100314 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200315
Akrondafaa7a2021-02-19 15:17:58 +0100316 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100317 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100318 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100319 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100320 };
Akrona10ad592020-08-03 11:20:23 +0200321
Akrondafaa7a2021-02-19 15:17:58 +0100322 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100323 if (!$inline->structures->empty) {
324 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100325 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100326 $text_id_esc,
327 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100328 );
Akrondafaa7a2021-02-19 15:17:58 +0100329 };
330
331 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100332 unless ($skip_inline_tokens || $inline->tokens->empty) {
333 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100334 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100335 $text_id_esc,
Akron692d17d2021-03-05 13:21:03 +0100336 # Either 0 = tokens without inline or 1 = tokens with inline
337 !$skip_inline_token_annotations
Akroneb12e232021-02-25 13:49:50 +0100338 );
Akrondafaa7a2021-02-19 15:17:58 +0100339 };
340
341 # reinit.
342 $dir = '';
343
Akron347be812020-09-29 07:52:52 +0200344 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200345 };
346
Peter Harders6f526a32020-06-29 21:44:41 +0200347
Akron347be812020-09-29 07:52:52 +0200348 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200349
Akronf8088e62021-02-18 16:18:59 +0100350 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100351
Akrond53913c2021-02-24 09:50:13 +0100352 # TODO:
353 # Maybe it's best, to keep the stripping of whitespace and
354 # to just remove the if-clause and to insert a blank by default
355 # (with possibly an option on how newlines in primary text should
356 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100357
358 # Remove consecutive whitespace at beginning and end (mostly one newline)
359 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200360
Akrond53913c2021-02-24 09:50:13 +0100361 # NOTE:
362 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200363
Akrond53913c2021-02-24 09:50:13 +0100364 # TODO:
365 # find a better solution, or create a warning, if a text has more
366 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200367
Akrond53913c2021-02-24 09:50:13 +0100368 # TODO:
369 # do testing with 2 different corpora
370 # (one with only one-line texts, the other with several lines per text)
371
Akronec503252023-04-24 18:03:17 +0200372 # line contains at least one non-tag character
373 if (m/^[^<]*$/ || m/(?:<[^>]+>[^<])|(?:[^<]<[^>]+>)/) {
Akrond53913c2021-02-24 09:50:13 +0100374
375 # Increment counter for text lines
376 $text_line++;
377
378 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100379 # (for 2nd line and consecutive lines)
380 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200381 }
Akronf57ed812020-07-27 10:37:52 +0200382
Akron347be812020-09-29 07:52:52 +0200383 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100384 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200385 };
Akrond3e1d282021-02-24 14:51:27 +0100386 }
Akronf57ed812020-07-27 10:37:52 +0200387
Marc Kupietza671ae52022-12-22 16:28:14 +0100388 elsif (m#^(.*)\<TEI\s+[^>]*?xml:id=(["'])(.+?)\2#) {
389 my $leadin = $1;
390 my $id = $3;
391 my $sigle = $3;
Akronf57ed812020-07-27 10:37:52 +0200392
Marc Kupietza671ae52022-12-22 16:28:14 +0100393 if ($what) {
394 $_ = $id;
395 eval "s|$what|$with|"; # s@ICC.German\.([^.]+\.[^.]+)\.(.+)@ICCGER/$1/$2@;
396 $sigle = $_;
397 $log->debug("Converted text id `$id' to sigle `$sigle'");
398 };
399 $sigle =~ s/\./-/g;
400
401 my @parts = split(/[\/_]/, $sigle);
402 if (@parts != 3) {
403 die $log->fatal(
404 "input line number $.: " .
405 "ids must have exactly three parts split by '/', but `$id` only has " . scalar(@parts) . " ".
406 "=> Aborting (line=$_)");
407 };
408
409 $dir = join("/", @parts);
410 $text_id_esc = "$parts[0]/$parts[1].$parts[2]";
411 $log->notice("$0: text_id=$text_id_esc");
412
413 if ($leadin !~ /^\s*$/) {
414 die $log->fatal(
415 "input line number $.: " .
416 'line with opening header tag is not in expected format ... ' .
417 "=> Aborting (line=$_)");
418 };
419 }
420
421 # Start of header section
422 elsif (m#^(.*)(\<(?:ids|tei)Header.*)$#) {
Akron347be812020-09-29 07:52:52 +0200423 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200424
Akrond20898f2021-02-19 15:52:17 +0100425 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100426 die $log->fatal(
427 "input line number $.: " .
428 'line with opening header tag is not in expected format ... ' .
429 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200430 };
431
432 # Parse header
Marc Kupietza671ae52022-12-22 16:28:14 +0100433 my $header = KorAP::XML::TEI::Header->new($content, $input_enc, $text_id_esc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200434
435 # Header was parseable
436 if ($header) {
437
438 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100439 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200440
Akronb3649472020-09-29 08:24:46 +0200441 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200442
443 $header->to_zip($zipper->new_stream($file));
444
445 # Header is for text level
446 if ($header->type eq 'text') {
447
448 # Remember dir and sigles
449 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200450 $text_id_esc = $header->id_esc;
451
452 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100453 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200454
Akrond53913c2021-02-24 09:50:13 +0100455 # Reset counter for text lines
456 # (needed for whitespace handling)
457 $text_line = 0;
458 };
459 };
460 };
461};
Peter Hardersd892a582020-02-12 15:45:22 +0100462
Akron347be812020-09-29 07:52:52 +0200463$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200464
Akron9df4a242021-02-19 15:31:16 +0100465$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100466
Akrond53913c2021-02-24 09:50:13 +0100467close $input_fh;
468
Peter Harders6f526a32020-06-29 21:44:41 +0200469
Akrond949e182020-02-14 12:23:57 +0100470__END__
471
472=pod
473
474=encoding utf8
475
476=head1 NAME
477
478tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
479
480=head1 SYNOPSIS
481
Akrona2cb2812021-10-30 10:29:08 +0200482 cat corpus.i5.xml | tei2korapxml - > corpus.korapxml.zip
Akrond949e182020-02-14 12:23:57 +0100483
484=head1 DESCRIPTION
485
Akronee434b12020-07-08 12:53:01 +0200486C<tei2korapxml> is a script to convert TEI P5 and
Akrond72baca2021-07-23 13:25:32 +0200487L<I5|https://www.ids-mannheim.de/digspra/kl/projekte/korpora/textmodell>
Akronee434b12020-07-08 12:53:01 +0200488based documents to the
489L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
Peter Harders6f526a32020-06-29 21:44:41 +0200490
Akrond949e182020-02-14 12:23:57 +0100491This program is usually called from inside another script.
492
Akronee434b12020-07-08 12:53:01 +0200493=head1 FORMATS
494
495=head2 Input restrictions
496
497=over 2
498
499=item
500
Akronee434b12020-07-08 12:53:01 +0200501TEI P5 formatted input with certain restrictions:
502
503=over 4
504
505=item
506
Akrone48bec42023-01-05 12:18:45 +0100507B<mandatory>: text-header with integrated textsigle
508(or convertable identifier), text-body
Akronee434b12020-07-08 12:53:01 +0200509
510=item
511
512B<optional>: corp-header with integrated corpsigle,
513doc-header with integrated docsigle
514
515=back
516
517=item
518
Akron0c41ab32020-09-29 07:33:33 +0200519All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200520newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200521(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200522into blanks between 2 tokens could lead to additional blanks,
523where there should be none (e.g.: punctuation characters like C<,> or
524C<.> should not be seperated from their predecessor token).
Akron8a0c4bf2021-03-16 16:51:21 +0100525(see also code section C<~ whitespace handling ~> in C<script/tei2korapxml>).
Akronee434b12020-07-08 12:53:01 +0200526
Akron940ca6f2021-10-11 12:38:39 +0200527=item
528
529Header types, like C<E<lt>idsHeader [...] type="document" [...] E<gt>>
530need to be defined in the same line as the header tag.
531
Akronee434b12020-07-08 12:53:01 +0200532=back
533
534=head2 Notes on the output
535
536=over 2
537
538=item
539
540zip file output (default on C<stdout>) with utf8 encoded entries
541(which together form the KorAP-XML format)
542
543=back
544
Akrond949e182020-02-14 12:23:57 +0100545=head1 INSTALLATION
546
Akrond26319b2023-01-12 15:34:41 +0100547C<tei2korapxml> requires C<libxml2-dev> bindings and L<File::ShareDir::Install> to be installed.
Marc Kupietze83a4e92021-03-16 20:51:26 +0100548When these requirements are met, the preferred way to install the script is
Akrond949e182020-02-14 12:23:57 +0100549to use L<cpanm|App::cpanminus>.
550
551 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
552
553In case everything went well, the C<tei2korapxml> tool will
554be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200555
Akrond949e182020-02-14 12:23:57 +0100556Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
557
558=head1 OPTIONS
559
560=over 2
561
Akrona2cb2812021-10-30 10:29:08 +0200562=item B<--input|-i>
563
564The input file to process. If no specific input is defined and a single
565dash C<-> is passed as an argument, data is read from C<STDIN>.
566
Akron132bdeb2024-06-06 14:28:56 +0200567=item B<--output|-o>
568
569The output zip file to be created. If no specific output is defined,
570data is written to C<STDOUT>.
Akrona2cb2812021-10-30 10:29:08 +0200571
Akron4e603a52020-07-27 14:23:49 +0200572=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100573
Akron4e603a52020-07-27 14:23:49 +0200574The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100575
576=item B<--help|-h>
577
578Print help information.
579
580=item B<--version|-v>
581
582Print version information.
583
Akrone48bec42023-01-05 12:18:45 +0100584=item B<--tokenizer-korap|-tk>
Akron2520a342022-03-29 18:18:05 +0200585
Akrone48bec42023-01-05 12:18:45 +0100586Use the standard KorAP/DeReKo tokenizer.
587
588=item B<--tokenizer-internal|-ti>
589
590Tokenize the data using two embedded tokenizers,
591that will take an I<aggressive> and a I<conservative>
592approach.
Akron2520a342022-03-29 18:18:05 +0200593
Akron4e603a52020-07-27 14:23:49 +0200594=item B<--tokenizer-call|-tc>
595
596Call an external tokenizer process, that will tokenize
Akron11484782021-11-03 20:12:14 +0100597from STDIN and outputs the offsets of all tokens.
598
599Texts are separated using C<\x04\n>. The external process
600should add a new line per text.
601
602If the L</--use-tokenizer-sentence-splits> option is activated,
603sentences are marked by offset as well in new lines.
604
605To use L<Datok|https://github.com/KorAP/Datok> including sentence
606splitting, call C<tei2korap> as follows:
607
608 $ cat corpus.i5.xml | tei2korapxml -s \
609 $ -tc 'datok tokenize \
610 $ -t ./tokenizer.matok \
611 $ -p --newline-after-eot --no-sentences \
612 $ --no-tokens --sentence-positions -' - \
613 $ > corpus.korapxml.zip
Akron4e603a52020-07-27 14:23:49 +0200614
Akronb93fabb2023-01-13 12:05:44 +0100615=item B<--no-tokenizer>
616
617Boolean flag indicating that no tokenizer should be used.
618This is meant to ensure that by default a final token layer always
619exists.
620If a separate tokenizer is chosen, this flag is ignored.
621
Akron75d63142021-02-23 18:40:56 +0100622=item B<--skip-inline-tokens>
623
624Boolean flag indicating that inline tokens should not
625be processed. Defaults to false (meaning inline tokens will be processed).
626
Akron692d17d2021-03-05 13:21:03 +0100627=item B<--skip-inline-token-annotations>
628
629Boolean flag indicating that inline token annotations should not
630be processed. Defaults to true (meaning inline token annotations
631won't be processed).
632
Akronca70a1d2021-02-25 16:21:31 +0100633=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100634
635Expects a comma-separated list of tags to be ignored when the structure
636is parsed. Content of these tags however will be processed.
637
Marc Kupietza671ae52022-12-22 16:28:14 +0100638=item B<--xmlid-to-textsigle> <from-regex>@<to-c/to-d/to-t>
639
Akrone48bec42023-01-05 12:18:45 +0100640Expects a regular replacement expression (separated by B<@> between the
Marc Kupietza671ae52022-12-22 16:28:14 +0100641search and the replacement) to convert text id attributes to text sigles
642with three parts (separated by B</>).
643
644Example:
645
646 tei2korapxml \
647 --xmlid-to-textsigle 'ICC.German\.([^.]+\.[^.]+)\.(.+)@ICCGER/$1/$2' \
648 -tk - < t/data/icc_german_sample.p5.xml
649
Akrone48bec42023-01-05 12:18:45 +0100650Converts text id C<ICC.German.DeReKo.WPD17.G11.00238> to
651sigle C<ICCGER/DeReKo.WPD17/G11.00238>.
Marc Kupietza671ae52022-12-22 16:28:14 +0100652
Akron1a5271a2021-02-18 13:18:15 +0100653=item B<--inline-tokens> <foundry>#[<file>]
654
655Define the foundry and file (without extension)
656to store inline token information in.
Akron8a0c4bf2021-03-16 16:51:21 +0100657Unless C<--skip-inline-token-annotations> is set,
658this will contain annotations as well.
Akron1a5271a2021-02-18 13:18:15 +0100659Defaults to C<tokens> and C<morpho>.
660
Akrone2819a12021-10-12 15:52:55 +0200661The inline token data will also be stored in the
662inline structures file (see I<--inline-structures>),
663unless the inline token foundry is prepended
664by an B<!> exclamation mark, indicating that inline
665tokens are stored exclusively in the inline tokens
666file.
667
668Example:
669
670 tei2korapxml --inline-tokens '!gingko#morpho' < data.i5.xml > korapxml.zip
671
Akrondd0be8f2021-02-18 19:29:41 +0100672=item B<--inline-structures> <foundry>#[<file>]
673
674Define the foundry and file (without extension)
675to store inline structure information in.
676Defaults to C<struct> and C<structures>.
677
Akron26a71522021-02-19 10:27:37 +0100678=item B<--base-foundry> <foundry>
679
680Define the base foundry to store newly generated
681token information in.
682Defaults to C<base>.
683
684=item B<--data-file> <file>
685
686Define the file (without extension)
687to store primary data information in.
688Defaults to C<data>.
689
690=item B<--header-file> <file>
691
692Define the file name (without extension)
693to store header information on
694the corpus, document, and text level in.
695Defaults to C<header>.
696
Marc Kupietz985da0c2021-02-15 19:29:50 +0100697=item B<--use-tokenizer-sentence-splits|-s>
698
699Replace existing with, or add new, sentence boundary information
Akron11484782021-11-03 20:12:14 +0100700provided by the tokenizer.
701Currently KorAP-tokenizer and certain external tokenizers support
702these boundaries.
Marc Kupietz985da0c2021-02-15 19:29:50 +0100703
Akron91705d72021-02-19 10:59:45 +0100704=item B<--tokens-file> <file>
705
706Define the file (without extension)
707to store generated token information in
708(either from the KorAP tokenizer or an externally called tokenizer).
709Defaults to C<tokens>.
710
Akron3378dfd2020-08-01 15:01:36 +0200711=item B<--log|-l>
712
713Loglevel for I<Log::Any>. Defaults to C<notice>.
714
Akrond949e182020-02-14 12:23:57 +0100715=back
716
Akronb3649472020-09-29 08:24:46 +0200717=head1 ENVIRONMENT VARIABLES
718
719=over 2
720
721=item B<KORAPXMLTEI_DEBUG>
722
723Activate minimal debugging.
724Defaults to C<false>.
725
Akronb3649472020-09-29 08:24:46 +0200726=back
727
Akrond949e182020-02-14 12:23:57 +0100728=head1 COPYRIGHT AND LICENSE
729
Akrone48bec42023-01-05 12:18:45 +0100730Copyright (C) 2021-2023, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100731
732Author: Peter Harders
733
Akronaabd0952020-09-29 07:35:08 +0200734Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100735
736L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
737Corpus Analysis Platform at the
Akrond72baca2021-07-23 13:25:32 +0200738L<Leibniz Institute for the German Language (IDS)|https://www.ids-mannheim.de/>,
Akrond949e182020-02-14 12:23:57 +0100739member of the
740L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
741
742This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100743L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100744
745=cut
Akronf8088e62021-02-18 16:18:59 +0100746
747# NOTES
748
Akronf8088e62021-02-18 16:18:59 +0100749## Notes on segfault prevention
750
Akron91577922021-02-19 10:32:54 +0100751binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100752(see notes on 'PerlIO layers' in 'man XML::LibXML'),
753removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
754see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
755see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.