blob: 54c5b9fd5fc29513d26168ea2b77a305f7f309a6 [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),
Akronb93fabb2023-01-13 12:05:44 +010050 'no-tokenizer' => \(my $no_tokenizer),
Akron75d63142021-02-23 18:40:56 +010051 'use-tokenizer-sentence-splits|s' => \(my $use_tokenizer_sentence_splits),
52 'inline-tokens=s' => \(my $inline_tokens = 'tokens#morpho'),
53 'inline-structures=s' => \(my $inline_structures = 'struct#structure'),
54 'skip-inline-tokens' => \(my $skip_inline_tokens = 0),
Akron692d17d2021-03-05 13:21:03 +010055 'skip-inline-token-annotations' => \(
56 my $skip_inline_token_annotations = ($ENV{KORAPXMLTEI_INLINE} ? 0 : 1)),
Akron54c3ff12021-02-25 11:33:37 +010057 'skip-inline-tags=s' => \(my $skip_inline_tags_str = ''),
Akrond3e1d282021-02-24 14:51:27 +010058 'base-foundry=s' => \(my $base_dir = 'base'),
59 'data-file=s' => \(my $data_file = 'data'),
Akrond53913c2021-02-24 09:50:13 +010060 'header-file=s' => \(my $header_file = 'header'),
61 'tokens-file=s' => \(my $tokens_file = 'tokens'),
Marc Kupietza671ae52022-12-22 16:28:14 +010062 'xmlid-to-textsigle|x=s'=> \(my $xmlid_to_textsigle = ''),
Akrond3e1d282021-02-24 14:51:27 +010063 'log|l=s' => \(my $log_level = 'notice'),
Akron2520a342022-03-29 18:18:05 +020064 'required-version|rv=s' => \(my $required_version),
Akrona2cb2812021-10-30 10:29:08 +020065 '' => \(my $stdio),
Akron75d63142021-02-23 18:40:56 +010066 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010067 pod2usage(
68 -verbose => 99,
69 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
70 -msg => $VERSION_MSG,
71 -output => '-'
72 )
73 },
74 'version|v' => sub {
75 pod2usage(
76 -verbose => 0,
77 -msg => $VERSION_MSG,
78 -output => '-'
Akrond3e1d282021-02-24 14:51:27 +010079 );
Akrond949e182020-02-14 12:23:57 +010080 }
Peter Hardersd892a582020-02-12 15:45:22 +010081);
82
Akrond3e1d282021-02-24 14:51:27 +010083
Akronb87c58d2021-02-23 17:23:30 +010084# Establish logger
Akron33db4ec2021-02-24 12:52:21 +010085binmode(STDERR, ':encoding(UTF-8)');
Akron3378dfd2020-08-01 15:01:36 +020086Log::Any::Adapter->set('Stderr', log_level => $log_level);
Akronb3649472020-09-29 08:24:46 +020087$log->notice('Debugging is activated') if DEBUG;
88
Akrond3e1d282021-02-24 14:51:27 +010089
Akron2520a342022-03-29 18:18:05 +020090if ($required_version) {
91 $required_version =~ /^\s*(\d+\.\d+\.\d+)\s*$/;
92 if (!$1 || $1 ne $VERSION) {
93 $log->error("Required version $required_version mismatches version $VERSION");
94 exit(1);
95 };
96};
97
98
Marc Kupietza671ae52022-12-22 16:28:14 +010099my ($what, $with);
100if ($xmlid_to_textsigle ne '') {
101 ($what, $with) = split('@', $xmlid_to_textsigle);
102 $what = qr!$what!;
103};
104
Akron0529e512021-02-22 09:55:35 +0100105# tag (without attributes), which contains the primary text
106my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +0200107# optional
Akron09e0b2c2020-07-28 15:57:01 +0200108
Akron54c3ff12021-02-25 11:33:37 +0100109# Remember to skip certain inline tags
110my %skip_inline_tags = ();
111if ($skip_inline_tags_str) {
112 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
113 $skip_inline_tags{$_} = 1;
114 };
115};
116
Akrond3e1d282021-02-24 14:51:27 +0100117# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200118my $ext_tok;
119if ($tokenizer_call) {
120 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
Akron11484782021-11-03 20:12:14 +0100121 $ext_tok->sentence_splits(1) if $use_tokenizer_sentence_splits;
Akron0c41ab32020-09-29 07:33:33 +0200122}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200123
Akronb93fabb2023-01-13 12:05:44 +0100124# KorAP tokenization
Akron0c41ab32020-09-29 07:33:33 +0200125elsif ($tokenizer_korap) {
Akronbd4281e2022-03-28 08:31:40 +0200126 eval {
127 require KorAP::XML::TEI::Tokenizer::KorAP;
128 1;
129 };
Akron2520a342022-03-29 18:18:05 +0200130
131 my $korap_tok_ver = $KorAP::XML::TEI::Tokenizer::KorAP::VERSION;
132 if ($korap_tok_ver ne $VERSION) {
133 $log->error("KorAP-Tokenizer version ($korap_tok_ver) differs from the expected version ($VERSION)");
134 exit(1);
135 };
136
Marc Kupietz985da0c2021-02-15 19:29:50 +0100137 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akronb93fabb2023-01-13 12:05:44 +0100138}
139
140# No internal tokenizer chosen
141elsif (!$tokenizer_intern && !$no_tokenizer) {
142 $log->error("No tokenizer chosen. If only internal tokens should be used, pass the --no-tokenizer flag");
143 exit(1);
Akron0c41ab32020-09-29 07:33:33 +0200144};
Peter Harders6f526a32020-06-29 21:44:41 +0200145
Akron11484782021-11-03 20:12:14 +0100146if ($use_tokenizer_sentence_splits) {
147 $skip_inline_tags{s} = 1;
148};
Akron0c41ab32020-09-29 07:33:33 +0200149
Akrond3e1d282021-02-24 14:51:27 +0100150# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100151my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
152my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100153
Peter Harders41c35622020-07-12 01:16:22 +0200154
Akrondd0be8f2021-02-18 19:29:41 +0100155# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100156# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100157my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100158
Akron1a5271a2021-02-18 13:18:15 +0100159# Name of the directory and the file containing all inline token informations
160# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
161my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100162
Akrone2819a12021-10-12 15:52:55 +0200163if (index($_tokens_dir, '!') == 0) {
164 $_tokens_dir = substr($_tokens_dir, 1);
165 $inline_tokens_exclusive = 1;
166};
167
Akronb87c58d2021-02-23 17:23:30 +0100168# Initialize zipper
Akrond53913c2021-02-24 09:50:13 +0100169my $zipper = KorAP::XML::TEI::Zipper->new($root_dir);
Akron09e0b2c2020-07-28 15:57:01 +0200170
Akronbc899192021-02-24 12:14:47 +0100171# text directory (below $root_dir)
172my $dir = '';
Akron09e0b2c2020-07-28 15:57:01 +0200173
Akronbc899192021-02-24 12:14:47 +0100174# Escaped version of text id
175my $text_id_esc;
Peter Harders6f526a32020-06-29 21:44:41 +0200176
Akrond53913c2021-02-24 09:50:13 +0100177# Default encoding of the text
178my $input_enc = 'UTF-8';
179
Akrond53913c2021-02-24 09:50:13 +0100180# text line (needed for whitespace handling)
181my $text_line = 0;
182
Peter Harders6f526a32020-06-29 21:44:41 +0200183
Akrond53913c2021-02-24 09:50:13 +0100184# Input file handle (default: stdin)
Akrona2cb2812021-10-30 10:29:08 +0200185my $input_fh;
Peter Hardersd892a582020-02-12 15:45:22 +0100186
Akrona2cb2812021-10-30 10:29:08 +0200187# Single dash was set
188if ($stdio) {
189 $input_fh = *STDIN;
190}
191
192# Input flag was passed
193elsif ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200194 unless (open($input_fh, '<', $input_fname)) {
195 die $log->fatal("File '$input_fname' could not be opened.");
196 };
Akrona2cb2812021-10-30 10:29:08 +0200197}
198
199# No input to process
200else {
201 pod2usage(
202 -verbose => 99,
203 -sections => 'NAME|SYNOPSIS',
204 -msg => $VERSION_MSG,
205 -output => '-'
206 );
207 exit;
Akrond53913c2021-02-24 09:50:13 +0100208};
Peter Harders6f526a32020-06-29 21:44:41 +0200209
Akronf8088e62021-02-18 16:18:59 +0100210# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200211binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200212
Peter Harders6f526a32020-06-29 21:44:41 +0200213
Akroneb12e232021-02-25 13:49:50 +0100214# Create inline parser object
215my $inline = KorAP::XML::TEI::Inline->new(
216 $skip_inline_tokens,
Akrone2819a12021-10-12 15:52:55 +0200217 \%skip_inline_tags,
218 $inline_tokens_exclusive
Akroneb12e232021-02-25 13:49:50 +0100219);
220
221
Akrond53913c2021-02-24 09:50:13 +0100222# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100223MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200224
Akrond53913c2021-02-24 09:50:13 +0100225 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100226 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200227
Akroneaa96232020-10-15 17:06:15 +0200228 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100229 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200230 $input_enc = $2;
231 next;
232 };
233
234 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100235 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200236
Akrond3e1d282021-02-24 14:51:27 +0100237 # Start of text body
238 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100239 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200240
Akrond53913c2021-02-24 09:50:13 +0100241 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200242 die $log->fatal("input line number $.: " .
243 "line with opening text-body tag '${_TEXT_BODY}' " .
244 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200245 };
Peter Harders6f526a32020-06-29 21:44:41 +0200246
Akrond53913c2021-02-24 09:50:13 +0100247 # Text body data extracted from input document ($input_fh),
248 # further processed by XML::LibXML::Reader
249 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200250
Akron347be812020-09-29 07:52:52 +0200251 # Iterate over all lines in the text body
252 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200253
Akrond3e1d282021-02-24 14:51:27 +0100254 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200255 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100256 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200257
Akrond53913c2021-02-24 09:50:13 +0100258 # End of text body
Akronb43b4912021-02-25 10:31:11 +0100259 if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200260
Akron91705d72021-02-19 10:59:45 +0100261 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200262
Akrond53913c2021-02-24 09:50:13 +0100263 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200264 die $log->fatal("input line number $.: " .
265 "line with closing text-body tag '${_TEXT_BODY}'".
266 " contains additional information ... => Aborting (line=$_)");
267 };
Peter Harders6f526a32020-06-29 21:44:41 +0200268
Akrondafaa7a2021-02-19 15:17:58 +0100269 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100270 $log->warn(
271 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100272 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100273 );
Akrondafaa7a2021-02-19 15:17:58 +0100274 next MAIN;
275 };
Peter Harders6f526a32020-06-29 21:44:41 +0200276
Akroneb12e232021-02-25 13:49:50 +0100277 # Parse inline structure
278 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100279
280 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100281 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100282 };
283
Akroneb12e232021-02-25 13:49:50 +0100284 my $data = $inline->data;
285
Akrond53913c2021-02-24 09:50:13 +0100286 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100287 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100288 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100289 $text_id_esc
290 );
291
Akrond53913c2021-02-24 09:50:13 +0100292 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100293 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100294
295 # Tokenize and output
296 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100297 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100298 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100299 );
Akrond53ab4b2021-02-24 09:56:12 +0100300
301 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100302 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100303 };
Akrondafaa7a2021-02-19 15:17:58 +0100304 };
Peter Harders6f526a32020-06-29 21:44:41 +0200305
Akrond53913c2021-02-24 09:50:13 +0100306 # Tokenize with internal tokenizer
307 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200308
Akrondafaa7a2021-02-19 15:17:58 +0100309 # Tokenize and output
310 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100311 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200312 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100313 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200314
Akrondafaa7a2021-02-19 15:17:58 +0100315 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100316 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100317 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100318 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100319 };
Akrona10ad592020-08-03 11:20:23 +0200320
Akrondafaa7a2021-02-19 15:17:58 +0100321 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100322 if (!$inline->structures->empty) {
323 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100324 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100325 $text_id_esc,
326 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100327 );
Akrondafaa7a2021-02-19 15:17:58 +0100328 };
329
330 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100331 unless ($skip_inline_tokens || $inline->tokens->empty) {
332 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100333 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100334 $text_id_esc,
Akron692d17d2021-03-05 13:21:03 +0100335 # Either 0 = tokens without inline or 1 = tokens with inline
336 !$skip_inline_token_annotations
Akroneb12e232021-02-25 13:49:50 +0100337 );
Akrondafaa7a2021-02-19 15:17:58 +0100338 };
339
340 # reinit.
341 $dir = '';
342
Akron347be812020-09-29 07:52:52 +0200343 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200344 };
345
Peter Harders6f526a32020-06-29 21:44:41 +0200346
Akron347be812020-09-29 07:52:52 +0200347 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200348
Akronf8088e62021-02-18 16:18:59 +0100349 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100350
Akrond53913c2021-02-24 09:50:13 +0100351 # TODO:
352 # Maybe it's best, to keep the stripping of whitespace and
353 # to just remove the if-clause and to insert a blank by default
354 # (with possibly an option on how newlines in primary text should
355 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100356
357 # Remove consecutive whitespace at beginning and end (mostly one newline)
358 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200359
Akrond53913c2021-02-24 09:50:13 +0100360 # NOTE:
361 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200362
Akrond53913c2021-02-24 09:50:13 +0100363 # TODO:
364 # find a better solution, or create a warning, if a text has more
365 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200366
Akrond53913c2021-02-24 09:50:13 +0100367 # TODO:
368 # do testing with 2 different corpora
369 # (one with only one-line texts, the other with several lines per text)
370
371 # line contains at least one tag with at least one character contents
372 if (m/<[^>]+>[^<]/) {
373
374 # Increment counter for text lines
375 $text_line++;
376
377 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100378 # (for 2nd line and consecutive lines)
379 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200380 }
Akronf57ed812020-07-27 10:37:52 +0200381
Akron347be812020-09-29 07:52:52 +0200382 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100383 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200384 };
Akrond3e1d282021-02-24 14:51:27 +0100385 }
Akronf57ed812020-07-27 10:37:52 +0200386
Marc Kupietza671ae52022-12-22 16:28:14 +0100387 elsif (m#^(.*)\<TEI\s+[^>]*?xml:id=(["'])(.+?)\2#) {
388 my $leadin = $1;
389 my $id = $3;
390 my $sigle = $3;
Akronf57ed812020-07-27 10:37:52 +0200391
Marc Kupietza671ae52022-12-22 16:28:14 +0100392 if ($what) {
393 $_ = $id;
394 eval "s|$what|$with|"; # s@ICC.German\.([^.]+\.[^.]+)\.(.+)@ICCGER/$1/$2@;
395 $sigle = $_;
396 $log->debug("Converted text id `$id' to sigle `$sigle'");
397 };
398 $sigle =~ s/\./-/g;
399
400 my @parts = split(/[\/_]/, $sigle);
401 if (@parts != 3) {
402 die $log->fatal(
403 "input line number $.: " .
404 "ids must have exactly three parts split by '/', but `$id` only has " . scalar(@parts) . " ".
405 "=> Aborting (line=$_)");
406 };
407
408 $dir = join("/", @parts);
409 $text_id_esc = "$parts[0]/$parts[1].$parts[2]";
410 $log->notice("$0: text_id=$text_id_esc");
411
412 if ($leadin !~ /^\s*$/) {
413 die $log->fatal(
414 "input line number $.: " .
415 'line with opening header tag is not in expected format ... ' .
416 "=> Aborting (line=$_)");
417 };
418 }
419
420 # Start of header section
421 elsif (m#^(.*)(\<(?:ids|tei)Header.*)$#) {
Akron347be812020-09-29 07:52:52 +0200422 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200423
Akrond20898f2021-02-19 15:52:17 +0100424 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100425 die $log->fatal(
426 "input line number $.: " .
427 'line with opening header tag is not in expected format ... ' .
428 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200429 };
430
431 # Parse header
Marc Kupietza671ae52022-12-22 16:28:14 +0100432 my $header = KorAP::XML::TEI::Header->new($content, $input_enc, $text_id_esc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200433
434 # Header was parseable
435 if ($header) {
436
437 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100438 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200439
Akronb3649472020-09-29 08:24:46 +0200440 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200441
442 $header->to_zip($zipper->new_stream($file));
443
444 # Header is for text level
445 if ($header->type eq 'text') {
446
447 # Remember dir and sigles
448 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200449 $text_id_esc = $header->id_esc;
450
451 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100452 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200453
Akrond53913c2021-02-24 09:50:13 +0100454 # Reset counter for text lines
455 # (needed for whitespace handling)
456 $text_line = 0;
457 };
458 };
459 };
460};
Peter Hardersd892a582020-02-12 15:45:22 +0100461
Akron347be812020-09-29 07:52:52 +0200462$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200463
Akron9df4a242021-02-19 15:31:16 +0100464$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100465
Akrond53913c2021-02-24 09:50:13 +0100466close $input_fh;
467
Peter Harders6f526a32020-06-29 21:44:41 +0200468
Akrond949e182020-02-14 12:23:57 +0100469__END__
470
471=pod
472
473=encoding utf8
474
475=head1 NAME
476
477tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
478
479=head1 SYNOPSIS
480
Akrona2cb2812021-10-30 10:29:08 +0200481 cat corpus.i5.xml | tei2korapxml - > corpus.korapxml.zip
Akrond949e182020-02-14 12:23:57 +0100482
483=head1 DESCRIPTION
484
Akronee434b12020-07-08 12:53:01 +0200485C<tei2korapxml> is a script to convert TEI P5 and
Akrond72baca2021-07-23 13:25:32 +0200486L<I5|https://www.ids-mannheim.de/digspra/kl/projekte/korpora/textmodell>
Akronee434b12020-07-08 12:53:01 +0200487based documents to the
488L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
Peter Harders6f526a32020-06-29 21:44:41 +0200489
Akrond949e182020-02-14 12:23:57 +0100490This program is usually called from inside another script.
491
Akronee434b12020-07-08 12:53:01 +0200492=head1 FORMATS
493
494=head2 Input restrictions
495
496=over 2
497
498=item
499
Akronee434b12020-07-08 12:53:01 +0200500TEI P5 formatted input with certain restrictions:
501
502=over 4
503
504=item
505
Akrone48bec42023-01-05 12:18:45 +0100506B<mandatory>: text-header with integrated textsigle
507(or convertable identifier), text-body
Akronee434b12020-07-08 12:53:01 +0200508
509=item
510
511B<optional>: corp-header with integrated corpsigle,
512doc-header with integrated docsigle
513
514=back
515
516=item
517
Akron0c41ab32020-09-29 07:33:33 +0200518All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200519newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200520(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200521into blanks between 2 tokens could lead to additional blanks,
522where there should be none (e.g.: punctuation characters like C<,> or
523C<.> should not be seperated from their predecessor token).
Akron8a0c4bf2021-03-16 16:51:21 +0100524(see also code section C<~ whitespace handling ~> in C<script/tei2korapxml>).
Akronee434b12020-07-08 12:53:01 +0200525
Akron940ca6f2021-10-11 12:38:39 +0200526=item
527
528Header types, like C<E<lt>idsHeader [...] type="document" [...] E<gt>>
529need to be defined in the same line as the header tag.
530
Akronee434b12020-07-08 12:53:01 +0200531=back
532
533=head2 Notes on the output
534
535=over 2
536
537=item
538
539zip file output (default on C<stdout>) with utf8 encoded entries
540(which together form the KorAP-XML format)
541
542=back
543
Akrond949e182020-02-14 12:23:57 +0100544=head1 INSTALLATION
545
Akrond26319b2023-01-12 15:34:41 +0100546C<tei2korapxml> requires C<libxml2-dev> bindings and L<File::ShareDir::Install> to be installed.
Marc Kupietze83a4e92021-03-16 20:51:26 +0100547When these requirements are met, the preferred way to install the script is
Akrond949e182020-02-14 12:23:57 +0100548to use L<cpanm|App::cpanminus>.
549
550 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
551
552In case everything went well, the C<tei2korapxml> tool will
553be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200554
Akrond949e182020-02-14 12:23:57 +0100555Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
556
557=head1 OPTIONS
558
559=over 2
560
Akrona2cb2812021-10-30 10:29:08 +0200561=item B<--input|-i>
562
563The input file to process. If no specific input is defined and a single
564dash C<-> is passed as an argument, data is read from C<STDIN>.
565
566
Akron4e603a52020-07-27 14:23:49 +0200567=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100568
Akron4e603a52020-07-27 14:23:49 +0200569The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100570
571=item B<--help|-h>
572
573Print help information.
574
575=item B<--version|-v>
576
577Print version information.
578
Akrone48bec42023-01-05 12:18:45 +0100579=item B<--tokenizer-korap|-tk>
Akron2520a342022-03-29 18:18:05 +0200580
Akrone48bec42023-01-05 12:18:45 +0100581Use the standard KorAP/DeReKo tokenizer.
582
583=item B<--tokenizer-internal|-ti>
584
585Tokenize the data using two embedded tokenizers,
586that will take an I<aggressive> and a I<conservative>
587approach.
Akron2520a342022-03-29 18:18:05 +0200588
Akron4e603a52020-07-27 14:23:49 +0200589=item B<--tokenizer-call|-tc>
590
591Call an external tokenizer process, that will tokenize
Akron11484782021-11-03 20:12:14 +0100592from STDIN and outputs the offsets of all tokens.
593
594Texts are separated using C<\x04\n>. The external process
595should add a new line per text.
596
597If the L</--use-tokenizer-sentence-splits> option is activated,
598sentences are marked by offset as well in new lines.
599
600To use L<Datok|https://github.com/KorAP/Datok> including sentence
601splitting, call C<tei2korap> as follows:
602
603 $ cat corpus.i5.xml | tei2korapxml -s \
604 $ -tc 'datok tokenize \
605 $ -t ./tokenizer.matok \
606 $ -p --newline-after-eot --no-sentences \
607 $ --no-tokens --sentence-positions -' - \
608 $ > corpus.korapxml.zip
Akron4e603a52020-07-27 14:23:49 +0200609
Akronb93fabb2023-01-13 12:05:44 +0100610=item B<--no-tokenizer>
611
612Boolean flag indicating that no tokenizer should be used.
613This is meant to ensure that by default a final token layer always
614exists.
615If a separate tokenizer is chosen, this flag is ignored.
616
Akron75d63142021-02-23 18:40:56 +0100617=item B<--skip-inline-tokens>
618
619Boolean flag indicating that inline tokens should not
620be processed. Defaults to false (meaning inline tokens will be processed).
621
Akron692d17d2021-03-05 13:21:03 +0100622=item B<--skip-inline-token-annotations>
623
624Boolean flag indicating that inline token annotations should not
625be processed. Defaults to true (meaning inline token annotations
626won't be processed).
627
Akronca70a1d2021-02-25 16:21:31 +0100628=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100629
630Expects a comma-separated list of tags to be ignored when the structure
631is parsed. Content of these tags however will be processed.
632
Marc Kupietza671ae52022-12-22 16:28:14 +0100633=item B<--xmlid-to-textsigle> <from-regex>@<to-c/to-d/to-t>
634
Akrone48bec42023-01-05 12:18:45 +0100635Expects a regular replacement expression (separated by B<@> between the
Marc Kupietza671ae52022-12-22 16:28:14 +0100636search and the replacement) to convert text id attributes to text sigles
637with three parts (separated by B</>).
638
639Example:
640
641 tei2korapxml \
642 --xmlid-to-textsigle 'ICC.German\.([^.]+\.[^.]+)\.(.+)@ICCGER/$1/$2' \
643 -tk - < t/data/icc_german_sample.p5.xml
644
Akrone48bec42023-01-05 12:18:45 +0100645Converts text id C<ICC.German.DeReKo.WPD17.G11.00238> to
646sigle C<ICCGER/DeReKo.WPD17/G11.00238>.
Marc Kupietza671ae52022-12-22 16:28:14 +0100647
Akron1a5271a2021-02-18 13:18:15 +0100648=item B<--inline-tokens> <foundry>#[<file>]
649
650Define the foundry and file (without extension)
651to store inline token information in.
Akron8a0c4bf2021-03-16 16:51:21 +0100652Unless C<--skip-inline-token-annotations> is set,
653this will contain annotations as well.
Akron1a5271a2021-02-18 13:18:15 +0100654Defaults to C<tokens> and C<morpho>.
655
Akrone2819a12021-10-12 15:52:55 +0200656The inline token data will also be stored in the
657inline structures file (see I<--inline-structures>),
658unless the inline token foundry is prepended
659by an B<!> exclamation mark, indicating that inline
660tokens are stored exclusively in the inline tokens
661file.
662
663Example:
664
665 tei2korapxml --inline-tokens '!gingko#morpho' < data.i5.xml > korapxml.zip
666
Akrondd0be8f2021-02-18 19:29:41 +0100667=item B<--inline-structures> <foundry>#[<file>]
668
669Define the foundry and file (without extension)
670to store inline structure information in.
671Defaults to C<struct> and C<structures>.
672
Akron26a71522021-02-19 10:27:37 +0100673=item B<--base-foundry> <foundry>
674
675Define the base foundry to store newly generated
676token information in.
677Defaults to C<base>.
678
679=item B<--data-file> <file>
680
681Define the file (without extension)
682to store primary data information in.
683Defaults to C<data>.
684
685=item B<--header-file> <file>
686
687Define the file name (without extension)
688to store header information on
689the corpus, document, and text level in.
690Defaults to C<header>.
691
Marc Kupietz985da0c2021-02-15 19:29:50 +0100692=item B<--use-tokenizer-sentence-splits|-s>
693
694Replace existing with, or add new, sentence boundary information
Akron11484782021-11-03 20:12:14 +0100695provided by the tokenizer.
696Currently KorAP-tokenizer and certain external tokenizers support
697these boundaries.
Marc Kupietz985da0c2021-02-15 19:29:50 +0100698
Akron91705d72021-02-19 10:59:45 +0100699=item B<--tokens-file> <file>
700
701Define the file (without extension)
702to store generated token information in
703(either from the KorAP tokenizer or an externally called tokenizer).
704Defaults to C<tokens>.
705
Akron3378dfd2020-08-01 15:01:36 +0200706=item B<--log|-l>
707
708Loglevel for I<Log::Any>. Defaults to C<notice>.
709
Akrond949e182020-02-14 12:23:57 +0100710=back
711
Akronb3649472020-09-29 08:24:46 +0200712=head1 ENVIRONMENT VARIABLES
713
714=over 2
715
716=item B<KORAPXMLTEI_DEBUG>
717
718Activate minimal debugging.
719Defaults to C<false>.
720
Akronb3649472020-09-29 08:24:46 +0200721=back
722
Akrond949e182020-02-14 12:23:57 +0100723=head1 COPYRIGHT AND LICENSE
724
Akrone48bec42023-01-05 12:18:45 +0100725Copyright (C) 2021-2023, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100726
727Author: Peter Harders
728
Akronaabd0952020-09-29 07:35:08 +0200729Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100730
731L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
732Corpus Analysis Platform at the
Akrond72baca2021-07-23 13:25:32 +0200733L<Leibniz Institute for the German Language (IDS)|https://www.ids-mannheim.de/>,
Akrond949e182020-02-14 12:23:57 +0100734member of the
735L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
736
737This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100738L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100739
740=cut
Akronf8088e62021-02-18 16:18:59 +0100741
742# NOTES
743
Akronf8088e62021-02-18 16:18:59 +0100744## Notes on segfault prevention
745
Akron91577922021-02-19 10:32:54 +0100746binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100747(see notes on 'PerlIO layers' in 'man XML::LibXML'),
748removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
749see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
750see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.