blob: 0d196a5d73867c41ddc4aded846ab2591233f28c [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
Marc Kupietz1e882fb2020-09-09 00:05:46 +020027eval {
28 require KorAP::XML::TEI::Tokenizer::KorAP;
29 1;
30};
Peter Harders1c5ce152020-07-22 18:02:50 +020031
Marc Kupietz41654662021-07-16 18:35:10 +020032our $VERSION = '2.2.0';
Peter Harders6f526a32020-06-29 21:44:41 +020033
Akrond949e182020-02-14 12:23:57 +010034our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n";
35
Akron33db4ec2021-02-24 12:52:21 +010036use constant {
37 # Set to 1 for minimal more debug output (no need to be parametrized)
Akroneb12e232021-02-25 13:49:50 +010038 DEBUG => $ENV{KORAPXMLTEI_DEBUG} // 0
Akron33db4ec2021-02-24 12:52:21 +010039};
Peter Hardersd892a582020-02-12 15:45:22 +010040
Akron692d17d2021-03-05 13:21:03 +010041if ($ENV{KORAPXMLTEI_INLINE}) {
42 warn 'KORAPXMLTEI_INLINE is deprecated in favor of --skip-inline-token-annotations';
43};
44
Akrone2819a12021-10-12 15:52:55 +020045# Inline tokens won't be stored in the structure file
46my $inline_tokens_exclusive = 0;
47
Peter Harders6f526a32020-06-29 21:44:41 +020048# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010049GetOptions(
Akrond3e1d282021-02-24 14:51:27 +010050 'root|r=s' => \(my $root_dir = '.'),
51 'input|i=s' => \(my $input_fname = ''),
Akron75d63142021-02-23 18:40:56 +010052 'tokenizer-call|tc=s' => \(my $tokenizer_call),
53 'tokenizer-korap|tk' => \(my $tokenizer_korap),
Akrond53913c2021-02-24 09:50:13 +010054 'tokenizer-internal|ti' => \(my $tokenizer_intern),
Akron75d63142021-02-23 18:40:56 +010055 'use-tokenizer-sentence-splits|s' => \(my $use_tokenizer_sentence_splits),
56 'inline-tokens=s' => \(my $inline_tokens = 'tokens#morpho'),
57 'inline-structures=s' => \(my $inline_structures = 'struct#structure'),
58 'skip-inline-tokens' => \(my $skip_inline_tokens = 0),
Akron692d17d2021-03-05 13:21:03 +010059 'skip-inline-token-annotations' => \(
60 my $skip_inline_token_annotations = ($ENV{KORAPXMLTEI_INLINE} ? 0 : 1)),
Akron54c3ff12021-02-25 11:33:37 +010061 'skip-inline-tags=s' => \(my $skip_inline_tags_str = ''),
Akrond3e1d282021-02-24 14:51:27 +010062 'base-foundry=s' => \(my $base_dir = 'base'),
63 'data-file=s' => \(my $data_file = 'data'),
Akrond53913c2021-02-24 09:50:13 +010064 'header-file=s' => \(my $header_file = 'header'),
65 'tokens-file=s' => \(my $tokens_file = 'tokens'),
Akrond3e1d282021-02-24 14:51:27 +010066 'log|l=s' => \(my $log_level = 'notice'),
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
Akron0529e512021-02-22 09:55:35 +010091# tag (without attributes), which contains the primary text
92my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +020093# optional
Akron09e0b2c2020-07-28 15:57:01 +020094
Akron0529e512021-02-22 09:55:35 +010095# TODO: IDS-specific (and redundant)
96my $_HEADER_TAG = 'idsHeader';
Akron0c41ab32020-09-29 07:33:33 +020097
Akrond3e1d282021-02-24 14:51:27 +010098
99# Define tokenizers
Marc Kupietz985da0c2021-02-15 19:29:50 +0100100if ($use_tokenizer_sentence_splits && !$tokenizer_korap) {
Akron33db4ec2021-02-24 12:52:21 +0100101 die $log->fatal(
102 'Sentence splitting is currently only supported by KorAP tokenizer ' .
103 '(use -tk to activate it)'
104 );
Akronb87c58d2021-02-23 17:23:30 +0100105};
Marc Kupietz985da0c2021-02-15 19:29:50 +0100106
Akron54c3ff12021-02-25 11:33:37 +0100107# Remember to skip certain inline tags
108my %skip_inline_tags = ();
109if ($skip_inline_tags_str) {
110 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
111 $skip_inline_tags{$_} = 1;
112 };
113};
114
Akrond3e1d282021-02-24 14:51:27 +0100115# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200116my $ext_tok;
117if ($tokenizer_call) {
118 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
119}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200120
Akron0c41ab32020-09-29 07:33:33 +0200121elsif ($tokenizer_korap) {
Marc Kupietz985da0c2021-02-15 19:29:50 +0100122 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron54c3ff12021-02-25 11:33:37 +0100123 if ($use_tokenizer_sentence_splits) {
124 $skip_inline_tags{s} = 1;
125 };
Akron0c41ab32020-09-29 07:33:33 +0200126};
Peter Harders6f526a32020-06-29 21:44:41 +0200127
Akron0c41ab32020-09-29 07:33:33 +0200128
Akrond3e1d282021-02-24 14:51:27 +0100129# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100130my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
131my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100132
Peter Harders41c35622020-07-12 01:16:22 +0200133
Akrondd0be8f2021-02-18 19:29:41 +0100134# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100135# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100136my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100137
Akron1a5271a2021-02-18 13:18:15 +0100138# Name of the directory and the file containing all inline token informations
139# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
140my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100141
Akrone2819a12021-10-12 15:52:55 +0200142if (index($_tokens_dir, '!') == 0) {
143 $_tokens_dir = substr($_tokens_dir, 1);
144 $inline_tokens_exclusive = 1;
145};
146
Akronb87c58d2021-02-23 17:23:30 +0100147# Initialize zipper
Akrond53913c2021-02-24 09:50:13 +0100148my $zipper = KorAP::XML::TEI::Zipper->new($root_dir);
Akron09e0b2c2020-07-28 15:57:01 +0200149
Akronbc899192021-02-24 12:14:47 +0100150# text directory (below $root_dir)
151my $dir = '';
Akron09e0b2c2020-07-28 15:57:01 +0200152
Akronbc899192021-02-24 12:14:47 +0100153# Escaped version of text id
154my $text_id_esc;
Peter Harders6f526a32020-06-29 21:44:41 +0200155
Akrond53913c2021-02-24 09:50:13 +0100156# Default encoding of the text
157my $input_enc = 'UTF-8';
158
Akrond53913c2021-02-24 09:50:13 +0100159# text line (needed for whitespace handling)
160my $text_line = 0;
161
Peter Harders6f526a32020-06-29 21:44:41 +0200162
Akrond53913c2021-02-24 09:50:13 +0100163# Input file handle (default: stdin)
164my $input_fh = *STDIN;
Peter Hardersd892a582020-02-12 15:45:22 +0100165
Akrond53913c2021-02-24 09:50:13 +0100166if ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200167 unless (open($input_fh, '<', $input_fname)) {
168 die $log->fatal("File '$input_fname' could not be opened.");
169 };
Akrond53913c2021-02-24 09:50:13 +0100170};
Peter Harders6f526a32020-06-29 21:44:41 +0200171
Akronf8088e62021-02-18 16:18:59 +0100172# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200173binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200174
Peter Harders6f526a32020-06-29 21:44:41 +0200175
Akroneb12e232021-02-25 13:49:50 +0100176# Create inline parser object
177my $inline = KorAP::XML::TEI::Inline->new(
178 $skip_inline_tokens,
Akrone2819a12021-10-12 15:52:55 +0200179 \%skip_inline_tags,
180 $inline_tokens_exclusive
Akroneb12e232021-02-25 13:49:50 +0100181);
182
183
Akrond53913c2021-02-24 09:50:13 +0100184# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100185MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200186
Akrond53913c2021-02-24 09:50:13 +0100187 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100188 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200189
Akroneaa96232020-10-15 17:06:15 +0200190 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100191 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200192 $input_enc = $2;
193 next;
194 };
195
196 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100197 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200198
Akrond3e1d282021-02-24 14:51:27 +0100199 # Start of text body
200 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100201 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200202
Akrond53913c2021-02-24 09:50:13 +0100203 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200204 die $log->fatal("input line number $.: " .
205 "line with opening text-body tag '${_TEXT_BODY}' " .
206 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200207 };
Peter Harders6f526a32020-06-29 21:44:41 +0200208
Akrond53913c2021-02-24 09:50:13 +0100209 # Text body data extracted from input document ($input_fh),
210 # further processed by XML::LibXML::Reader
211 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200212
Akron347be812020-09-29 07:52:52 +0200213 # Iterate over all lines in the text body
214 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200215
Akrond3e1d282021-02-24 14:51:27 +0100216 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200217 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100218 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200219
Akrond53913c2021-02-24 09:50:13 +0100220 # End of text body
Akronb43b4912021-02-25 10:31:11 +0100221 if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200222
Akron91705d72021-02-19 10:59:45 +0100223 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200224
Akrond53913c2021-02-24 09:50:13 +0100225 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200226 die $log->fatal("input line number $.: " .
227 "line with closing text-body tag '${_TEXT_BODY}'".
228 " contains additional information ... => Aborting (line=$_)");
229 };
Peter Harders6f526a32020-06-29 21:44:41 +0200230
Akrondafaa7a2021-02-19 15:17:58 +0100231 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100232 $log->warn(
233 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100234 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100235 );
Akrondafaa7a2021-02-19 15:17:58 +0100236 next MAIN;
237 };
Peter Harders6f526a32020-06-29 21:44:41 +0200238
Akroneb12e232021-02-25 13:49:50 +0100239 # Parse inline structure
240 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100241
242 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100243 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100244 };
245
Akroneb12e232021-02-25 13:49:50 +0100246 my $data = $inline->data;
247
Akrond53913c2021-02-24 09:50:13 +0100248 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100249 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100250 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100251 $text_id_esc
252 );
253
Akrond53913c2021-02-24 09:50:13 +0100254 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100255 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100256
257 # Tokenize and output
258 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100259 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100260 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100261 );
Akrond53ab4b2021-02-24 09:56:12 +0100262
263 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100264 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100265 };
Akrondafaa7a2021-02-19 15:17:58 +0100266 };
Peter Harders6f526a32020-06-29 21:44:41 +0200267
Akrond53913c2021-02-24 09:50:13 +0100268 # Tokenize with internal tokenizer
269 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200270
Akrondafaa7a2021-02-19 15:17:58 +0100271 # Tokenize and output
272 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100273 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200274 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100275 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200276
Akrondafaa7a2021-02-19 15:17:58 +0100277 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100278 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100279 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100280 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100281 };
Akrona10ad592020-08-03 11:20:23 +0200282
Akrondafaa7a2021-02-19 15:17:58 +0100283 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100284 if (!$inline->structures->empty) {
285 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100286 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100287 $text_id_esc,
288 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100289 );
Akrondafaa7a2021-02-19 15:17:58 +0100290 };
291
292 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100293 unless ($skip_inline_tokens || $inline->tokens->empty) {
294 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100295 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100296 $text_id_esc,
Akron692d17d2021-03-05 13:21:03 +0100297 # Either 0 = tokens without inline or 1 = tokens with inline
298 !$skip_inline_token_annotations
Akroneb12e232021-02-25 13:49:50 +0100299 );
Akrondafaa7a2021-02-19 15:17:58 +0100300 };
301
302 # reinit.
303 $dir = '';
304
Akron347be812020-09-29 07:52:52 +0200305 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200306 };
307
Peter Harders6f526a32020-06-29 21:44:41 +0200308
Akron347be812020-09-29 07:52:52 +0200309 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200310
Akronf8088e62021-02-18 16:18:59 +0100311 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100312
Akrond53913c2021-02-24 09:50:13 +0100313 # TODO:
314 # Maybe it's best, to keep the stripping of whitespace and
315 # to just remove the if-clause and to insert a blank by default
316 # (with possibly an option on how newlines in primary text should
317 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100318
319 # Remove consecutive whitespace at beginning and end (mostly one newline)
320 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200321
Akrond53913c2021-02-24 09:50:13 +0100322 # NOTE:
323 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200324
Akrond53913c2021-02-24 09:50:13 +0100325 # TODO:
326 # find a better solution, or create a warning, if a text has more
327 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200328
Akrond53913c2021-02-24 09:50:13 +0100329 # TODO:
330 # do testing with 2 different corpora
331 # (one with only one-line texts, the other with several lines per text)
332
333 # line contains at least one tag with at least one character contents
334 if (m/<[^>]+>[^<]/) {
335
336 # Increment counter for text lines
337 $text_line++;
338
339 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100340 # (for 2nd line and consecutive lines)
341 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200342 }
Akronf57ed812020-07-27 10:37:52 +0200343
Akron347be812020-09-29 07:52:52 +0200344 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100345 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200346 };
Akrond3e1d282021-02-24 14:51:27 +0100347 }
Akronf57ed812020-07-27 10:37:52 +0200348
Akrond3e1d282021-02-24 14:51:27 +0100349 # Start of header section
350 elsif (m#^(.*)(\<${_HEADER_TAG}[^>]*?type=["'].*)$#) {
Akronf57ed812020-07-27 10:37:52 +0200351
Akron347be812020-09-29 07:52:52 +0200352 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200353
Akrond20898f2021-02-19 15:52:17 +0100354 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100355 die $log->fatal(
356 "input line number $.: " .
357 'line with opening header tag is not in expected format ... ' .
358 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200359 };
360
361 # Parse header
Akroneaa96232020-10-15 17:06:15 +0200362 my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200363
364 # Header was parseable
365 if ($header) {
366
367 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100368 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200369
Akronb3649472020-09-29 08:24:46 +0200370 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200371
372 $header->to_zip($zipper->new_stream($file));
373
374 # Header is for text level
375 if ($header->type eq 'text') {
376
377 # Remember dir and sigles
378 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200379 $text_id_esc = $header->id_esc;
380
381 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100382 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200383
Akrond53913c2021-02-24 09:50:13 +0100384 # Reset counter for text lines
385 # (needed for whitespace handling)
386 $text_line = 0;
387 };
388 };
389 };
390};
Peter Hardersd892a582020-02-12 15:45:22 +0100391
Akron347be812020-09-29 07:52:52 +0200392$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200393
Akron9df4a242021-02-19 15:31:16 +0100394$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100395
Akrond53913c2021-02-24 09:50:13 +0100396close $input_fh;
397
Peter Harders6f526a32020-06-29 21:44:41 +0200398
Akrond949e182020-02-14 12:23:57 +0100399__END__
400
401=pod
402
403=encoding utf8
404
405=head1 NAME
406
407tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
408
409=head1 SYNOPSIS
410
411 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
412
413=head1 DESCRIPTION
414
Akronee434b12020-07-08 12:53:01 +0200415C<tei2korapxml> is a script to convert TEI P5 and
Akrond72baca2021-07-23 13:25:32 +0200416L<I5|https://www.ids-mannheim.de/digspra/kl/projekte/korpora/textmodell>
Akronee434b12020-07-08 12:53:01 +0200417based documents to the
418L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
419If no specific input is defined, data is
Akrond949e182020-02-14 12:23:57 +0100420read from C<STDIN>. If no specific output is defined, data is written
421to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +0200422
Akrond949e182020-02-14 12:23:57 +0100423This program is usually called from inside another script.
424
Akronee434b12020-07-08 12:53:01 +0200425=head1 FORMATS
426
427=head2 Input restrictions
428
429=over 2
430
431=item
432
Akronee434b12020-07-08 12:53:01 +0200433TEI P5 formatted input with certain restrictions:
434
435=over 4
436
437=item
438
439B<mandatory>: text-header with integrated textsigle, text-body
440
441=item
442
443B<optional>: corp-header with integrated corpsigle,
444doc-header with integrated docsigle
445
446=back
447
448=item
449
Akron0c41ab32020-09-29 07:33:33 +0200450All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200451newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200452(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200453into blanks between 2 tokens could lead to additional blanks,
454where there should be none (e.g.: punctuation characters like C<,> or
455C<.> should not be seperated from their predecessor token).
Akron8a0c4bf2021-03-16 16:51:21 +0100456(see also code section C<~ whitespace handling ~> in C<script/tei2korapxml>).
Akronee434b12020-07-08 12:53:01 +0200457
Akron940ca6f2021-10-11 12:38:39 +0200458=item
459
460Header types, like C<E<lt>idsHeader [...] type="document" [...] E<gt>>
461need to be defined in the same line as the header tag.
462
Akronee434b12020-07-08 12:53:01 +0200463=back
464
465=head2 Notes on the output
466
467=over 2
468
469=item
470
471zip file output (default on C<stdout>) with utf8 encoded entries
472(which together form the KorAP-XML format)
473
474=back
475
Akrond949e182020-02-14 12:23:57 +0100476=head1 INSTALLATION
477
Marc Kupietze83a4e92021-03-16 20:51:26 +0100478C<tei2korapxml> requires L<libxml2-dev> bindings and L<File::ShareDir::Install> to be installed.
479When these requirements are met, the preferred way to install the script is
Akrond949e182020-02-14 12:23:57 +0100480to use L<cpanm|App::cpanminus>.
481
482 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
483
484In case everything went well, the C<tei2korapxml> tool will
485be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200486
Akrond949e182020-02-14 12:23:57 +0100487Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
488
489=head1 OPTIONS
490
491=over 2
492
Akron4e603a52020-07-27 14:23:49 +0200493=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100494
Akron4e603a52020-07-27 14:23:49 +0200495The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100496
497=item B<--help|-h>
498
499Print help information.
500
501=item B<--version|-v>
502
503Print version information.
504
Akron4e603a52020-07-27 14:23:49 +0200505=item B<--tokenizer-call|-tc>
506
507Call an external tokenizer process, that will tokenize
508a single line from STDIN and outputs one token per line.
509
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200510=item B<--tokenizer-korap|-tk>
511
512Use the standard KorAP/DeReKo tokenizer.
513
Akron6d7b8e42020-09-29 07:37:41 +0200514=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200515
516Tokenize the data using two embedded tokenizers,
517that will take an I<Aggressive> and a I<conservative>
518approach.
519
Akron75d63142021-02-23 18:40:56 +0100520=item B<--skip-inline-tokens>
521
522Boolean flag indicating that inline tokens should not
523be processed. Defaults to false (meaning inline tokens will be processed).
524
Akron692d17d2021-03-05 13:21:03 +0100525=item B<--skip-inline-token-annotations>
526
527Boolean flag indicating that inline token annotations should not
528be processed. Defaults to true (meaning inline token annotations
529won't be processed).
530
Akronca70a1d2021-02-25 16:21:31 +0100531=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100532
533Expects a comma-separated list of tags to be ignored when the structure
534is parsed. Content of these tags however will be processed.
535
Akron1a5271a2021-02-18 13:18:15 +0100536=item B<--inline-tokens> <foundry>#[<file>]
537
538Define the foundry and file (without extension)
539to store inline token information in.
Akron8a0c4bf2021-03-16 16:51:21 +0100540Unless C<--skip-inline-token-annotations> is set,
541this will contain annotations as well.
Akron1a5271a2021-02-18 13:18:15 +0100542Defaults to C<tokens> and C<morpho>.
543
Akrone2819a12021-10-12 15:52:55 +0200544The inline token data will also be stored in the
545inline structures file (see I<--inline-structures>),
546unless the inline token foundry is prepended
547by an B<!> exclamation mark, indicating that inline
548tokens are stored exclusively in the inline tokens
549file.
550
551Example:
552
553 tei2korapxml --inline-tokens '!gingko#morpho' < data.i5.xml > korapxml.zip
554
Akrondd0be8f2021-02-18 19:29:41 +0100555=item B<--inline-structures> <foundry>#[<file>]
556
557Define the foundry and file (without extension)
558to store inline structure information in.
559Defaults to C<struct> and C<structures>.
560
Akron26a71522021-02-19 10:27:37 +0100561=item B<--base-foundry> <foundry>
562
563Define the base foundry to store newly generated
564token information in.
565Defaults to C<base>.
566
567=item B<--data-file> <file>
568
569Define the file (without extension)
570to store primary data information in.
571Defaults to C<data>.
572
573=item B<--header-file> <file>
574
575Define the file name (without extension)
576to store header information on
577the corpus, document, and text level in.
578Defaults to C<header>.
579
Marc Kupietz985da0c2021-02-15 19:29:50 +0100580=item B<--use-tokenizer-sentence-splits|-s>
581
582Replace existing with, or add new, sentence boundary information
583provided by the KorAP tokenizer (currently supported only).
584
Akron91705d72021-02-19 10:59:45 +0100585=item B<--tokens-file> <file>
586
587Define the file (without extension)
588to store generated token information in
589(either from the KorAP tokenizer or an externally called tokenizer).
590Defaults to C<tokens>.
591
Akron3378dfd2020-08-01 15:01:36 +0200592=item B<--log|-l>
593
594Loglevel for I<Log::Any>. Defaults to C<notice>.
595
Akrond949e182020-02-14 12:23:57 +0100596=back
597
Akronb3649472020-09-29 08:24:46 +0200598=head1 ENVIRONMENT VARIABLES
599
600=over 2
601
602=item B<KORAPXMLTEI_DEBUG>
603
604Activate minimal debugging.
605Defaults to C<false>.
606
Akronb3649472020-09-29 08:24:46 +0200607=back
608
Akrond949e182020-02-14 12:23:57 +0100609=head1 COPYRIGHT AND LICENSE
610
Marc Kupietze955ecc2021-02-17 17:42:01 +0100611Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100612
613Author: Peter Harders
614
Akronaabd0952020-09-29 07:35:08 +0200615Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100616
617L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
618Corpus Analysis Platform at the
Akrond72baca2021-07-23 13:25:32 +0200619L<Leibniz Institute for the German Language (IDS)|https://www.ids-mannheim.de/>,
Akrond949e182020-02-14 12:23:57 +0100620member of the
621L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
622
623This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100624L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100625
626=cut
Akronf8088e62021-02-18 16:18:59 +0100627
628# NOTES
629
Akronf8088e62021-02-18 16:18:59 +0100630## Notes on segfault prevention
631
Akron91577922021-02-19 10:32:54 +0100632binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100633(see notes on 'PerlIO layers' in 'man XML::LibXML'),
634removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
635see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
636see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.