blob: 574040756323ea211a7417d9fac4781a694a8162 [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
Akrond3e1d282021-02-24 14:51:27 +010032our $VERSION = '1.01';
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
Peter Harders6f526a32020-06-29 21:44:41 +020041# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010042GetOptions(
Akrond3e1d282021-02-24 14:51:27 +010043 'root|r=s' => \(my $root_dir = '.'),
44 'input|i=s' => \(my $input_fname = ''),
Akron75d63142021-02-23 18:40:56 +010045 'tokenizer-call|tc=s' => \(my $tokenizer_call),
46 'tokenizer-korap|tk' => \(my $tokenizer_korap),
Akrond53913c2021-02-24 09:50:13 +010047 'tokenizer-internal|ti' => \(my $tokenizer_intern),
Akron75d63142021-02-23 18:40:56 +010048 'use-tokenizer-sentence-splits|s' => \(my $use_tokenizer_sentence_splits),
49 'inline-tokens=s' => \(my $inline_tokens = 'tokens#morpho'),
50 'inline-structures=s' => \(my $inline_structures = 'struct#structure'),
51 'skip-inline-tokens' => \(my $skip_inline_tokens = 0),
Akron54c3ff12021-02-25 11:33:37 +010052 'skip-inline-tags=s' => \(my $skip_inline_tags_str = ''),
Akrond3e1d282021-02-24 14:51:27 +010053 'base-foundry=s' => \(my $base_dir = 'base'),
54 'data-file=s' => \(my $data_file = 'data'),
Akrond53913c2021-02-24 09:50:13 +010055 'header-file=s' => \(my $header_file = 'header'),
56 'tokens-file=s' => \(my $tokens_file = 'tokens'),
Akrond3e1d282021-02-24 14:51:27 +010057 'log|l=s' => \(my $log_level = 'notice'),
Akron75d63142021-02-23 18:40:56 +010058 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010059 pod2usage(
60 -verbose => 99,
61 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
62 -msg => $VERSION_MSG,
63 -output => '-'
64 )
65 },
66 'version|v' => sub {
67 pod2usage(
68 -verbose => 0,
69 -msg => $VERSION_MSG,
70 -output => '-'
Akrond3e1d282021-02-24 14:51:27 +010071 );
Akrond949e182020-02-14 12:23:57 +010072 }
Peter Hardersd892a582020-02-12 15:45:22 +010073);
74
Akrond3e1d282021-02-24 14:51:27 +010075
Akronb87c58d2021-02-23 17:23:30 +010076# Establish logger
Akron33db4ec2021-02-24 12:52:21 +010077binmode(STDERR, ':encoding(UTF-8)');
Akron3378dfd2020-08-01 15:01:36 +020078Log::Any::Adapter->set('Stderr', log_level => $log_level);
Akronb3649472020-09-29 08:24:46 +020079$log->notice('Debugging is activated') if DEBUG;
80
Akrond3e1d282021-02-24 14:51:27 +010081
Akron0529e512021-02-22 09:55:35 +010082# tag (without attributes), which contains the primary text
83my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +020084# optional
Akron09e0b2c2020-07-28 15:57:01 +020085
Akron0529e512021-02-22 09:55:35 +010086# TODO: IDS-specific (and redundant)
87my $_HEADER_TAG = 'idsHeader';
Akron0c41ab32020-09-29 07:33:33 +020088
Akrond3e1d282021-02-24 14:51:27 +010089
90# Define tokenizers
Marc Kupietz985da0c2021-02-15 19:29:50 +010091if ($use_tokenizer_sentence_splits && !$tokenizer_korap) {
Akron33db4ec2021-02-24 12:52:21 +010092 die $log->fatal(
93 'Sentence splitting is currently only supported by KorAP tokenizer ' .
94 '(use -tk to activate it)'
95 );
Akronb87c58d2021-02-23 17:23:30 +010096};
Marc Kupietz985da0c2021-02-15 19:29:50 +010097
Akron54c3ff12021-02-25 11:33:37 +010098# Remember to skip certain inline tags
99my %skip_inline_tags = ();
100if ($skip_inline_tags_str) {
101 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
102 $skip_inline_tags{$_} = 1;
103 };
104};
105
Akrond3e1d282021-02-24 14:51:27 +0100106# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200107my $ext_tok;
108if ($tokenizer_call) {
109 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
110}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200111
Akron0c41ab32020-09-29 07:33:33 +0200112elsif ($tokenizer_korap) {
Marc Kupietz985da0c2021-02-15 19:29:50 +0100113 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron54c3ff12021-02-25 11:33:37 +0100114 if ($use_tokenizer_sentence_splits) {
115 $skip_inline_tags{s} = 1;
116 };
Akron0c41ab32020-09-29 07:33:33 +0200117};
Peter Harders6f526a32020-06-29 21:44:41 +0200118
Akron0c41ab32020-09-29 07:33:33 +0200119
Akrond3e1d282021-02-24 14:51:27 +0100120# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100121my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
122my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100123
Peter Harders41c35622020-07-12 01:16:22 +0200124
Akrondd0be8f2021-02-18 19:29:41 +0100125# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100126# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100127my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100128
Akron1a5271a2021-02-18 13:18:15 +0100129# Name of the directory and the file containing all inline token informations
130# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
131my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100132
Akron4e3c7e32021-02-18 15:19:53 +0100133# Handling inline annotations (inside $_TOKENS_TAG)
Akronb87c58d2021-02-23 17:23:30 +0100134my $_INLINE_ANNOT = $ENV{KORAPXMLTEI_INLINE} ? 1 : 0;
135
Akronb87c58d2021-02-23 17:23:30 +0100136# Initialize zipper
Akrond53913c2021-02-24 09:50:13 +0100137my $zipper = KorAP::XML::TEI::Zipper->new($root_dir);
Akron09e0b2c2020-07-28 15:57:01 +0200138
Akronbc899192021-02-24 12:14:47 +0100139# text directory (below $root_dir)
140my $dir = '';
Akron09e0b2c2020-07-28 15:57:01 +0200141
Akronbc899192021-02-24 12:14:47 +0100142# Escaped version of text id
143my $text_id_esc;
Peter Harders6f526a32020-06-29 21:44:41 +0200144
Akrond53913c2021-02-24 09:50:13 +0100145# Default encoding of the text
146my $input_enc = 'UTF-8';
147
Akrond53913c2021-02-24 09:50:13 +0100148# text line (needed for whitespace handling)
149my $text_line = 0;
150
Peter Harders6f526a32020-06-29 21:44:41 +0200151
Akrond53913c2021-02-24 09:50:13 +0100152# Input file handle (default: stdin)
153my $input_fh = *STDIN;
Peter Hardersd892a582020-02-12 15:45:22 +0100154
Akrond53913c2021-02-24 09:50:13 +0100155if ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200156 unless (open($input_fh, '<', $input_fname)) {
157 die $log->fatal("File '$input_fname' could not be opened.");
158 };
Akrond53913c2021-02-24 09:50:13 +0100159};
Peter Harders6f526a32020-06-29 21:44:41 +0200160
Akronf8088e62021-02-18 16:18:59 +0100161# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200162binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200163
Peter Harders6f526a32020-06-29 21:44:41 +0200164
Akroneb12e232021-02-25 13:49:50 +0100165# Create inline parser object
166my $inline = KorAP::XML::TEI::Inline->new(
167 $skip_inline_tokens,
168 \%skip_inline_tags
169);
170
171
Akrond53913c2021-02-24 09:50:13 +0100172# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100173MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200174
Akrond53913c2021-02-24 09:50:13 +0100175 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100176 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200177
Akroneaa96232020-10-15 17:06:15 +0200178 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100179 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200180 $input_enc = $2;
181 next;
182 };
183
184 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100185 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200186
Akrond3e1d282021-02-24 14:51:27 +0100187 # Start of text body
188 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100189 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200190
Akrond53913c2021-02-24 09:50:13 +0100191 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200192 die $log->fatal("input line number $.: " .
193 "line with opening text-body tag '${_TEXT_BODY}' " .
194 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200195 };
Peter Harders6f526a32020-06-29 21:44:41 +0200196
Akrond53913c2021-02-24 09:50:13 +0100197 # Text body data extracted from input document ($input_fh),
198 # further processed by XML::LibXML::Reader
199 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200200
Akron347be812020-09-29 07:52:52 +0200201 # Iterate over all lines in the text body
202 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200203
Akrond3e1d282021-02-24 14:51:27 +0100204 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200205 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100206 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200207
Akrond53913c2021-02-24 09:50:13 +0100208 # End of text body
Akronb43b4912021-02-25 10:31:11 +0100209 if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200210
Akron91705d72021-02-19 10:59:45 +0100211 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200212
Akrond53913c2021-02-24 09:50:13 +0100213 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200214 die $log->fatal("input line number $.: " .
215 "line with closing text-body tag '${_TEXT_BODY}'".
216 " contains additional information ... => Aborting (line=$_)");
217 };
Peter Harders6f526a32020-06-29 21:44:41 +0200218
Akrondafaa7a2021-02-19 15:17:58 +0100219 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100220 $log->warn(
221 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100222 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100223 );
Akrondafaa7a2021-02-19 15:17:58 +0100224 next MAIN;
225 };
Peter Harders6f526a32020-06-29 21:44:41 +0200226
Akroneb12e232021-02-25 13:49:50 +0100227 # Parse inline structure
228 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100229
230 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100231 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100232 };
233
Akroneb12e232021-02-25 13:49:50 +0100234 my $data = $inline->data;
235
Akrond53913c2021-02-24 09:50:13 +0100236 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100237 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100238 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100239 $text_id_esc
240 );
241
Akrond53913c2021-02-24 09:50:13 +0100242 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100243 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100244
245 # Tokenize and output
246 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100247 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100248 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100249 );
Akrond53ab4b2021-02-24 09:56:12 +0100250
251 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100252 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100253 };
Akrondafaa7a2021-02-19 15:17:58 +0100254 };
Peter Harders6f526a32020-06-29 21:44:41 +0200255
Akrond53913c2021-02-24 09:50:13 +0100256 # Tokenize with internal tokenizer
257 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200258
Akrondafaa7a2021-02-19 15:17:58 +0100259 # Tokenize and output
260 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100261 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200262 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100263 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200264
Akrondafaa7a2021-02-19 15:17:58 +0100265 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100266 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100267 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100268 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100269 };
Akrona10ad592020-08-03 11:20:23 +0200270
Akrondafaa7a2021-02-19 15:17:58 +0100271 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100272 if (!$inline->structures->empty) {
273 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100274 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100275 $text_id_esc,
276 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100277 );
Akrondafaa7a2021-02-19 15:17:58 +0100278 };
279
280 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100281 unless ($skip_inline_tokens || $inline->tokens->empty) {
282 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100283 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100284 $text_id_esc,
285 $_INLINE_ANNOT # Either 0 = tokens without inline or 1 = tokens with inline
Akroneb12e232021-02-25 13:49:50 +0100286 );
Akrondafaa7a2021-02-19 15:17:58 +0100287 };
288
289 # reinit.
290 $dir = '';
291
Akron347be812020-09-29 07:52:52 +0200292 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200293 };
294
Peter Harders6f526a32020-06-29 21:44:41 +0200295
Akron347be812020-09-29 07:52:52 +0200296 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200297
Akronf8088e62021-02-18 16:18:59 +0100298 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100299
Akrond53913c2021-02-24 09:50:13 +0100300 # TODO:
301 # Maybe it's best, to keep the stripping of whitespace and
302 # to just remove the if-clause and to insert a blank by default
303 # (with possibly an option on how newlines in primary text should
304 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100305
306 # Remove consecutive whitespace at beginning and end (mostly one newline)
307 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200308
Akrond53913c2021-02-24 09:50:13 +0100309 # NOTE:
310 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200311
Akrond53913c2021-02-24 09:50:13 +0100312 # TODO:
313 # find a better solution, or create a warning, if a text has more
314 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200315
Akrond53913c2021-02-24 09:50:13 +0100316 # TODO:
317 # do testing with 2 different corpora
318 # (one with only one-line texts, the other with several lines per text)
319
320 # line contains at least one tag with at least one character contents
321 if (m/<[^>]+>[^<]/) {
322
323 # Increment counter for text lines
324 $text_line++;
325
326 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100327 # (for 2nd line and consecutive lines)
328 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200329 }
Akronf57ed812020-07-27 10:37:52 +0200330
Akron347be812020-09-29 07:52:52 +0200331 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100332 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200333 };
Akrond3e1d282021-02-24 14:51:27 +0100334 }
Akronf57ed812020-07-27 10:37:52 +0200335
Akrond3e1d282021-02-24 14:51:27 +0100336 # Start of header section
337 elsif (m#^(.*)(\<${_HEADER_TAG}[^>]*?type=["'].*)$#) {
Akronf57ed812020-07-27 10:37:52 +0200338
Akron347be812020-09-29 07:52:52 +0200339 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200340
Akrond20898f2021-02-19 15:52:17 +0100341 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100342 die $log->fatal(
343 "input line number $.: " .
344 'line with opening header tag is not in expected format ... ' .
345 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200346 };
347
348 # Parse header
Akroneaa96232020-10-15 17:06:15 +0200349 my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200350
351 # Header was parseable
352 if ($header) {
353
354 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100355 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200356
Akronb3649472020-09-29 08:24:46 +0200357 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200358
359 $header->to_zip($zipper->new_stream($file));
360
361 # Header is for text level
362 if ($header->type eq 'text') {
363
364 # Remember dir and sigles
365 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200366 $text_id_esc = $header->id_esc;
367
368 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100369 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200370
Akrond53913c2021-02-24 09:50:13 +0100371 # Reset counter for text lines
372 # (needed for whitespace handling)
373 $text_line = 0;
374 };
375 };
376 };
377};
Peter Hardersd892a582020-02-12 15:45:22 +0100378
Akron347be812020-09-29 07:52:52 +0200379$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200380
Akron9df4a242021-02-19 15:31:16 +0100381$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100382
Akrond53913c2021-02-24 09:50:13 +0100383close $input_fh;
384
Peter Harders6f526a32020-06-29 21:44:41 +0200385
Akrond949e182020-02-14 12:23:57 +0100386__END__
387
388=pod
389
390=encoding utf8
391
392=head1 NAME
393
394tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
395
396=head1 SYNOPSIS
397
398 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
399
400=head1 DESCRIPTION
401
Akronee434b12020-07-08 12:53:01 +0200402C<tei2korapxml> is a script to convert TEI P5 and
403L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html>
404based documents to the
405L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
406If no specific input is defined, data is
Akrond949e182020-02-14 12:23:57 +0100407read from C<STDIN>. If no specific output is defined, data is written
408to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +0200409
Akrond949e182020-02-14 12:23:57 +0100410This program is usually called from inside another script.
411
Akronee434b12020-07-08 12:53:01 +0200412=head1 FORMATS
413
414=head2 Input restrictions
415
416=over 2
417
418=item
419
Akronee434b12020-07-08 12:53:01 +0200420TEI P5 formatted input with certain restrictions:
421
422=over 4
423
424=item
425
426B<mandatory>: text-header with integrated textsigle, text-body
427
428=item
429
430B<optional>: corp-header with integrated corpsigle,
431doc-header with integrated docsigle
432
433=back
434
435=item
436
Akron0c41ab32020-09-29 07:33:33 +0200437All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200438newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200439(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200440into blanks between 2 tokens could lead to additional blanks,
441where there should be none (e.g.: punctuation characters like C<,> or
442C<.> should not be seperated from their predecessor token).
443(see also code section C<~ whitespace handling ~>).
444
445=back
446
447=head2 Notes on the output
448
449=over 2
450
451=item
452
453zip file output (default on C<stdout>) with utf8 encoded entries
454(which together form the KorAP-XML format)
455
456=back
457
Akrond949e182020-02-14 12:23:57 +0100458=head1 INSTALLATION
459
460C<tei2korapxml> requires L<libxml2-dev> bindings to build. When
461these bindings are available, the preferred way to install the script is
462to use L<cpanm|App::cpanminus>.
463
464 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
465
466In case everything went well, the C<tei2korapxml> tool will
467be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200468
Akrond949e182020-02-14 12:23:57 +0100469Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
470
471=head1 OPTIONS
472
473=over 2
474
Akron4e603a52020-07-27 14:23:49 +0200475=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100476
Akron4e603a52020-07-27 14:23:49 +0200477The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100478
479=item B<--help|-h>
480
481Print help information.
482
483=item B<--version|-v>
484
485Print version information.
486
Akron4e603a52020-07-27 14:23:49 +0200487=item B<--tokenizer-call|-tc>
488
489Call an external tokenizer process, that will tokenize
490a single line from STDIN and outputs one token per line.
491
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200492=item B<--tokenizer-korap|-tk>
493
494Use the standard KorAP/DeReKo tokenizer.
495
Akron6d7b8e42020-09-29 07:37:41 +0200496=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200497
498Tokenize the data using two embedded tokenizers,
499that will take an I<Aggressive> and a I<conservative>
500approach.
501
Akron75d63142021-02-23 18:40:56 +0100502=item B<--skip-inline-tokens>
503
504Boolean flag indicating that inline tokens should not
505be processed. Defaults to false (meaning inline tokens will be processed).
506
Akronca70a1d2021-02-25 16:21:31 +0100507=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100508
509Expects a comma-separated list of tags to be ignored when the structure
510is parsed. Content of these tags however will be processed.
511
Akron1a5271a2021-02-18 13:18:15 +0100512=item B<--inline-tokens> <foundry>#[<file>]
513
514Define the foundry and file (without extension)
515to store inline token information in.
516If L</KORAPXMLTEI_INLINE> is set, this will contain
517annotations as well.
518Defaults to C<tokens> and C<morpho>.
519
Akrondd0be8f2021-02-18 19:29:41 +0100520=item B<--inline-structures> <foundry>#[<file>]
521
522Define the foundry and file (without extension)
523to store inline structure information in.
524Defaults to C<struct> and C<structures>.
525
Akron26a71522021-02-19 10:27:37 +0100526=item B<--base-foundry> <foundry>
527
528Define the base foundry to store newly generated
529token information in.
530Defaults to C<base>.
531
532=item B<--data-file> <file>
533
534Define the file (without extension)
535to store primary data information in.
536Defaults to C<data>.
537
538=item B<--header-file> <file>
539
540Define the file name (without extension)
541to store header information on
542the corpus, document, and text level in.
543Defaults to C<header>.
544
Marc Kupietz985da0c2021-02-15 19:29:50 +0100545=item B<--use-tokenizer-sentence-splits|-s>
546
547Replace existing with, or add new, sentence boundary information
548provided by the KorAP tokenizer (currently supported only).
549
Akron91705d72021-02-19 10:59:45 +0100550=item B<--tokens-file> <file>
551
552Define the file (without extension)
553to store generated token information in
554(either from the KorAP tokenizer or an externally called tokenizer).
555Defaults to C<tokens>.
556
Akron3378dfd2020-08-01 15:01:36 +0200557=item B<--log|-l>
558
559Loglevel for I<Log::Any>. Defaults to C<notice>.
560
Akrond949e182020-02-14 12:23:57 +0100561=back
562
Akronb3649472020-09-29 08:24:46 +0200563=head1 ENVIRONMENT VARIABLES
564
565=over 2
566
567=item B<KORAPXMLTEI_DEBUG>
568
569Activate minimal debugging.
570Defaults to C<false>.
571
572=item B<KORAPXMLTEI_INLINE>
573
574Process inline annotations, if present.
575Defaults to C<false>.
576
577=back
578
Akrond949e182020-02-14 12:23:57 +0100579=head1 COPYRIGHT AND LICENSE
580
Marc Kupietze955ecc2021-02-17 17:42:01 +0100581Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100582
583Author: Peter Harders
584
Akronaabd0952020-09-29 07:35:08 +0200585Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100586
587L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
588Corpus Analysis Platform at the
589L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
590member of the
591L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
592
593This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100594L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100595
596=cut
Akronf8088e62021-02-18 16:18:59 +0100597
598# NOTES
599
Akronf8088e62021-02-18 16:18:59 +0100600## Notes on segfault prevention
601
Akron91577922021-02-19 10:32:54 +0100602binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100603(see notes on 'PerlIO layers' in 'man XML::LibXML'),
604removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
605see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
606see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.