blob: 6f3ad1fae582353b82f99934b9ab91505780b53f [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
Akrona2cb2812021-10-30 10:29:08 +020032our $VERSION = '2.3.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'),
Akrona2cb2812021-10-30 10:29:08 +020067 '' => \(my $stdio),
Akron75d63142021-02-23 18:40:56 +010068 'help|h' => sub {
Akrond949e182020-02-14 12:23:57 +010069 pod2usage(
70 -verbose => 99,
71 -sections => 'NAME|DESCRIPTION|SYNOPSIS|ARGUMENTS|OPTIONS',
72 -msg => $VERSION_MSG,
73 -output => '-'
74 )
75 },
76 'version|v' => sub {
77 pod2usage(
78 -verbose => 0,
79 -msg => $VERSION_MSG,
80 -output => '-'
Akrond3e1d282021-02-24 14:51:27 +010081 );
Akrond949e182020-02-14 12:23:57 +010082 }
Peter Hardersd892a582020-02-12 15:45:22 +010083);
84
Akrond3e1d282021-02-24 14:51:27 +010085
Akronb87c58d2021-02-23 17:23:30 +010086# Establish logger
Akron33db4ec2021-02-24 12:52:21 +010087binmode(STDERR, ':encoding(UTF-8)');
Akron3378dfd2020-08-01 15:01:36 +020088Log::Any::Adapter->set('Stderr', log_level => $log_level);
Akronb3649472020-09-29 08:24:46 +020089$log->notice('Debugging is activated') if DEBUG;
90
Akrond3e1d282021-02-24 14:51:27 +010091
Akron0529e512021-02-22 09:55:35 +010092# tag (without attributes), which contains the primary text
93my $_TEXT_BODY = 'text';
Akron0c41ab32020-09-29 07:33:33 +020094# optional
Akron09e0b2c2020-07-28 15:57:01 +020095
Akron0529e512021-02-22 09:55:35 +010096# TODO: IDS-specific (and redundant)
97my $_HEADER_TAG = 'idsHeader';
Akron0c41ab32020-09-29 07:33:33 +020098
Akrond3e1d282021-02-24 14:51:27 +010099
100# Define tokenizers
Marc Kupietz985da0c2021-02-15 19:29:50 +0100101if ($use_tokenizer_sentence_splits && !$tokenizer_korap) {
Akron33db4ec2021-02-24 12:52:21 +0100102 die $log->fatal(
103 'Sentence splitting is currently only supported by KorAP tokenizer ' .
104 '(use -tk to activate it)'
105 );
Akronb87c58d2021-02-23 17:23:30 +0100106};
Marc Kupietz985da0c2021-02-15 19:29:50 +0100107
Akron54c3ff12021-02-25 11:33:37 +0100108# Remember to skip certain inline tags
109my %skip_inline_tags = ();
110if ($skip_inline_tags_str) {
111 foreach (split /\s*,\s*/, $skip_inline_tags_str) {
112 $skip_inline_tags{$_} = 1;
113 };
114};
115
Akrond3e1d282021-02-24 14:51:27 +0100116# External tokenization
Akron0c41ab32020-09-29 07:33:33 +0200117my $ext_tok;
118if ($tokenizer_call) {
119 $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call);
120}
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200121
Akron0c41ab32020-09-29 07:33:33 +0200122elsif ($tokenizer_korap) {
Marc Kupietz985da0c2021-02-15 19:29:50 +0100123 $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits);
Akron54c3ff12021-02-25 11:33:37 +0100124 if ($use_tokenizer_sentence_splits) {
125 $skip_inline_tags{s} = 1;
126 };
Akron0c41ab32020-09-29 07:33:33 +0200127};
Peter Harders6f526a32020-06-29 21:44:41 +0200128
Akron0c41ab32020-09-29 07:33:33 +0200129
Akrond3e1d282021-02-24 14:51:27 +0100130# Internal tokenization
Akronb87c58d2021-02-23 17:23:30 +0100131my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new;
132my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new;
Akrond3e1d282021-02-24 14:51:27 +0100133
Peter Harders41c35622020-07-12 01:16:22 +0200134
Akrondd0be8f2021-02-18 19:29:41 +0100135# Name of the directory and the file containing all inline structure informations
Akrond53913c2021-02-24 09:50:13 +0100136# except for $_TOKENS_TAG information
Akrondd0be8f2021-02-18 19:29:41 +0100137my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure';
Akrondd0be8f2021-02-18 19:29:41 +0100138
Akron1a5271a2021-02-18 13:18:15 +0100139# Name of the directory and the file containing all inline token informations
140# i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set
141my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho';
Akron1a5271a2021-02-18 13:18:15 +0100142
Akrone2819a12021-10-12 15:52:55 +0200143if (index($_tokens_dir, '!') == 0) {
144 $_tokens_dir = substr($_tokens_dir, 1);
145 $inline_tokens_exclusive = 1;
146};
147
Akronb87c58d2021-02-23 17:23:30 +0100148# Initialize zipper
Akrond53913c2021-02-24 09:50:13 +0100149my $zipper = KorAP::XML::TEI::Zipper->new($root_dir);
Akron09e0b2c2020-07-28 15:57:01 +0200150
Akronbc899192021-02-24 12:14:47 +0100151# text directory (below $root_dir)
152my $dir = '';
Akron09e0b2c2020-07-28 15:57:01 +0200153
Akronbc899192021-02-24 12:14:47 +0100154# Escaped version of text id
155my $text_id_esc;
Peter Harders6f526a32020-06-29 21:44:41 +0200156
Akrond53913c2021-02-24 09:50:13 +0100157# Default encoding of the text
158my $input_enc = 'UTF-8';
159
Akrond53913c2021-02-24 09:50:13 +0100160# text line (needed for whitespace handling)
161my $text_line = 0;
162
Peter Harders6f526a32020-06-29 21:44:41 +0200163
Akrond53913c2021-02-24 09:50:13 +0100164# Input file handle (default: stdin)
Akrona2cb2812021-10-30 10:29:08 +0200165my $input_fh;
Peter Hardersd892a582020-02-12 15:45:22 +0100166
Akrona2cb2812021-10-30 10:29:08 +0200167# Single dash was set
168if ($stdio) {
169 $input_fh = *STDIN;
170}
171
172# Input flag was passed
173elsif ($input_fname ne '') {
Akron347be812020-09-29 07:52:52 +0200174 unless (open($input_fh, '<', $input_fname)) {
175 die $log->fatal("File '$input_fname' could not be opened.");
176 };
Akrona2cb2812021-10-30 10:29:08 +0200177}
178
179# No input to process
180else {
181 pod2usage(
182 -verbose => 99,
183 -sections => 'NAME|SYNOPSIS',
184 -msg => $VERSION_MSG,
185 -output => '-'
186 );
187 exit;
Akrond53913c2021-02-24 09:50:13 +0100188};
Peter Harders6f526a32020-06-29 21:44:41 +0200189
Akronf8088e62021-02-18 16:18:59 +0100190# Prevents segfaulting (see notes on segfault prevention)
Akron347be812020-09-29 07:52:52 +0200191binmode $input_fh;
Peter Harders6f526a32020-06-29 21:44:41 +0200192
Peter Harders6f526a32020-06-29 21:44:41 +0200193
Akroneb12e232021-02-25 13:49:50 +0100194# Create inline parser object
195my $inline = KorAP::XML::TEI::Inline->new(
196 $skip_inline_tokens,
Akrone2819a12021-10-12 15:52:55 +0200197 \%skip_inline_tags,
198 $inline_tokens_exclusive
Akroneb12e232021-02-25 13:49:50 +0100199);
200
201
Akrond53913c2021-02-24 09:50:13 +0100202# Reading input document
Akrond3e1d282021-02-24 14:51:27 +0100203MAIN: while (<$input_fh>) {
Akron347be812020-09-29 07:52:52 +0200204
Akrond53913c2021-02-24 09:50:13 +0100205 # remove HTML (multi-line) comments (<!--...-->)
Akrond3e1d282021-02-24 14:51:27 +0100206 $_ = remove_xml_comments($input_fh, $_);
Akron347be812020-09-29 07:52:52 +0200207
Akroneaa96232020-10-15 17:06:15 +0200208 # Set input encoding
Akrond53913c2021-02-24 09:50:13 +0100209 if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) {
Akroneaa96232020-10-15 17:06:15 +0200210 $input_enc = $2;
211 next;
212 };
213
214 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100215 $_ = replace_entities($_);
Akroneaa96232020-10-15 17:06:15 +0200216
Akrond3e1d282021-02-24 14:51:27 +0100217 # Start of text body
218 if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) {
Akrond53913c2021-02-24 09:50:13 +0100219 my $suffix = $2;
Akron347be812020-09-29 07:52:52 +0200220
Akrond53913c2021-02-24 09:50:13 +0100221 if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200222 die $log->fatal("input line number $.: " .
223 "line with opening text-body tag '${_TEXT_BODY}' " .
224 "contains additional information ... => Aborting (line=$_)");
Akron0bb7e722020-09-29 07:48:33 +0200225 };
Peter Harders6f526a32020-06-29 21:44:41 +0200226
Akrond53913c2021-02-24 09:50:13 +0100227 # Text body data extracted from input document ($input_fh),
228 # further processed by XML::LibXML::Reader
229 my $text_buffer = '';
Peter Harders90157342020-07-01 21:05:14 +0200230
Akron347be812020-09-29 07:52:52 +0200231 # Iterate over all lines in the text body
232 while (<$input_fh>) {
Peter Harders90157342020-07-01 21:05:14 +0200233
Akrond3e1d282021-02-24 14:51:27 +0100234 $_ = remove_xml_comments($input_fh, $_);
Akroneaa96232020-10-15 17:06:15 +0200235 $_ = decode($input_enc, $_);
Marc Kupietz8a954e52021-02-16 22:03:07 +0100236 $_ = replace_entities($_);
Peter Harders6f526a32020-06-29 21:44:41 +0200237
Akrond53913c2021-02-24 09:50:13 +0100238 # End of text body
Akronb43b4912021-02-25 10:31:11 +0100239 if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) {
Peter Harders6f526a32020-06-29 21:44:41 +0200240
Akron91705d72021-02-19 10:59:45 +0100241 # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files
Peter Harders6f526a32020-06-29 21:44:41 +0200242
Akrond53913c2021-02-24 09:50:13 +0100243 if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) {
Akron347be812020-09-29 07:52:52 +0200244 die $log->fatal("input line number $.: " .
245 "line with closing text-body tag '${_TEXT_BODY}'".
246 " contains additional information ... => Aborting (line=$_)");
247 };
Peter Harders6f526a32020-06-29 21:44:41 +0200248
Akrondafaa7a2021-02-19 15:17:58 +0100249 if ($dir eq '') {
Akrond53913c2021-02-24 09:50:13 +0100250 $log->warn(
251 "Maybe empty textSigle => skipping this text ...\n" .
Akroneb12e232021-02-25 13:49:50 +0100252 'data=' . substr($inline->data->data, 0, 200)
Akrond53913c2021-02-24 09:50:13 +0100253 );
Akrondafaa7a2021-02-19 15:17:58 +0100254 next MAIN;
255 };
Peter Harders6f526a32020-06-29 21:44:41 +0200256
Akroneb12e232021-02-25 13:49:50 +0100257 # Parse inline structure
258 $inline->parse($text_id_esc, \$text_buffer);
Akrondafaa7a2021-02-19 15:17:58 +0100259
260 if (DEBUG) {
Akrond53913c2021-02-24 09:50:13 +0100261 $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml");
Akrondafaa7a2021-02-19 15:17:58 +0100262 };
263
Akroneb12e232021-02-25 13:49:50 +0100264 my $data = $inline->data;
265
Akrond53913c2021-02-24 09:50:13 +0100266 # Write data.xml
Akrondafaa7a2021-02-19 15:17:58 +0100267 $data->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100268 $zipper->new_stream("$dir/${data_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100269 $text_id_esc
270 );
271
Akrond53913c2021-02-24 09:50:13 +0100272 # Tokenize with external tokenizer
Akron9df4a242021-02-19 15:31:16 +0100273 if ($ext_tok) {
Akrondafaa7a2021-02-19 15:17:58 +0100274
275 # Tokenize and output
276 $ext_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100277 $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100278 $text_id_esc
Akrond20898f2021-02-19 15:52:17 +0100279 );
Akrond53ab4b2021-02-24 09:56:12 +0100280
281 if ($use_tokenizer_sentence_splits) {
Akroneb12e232021-02-25 13:49:50 +0100282 $ext_tok->sentencize_from_previous_input($inline->structures);
Akrond53ab4b2021-02-24 09:56:12 +0100283 };
Akrondafaa7a2021-02-19 15:17:58 +0100284 };
Peter Harders6f526a32020-06-29 21:44:41 +0200285
Akrond53913c2021-02-24 09:50:13 +0100286 # Tokenize with internal tokenizer
287 if ($tokenizer_intern) {
Peter Harders6f526a32020-06-29 21:44:41 +0200288
Akrondafaa7a2021-02-19 15:17:58 +0100289 # Tokenize and output
290 $cons_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100291 $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'),
Akron347be812020-09-29 07:52:52 +0200292 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100293 )->reset;
Akron598d1a72020-08-02 17:33:31 +0200294
Akrondafaa7a2021-02-19 15:17:58 +0100295 $aggr_tok->tokenize($data->data)->to_zip(
Akrond53913c2021-02-24 09:50:13 +0100296 $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'),
Akrondafaa7a2021-02-19 15:17:58 +0100297 $text_id_esc
Akroncc27d792021-02-24 12:32:20 +0100298 )->reset;
Akrondafaa7a2021-02-19 15:17:58 +0100299 };
Akrona10ad592020-08-03 11:20:23 +0200300
Akrondafaa7a2021-02-19 15:17:58 +0100301 # ~ write structures ~
Akroneb12e232021-02-25 13:49:50 +0100302 if (!$inline->structures->empty) {
303 $inline->structures->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100304 $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100305 $text_id_esc,
306 2 # = structure serialization
Akroneb12e232021-02-25 13:49:50 +0100307 );
Akrondafaa7a2021-02-19 15:17:58 +0100308 };
309
310 # ~ write tokens ~
Akroneb12e232021-02-25 13:49:50 +0100311 unless ($skip_inline_tokens || $inline->tokens->empty) {
312 $inline->tokens->to_zip(
Akronb87c58d2021-02-23 17:23:30 +0100313 $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"),
Akrondafaa7a2021-02-19 15:17:58 +0100314 $text_id_esc,
Akron692d17d2021-03-05 13:21:03 +0100315 # Either 0 = tokens without inline or 1 = tokens with inline
316 !$skip_inline_token_annotations
Akroneb12e232021-02-25 13:49:50 +0100317 );
Akrondafaa7a2021-02-19 15:17:58 +0100318 };
319
320 # reinit.
321 $dir = '';
322
Akron347be812020-09-29 07:52:52 +0200323 next MAIN;
Akron598d1a72020-08-02 17:33:31 +0200324 };
325
Peter Harders6f526a32020-06-29 21:44:41 +0200326
Akron347be812020-09-29 07:52:52 +0200327 # ~ whitespace handling ~
Peter Harders6f526a32020-06-29 21:44:41 +0200328
Akronf8088e62021-02-18 16:18:59 +0100329 # Fix whitespaces (see notes on whitespace fixing)
Peter Hardersd892a582020-02-12 15:45:22 +0100330
Akrond53913c2021-02-24 09:50:13 +0100331 # TODO:
332 # Maybe it's best, to keep the stripping of whitespace and
333 # to just remove the if-clause and to insert a blank by default
334 # (with possibly an option on how newlines in primary text should
335 # be handled (stripped or replaced by a whitespace)).
Akronf8088e62021-02-18 16:18:59 +0100336
337 # Remove consecutive whitespace at beginning and end (mostly one newline)
338 s/^\s+//; s/\s+$//;
Akronf57ed812020-07-27 10:37:52 +0200339
Akrond53913c2021-02-24 09:50:13 +0100340 # NOTE:
341 # this is only relevant, if a text consists of more than one line
Akronf57ed812020-07-27 10:37:52 +0200342
Akrond53913c2021-02-24 09:50:13 +0100343 # TODO:
344 # find a better solution, or create a warning, if a text has more
345 # than one line ($text_line > 1)
Akronf57ed812020-07-27 10:37:52 +0200346
Akrond53913c2021-02-24 09:50:13 +0100347 # TODO:
348 # do testing with 2 different corpora
349 # (one with only one-line texts, the other with several lines per text)
350
351 # line contains at least one tag with at least one character contents
352 if (m/<[^>]+>[^<]/) {
353
354 # Increment counter for text lines
355 $text_line++;
356
357 # insert blank before 1st character
Akron6e2b1252021-02-24 12:41:15 +0100358 # (for 2nd line and consecutive lines)
359 $_ = ' ' . $_ if $text_line > 1;
Akron347be812020-09-29 07:52:52 +0200360 }
Akronf57ed812020-07-27 10:37:52 +0200361
Akron347be812020-09-29 07:52:52 +0200362 # add line to buffer
Akrond53913c2021-02-24 09:50:13 +0100363 $text_buffer .= $_;
Akron347be812020-09-29 07:52:52 +0200364 };
Akrond3e1d282021-02-24 14:51:27 +0100365 }
Akronf57ed812020-07-27 10:37:52 +0200366
Akrond3e1d282021-02-24 14:51:27 +0100367 # Start of header section
368 elsif (m#^(.*)(\<${_HEADER_TAG}[^>]*?type=["'].*)$#) {
Akronf57ed812020-07-27 10:37:52 +0200369
Akron347be812020-09-29 07:52:52 +0200370 my $content = "$2\n";
Akronf57ed812020-07-27 10:37:52 +0200371
Akrond20898f2021-02-19 15:52:17 +0100372 if ($1 !~ /^\s*$/) {
Akrond53913c2021-02-24 09:50:13 +0100373 die $log->fatal(
374 "input line number $.: " .
375 'line with opening header tag is not in expected format ... ' .
376 "=> Aborting (line=$_)");
Akron347be812020-09-29 07:52:52 +0200377 };
378
379 # Parse header
Akroneaa96232020-10-15 17:06:15 +0200380 my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh);
Akron347be812020-09-29 07:52:52 +0200381
382 # Header was parseable
383 if ($header) {
384
385 # Write header to zip
Akrond53913c2021-02-24 09:50:13 +0100386 my $file = $header->dir . '/' . $header_file . '.xml';
Akron347be812020-09-29 07:52:52 +0200387
Akronb3649472020-09-29 08:24:46 +0200388 $log->debug("Writing file $file") if DEBUG;
Akron347be812020-09-29 07:52:52 +0200389
390 $header->to_zip($zipper->new_stream($file));
391
392 # Header is for text level
393 if ($header->type eq 'text') {
394
395 # Remember dir and sigles
396 $dir = $header->dir;
Akron347be812020-09-29 07:52:52 +0200397 $text_id_esc = $header->id_esc;
398
399 # log output for seeing progression
Akronbc899192021-02-24 12:14:47 +0100400 $log->notice("$0: text_id=$text_id_esc");
Akron347be812020-09-29 07:52:52 +0200401
Akrond53913c2021-02-24 09:50:13 +0100402 # Reset counter for text lines
403 # (needed for whitespace handling)
404 $text_line = 0;
405 };
406 };
407 };
408};
Peter Hardersd892a582020-02-12 15:45:22 +0100409
Akron347be812020-09-29 07:52:52 +0200410$zipper->close;
Peter Harders6f526a32020-06-29 21:44:41 +0200411
Akron9df4a242021-02-19 15:31:16 +0100412$ext_tok->close if $ext_tok;
Peter Hardersd892a582020-02-12 15:45:22 +0100413
Akrond53913c2021-02-24 09:50:13 +0100414close $input_fh;
415
Peter Harders6f526a32020-06-29 21:44:41 +0200416
Akrond949e182020-02-14 12:23:57 +0100417__END__
418
419=pod
420
421=encoding utf8
422
423=head1 NAME
424
425tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML
426
427=head1 SYNOPSIS
428
Akrona2cb2812021-10-30 10:29:08 +0200429 cat corpus.i5.xml | tei2korapxml - > corpus.korapxml.zip
Akrond949e182020-02-14 12:23:57 +0100430
431=head1 DESCRIPTION
432
Akronee434b12020-07-08 12:53:01 +0200433C<tei2korapxml> is a script to convert TEI P5 and
Akrond72baca2021-07-23 13:25:32 +0200434L<I5|https://www.ids-mannheim.de/digspra/kl/projekte/korpora/textmodell>
Akronee434b12020-07-08 12:53:01 +0200435based documents to the
436L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>.
Peter Harders6f526a32020-06-29 21:44:41 +0200437
Akrond949e182020-02-14 12:23:57 +0100438This program is usually called from inside another script.
439
Akronee434b12020-07-08 12:53:01 +0200440=head1 FORMATS
441
442=head2 Input restrictions
443
444=over 2
445
446=item
447
Akronee434b12020-07-08 12:53:01 +0200448TEI P5 formatted input with certain restrictions:
449
450=over 4
451
452=item
453
454B<mandatory>: text-header with integrated textsigle, text-body
455
456=item
457
458B<optional>: corp-header with integrated corpsigle,
459doc-header with integrated docsigle
460
461=back
462
463=item
464
Akron0c41ab32020-09-29 07:33:33 +0200465All tokens inside the primary text may not be
Akronee434b12020-07-08 12:53:01 +0200466newline seperated, because newlines are removed
Akron0c41ab32020-09-29 07:33:33 +0200467(see L<KorAP::XML::TEI::Data>) and a conversion of newlines
Akronee434b12020-07-08 12:53:01 +0200468into blanks between 2 tokens could lead to additional blanks,
469where there should be none (e.g.: punctuation characters like C<,> or
470C<.> should not be seperated from their predecessor token).
Akron8a0c4bf2021-03-16 16:51:21 +0100471(see also code section C<~ whitespace handling ~> in C<script/tei2korapxml>).
Akronee434b12020-07-08 12:53:01 +0200472
Akron940ca6f2021-10-11 12:38:39 +0200473=item
474
475Header types, like C<E<lt>idsHeader [...] type="document" [...] E<gt>>
476need to be defined in the same line as the header tag.
477
Akronee434b12020-07-08 12:53:01 +0200478=back
479
480=head2 Notes on the output
481
482=over 2
483
484=item
485
486zip file output (default on C<stdout>) with utf8 encoded entries
487(which together form the KorAP-XML format)
488
489=back
490
Akrond949e182020-02-14 12:23:57 +0100491=head1 INSTALLATION
492
Marc Kupietze83a4e92021-03-16 20:51:26 +0100493C<tei2korapxml> requires L<libxml2-dev> bindings and L<File::ShareDir::Install> to be installed.
494When these requirements are met, the preferred way to install the script is
Akrond949e182020-02-14 12:23:57 +0100495to use L<cpanm|App::cpanminus>.
496
497 $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git
498
499In case everything went well, the C<tei2korapxml> tool will
500be available on your command line immediately.
Peter Harders6f526a32020-06-29 21:44:41 +0200501
Akrond949e182020-02-14 12:23:57 +0100502Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16.
503
504=head1 OPTIONS
505
506=over 2
507
Akrona2cb2812021-10-30 10:29:08 +0200508=item B<--input|-i>
509
510The input file to process. If no specific input is defined and a single
511dash C<-> is passed as an argument, data is read from C<STDIN>.
512
513
Akron4e603a52020-07-27 14:23:49 +0200514=item B<--root|-r>
Akrond949e182020-02-14 12:23:57 +0100515
Akron4e603a52020-07-27 14:23:49 +0200516The root directory for output. Defaults to C<.>.
Akrond949e182020-02-14 12:23:57 +0100517
518=item B<--help|-h>
519
520Print help information.
521
522=item B<--version|-v>
523
524Print version information.
525
Akron4e603a52020-07-27 14:23:49 +0200526=item B<--tokenizer-call|-tc>
527
528Call an external tokenizer process, that will tokenize
529a single line from STDIN and outputs one token per line.
530
Marc Kupietz1e882fb2020-09-09 00:05:46 +0200531=item B<--tokenizer-korap|-tk>
532
533Use the standard KorAP/DeReKo tokenizer.
534
Akron6d7b8e42020-09-29 07:37:41 +0200535=item B<--tokenizer-internal|-ti>
Akron4e603a52020-07-27 14:23:49 +0200536
537Tokenize the data using two embedded tokenizers,
538that will take an I<Aggressive> and a I<conservative>
539approach.
540
Akron75d63142021-02-23 18:40:56 +0100541=item B<--skip-inline-tokens>
542
543Boolean flag indicating that inline tokens should not
544be processed. Defaults to false (meaning inline tokens will be processed).
545
Akron692d17d2021-03-05 13:21:03 +0100546=item B<--skip-inline-token-annotations>
547
548Boolean flag indicating that inline token annotations should not
549be processed. Defaults to true (meaning inline token annotations
550won't be processed).
551
Akronca70a1d2021-02-25 16:21:31 +0100552=item B<--skip-inline-tags> <tags>
Akron54c3ff12021-02-25 11:33:37 +0100553
554Expects a comma-separated list of tags to be ignored when the structure
555is parsed. Content of these tags however will be processed.
556
Akron1a5271a2021-02-18 13:18:15 +0100557=item B<--inline-tokens> <foundry>#[<file>]
558
559Define the foundry and file (without extension)
560to store inline token information in.
Akron8a0c4bf2021-03-16 16:51:21 +0100561Unless C<--skip-inline-token-annotations> is set,
562this will contain annotations as well.
Akron1a5271a2021-02-18 13:18:15 +0100563Defaults to C<tokens> and C<morpho>.
564
Akrone2819a12021-10-12 15:52:55 +0200565The inline token data will also be stored in the
566inline structures file (see I<--inline-structures>),
567unless the inline token foundry is prepended
568by an B<!> exclamation mark, indicating that inline
569tokens are stored exclusively in the inline tokens
570file.
571
572Example:
573
574 tei2korapxml --inline-tokens '!gingko#morpho' < data.i5.xml > korapxml.zip
575
Akrondd0be8f2021-02-18 19:29:41 +0100576=item B<--inline-structures> <foundry>#[<file>]
577
578Define the foundry and file (without extension)
579to store inline structure information in.
580Defaults to C<struct> and C<structures>.
581
Akron26a71522021-02-19 10:27:37 +0100582=item B<--base-foundry> <foundry>
583
584Define the base foundry to store newly generated
585token information in.
586Defaults to C<base>.
587
588=item B<--data-file> <file>
589
590Define the file (without extension)
591to store primary data information in.
592Defaults to C<data>.
593
594=item B<--header-file> <file>
595
596Define the file name (without extension)
597to store header information on
598the corpus, document, and text level in.
599Defaults to C<header>.
600
Marc Kupietz985da0c2021-02-15 19:29:50 +0100601=item B<--use-tokenizer-sentence-splits|-s>
602
603Replace existing with, or add new, sentence boundary information
604provided by the KorAP tokenizer (currently supported only).
605
Akron91705d72021-02-19 10:59:45 +0100606=item B<--tokens-file> <file>
607
608Define the file (without extension)
609to store generated token information in
610(either from the KorAP tokenizer or an externally called tokenizer).
611Defaults to C<tokens>.
612
Akron3378dfd2020-08-01 15:01:36 +0200613=item B<--log|-l>
614
615Loglevel for I<Log::Any>. Defaults to C<notice>.
616
Akrond949e182020-02-14 12:23:57 +0100617=back
618
Akronb3649472020-09-29 08:24:46 +0200619=head1 ENVIRONMENT VARIABLES
620
621=over 2
622
623=item B<KORAPXMLTEI_DEBUG>
624
625Activate minimal debugging.
626Defaults to C<false>.
627
Akronb3649472020-09-29 08:24:46 +0200628=back
629
Akrond949e182020-02-14 12:23:57 +0100630=head1 COPYRIGHT AND LICENSE
631
Marc Kupietze955ecc2021-02-17 17:42:01 +0100632Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/>
Akrond949e182020-02-14 12:23:57 +0100633
634Author: Peter Harders
635
Akronaabd0952020-09-29 07:35:08 +0200636Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober
Akrond949e182020-02-14 12:23:57 +0100637
638L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/>
639Corpus Analysis Platform at the
Akrond72baca2021-07-23 13:25:32 +0200640L<Leibniz Institute for the German Language (IDS)|https://www.ids-mannheim.de/>,
Akrond949e182020-02-14 12:23:57 +0100641member of the
642L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>.
643
644This program is free software published under the
Marc Kupietze955ecc2021-02-17 17:42:01 +0100645L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>.
Akrond949e182020-02-14 12:23:57 +0100646
647=cut
Akronf8088e62021-02-18 16:18:59 +0100648
649# NOTES
650
Akronf8088e62021-02-18 16:18:59 +0100651## Notes on segfault prevention
652
Akron91577922021-02-19 10:32:54 +0100653binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop
Akronf8088e62021-02-18 16:18:59 +0100654(see notes on 'PerlIO layers' in 'man XML::LibXML'),
655removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular
656see in perluniintro: You can switch encodings on an already opened stream by using "binmode()
657see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data.