blob: 63ea5254704ea188a43a8763b18fbb99ec16703e [file] [log] [blame]
Akron9cb13942020-02-14 07:39:54 +01001#!/usr/bin/env perl
Peter Hardersd892a582020-02-12 15:45:22 +01002use strict;
3use warnings;
Peter Harders6f526a32020-06-29 21:44:41 +02004
Akron3378dfd2020-08-01 15:01:36 +02005use Log::Any '$log';
6use Log::Any::Adapter;
Peter Harders6f526a32020-06-29 21:44:41 +02007use Pod::Usage;
8use Getopt::Long qw(GetOptions :config no_auto_abbrev);
9
10use File::Basename qw(dirname);
Peter Hardersd892a582020-02-12 15:45:22 +010011
Akroneaa96232020-10-15 17:06:15 +020012use Encode qw(decode);
Peter Hardersd892a582020-02-12 15:45:22 +010013
Akron4f67cd42020-07-02 12:27:58 +020014use FindBin;
15BEGIN {
16 unshift @INC, "$FindBin::Bin/../lib";
17};
18
Marc Kupietz8a954e52021-02-16 22:03:07 +010019use KorAP::XML::TEI qw!remove_xml_comments replace_entities!;
Akron8b511f92020-07-09 17:28:08 +020020use KorAP::XML::TEI::Tokenizer::External;
Akrond9627472020-07-09 16:53:09 +020021use KorAP::XML::TEI::Tokenizer::Conservative;
22use KorAP::XML::TEI::Tokenizer::Aggressive;
Akron85717512020-07-08 11:19:19 +020023use KorAP::XML::TEI::Zipper;
Akronf57ed812020-07-27 10:37:52 +020024use KorAP::XML::TEI::Header;
Akroneb12e232021-02-25 13:49:50 +010025use KorAP::XML::TEI::Inline;
Peter Hardersd892a582020-02-12 15:45:22 +010026
Akron85269c02022-11-07 14:03:31 +010027our $VERSION = '2.3.4';
Peter Harders6f526a32020-06-29 21:44:41 +020028
Akrond949e182020-02-14 12:23:57 +010029our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n";
30
Akron33db4ec2021-02-24 12:52:21 +010031use constant {
32 # Set to 1 for minimal more debug output (no need to be parametrized)
Akroneb12e232021-02-25 13:49:50 +010033 DEBUG => $ENV{KORAPXMLTEI_DEBUG} // 0
Akron33db4ec2021-02-24 12:52:21 +010034};
Peter Hardersd892a582020-02-12 15:45:22 +010035
Akron692d17d2021-03-05 13:21:03 +010036if ($ENV{KORAPXMLTEI_INLINE}) {
37 warn 'KORAPXMLTEI_INLINE is deprecated in favor of --skip-inline-token-annotations';
38};
39
Akrone2819a12021-10-12 15:52:55 +020040# Inline tokens won't be stored in the structure file
41my $inline_tokens_exclusive = 0;
42
Peter Harders6f526a32020-06-29 21:44:41 +020043# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010044GetOptions(
Akrond3e1d282021-02-24 14:51:27 +010045 'root|r=s' => \(my $root_dir = '.'),
46 'input|i=s' => \(my $input_fname = ''),
Akron75d63142021-02-23 18:40:56 +010047 'tokenizer-call|tc=s' => \(my $tokenizer_call),
48 'tokenizer-korap|tk' => \(my $tokenizer_korap),
Akrond53913c2021-02-24 09:50:13 +010049 'tokenizer-internal|ti' => \(my $tokenizer_intern),
Akron75d63142021-02-23 18:40:56 +010050 'use-tokenizer-sentence-splits|s' => \(my $use_tokenizer_sentence_splits),
51 'inline-tokens=s' => \(my $inline_tokens = 'tokens#morpho'),
52 'inline-structures=s' => \(my $inline_structures = 'struct#structure'),
53 'skip-inline-tokens' => \(my $skip_inline_tokens = 0),
Akron692d17d2021-03-05 13:21:03 +010054 'skip-inline-token-annotations' => \(
55 my $skip_inline_token_annotations = ($ENV{KORAPXMLTEI_INLINE} ? 0 : 1)),
Akron54c3ff12021-02-25 11:33:37 +010056 'skip-inline-tags=s' => \(my $skip_inline_tags_str = ''),
Akrond3e1d282021-02-24 14:51:27 +010057 'base-foundry=s' => \(my $base_dir = 'base'),
58 'data-file=s' => \(my $data_file = 'data'),
Akrond53913c2021-02-24 09:50:13 +010059 'header-file=s' => \(my $header_file = 'header'),
60 'tokens-file=s' => \(my $tokens_file = 'tokens'),
Akrond3e1d282021-02-24 14:51:27 +010061 'log|l=s' => \(my $log_level = 'notice'),
Akrona2cb2812021-10-30 10:29:08 +020062 '' => \(my $stdio),
Akron75d63142021-02-23 18:40:56 +010063 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010064 pod2usage(
65 -verbose => 99,
66 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
67 -msg => $VERSION_MSG,
68 -output => '-'
69 )
70 },
71 'version|v' => sub {
72 pod2usage(
73 -verbose => 0,
74 -msg => $VERSION_MSG,
75 -output => '-'
Akrond3e1d282021-02-24 14:51:27 +010076 );
Akrond949e182020-02-14 12:23:57 +010077 }
Peter Hardersd892a582020-02-12 15:45:22 +010078);
79
Akrond3e1d282021-02-24 14:51:27 +010080
Akronb87c58d2021-02-23 17:23:30 +010081# Establish logger
Akron33db4ec2021-02-24 12:52:21 +010082binmode(STDERR, ':encoding(UTF-8)');
Akron3378dfd2020-08-01 15:01:36 +020083Log::Any::Adapter->set('Stderr', log_level => $log_level);
Akronb3649472020-09-29 08:24:46 +020084$log->notice('Debugging is activated') if DEBUG;
85
Akrond3e1d282021-02-24 14:51:27 +010086
Akron0529e512021-02-22 09:55:35 +010087# tag (without attributes), which contains the primary text
88my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +020089# optional
Akron09e0b2c2020-07-28 15:57:01 +020090
Akron0529e512021-02-22 09:55:35 +010091# TODO: IDS-specific (and redundant)
92my $_HEADER_TAG = 'idsHeader';
Akron0c41ab32020-09-29 07:33:33 +020093
Akron54c3ff12021-02-25 11:33:37 +010094# Remember to skip certain inline tags
95my %skip_inline_tags = ();
96if ($skip_inline_tags_str) {
97 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
98 $skip_inline_tags{$_} = 1;
99 };
100};
101
Akrond3e1d282021-02-24 14:51:27 +0100102# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200103my $ext_tok;
104if ($tokenizer_call) {
105 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
Akron11484782021-11-03 20:12:14 +0100106 $ext_tok->sentence_splits(1) if $use_tokenizer_sentence_splits;
Akron0c41ab32020-09-29 07:33:33 +0200107}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200108
Akron0c41ab32020-09-29 07:33:33 +0200109elsif ($tokenizer_korap) {
Akronbd4281e2022-03-28 08:31:40 +0200110 eval {
111 require KorAP::XML::TEI::Tokenizer::KorAP;
112 1;
113 };
Marc Kupietz985da0c2021-02-15 19:29:50 +0100114 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron0c41ab32020-09-29 07:33:33 +0200115};
Peter Harders6f526a32020-06-29 21:44:41 +0200116
Akron11484782021-11-03 20:12:14 +0100117if ($use_tokenizer_sentence_splits) {
118 $skip_inline_tags{s} = 1;
119};
Akron0c41ab32020-09-29 07:33:33 +0200120
Akrond3e1d282021-02-24 14:51:27 +0100121# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100122my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
123my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100124
Peter Harders41c35622020-07-12 01:16:22 +0200125
Akrondd0be8f2021-02-18 19:29:41 +0100126# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100127# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100128my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100129
Akron1a5271a2021-02-18 13:18:15 +0100130# Name of the directory and the file containing all inline token informations
131# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
132my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100133
Akrone2819a12021-10-12 15:52:55 +0200134if (index($_tokens_dir, '!') == 0) {
135 $_tokens_dir = substr($_tokens_dir, 1);
136 $inline_tokens_exclusive = 1;
137};
138
Akronb87c58d2021-02-23 17:23:30 +0100139# Initialize zipper
Akrond53913c2021-02-24 09:50:13 +0100140my $zipper = KorAP::XML::TEI::Zipper->new($root_dir);
Akron09e0b2c2020-07-28 15:57:01 +0200141
Akronbc899192021-02-24 12:14:47 +0100142# text directory (below $root_dir)
143my $dir = '';
Akron09e0b2c2020-07-28 15:57:01 +0200144
Akronbc899192021-02-24 12:14:47 +0100145# Escaped version of text id
146my $text_id_esc;
Peter Harders6f526a32020-06-29 21:44:41 +0200147
Akrond53913c2021-02-24 09:50:13 +0100148# Default encoding of the text
149my $input_enc = 'UTF-8';
150
Akrond53913c2021-02-24 09:50:13 +0100151# text line (needed for whitespace handling)
152my $text_line = 0;
153
Peter Harders6f526a32020-06-29 21:44:41 +0200154
Akrond53913c2021-02-24 09:50:13 +0100155# Input file handle (default: stdin)
Akrona2cb2812021-10-30 10:29:08 +0200156my $input_fh;
Peter Hardersd892a582020-02-12 15:45:22 +0100157
Akrona2cb2812021-10-30 10:29:08 +0200158# Single dash was set
159if ($stdio) {
160 $input_fh = *STDIN;
161}
162
163# Input flag was passed
164elsif ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200165 unless (open($input_fh, '<', $input_fname)) {
166 die $log->fatal("File '$input_fname' could not be opened.");
167 };
Akrona2cb2812021-10-30 10:29:08 +0200168}
169
170# No input to process
171else {
172 pod2usage(
173 -verbose => 99,
174 -sections => 'NAME|SYNOPSIS',
175 -msg => $VERSION_MSG,
176 -output => '-'
177 );
178 exit;
Akrond53913c2021-02-24 09:50:13 +0100179};
Peter Harders6f526a32020-06-29 21:44:41 +0200180
Akronf8088e62021-02-18 16:18:59 +0100181# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200182binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200183
Peter Harders6f526a32020-06-29 21:44:41 +0200184
Akroneb12e232021-02-25 13:49:50 +0100185# Create inline parser object
186my $inline = KorAP::XML::TEI::Inline->new(
187 $skip_inline_tokens,
Akrone2819a12021-10-12 15:52:55 +0200188 \%skip_inline_tags,
189 $inline_tokens_exclusive
Akroneb12e232021-02-25 13:49:50 +0100190);
191
192
Akrond53913c2021-02-24 09:50:13 +0100193# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100194MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200195
Akrond53913c2021-02-24 09:50:13 +0100196 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100197 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200198
Akroneaa96232020-10-15 17:06:15 +0200199 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100200 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200201 $input_enc = $2;
202 next;
203 };
204
205 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100206 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200207
Akrond3e1d282021-02-24 14:51:27 +0100208 # Start of text body
209 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100210 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200211
Akrond53913c2021-02-24 09:50:13 +0100212 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200213 die $log->fatal("input line number $.: " .
214 "line with opening text-body tag '${_TEXT_BODY}' " .
215 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200216 };
Peter Harders6f526a32020-06-29 21:44:41 +0200217
Akrond53913c2021-02-24 09:50:13 +0100218 # Text body data extracted from input document ($input_fh),
219 # further processed by XML::LibXML::Reader
220 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200221
Akron347be812020-09-29 07:52:52 +0200222 # Iterate over all lines in the text body
223 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200224
Akrond3e1d282021-02-24 14:51:27 +0100225 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200226 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100227 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200228
Akrond53913c2021-02-24 09:50:13 +0100229 # End of text body
Akronb43b4912021-02-25 10:31:11 +0100230 if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200231
Akron91705d72021-02-19 10:59:45 +0100232 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200233
Akrond53913c2021-02-24 09:50:13 +0100234 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200235 die $log->fatal("input line number $.: " .
236 "line with closing text-body tag '${_TEXT_BODY}'".
237 " contains additional information ... => Aborting (line=$_)");
238 };
Peter Harders6f526a32020-06-29 21:44:41 +0200239
Akrondafaa7a2021-02-19 15:17:58 +0100240 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100241 $log->warn(
242 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100243 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100244 );
Akrondafaa7a2021-02-19 15:17:58 +0100245 next MAIN;
246 };
Peter Harders6f526a32020-06-29 21:44:41 +0200247
Akroneb12e232021-02-25 13:49:50 +0100248 # Parse inline structure
249 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100250
251 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100252 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100253 };
254
Akroneb12e232021-02-25 13:49:50 +0100255 my $data = $inline->data;
256
Akrond53913c2021-02-24 09:50:13 +0100257 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100258 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100259 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100260 $text_id_esc
261 );
262
Akrond53913c2021-02-24 09:50:13 +0100263 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100264 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100265
266 # Tokenize and output
267 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100268 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100269 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100270 );
Akrond53ab4b2021-02-24 09:56:12 +0100271
272 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100273 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100274 };
Akrondafaa7a2021-02-19 15:17:58 +0100275 };
Peter Harders6f526a32020-06-29 21:44:41 +0200276
Akrond53913c2021-02-24 09:50:13 +0100277 # Tokenize with internal tokenizer
278 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200279
Akrondafaa7a2021-02-19 15:17:58 +0100280 # Tokenize and output
281 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100282 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200283 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100284 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200285
Akrondafaa7a2021-02-19 15:17:58 +0100286 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100287 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100288 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100289 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100290 };
Akrona10ad592020-08-03 11:20:23 +0200291
Akrondafaa7a2021-02-19 15:17:58 +0100292 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100293 if (!$inline->structures->empty) {
294 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100295 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100296 $text_id_esc,
297 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100298 );
Akrondafaa7a2021-02-19 15:17:58 +0100299 };
300
301 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100302 unless ($skip_inline_tokens || $inline->tokens->empty) {
303 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100304 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100305 $text_id_esc,
Akron692d17d2021-03-05 13:21:03 +0100306 # Either 0 = tokens without inline or 1 = tokens with inline
307 !$skip_inline_token_annotations
Akroneb12e232021-02-25 13:49:50 +0100308 );
Akrondafaa7a2021-02-19 15:17:58 +0100309 };
310
311 # reinit.
312 $dir = '';
313
Akron347be812020-09-29 07:52:52 +0200314 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200315 };
316
Peter Harders6f526a32020-06-29 21:44:41 +0200317
Akron347be812020-09-29 07:52:52 +0200318 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200319
Akronf8088e62021-02-18 16:18:59 +0100320 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100321
Akrond53913c2021-02-24 09:50:13 +0100322 # TODO:
323 # Maybe it's best, to keep the stripping of whitespace and
324 # to just remove the if-clause and to insert a blank by default
325 # (with possibly an option on how newlines in primary text should
326 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100327
328 # Remove consecutive whitespace at beginning and end (mostly one newline)
329 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200330
Akrond53913c2021-02-24 09:50:13 +0100331 # NOTE:
332 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200333
Akrond53913c2021-02-24 09:50:13 +0100334 # TODO:
335 # find a better solution, or create a warning, if a text has more
336 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200337
Akrond53913c2021-02-24 09:50:13 +0100338 # TODO:
339 # do testing with 2 different corpora
340 # (one with only one-line texts, the other with several lines per text)
341
342 # line contains at least one tag with at least one character contents
343 if (m/<[^>]+>[^<]/) {
344
345 # Increment counter for text lines
346 $text_line++;
347
348 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100349 # (for 2nd line and consecutive lines)
350 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200351 }
Akronf57ed812020-07-27 10:37:52 +0200352
Akron347be812020-09-29 07:52:52 +0200353 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100354 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200355 };
Akrond3e1d282021-02-24 14:51:27 +0100356 }
Akronf57ed812020-07-27 10:37:52 +0200357
Akrond3e1d282021-02-24 14:51:27 +0100358 # Start of header section
359 elsif (m#^(.*)(\<${_HEADER_TAG}[^>]*?type=["'].*)$#) {
Akronf57ed812020-07-27 10:37:52 +0200360
Akron347be812020-09-29 07:52:52 +0200361 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200362
Akrond20898f2021-02-19 15:52:17 +0100363 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100364 die $log->fatal(
365 "input line number $.: " .
366 'line with opening header tag is not in expected format ... ' .
367 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200368 };
369
370 # Parse header
Akroneaa96232020-10-15 17:06:15 +0200371 my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200372
373 # Header was parseable
374 if ($header) {
375
376 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100377 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200378
Akronb3649472020-09-29 08:24:46 +0200379 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200380
381 $header->to_zip($zipper->new_stream($file));
382
383 # Header is for text level
384 if ($header->type eq 'text') {
385
386 # Remember dir and sigles
387 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200388 $text_id_esc = $header->id_esc;
389
390 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100391 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200392
Akrond53913c2021-02-24 09:50:13 +0100393 # Reset counter for text lines
394 # (needed for whitespace handling)
395 $text_line = 0;
396 };
397 };
398 };
399};
Peter Hardersd892a582020-02-12 15:45:22 +0100400
Akron347be812020-09-29 07:52:52 +0200401$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200402
Akron9df4a242021-02-19 15:31:16 +0100403$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100404
Akrond53913c2021-02-24 09:50:13 +0100405close $input_fh;
406
Peter Harders6f526a32020-06-29 21:44:41 +0200407
Akrond949e182020-02-14 12:23:57 +0100408__END__
409
410=pod
411
412=encoding utf8
413
414=head1 NAME
415
416tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
417
418=head1 SYNOPSIS
419
Akrona2cb2812021-10-30 10:29:08 +0200420 cat corpus.i5.xml | tei2korapxml - > corpus.korapxml.zip
Akrond949e182020-02-14 12:23:57 +0100421
422=head1 DESCRIPTION
423
Akronee434b12020-07-08 12:53:01 +0200424C<tei2korapxml> is a script to convert TEI P5 and
Akrond72baca2021-07-23 13:25:32 +0200425L<I5|https://www.ids-mannheim.de/digspra/kl/projekte/korpora/textmodell>
Akronee434b12020-07-08 12:53:01 +0200426based documents to the
427L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
Peter Harders6f526a32020-06-29 21:44:41 +0200428
Akrond949e182020-02-14 12:23:57 +0100429This program is usually called from inside another script.
430
Akronee434b12020-07-08 12:53:01 +0200431=head1 FORMATS
432
433=head2 Input restrictions
434
435=over 2
436
437=item
438
Akronee434b12020-07-08 12:53:01 +0200439TEI P5 formatted input with certain restrictions:
440
441=over 4
442
443=item
444
445B<mandatory>: text-header with integrated textsigle, text-body
446
447=item
448
449B<optional>: corp-header with integrated corpsigle,
450doc-header with integrated docsigle
451
452=back
453
454=item
455
Akron0c41ab32020-09-29 07:33:33 +0200456All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200457newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200458(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200459into blanks between 2 tokens could lead to additional blanks,
460where there should be none (e.g.: punctuation characters like C<,> or
461C<.> should not be seperated from their predecessor token).
Akron8a0c4bf2021-03-16 16:51:21 +0100462(see also code section C<~ whitespace handling ~> in C<script/tei2korapxml>).
Akronee434b12020-07-08 12:53:01 +0200463
Akron940ca6f2021-10-11 12:38:39 +0200464=item
465
466Header types, like C<E<lt>idsHeader [...] type="document" [...] E<gt>>
467need to be defined in the same line as the header tag.
468
Akronee434b12020-07-08 12:53:01 +0200469=back
470
471=head2 Notes on the output
472
473=over 2
474
475=item
476
477zip file output (default on C<stdout>) with utf8 encoded entries
478(which together form the KorAP-XML format)
479
480=back
481
Akrond949e182020-02-14 12:23:57 +0100482=head1 INSTALLATION
483
Marc Kupietze83a4e92021-03-16 20:51:26 +0100484C<tei2korapxml> requires L<libxml2-dev> bindings and L<File::ShareDir::Install> to be installed.
485When these requirements are met, the preferred way to install the script is
Akrond949e182020-02-14 12:23:57 +0100486to use L<cpanm|App::cpanminus>.
487
488 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
489
490In case everything went well, the C<tei2korapxml> tool will
491be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200492
Akrond949e182020-02-14 12:23:57 +0100493Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
494
495=head1 OPTIONS
496
497=over 2
498
Akrona2cb2812021-10-30 10:29:08 +0200499=item B<--input|-i>
500
501The input file to process. If no specific input is defined and a single
502dash C<-> is passed as an argument, data is read from C<STDIN>.
503
504
Akron4e603a52020-07-27 14:23:49 +0200505=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100506
Akron4e603a52020-07-27 14:23:49 +0200507The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100508
509=item B<--help|-h>
510
511Print help information.
512
513=item B<--version|-v>
514
515Print version information.
516
Akron4e603a52020-07-27 14:23:49 +0200517=item B<--tokenizer-call|-tc>
518
519Call an external tokenizer process, that will tokenize
Akron11484782021-11-03 20:12:14 +0100520from STDIN and outputs the offsets of all tokens.
521
522Texts are separated using C<\x04\n>. The external process
523should add a new line per text.
524
525If the L</--use-tokenizer-sentence-splits> option is activated,
526sentences are marked by offset as well in new lines.
527
528To use L<Datok|https://github.com/KorAP/Datok> including sentence
529splitting, call C<tei2korap> as follows:
530
531 $ cat corpus.i5.xml | tei2korapxml -s \
532 $ -tc 'datok tokenize \
533 $ -t ./tokenizer.matok \
534 $ -p --newline-after-eot --no-sentences \
535 $ --no-tokens --sentence-positions -' - \
536 $ > corpus.korapxml.zip
Akron4e603a52020-07-27 14:23:49 +0200537
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200538=item B<--tokenizer-korap|-tk>
539
540Use the standard KorAP/DeReKo tokenizer.
541
Akron6d7b8e42020-09-29 07:37:41 +0200542=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200543
544Tokenize the data using two embedded tokenizers,
545that will take an I<Aggressive> and a I<conservative>
546approach.
547
Akron75d63142021-02-23 18:40:56 +0100548=item B<--skip-inline-tokens>
549
550Boolean flag indicating that inline tokens should not
551be processed. Defaults to false (meaning inline tokens will be processed).
552
Akron692d17d2021-03-05 13:21:03 +0100553=item B<--skip-inline-token-annotations>
554
555Boolean flag indicating that inline token annotations should not
556be processed. Defaults to true (meaning inline token annotations
557won't be processed).
558
Akronca70a1d2021-02-25 16:21:31 +0100559=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100560
561Expects a comma-separated list of tags to be ignored when the structure
562is parsed. Content of these tags however will be processed.
563
Akron1a5271a2021-02-18 13:18:15 +0100564=item B<--inline-tokens> <foundry>#[<file>]
565
566Define the foundry and file (without extension)
567to store inline token information in.
Akron8a0c4bf2021-03-16 16:51:21 +0100568Unless C<--skip-inline-token-annotations> is set,
569this will contain annotations as well.
Akron1a5271a2021-02-18 13:18:15 +0100570Defaults to C<tokens> and C<morpho>.
571
Akrone2819a12021-10-12 15:52:55 +0200572The inline token data will also be stored in the
573inline structures file (see I<--inline-structures>),
574unless the inline token foundry is prepended
575by an B<!> exclamation mark, indicating that inline
576tokens are stored exclusively in the inline tokens
577file.
578
579Example:
580
581 tei2korapxml --inline-tokens '!gingko#morpho' < data.i5.xml > korapxml.zip
582
Akrondd0be8f2021-02-18 19:29:41 +0100583=item B<--inline-structures> <foundry>#[<file>]
584
585Define the foundry and file (without extension)
586to store inline structure information in.
587Defaults to C<struct> and C<structures>.
588
Akron26a71522021-02-19 10:27:37 +0100589=item B<--base-foundry> <foundry>
590
591Define the base foundry to store newly generated
592token information in.
593Defaults to C<base>.
594
595=item B<--data-file> <file>
596
597Define the file (without extension)
598to store primary data information in.
599Defaults to C<data>.
600
601=item B<--header-file> <file>
602
603Define the file name (without extension)
604to store header information on
605the corpus, document, and text level in.
606Defaults to C<header>.
607
Marc Kupietz985da0c2021-02-15 19:29:50 +0100608=item B<--use-tokenizer-sentence-splits|-s>
609
610Replace existing with, or add new, sentence boundary information
Akron11484782021-11-03 20:12:14 +0100611provided by the tokenizer.
612Currently KorAP-tokenizer and certain external tokenizers support
613these boundaries.
Marc Kupietz985da0c2021-02-15 19:29:50 +0100614
Akron91705d72021-02-19 10:59:45 +0100615=item B<--tokens-file> <file>
616
617Define the file (without extension)
618to store generated token information in
619(either from the KorAP tokenizer or an externally called tokenizer).
620Defaults to C<tokens>.
621
Akron3378dfd2020-08-01 15:01:36 +0200622=item B<--log|-l>
623
624Loglevel for I<Log::Any>. Defaults to C<notice>.
625
Akrond949e182020-02-14 12:23:57 +0100626=back
627
Akronb3649472020-09-29 08:24:46 +0200628=head1 ENVIRONMENT VARIABLES
629
630=over 2
631
632=item B<KORAPXMLTEI_DEBUG>
633
634Activate minimal debugging.
635Defaults to C<false>.
636
Akronb3649472020-09-29 08:24:46 +0200637=back
638
Akrond949e182020-02-14 12:23:57 +0100639=head1 COPYRIGHT AND LICENSE
640
Marc Kupietze955ecc2021-02-17 17:42:01 +0100641Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100642
643Author: Peter Harders
644
Akronaabd0952020-09-29 07:35:08 +0200645Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100646
647L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
648Corpus Analysis Platform at the
Akrond72baca2021-07-23 13:25:32 +0200649L<Leibniz Institute for the German Language (IDS)|https://www.ids-mannheim.de/>,
Akrond949e182020-02-14 12:23:57 +0100650member of the
651L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
652
653This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100654L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100655
656=cut
Akronf8088e62021-02-18 16:18:59 +0100657
658# NOTES
659
Akronf8088e62021-02-18 16:18:59 +0100660## Notes on segfault prevention
661
Akron91577922021-02-19 10:32:54 +0100662binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100663(see notes on 'PerlIO layers' in 'man XML::LibXML'),
664removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
665see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
666see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.