blob: e0b07eac20b49fce92c5736a67ac52f10ee94489 [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
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
Peter Harders6f526a32020-06-29 21:44:41 +020045# Parse options from the command line
Peter Hardersd892a582020-02-12 15:45:22 +010046GetOptions(
Akrond3e1d282021-02-24 14:51:27 +010047 'root|r=s' => \(my $root_dir = '.'),
48 'input|i=s' => \(my $input_fname = ''),
Akron75d63142021-02-23 18:40:56 +010049 'tokenizer-call|tc=s' => \(my $tokenizer_call),
50 'tokenizer-korap|tk' => \(my $tokenizer_korap),
Akrond53913c2021-02-24 09:50:13 +010051 'tokenizer-internal|ti' => \(my $tokenizer_intern),
Akron75d63142021-02-23 18:40:56 +010052 'use-tokenizer-sentence-splits|s' => \(my $use_tokenizer_sentence_splits),
53 'inline-tokens=s' => \(my $inline_tokens = 'tokens#morpho'),
54 'inline-structures=s' => \(my $inline_structures = 'struct#structure'),
55 'skip-inline-tokens' => \(my $skip_inline_tokens = 0),
Akron692d17d2021-03-05 13:21:03 +010056 'skip-inline-token-annotations' => \(
57 my $skip_inline_token_annotations = ($ENV{KORAPXMLTEI_INLINE} ? 0 : 1)),
Akron54c3ff12021-02-25 11:33:37 +010058 'skip-inline-tags=s' => \(my $skip_inline_tags_str = ''),
Akrond3e1d282021-02-24 14:51:27 +010059 'base-foundry=s' => \(my $base_dir = 'base'),
60 'data-file=s' => \(my $data_file = 'data'),
Akrond53913c2021-02-24 09:50:13 +010061 'header-file=s' => \(my $header_file = 'header'),
62 'tokens-file=s' => \(my $tokens_file = 'tokens'),
Akrond3e1d282021-02-24 14:51:27 +010063 'log|l=s' => \(my $log_level = 'notice'),
Akron75d63142021-02-23 18:40:56 +010064 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010065 pod2usage(
66 -verbose => 99,
67 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
68 -msg => $VERSION_MSG,
69 -output => '-'
70 )
71 },
72 'version|v' => sub {
73 pod2usage(
74 -verbose => 0,
75 -msg => $VERSION_MSG,
76 -output => '-'
Akrond3e1d282021-02-24 14:51:27 +010077 );
Akrond949e182020-02-14 12:23:57 +010078 }
Peter Hardersd892a582020-02-12 15:45:22 +010079);
80
Akrond3e1d282021-02-24 14:51:27 +010081
Akronb87c58d2021-02-23 17:23:30 +010082# Establish logger
Akron33db4ec2021-02-24 12:52:21 +010083binmode(STDERR, ':encoding(UTF-8)');
Akron3378dfd2020-08-01 15:01:36 +020084Log::Any::Adapter->set('Stderr', log_level => $log_level);
Akronb3649472020-09-29 08:24:46 +020085$log->notice('Debugging is activated') if DEBUG;
86
Akrond3e1d282021-02-24 14:51:27 +010087
Akron0529e512021-02-22 09:55:35 +010088# tag (without attributes), which contains the primary text
89my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +020090# optional
Akron09e0b2c2020-07-28 15:57:01 +020091
Akron0529e512021-02-22 09:55:35 +010092# TODO: IDS-specific (and redundant)
93my $_HEADER_TAG = 'idsHeader';
Akron0c41ab32020-09-29 07:33:33 +020094
Akrond3e1d282021-02-24 14:51:27 +010095
96# Define tokenizers
Marc Kupietz985da0c2021-02-15 19:29:50 +010097if ($use_tokenizer_sentence_splits && !$tokenizer_korap) {
Akron33db4ec2021-02-24 12:52:21 +010098 die $log->fatal(
99 'Sentence splitting is currently only supported by KorAP tokenizer ' .
100 '(use -tk to activate it)'
101 );
Akronb87c58d2021-02-23 17:23:30 +0100102};
Marc Kupietz985da0c2021-02-15 19:29:50 +0100103
Akron54c3ff12021-02-25 11:33:37 +0100104# Remember to skip certain inline tags
105my %skip_inline_tags = ();
106if ($skip_inline_tags_str) {
107 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
108 $skip_inline_tags{$_} = 1;
109 };
110};
111
Akrond3e1d282021-02-24 14:51:27 +0100112# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200113my $ext_tok;
114if ($tokenizer_call) {
115 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
116}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200117
Akron0c41ab32020-09-29 07:33:33 +0200118elsif ($tokenizer_korap) {
Marc Kupietz985da0c2021-02-15 19:29:50 +0100119 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron54c3ff12021-02-25 11:33:37 +0100120 if ($use_tokenizer_sentence_splits) {
121 $skip_inline_tags{s} = 1;
122 };
Akron0c41ab32020-09-29 07:33:33 +0200123};
Peter Harders6f526a32020-06-29 21:44:41 +0200124
Akron0c41ab32020-09-29 07:33:33 +0200125
Akrond3e1d282021-02-24 14:51:27 +0100126# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100127my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
128my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100129
Peter Harders41c35622020-07-12 01:16:22 +0200130
Akrondd0be8f2021-02-18 19:29:41 +0100131# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100132# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100133my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100134
Akron1a5271a2021-02-18 13:18:15 +0100135# Name of the directory and the file containing all inline token informations
136# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
137my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100138
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)
156my $input_fh = *STDIN;
Peter Hardersd892a582020-02-12 15:45:22 +0100157
Akrond53913c2021-02-24 09:50:13 +0100158if ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200159 unless (open($input_fh, '<', $input_fname)) {
160 die $log->fatal("File '$input_fname' could not be opened.");
161 };
Akrond53913c2021-02-24 09:50:13 +0100162};
Peter Harders6f526a32020-06-29 21:44:41 +0200163
Akronf8088e62021-02-18 16:18:59 +0100164# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200165binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200166
Peter Harders6f526a32020-06-29 21:44:41 +0200167
Akroneb12e232021-02-25 13:49:50 +0100168# Create inline parser object
169my $inline = KorAP::XML::TEI::Inline->new(
170 $skip_inline_tokens,
171 \%skip_inline_tags
172);
173
174
Akrond53913c2021-02-24 09:50:13 +0100175# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100176MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200177
Akrond53913c2021-02-24 09:50:13 +0100178 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100179 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200180
Akroneaa96232020-10-15 17:06:15 +0200181 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100182 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200183 $input_enc = $2;
184 next;
185 };
186
187 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100188 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200189
Akrond3e1d282021-02-24 14:51:27 +0100190 # Start of text body
191 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100192 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200193
Akrond53913c2021-02-24 09:50:13 +0100194 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200195 die $log->fatal("input line number $.: " .
196 "line with opening text-body tag '${_TEXT_BODY}' " .
197 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200198 };
Peter Harders6f526a32020-06-29 21:44:41 +0200199
Akrond53913c2021-02-24 09:50:13 +0100200 # Text body data extracted from input document ($input_fh),
201 # further processed by XML::LibXML::Reader
202 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200203
Akron347be812020-09-29 07:52:52 +0200204 # Iterate over all lines in the text body
205 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200206
Akrond3e1d282021-02-24 14:51:27 +0100207 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200208 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100209 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200210
Akrond53913c2021-02-24 09:50:13 +0100211 # End of text body
Akronb43b4912021-02-25 10:31:11 +0100212 if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200213
Akron91705d72021-02-19 10:59:45 +0100214 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200215
Akrond53913c2021-02-24 09:50:13 +0100216 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200217 die $log->fatal("input line number $.: " .
218 "line with closing text-body tag '${_TEXT_BODY}'".
219 " contains additional information ... => Aborting (line=$_)");
220 };
Peter Harders6f526a32020-06-29 21:44:41 +0200221
Akrondafaa7a2021-02-19 15:17:58 +0100222 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100223 $log->warn(
224 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100225 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100226 );
Akrondafaa7a2021-02-19 15:17:58 +0100227 next MAIN;
228 };
Peter Harders6f526a32020-06-29 21:44:41 +0200229
Akroneb12e232021-02-25 13:49:50 +0100230 # Parse inline structure
231 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100232
233 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100234 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100235 };
236
Akroneb12e232021-02-25 13:49:50 +0100237 my $data = $inline->data;
238
Akrond53913c2021-02-24 09:50:13 +0100239 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100240 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100241 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100242 $text_id_esc
243 );
244
Akrond53913c2021-02-24 09:50:13 +0100245 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100246 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100247
248 # Tokenize and output
249 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100250 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100251 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100252 );
Akrond53ab4b2021-02-24 09:56:12 +0100253
254 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100255 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100256 };
Akrondafaa7a2021-02-19 15:17:58 +0100257 };
Peter Harders6f526a32020-06-29 21:44:41 +0200258
Akrond53913c2021-02-24 09:50:13 +0100259 # Tokenize with internal tokenizer
260 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200261
Akrondafaa7a2021-02-19 15:17:58 +0100262 # Tokenize and output
263 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100264 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200265 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100266 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200267
Akrondafaa7a2021-02-19 15:17:58 +0100268 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100269 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100270 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100271 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100272 };
Akrona10ad592020-08-03 11:20:23 +0200273
Akrondafaa7a2021-02-19 15:17:58 +0100274 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100275 if (!$inline->structures->empty) {
276 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100277 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100278 $text_id_esc,
279 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100280 );
Akrondafaa7a2021-02-19 15:17:58 +0100281 };
282
283 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100284 unless ($skip_inline_tokens || $inline->tokens->empty) {
285 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100286 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100287 $text_id_esc,
Akron692d17d2021-03-05 13:21:03 +0100288 # Either 0 = tokens without inline or 1 = tokens with inline
289 !$skip_inline_token_annotations
Akroneb12e232021-02-25 13:49:50 +0100290 );
Akrondafaa7a2021-02-19 15:17:58 +0100291 };
292
293 # reinit.
294 $dir = '';
295
Akron347be812020-09-29 07:52:52 +0200296 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200297 };
298
Peter Harders6f526a32020-06-29 21:44:41 +0200299
Akron347be812020-09-29 07:52:52 +0200300 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200301
Akronf8088e62021-02-18 16:18:59 +0100302 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100303
Akrond53913c2021-02-24 09:50:13 +0100304 # TODO:
305 # Maybe it's best, to keep the stripping of whitespace and
306 # to just remove the if-clause and to insert a blank by default
307 # (with possibly an option on how newlines in primary text should
308 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100309
310 # Remove consecutive whitespace at beginning and end (mostly one newline)
311 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200312
Akrond53913c2021-02-24 09:50:13 +0100313 # NOTE:
314 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200315
Akrond53913c2021-02-24 09:50:13 +0100316 # TODO:
317 # find a better solution, or create a warning, if a text has more
318 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200319
Akrond53913c2021-02-24 09:50:13 +0100320 # TODO:
321 # do testing with 2 different corpora
322 # (one with only one-line texts, the other with several lines per text)
323
324 # line contains at least one tag with at least one character contents
325 if (m/<[^>]+>[^<]/) {
326
327 # Increment counter for text lines
328 $text_line++;
329
330 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100331 # (for 2nd line and consecutive lines)
332 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200333 }
Akronf57ed812020-07-27 10:37:52 +0200334
Akron347be812020-09-29 07:52:52 +0200335 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100336 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200337 };
Akrond3e1d282021-02-24 14:51:27 +0100338 }
Akronf57ed812020-07-27 10:37:52 +0200339
Akrond3e1d282021-02-24 14:51:27 +0100340 # Start of header section
341 elsif (m#^(.*)(\<${_HEADER_TAG}[^>]*?type=["'].*)$#) {
Akronf57ed812020-07-27 10:37:52 +0200342
Akron347be812020-09-29 07:52:52 +0200343 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200344
Akrond20898f2021-02-19 15:52:17 +0100345 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100346 die $log->fatal(
347 "input line number $.: " .
348 'line with opening header tag is not in expected format ... ' .
349 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200350 };
351
352 # Parse header
Akroneaa96232020-10-15 17:06:15 +0200353 my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200354
355 # Header was parseable
356 if ($header) {
357
358 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100359 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200360
Akronb3649472020-09-29 08:24:46 +0200361 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200362
363 $header->to_zip($zipper->new_stream($file));
364
365 # Header is for text level
366 if ($header->type eq 'text') {
367
368 # Remember dir and sigles
369 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200370 $text_id_esc = $header->id_esc;
371
372 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100373 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200374
Akrond53913c2021-02-24 09:50:13 +0100375 # Reset counter for text lines
376 # (needed for whitespace handling)
377 $text_line = 0;
378 };
379 };
380 };
381};
Peter Hardersd892a582020-02-12 15:45:22 +0100382
Akron347be812020-09-29 07:52:52 +0200383$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200384
Akron9df4a242021-02-19 15:31:16 +0100385$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100386
Akrond53913c2021-02-24 09:50:13 +0100387close $input_fh;
388
Peter Harders6f526a32020-06-29 21:44:41 +0200389
Akrond949e182020-02-14 12:23:57 +0100390__END__
391
392=pod
393
394=encoding utf8
395
396=head1 NAME
397
398tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
399
400=head1 SYNOPSIS
401
402 cat corpus.i5.xml | tei2korapxml > corpus.korapxml.zip
403
404=head1 DESCRIPTION
405
Akronee434b12020-07-08 12:53:01 +0200406C<tei2korapxml> is a script to convert TEI P5 and
407L<I5|https://www1.ids-mannheim.de/kl/projekte/korpora/textmodell.html>
408based documents to the
409L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
410If no specific input is defined, data is
Akrond949e182020-02-14 12:23:57 +0100411read from C<STDIN>. If no specific output is defined, data is written
412to C<STDOUT>.
Peter Harders6f526a32020-06-29 21:44:41 +0200413
Akrond949e182020-02-14 12:23:57 +0100414This program is usually called from inside another script.
415
Akronee434b12020-07-08 12:53:01 +0200416=head1 FORMATS
417
418=head2 Input restrictions
419
420=over 2
421
422=item
423
Akronee434b12020-07-08 12:53:01 +0200424TEI P5 formatted input with certain restrictions:
425
426=over 4
427
428=item
429
430B<mandatory>: text-header with integrated textsigle, text-body
431
432=item
433
434B<optional>: corp-header with integrated corpsigle,
435doc-header with integrated docsigle
436
437=back
438
439=item
440
Akron0c41ab32020-09-29 07:33:33 +0200441All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200442newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200443(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200444into blanks between 2 tokens could lead to additional blanks,
445where there should be none (e.g.: punctuation characters like C<,> or
446C<.> should not be seperated from their predecessor token).
447(see also code section C<~ whitespace handling ~>).
448
449=back
450
451=head2 Notes on the output
452
453=over 2
454
455=item
456
457zip file output (default on C<stdout>) with utf8 encoded entries
458(which together form the KorAP-XML format)
459
460=back
461
Akrond949e182020-02-14 12:23:57 +0100462=head1 INSTALLATION
463
464C<tei2korapxml> requires L<libxml2-dev> bindings to build. When
465these bindings are available, the preferred way to install the script is
466to use L<cpanm|App::cpanminus>.
467
468 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
469
470In case everything went well, the C<tei2korapxml> tool will
471be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200472
Akrond949e182020-02-14 12:23:57 +0100473Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
474
475=head1 OPTIONS
476
477=over 2
478
Akron4e603a52020-07-27 14:23:49 +0200479=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100480
Akron4e603a52020-07-27 14:23:49 +0200481The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100482
483=item B<--help|-h>
484
485Print help information.
486
487=item B<--version|-v>
488
489Print version information.
490
Akron4e603a52020-07-27 14:23:49 +0200491=item B<--tokenizer-call|-tc>
492
493Call an external tokenizer process, that will tokenize
494a single line from STDIN and outputs one token per line.
495
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200496=item B<--tokenizer-korap|-tk>
497
498Use the standard KorAP/DeReKo tokenizer.
499
Akron6d7b8e42020-09-29 07:37:41 +0200500=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200501
502Tokenize the data using two embedded tokenizers,
503that will take an I<Aggressive> and a I<conservative>
504approach.
505
Akron75d63142021-02-23 18:40:56 +0100506=item B<--skip-inline-tokens>
507
508Boolean flag indicating that inline tokens should not
509be processed. Defaults to false (meaning inline tokens will be processed).
510
Akron692d17d2021-03-05 13:21:03 +0100511=item B<--skip-inline-token-annotations>
512
513Boolean flag indicating that inline token annotations should not
514be processed. Defaults to true (meaning inline token annotations
515won't be processed).
516
Akronca70a1d2021-02-25 16:21:31 +0100517=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100518
519Expects a comma-separated list of tags to be ignored when the structure
520is parsed. Content of these tags however will be processed.
521
Akron1a5271a2021-02-18 13:18:15 +0100522=item B<--inline-tokens> <foundry>#[<file>]
523
524Define the foundry and file (without extension)
525to store inline token information in.
526If L</KORAPXMLTEI_INLINE> is set, this will contain
527annotations as well.
528Defaults to C<tokens> and C<morpho>.
529
Akrondd0be8f2021-02-18 19:29:41 +0100530=item B<--inline-structures> <foundry>#[<file>]
531
532Define the foundry and file (without extension)
533to store inline structure information in.
534Defaults to C<struct> and C<structures>.
535
Akron26a71522021-02-19 10:27:37 +0100536=item B<--base-foundry> <foundry>
537
538Define the base foundry to store newly generated
539token information in.
540Defaults to C<base>.
541
542=item B<--data-file> <file>
543
544Define the file (without extension)
545to store primary data information in.
546Defaults to C<data>.
547
548=item B<--header-file> <file>
549
550Define the file name (without extension)
551to store header information on
552the corpus, document, and text level in.
553Defaults to C<header>.
554
Marc Kupietz985da0c2021-02-15 19:29:50 +0100555=item B<--use-tokenizer-sentence-splits|-s>
556
557Replace existing with, or add new, sentence boundary information
558provided by the KorAP tokenizer (currently supported only).
559
Akron91705d72021-02-19 10:59:45 +0100560=item B<--tokens-file> <file>
561
562Define the file (without extension)
563to store generated token information in
564(either from the KorAP tokenizer or an externally called tokenizer).
565Defaults to C<tokens>.
566
Akron3378dfd2020-08-01 15:01:36 +0200567=item B<--log|-l>
568
569Loglevel for I<Log::Any>. Defaults to C<notice>.
570
Akrond949e182020-02-14 12:23:57 +0100571=back
572
Akronb3649472020-09-29 08:24:46 +0200573=head1 ENVIRONMENT VARIABLES
574
575=over 2
576
577=item B<KORAPXMLTEI_DEBUG>
578
579Activate minimal debugging.
580Defaults to C<false>.
581
Akronb3649472020-09-29 08:24:46 +0200582=back
583
Akrond949e182020-02-14 12:23:57 +0100584=head1 COPYRIGHT AND LICENSE
585
Marc Kupietze955ecc2021-02-17 17:42:01 +0100586Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100587
588Author: Peter Harders
589
Akronaabd0952020-09-29 07:35:08 +0200590Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100591
592L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
593Corpus Analysis Platform at the
594L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>,
595member of the
596L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
597
598This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100599L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100600
601=cut
Akronf8088e62021-02-18 16:18:59 +0100602
603# NOTES
604
Akronf8088e62021-02-18 16:18:59 +0100605## Notes on segfault prevention
606
Akron91577922021-02-19 10:32:54 +0100607binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100608(see notes on 'PerlIO layers' in 'man XML::LibXML'),
609removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
610see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
611see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.