| Akron | 9cb1394 | 2020-02-14 07:39:54 +0100 | [diff] [blame] | 1 | #!/usr/bin/env perl | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 2 | use strict; | 
 | 3 | use warnings; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 4 |  | 
| Akron | 3378dfd | 2020-08-01 15:01:36 +0200 | [diff] [blame] | 5 | use Log::Any '$log'; | 
 | 6 | use Log::Any::Adapter; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 7 | use Pod::Usage; | 
 | 8 | use Getopt::Long qw(GetOptions :config no_auto_abbrev); | 
 | 9 |  | 
 | 10 | use File::Basename qw(dirname); | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 11 |  | 
| Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 12 | use Encode qw(decode); | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 13 |  | 
| Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 14 | use FindBin; | 
 | 15 | BEGIN { | 
 | 16 |   unshift @INC, "$FindBin::Bin/../lib"; | 
 | 17 | }; | 
 | 18 |  | 
| Marc Kupietz | 8a954e5 | 2021-02-16 22:03:07 +0100 | [diff] [blame] | 19 | use KorAP::XML::TEI qw!remove_xml_comments replace_entities!; | 
| Akron | 8b511f9 | 2020-07-09 17:28:08 +0200 | [diff] [blame] | 20 | use KorAP::XML::TEI::Tokenizer::External; | 
| Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 21 | use KorAP::XML::TEI::Tokenizer::Conservative; | 
 | 22 | use KorAP::XML::TEI::Tokenizer::Aggressive; | 
| Akron | 8571751 | 2020-07-08 11:19:19 +0200 | [diff] [blame] | 23 | use KorAP::XML::TEI::Zipper; | 
| Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 24 | use KorAP::XML::TEI::Header; | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 25 | use KorAP::XML::TEI::Inline; | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 26 |  | 
| Marc Kupietz | 1e882fb | 2020-09-09 00:05:46 +0200 | [diff] [blame] | 27 | eval { | 
 | 28 |   require KorAP::XML::TEI::Tokenizer::KorAP; | 
 | 29 |   1; | 
 | 30 | }; | 
| Peter Harders | 1c5ce15 | 2020-07-22 18:02:50 +0200 | [diff] [blame] | 31 |  | 
| Akron | a2cb281 | 2021-10-30 10:29:08 +0200 | [diff] [blame] | 32 | our $VERSION = '2.3.0'; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 33 |  | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 34 | our $VERSION_MSG = "\ntei2korapxml - v$VERSION\n"; | 
 | 35 |  | 
| Akron | 33db4ec | 2021-02-24 12:52:21 +0100 | [diff] [blame] | 36 | use constant { | 
 | 37 |   # Set to 1 for minimal more debug output (no need to be parametrized) | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 38 |   DEBUG => $ENV{KORAPXMLTEI_DEBUG} // 0 | 
| Akron | 33db4ec | 2021-02-24 12:52:21 +0100 | [diff] [blame] | 39 | }; | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 40 |  | 
| Akron | 692d17d | 2021-03-05 13:21:03 +0100 | [diff] [blame] | 41 | if ($ENV{KORAPXMLTEI_INLINE}) { | 
 | 42 |   warn 'KORAPXMLTEI_INLINE is deprecated in favor of --skip-inline-token-annotations'; | 
 | 43 | }; | 
 | 44 |  | 
| Akron | e2819a1 | 2021-10-12 15:52:55 +0200 | [diff] [blame] | 45 | # Inline tokens won't be stored in the structure file | 
 | 46 | my $inline_tokens_exclusive = 0; | 
 | 47 |  | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 48 | # Parse options from the command line | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 49 | GetOptions( | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 50 |   'root|r=s'              => \(my $root_dir    = '.'), | 
 | 51 |   'input|i=s'             => \(my $input_fname = ''), | 
| Akron | 75d6314 | 2021-02-23 18:40:56 +0100 | [diff] [blame] | 52 |   'tokenizer-call|tc=s'   => \(my $tokenizer_call), | 
 | 53 |   'tokenizer-korap|tk'    => \(my $tokenizer_korap), | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 54 |   'tokenizer-internal|ti' => \(my $tokenizer_intern), | 
| Akron | 75d6314 | 2021-02-23 18:40:56 +0100 | [diff] [blame] | 55 |   '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), | 
| Akron | 692d17d | 2021-03-05 13:21:03 +0100 | [diff] [blame] | 59 |   'skip-inline-token-annotations' => \( | 
 | 60 |     my $skip_inline_token_annotations = ($ENV{KORAPXMLTEI_INLINE} ? 0 : 1)), | 
| Akron | 54c3ff1 | 2021-02-25 11:33:37 +0100 | [diff] [blame] | 61 |   'skip-inline-tags=s'    => \(my $skip_inline_tags_str = ''), | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 62 |   'base-foundry=s'        => \(my $base_dir    = 'base'), | 
 | 63 |   'data-file=s'           => \(my $data_file   = 'data'), | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 64 |   'header-file=s'         => \(my $header_file = 'header'), | 
 | 65 |   'tokens-file=s'         => \(my $tokens_file = 'tokens'), | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 66 |   'log|l=s'               => \(my $log_level   = 'notice'), | 
| Akron | a2cb281 | 2021-10-30 10:29:08 +0200 | [diff] [blame] | 67 |   ''                      => \(my $stdio), | 
| Akron | 75d6314 | 2021-02-23 18:40:56 +0100 | [diff] [blame] | 68 |   'help|h' => sub { | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 69 |     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 => '-' | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 81 |     ); | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 82 |   } | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 83 | ); | 
 | 84 |  | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 85 |  | 
| Akron | b87c58d | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 86 | # Establish logger | 
| Akron | 33db4ec | 2021-02-24 12:52:21 +0100 | [diff] [blame] | 87 | binmode(STDERR, ':encoding(UTF-8)'); | 
| Akron | 3378dfd | 2020-08-01 15:01:36 +0200 | [diff] [blame] | 88 | Log::Any::Adapter->set('Stderr', log_level => $log_level); | 
| Akron | b364947 | 2020-09-29 08:24:46 +0200 | [diff] [blame] | 89 | $log->notice('Debugging is activated') if DEBUG; | 
 | 90 |  | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 91 |  | 
| Akron | 0529e51 | 2021-02-22 09:55:35 +0100 | [diff] [blame] | 92 | # tag (without attributes), which contains the primary text | 
 | 93 | my $_TEXT_BODY = 'text'; | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 94 | # optional | 
| Akron | 09e0b2c | 2020-07-28 15:57:01 +0200 | [diff] [blame] | 95 |  | 
| Akron | 0529e51 | 2021-02-22 09:55:35 +0100 | [diff] [blame] | 96 | # TODO: IDS-specific (and redundant) | 
 | 97 | my $_HEADER_TAG = 'idsHeader'; | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 98 |  | 
| Akron | 54c3ff1 | 2021-02-25 11:33:37 +0100 | [diff] [blame] | 99 | # Remember to skip certain inline tags | 
 | 100 | my %skip_inline_tags = (); | 
 | 101 | if ($skip_inline_tags_str) { | 
 | 102 |   foreach (split /\s*,\s*/, $skip_inline_tags_str) { | 
 | 103 |     $skip_inline_tags{$_} = 1; | 
 | 104 |   }; | 
 | 105 | }; | 
 | 106 |  | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 107 | # External tokenization | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 108 | my $ext_tok; | 
 | 109 | if ($tokenizer_call) { | 
 | 110 |   $ext_tok = KorAP::XML::TEI::Tokenizer::External->new($tokenizer_call); | 
| Akron | 1148478 | 2021-11-03 20:12:14 +0100 | [diff] [blame^] | 111 |   $ext_tok->sentence_splits(1) if $use_tokenizer_sentence_splits; | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 112 | } | 
| Marc Kupietz | 1e882fb | 2020-09-09 00:05:46 +0200 | [diff] [blame] | 113 |  | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 114 | elsif ($tokenizer_korap) { | 
| Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 115 |   $ext_tok = KorAP::XML::TEI::Tokenizer::KorAP->new($use_tokenizer_sentence_splits); | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 116 | }; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 117 |  | 
| Akron | 1148478 | 2021-11-03 20:12:14 +0100 | [diff] [blame^] | 118 | if ($use_tokenizer_sentence_splits) { | 
 | 119 |   $skip_inline_tags{s} = 1; | 
 | 120 | }; | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 121 |  | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 122 | # Internal tokenization | 
| Akron | b87c58d | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 123 | my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new; | 
 | 124 | my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new; | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 125 |  | 
| Peter Harders | 41c3562 | 2020-07-12 01:16:22 +0200 | [diff] [blame] | 126 |  | 
| Akron | dd0be8f | 2021-02-18 19:29:41 +0100 | [diff] [blame] | 127 | # Name of the directory and the file containing all inline structure informations | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 128 | # except for $_TOKENS_TAG information | 
| Akron | dd0be8f | 2021-02-18 19:29:41 +0100 | [diff] [blame] | 129 | my ($_structure_dir, $_structure_file) = split '#', $inline_structures . '#structure'; | 
| Akron | dd0be8f | 2021-02-18 19:29:41 +0100 | [diff] [blame] | 130 |  | 
| Akron | 1a5271a | 2021-02-18 13:18:15 +0100 | [diff] [blame] | 131 | # Name of the directory and the file containing all inline token informations | 
 | 132 | # i.e. tokens of the $_TOKENS_TAG, if $_TOKENS_PROC is set | 
 | 133 | my ($_tokens_dir, $_tokens_file) = split '#', $inline_tokens . '#morpho'; | 
| Akron | 1a5271a | 2021-02-18 13:18:15 +0100 | [diff] [blame] | 134 |  | 
| Akron | e2819a1 | 2021-10-12 15:52:55 +0200 | [diff] [blame] | 135 | if (index($_tokens_dir, '!') == 0) { | 
 | 136 |   $_tokens_dir = substr($_tokens_dir, 1); | 
 | 137 |   $inline_tokens_exclusive = 1; | 
 | 138 | }; | 
 | 139 |  | 
| Akron | b87c58d | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 140 | # Initialize zipper | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 141 | my $zipper = KorAP::XML::TEI::Zipper->new($root_dir); | 
| Akron | 09e0b2c | 2020-07-28 15:57:01 +0200 | [diff] [blame] | 142 |  | 
| Akron | bc89919 | 2021-02-24 12:14:47 +0100 | [diff] [blame] | 143 | # text directory (below $root_dir) | 
 | 144 | my $dir = ''; | 
| Akron | 09e0b2c | 2020-07-28 15:57:01 +0200 | [diff] [blame] | 145 |  | 
| Akron | bc89919 | 2021-02-24 12:14:47 +0100 | [diff] [blame] | 146 | # Escaped version of text id | 
 | 147 | my $text_id_esc; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 148 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 149 | # Default encoding of the text | 
 | 150 | my $input_enc = 'UTF-8'; | 
 | 151 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 152 | # text line (needed for whitespace handling) | 
 | 153 | my $text_line = 0; | 
 | 154 |  | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 155 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 156 | # Input file handle (default: stdin) | 
| Akron | a2cb281 | 2021-10-30 10:29:08 +0200 | [diff] [blame] | 157 | my $input_fh; | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 158 |  | 
| Akron | a2cb281 | 2021-10-30 10:29:08 +0200 | [diff] [blame] | 159 | # Single dash was set | 
 | 160 | if ($stdio) { | 
 | 161 |   $input_fh = *STDIN; | 
 | 162 | } | 
 | 163 |  | 
 | 164 | # Input flag was passed | 
 | 165 | elsif ($input_fname ne '') { | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 166 |   unless (open($input_fh, '<', $input_fname)) { | 
 | 167 |     die $log->fatal("File '$input_fname' could not be opened."); | 
 | 168 |   }; | 
| Akron | a2cb281 | 2021-10-30 10:29:08 +0200 | [diff] [blame] | 169 | } | 
 | 170 |  | 
 | 171 | # No input to process | 
 | 172 | else { | 
 | 173 |   pod2usage( | 
 | 174 |     -verbose => 99, | 
 | 175 |     -sections => 'NAME|SYNOPSIS', | 
 | 176 |     -msg => $VERSION_MSG, | 
 | 177 |     -output => '-' | 
 | 178 |   ); | 
 | 179 |   exit; | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 180 | }; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 181 |  | 
| Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 182 | # Prevents segfaulting (see notes on segfault prevention) | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 183 | binmode $input_fh; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 184 |  | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 185 |  | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 186 | # Create inline parser object | 
 | 187 | my $inline = KorAP::XML::TEI::Inline->new( | 
 | 188 |   $skip_inline_tokens, | 
| Akron | e2819a1 | 2021-10-12 15:52:55 +0200 | [diff] [blame] | 189 |   \%skip_inline_tags, | 
 | 190 |   $inline_tokens_exclusive | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 191 | ); | 
 | 192 |  | 
 | 193 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 194 | # Reading input document | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 195 | MAIN: while (<$input_fh>) { | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 196 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 197 |   # remove HTML (multi-line) comments (<!--...-->) | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 198 |   $_ = remove_xml_comments($input_fh, $_); | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 199 |  | 
| Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 200 |   # Set input encoding | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 201 |   if (index($_, '<?xml') == 0 && $_ =~ /\sencoding=(['"])([^\1]+?)\1/) { | 
| Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 202 |     $input_enc = $2; | 
 | 203 |     next; | 
 | 204 |   }; | 
 | 205 |  | 
 | 206 |   $_ = decode($input_enc, $_); | 
| Marc Kupietz | 8a954e5 | 2021-02-16 22:03:07 +0100 | [diff] [blame] | 207 |   $_ = replace_entities($_); | 
| Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 208 |  | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 209 |   # Start of text body | 
 | 210 |   if (index($_, $_TEXT_BODY) >= 0 && m#^(.*)<${_TEXT_BODY}(?: [^>]*)?>(.*)$#) { | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 211 |     my $suffix = $2; | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 212 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 213 |     if ($1 !~ /^\s*$/ || $suffix !~ /^\s*$/) { | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 214 |       die $log->fatal("input line number $.: " . | 
 | 215 |                         "line with opening text-body tag '${_TEXT_BODY}' " . | 
 | 216 |                         "contains additional information ... => Aborting (line=$_)"); | 
| Akron | 0bb7e72 | 2020-09-29 07:48:33 +0200 | [diff] [blame] | 217 |     }; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 218 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 219 |     # Text body data extracted from input document ($input_fh), | 
 | 220 |     # further processed by XML::LibXML::Reader | 
 | 221 |     my $text_buffer = ''; | 
| Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 222 |  | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 223 |     # Iterate over all lines in the text body | 
 | 224 |     while (<$input_fh>) { | 
| Peter Harders | 9015734 | 2020-07-01 21:05:14 +0200 | [diff] [blame] | 225 |  | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 226 |       $_ = remove_xml_comments($input_fh, $_); | 
| Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 227 |       $_ = decode($input_enc, $_); | 
| Marc Kupietz | 8a954e5 | 2021-02-16 22:03:07 +0100 | [diff] [blame] | 228 |       $_ = replace_entities($_); | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 229 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 230 |       # End of text body | 
| Akron | b43b491 | 2021-02-25 10:31:11 +0100 | [diff] [blame] | 231 |       if ((my $pos = index($_, '</' . $_TEXT_BODY)) >= 0) { | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 232 |  | 
| Akron | 91705d7 | 2021-02-19 10:59:45 +0100 | [diff] [blame] | 233 |         # write data.xml, structure.xml and evtl. morpho.xml and/or tokenization files | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 234 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 235 |         if ((substr($_, 0, $pos) . substr($_, length("</$_TEXT_BODY>") + $pos)) !~ /^\s*$/) { | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 236 |           die $log->fatal("input line number $.: " . | 
 | 237 |                             "line with closing text-body tag '${_TEXT_BODY}'". | 
 | 238 |                             " contains additional information ... => Aborting (line=$_)"); | 
 | 239 |         }; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 240 |  | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 241 |         if ($dir eq '') { | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 242 |           $log->warn( | 
 | 243 |             "Maybe empty textSigle => skipping this text ...\n" . | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 244 |               'data=' . substr($inline->data->data, 0, 200) | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 245 |             ); | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 246 |           next MAIN; | 
 | 247 |         }; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 248 |  | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 249 |         # Parse inline structure | 
 | 250 |         $inline->parse($text_id_esc, \$text_buffer); | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 251 |  | 
 | 252 |         if (DEBUG) { | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 253 |           $log->debug("Writing (utf8-formatted) xml file $dir/${data_file}.xml"); | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 254 |         }; | 
 | 255 |  | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 256 |         my $data = $inline->data; | 
 | 257 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 258 |         # Write data.xml | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 259 |         $data->to_zip( | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 260 |           $zipper->new_stream("$dir/${data_file}.xml"), | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 261 |           $text_id_esc | 
 | 262 |         ); | 
 | 263 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 264 |         # Tokenize with external tokenizer | 
| Akron | 9df4a24 | 2021-02-19 15:31:16 +0100 | [diff] [blame] | 265 |         if ($ext_tok) { | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 266 |  | 
 | 267 |           # Tokenize and output | 
 | 268 |           $ext_tok->tokenize($data->data)->to_zip( | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 269 |             $zipper->new_stream("$dir/$base_dir/${tokens_file}.xml"), | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 270 |             $text_id_esc | 
| Akron | d20898f | 2021-02-19 15:52:17 +0100 | [diff] [blame] | 271 |           ); | 
| Akron | d53ab4b | 2021-02-24 09:56:12 +0100 | [diff] [blame] | 272 |  | 
 | 273 |           if ($use_tokenizer_sentence_splits) { | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 274 |             $ext_tok->sentencize_from_previous_input($inline->structures); | 
| Akron | d53ab4b | 2021-02-24 09:56:12 +0100 | [diff] [blame] | 275 |           }; | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 276 |         }; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 277 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 278 |         # Tokenize with internal tokenizer | 
 | 279 |         if ($tokenizer_intern) { | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 280 |  | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 281 |           # Tokenize and output | 
 | 282 |           $cons_tok->tokenize($data->data)->to_zip( | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 283 |             $zipper->new_stream("$dir/$base_dir/" . $cons_tok->name . '.xml'), | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 284 |             $text_id_esc | 
| Akron | cc27d79 | 2021-02-24 12:32:20 +0100 | [diff] [blame] | 285 |           )->reset; | 
| Akron | 598d1a7 | 2020-08-02 17:33:31 +0200 | [diff] [blame] | 286 |  | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 287 |           $aggr_tok->tokenize($data->data)->to_zip( | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 288 |             $zipper->new_stream("$dir/$base_dir/" . $aggr_tok->name . '.xml'), | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 289 |             $text_id_esc | 
| Akron | cc27d79 | 2021-02-24 12:32:20 +0100 | [diff] [blame] | 290 |           )->reset; | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 291 |         }; | 
| Akron | a10ad59 | 2020-08-03 11:20:23 +0200 | [diff] [blame] | 292 |  | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 293 |         # ~ write structures ~ | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 294 |         if (!$inline->structures->empty) { | 
 | 295 |           $inline->structures->to_zip( | 
| Akron | b87c58d | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 296 |             $zipper->new_stream("$dir/$_structure_dir/${_structure_file}.xml"), | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 297 |             $text_id_esc, | 
 | 298 |             2 # = structure serialization | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 299 |           ); | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 300 |         }; | 
 | 301 |  | 
 | 302 |         # ~ write tokens ~ | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 303 |         unless ($skip_inline_tokens || $inline->tokens->empty) { | 
 | 304 |           $inline->tokens->to_zip( | 
| Akron | b87c58d | 2021-02-23 17:23:30 +0100 | [diff] [blame] | 305 |             $zipper->new_stream("$dir/$_tokens_dir/${_tokens_file}.xml"), | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 306 |             $text_id_esc, | 
| Akron | 692d17d | 2021-03-05 13:21:03 +0100 | [diff] [blame] | 307 |             # Either 0 = tokens without inline or 1 = tokens with inline | 
 | 308 |             !$skip_inline_token_annotations | 
| Akron | eb12e23 | 2021-02-25 13:49:50 +0100 | [diff] [blame] | 309 |           ); | 
| Akron | dafaa7a | 2021-02-19 15:17:58 +0100 | [diff] [blame] | 310 |         }; | 
 | 311 |  | 
 | 312 |         # reinit. | 
 | 313 |         $dir = ''; | 
 | 314 |  | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 315 |         next MAIN; | 
| Akron | 598d1a7 | 2020-08-02 17:33:31 +0200 | [diff] [blame] | 316 |       }; | 
 | 317 |  | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 318 |  | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 319 |       # ~ whitespace handling ~ | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 320 |  | 
| Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 321 |       # Fix whitespaces (see notes on whitespace fixing) | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 322 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 323 |       # TODO: | 
 | 324 |       #   Maybe it's best, to keep the stripping of whitespace and | 
 | 325 |       #   to just remove the if-clause and to insert a blank by default | 
 | 326 |       #   (with possibly an option on how newlines in primary text should | 
 | 327 |       #   be handled (stripped or replaced by a whitespace)). | 
| Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 328 |  | 
 | 329 |       # Remove consecutive whitespace at beginning and end (mostly one newline) | 
 | 330 |       s/^\s+//; s/\s+$//; | 
| Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 331 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 332 |       # NOTE: | 
 | 333 |       #   this is only relevant, if a text consists of more than one line | 
| Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 334 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 335 |       # TODO: | 
 | 336 |       #   find a better solution, or create a warning, if a text has more | 
 | 337 |       #   than one line ($text_line > 1) | 
| Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 338 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 339 |       # TODO: | 
 | 340 |       #   do testing with 2 different corpora | 
 | 341 |       #   (one with only one-line texts, the other with several lines per text) | 
 | 342 |  | 
 | 343 |       # line contains at least one tag with at least one character contents | 
 | 344 |       if (m/<[^>]+>[^<]/) { | 
 | 345 |  | 
 | 346 |         # Increment counter for text lines | 
 | 347 |         $text_line++; | 
 | 348 |  | 
 | 349 |         # insert blank before 1st character | 
| Akron | 6e2b125 | 2021-02-24 12:41:15 +0100 | [diff] [blame] | 350 |         # (for 2nd line and consecutive lines) | 
 | 351 |         $_ = ' ' . $_ if $text_line > 1; | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 352 |       } | 
| Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 353 |  | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 354 |       # add line to buffer | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 355 |       $text_buffer .= $_; | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 356 |     }; | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 357 |   } | 
| Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 358 |  | 
| Akron | d3e1d28 | 2021-02-24 14:51:27 +0100 | [diff] [blame] | 359 |   # Start of header section | 
 | 360 |   elsif (m#^(.*)(\<${_HEADER_TAG}[^>]*?type=["'].*)$#) { | 
| Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 361 |  | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 362 |     my $content = "$2\n"; | 
| Akron | f57ed81 | 2020-07-27 10:37:52 +0200 | [diff] [blame] | 363 |  | 
| Akron | d20898f | 2021-02-19 15:52:17 +0100 | [diff] [blame] | 364 |     if ($1 !~ /^\s*$/) { | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 365 |       die $log->fatal( | 
 | 366 |         "input line number $.: " . | 
 | 367 |           'line with opening header tag is not in expected format ... ' . | 
 | 368 |           "=> Aborting (line=$_)"); | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 369 |     }; | 
 | 370 |  | 
 | 371 |     # Parse header | 
| Akron | eaa9623 | 2020-10-15 17:06:15 +0200 | [diff] [blame] | 372 |     my $header = KorAP::XML::TEI::Header->new($content, $input_enc)->parse($input_fh); | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 373 |  | 
 | 374 |     # Header was parseable | 
 | 375 |     if ($header) { | 
 | 376 |  | 
 | 377 |       # Write header to zip | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 378 |       my $file = $header->dir . '/' . $header_file . '.xml'; | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 379 |  | 
| Akron | b364947 | 2020-09-29 08:24:46 +0200 | [diff] [blame] | 380 |       $log->debug("Writing file $file") if DEBUG; | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 381 |  | 
 | 382 |       $header->to_zip($zipper->new_stream($file)); | 
 | 383 |  | 
 | 384 |       # Header is for text level | 
 | 385 |       if ($header->type eq 'text') { | 
 | 386 |  | 
 | 387 |         # Remember dir and sigles | 
 | 388 |         $dir         = $header->dir; | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 389 |         $text_id_esc = $header->id_esc; | 
 | 390 |  | 
 | 391 |         # log output for seeing progression | 
| Akron | bc89919 | 2021-02-24 12:14:47 +0100 | [diff] [blame] | 392 |         $log->notice("$0: text_id=$text_id_esc"); | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 393 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 394 |         # Reset counter for text lines | 
 | 395 |         # (needed for whitespace handling) | 
 | 396 |         $text_line = 0; | 
 | 397 |       }; | 
 | 398 |     }; | 
 | 399 |   }; | 
 | 400 | }; | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 401 |  | 
| Akron | 347be81 | 2020-09-29 07:52:52 +0200 | [diff] [blame] | 402 | $zipper->close; | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 403 |  | 
| Akron | 9df4a24 | 2021-02-19 15:31:16 +0100 | [diff] [blame] | 404 | $ext_tok->close if $ext_tok; | 
| Peter Harders | d892a58 | 2020-02-12 15:45:22 +0100 | [diff] [blame] | 405 |  | 
| Akron | d53913c | 2021-02-24 09:50:13 +0100 | [diff] [blame] | 406 | close $input_fh; | 
 | 407 |  | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 408 |  | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 409 | __END__ | 
 | 410 |  | 
 | 411 | =pod | 
 | 412 |  | 
 | 413 | =encoding utf8 | 
 | 414 |  | 
 | 415 | =head1 NAME | 
 | 416 |  | 
 | 417 | tei2korapxml - Conversion of TEI P5 based formats to KorAP-XML | 
 | 418 |  | 
 | 419 | =head1 SYNOPSIS | 
 | 420 |  | 
| Akron | a2cb281 | 2021-10-30 10:29:08 +0200 | [diff] [blame] | 421 |   cat corpus.i5.xml | tei2korapxml - > corpus.korapxml.zip | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 422 |  | 
 | 423 | =head1 DESCRIPTION | 
 | 424 |  | 
| Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 425 | C<tei2korapxml> is a script to convert TEI P5 and | 
| Akron | d72baca | 2021-07-23 13:25:32 +0200 | [diff] [blame] | 426 | L<I5|https://www.ids-mannheim.de/digspra/kl/projekte/korpora/textmodell> | 
| Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 427 | based documents to the | 
 | 428 | L<KorAP-XML format|https://github.com/KorAP/KorAP-XML-Krill#about-korap-xml>. | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 429 |  | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 430 | This program is usually called from inside another script. | 
 | 431 |  | 
| Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 432 | =head1 FORMATS | 
 | 433 |  | 
 | 434 | =head2 Input restrictions | 
 | 435 |  | 
 | 436 | =over 2 | 
 | 437 |  | 
 | 438 | =item | 
 | 439 |  | 
| Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 440 | TEI P5 formatted input with certain restrictions: | 
 | 441 |  | 
 | 442 | =over 4 | 
 | 443 |  | 
 | 444 | =item | 
 | 445 |  | 
 | 446 | B<mandatory>: text-header with integrated textsigle, text-body | 
 | 447 |  | 
 | 448 | =item | 
 | 449 |  | 
 | 450 | B<optional>: corp-header with integrated corpsigle, | 
 | 451 | doc-header with integrated docsigle | 
 | 452 |  | 
 | 453 | =back | 
 | 454 |  | 
 | 455 | =item | 
 | 456 |  | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 457 | All tokens inside the primary text may not be | 
| Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 458 | newline seperated, because newlines are removed | 
| Akron | 0c41ab3 | 2020-09-29 07:33:33 +0200 | [diff] [blame] | 459 | (see L<KorAP::XML::TEI::Data>) and a conversion of newlines | 
| Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 460 | into blanks between 2 tokens could lead to additional blanks, | 
 | 461 | where there should be none (e.g.: punctuation characters like C<,> or | 
 | 462 | C<.> should not be seperated from their predecessor token). | 
| Akron | 8a0c4bf | 2021-03-16 16:51:21 +0100 | [diff] [blame] | 463 | (see also code section C<~ whitespace handling ~> in C<script/tei2korapxml>). | 
| Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 464 |  | 
| Akron | 940ca6f | 2021-10-11 12:38:39 +0200 | [diff] [blame] | 465 | =item | 
 | 466 |  | 
 | 467 | Header types, like C<E<lt>idsHeader [...] type="document" [...] E<gt>> | 
 | 468 | need to be defined in the same line as the header tag. | 
 | 469 |  | 
| Akron | ee434b1 | 2020-07-08 12:53:01 +0200 | [diff] [blame] | 470 | =back | 
 | 471 |  | 
 | 472 | =head2 Notes on the output | 
 | 473 |  | 
 | 474 | =over 2 | 
 | 475 |  | 
 | 476 | =item | 
 | 477 |  | 
 | 478 | zip file output (default on C<stdout>) with utf8 encoded entries | 
 | 479 | (which together form the KorAP-XML format) | 
 | 480 |  | 
 | 481 | =back | 
 | 482 |  | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 483 | =head1 INSTALLATION | 
 | 484 |  | 
| Marc Kupietz | e83a4e9 | 2021-03-16 20:51:26 +0100 | [diff] [blame] | 485 | C<tei2korapxml> requires L<libxml2-dev> bindings and L<File::ShareDir::Install> to be installed. | 
 | 486 | When these requirements are met, the preferred way to install the script is | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 487 | to use L<cpanm|App::cpanminus>. | 
 | 488 |  | 
 | 489 |   $ cpanm https://github.com/KorAP/KorAP-XML-TEI.git | 
 | 490 |  | 
 | 491 | In case everything went well, the C<tei2korapxml> tool will | 
 | 492 | be available on your command line immediately. | 
| Peter Harders | 6f526a3 | 2020-06-29 21:44:41 +0200 | [diff] [blame] | 493 |  | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 494 | Minimum requirement for L<KorAP::XML::TEI> is Perl 5.16. | 
 | 495 |  | 
 | 496 | =head1 OPTIONS | 
 | 497 |  | 
 | 498 | =over 2 | 
 | 499 |  | 
| Akron | a2cb281 | 2021-10-30 10:29:08 +0200 | [diff] [blame] | 500 | =item B<--input|-i> | 
 | 501 |  | 
 | 502 | The input file to process. If no specific input is defined and a single | 
 | 503 | dash C<-> is passed as an argument, data is read from C<STDIN>. | 
 | 504 |  | 
 | 505 |  | 
| Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 506 | =item B<--root|-r> | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 507 |  | 
| Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 508 | The root directory for output. Defaults to C<.>. | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 509 |  | 
 | 510 | =item B<--help|-h> | 
 | 511 |  | 
 | 512 | Print help information. | 
 | 513 |  | 
 | 514 | =item B<--version|-v> | 
 | 515 |  | 
 | 516 | Print version information. | 
 | 517 |  | 
| Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 518 | =item B<--tokenizer-call|-tc> | 
 | 519 |  | 
 | 520 | Call an external tokenizer process, that will tokenize | 
| Akron | 1148478 | 2021-11-03 20:12:14 +0100 | [diff] [blame^] | 521 | from STDIN and outputs the offsets of all tokens. | 
 | 522 |  | 
 | 523 | Texts are separated using C<\x04\n>. The external process | 
 | 524 | should add a new line per text. | 
 | 525 |  | 
 | 526 | If the L</--use-tokenizer-sentence-splits> option is activated, | 
 | 527 | sentences are marked by offset as well in new lines. | 
 | 528 |  | 
 | 529 | To use L<Datok|https://github.com/KorAP/Datok> including sentence | 
 | 530 | splitting, call C<tei2korap> as follows: | 
 | 531 |  | 
 | 532 |   $ cat corpus.i5.xml | tei2korapxml -s \ | 
 | 533 |   $   -tc 'datok tokenize \ | 
 | 534 |   $        -t ./tokenizer.matok \ | 
 | 535 |   $        -p --newline-after-eot --no-sentences \ | 
 | 536 |   $        --no-tokens --sentence-positions -' - \ | 
 | 537 |   $        > corpus.korapxml.zip | 
| Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 538 |  | 
| Marc Kupietz | 1e882fb | 2020-09-09 00:05:46 +0200 | [diff] [blame] | 539 | =item B<--tokenizer-korap|-tk> | 
 | 540 |  | 
 | 541 | Use the standard KorAP/DeReKo tokenizer. | 
 | 542 |  | 
| Akron | 6d7b8e4 | 2020-09-29 07:37:41 +0200 | [diff] [blame] | 543 | =item B<--tokenizer-internal|-ti> | 
| Akron | 4e603a5 | 2020-07-27 14:23:49 +0200 | [diff] [blame] | 544 |  | 
 | 545 | Tokenize the data using two embedded tokenizers, | 
 | 546 | that will take an I<Aggressive> and a I<conservative> | 
 | 547 | approach. | 
 | 548 |  | 
| Akron | 75d6314 | 2021-02-23 18:40:56 +0100 | [diff] [blame] | 549 | =item B<--skip-inline-tokens> | 
 | 550 |  | 
 | 551 | Boolean flag indicating that inline tokens should not | 
 | 552 | be processed. Defaults to false (meaning inline tokens will be processed). | 
 | 553 |  | 
| Akron | 692d17d | 2021-03-05 13:21:03 +0100 | [diff] [blame] | 554 | =item B<--skip-inline-token-annotations> | 
 | 555 |  | 
 | 556 | Boolean flag indicating that inline token annotations should not | 
 | 557 | be processed. Defaults to true (meaning inline token annotations | 
 | 558 | won't be processed). | 
 | 559 |  | 
| Akron | ca70a1d | 2021-02-25 16:21:31 +0100 | [diff] [blame] | 560 | =item B<--skip-inline-tags> <tags> | 
| Akron | 54c3ff1 | 2021-02-25 11:33:37 +0100 | [diff] [blame] | 561 |  | 
 | 562 | Expects a comma-separated list of tags to be ignored when the structure | 
 | 563 | is parsed. Content of these tags however will be processed. | 
 | 564 |  | 
| Akron | 1a5271a | 2021-02-18 13:18:15 +0100 | [diff] [blame] | 565 | =item B<--inline-tokens> <foundry>#[<file>] | 
 | 566 |  | 
 | 567 | Define the foundry and file (without extension) | 
 | 568 | to store inline token information in. | 
| Akron | 8a0c4bf | 2021-03-16 16:51:21 +0100 | [diff] [blame] | 569 | Unless C<--skip-inline-token-annotations> is set, | 
 | 570 | this will contain annotations as well. | 
| Akron | 1a5271a | 2021-02-18 13:18:15 +0100 | [diff] [blame] | 571 | Defaults to C<tokens> and C<morpho>. | 
 | 572 |  | 
| Akron | e2819a1 | 2021-10-12 15:52:55 +0200 | [diff] [blame] | 573 | The inline token data will also be stored in the | 
 | 574 | inline structures file (see I<--inline-structures>), | 
 | 575 | unless the inline token foundry is prepended | 
 | 576 | by an B<!> exclamation mark, indicating that inline | 
 | 577 | tokens are stored exclusively in the inline tokens | 
 | 578 | file. | 
 | 579 |  | 
 | 580 | Example: | 
 | 581 |  | 
 | 582 |   tei2korapxml --inline-tokens '!gingko#morpho' < data.i5.xml > korapxml.zip | 
 | 583 |  | 
| Akron | dd0be8f | 2021-02-18 19:29:41 +0100 | [diff] [blame] | 584 | =item B<--inline-structures> <foundry>#[<file>] | 
 | 585 |  | 
 | 586 | Define the foundry and file (without extension) | 
 | 587 | to store inline structure information in. | 
 | 588 | Defaults to C<struct> and C<structures>. | 
 | 589 |  | 
| Akron | 26a7152 | 2021-02-19 10:27:37 +0100 | [diff] [blame] | 590 | =item B<--base-foundry> <foundry> | 
 | 591 |  | 
 | 592 | Define the base foundry to store newly generated | 
 | 593 | token information in. | 
 | 594 | Defaults to C<base>. | 
 | 595 |  | 
 | 596 | =item B<--data-file> <file> | 
 | 597 |  | 
 | 598 | Define the file (without extension) | 
 | 599 | to store primary data information in. | 
 | 600 | Defaults to C<data>. | 
 | 601 |  | 
 | 602 | =item B<--header-file> <file> | 
 | 603 |  | 
 | 604 | Define the file name (without extension) | 
 | 605 | to store header information on | 
 | 606 | the corpus, document, and text level in. | 
 | 607 | Defaults to C<header>. | 
 | 608 |  | 
| Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 609 | =item B<--use-tokenizer-sentence-splits|-s> | 
 | 610 |  | 
 | 611 | Replace existing with, or add new, sentence boundary information | 
| Akron | 1148478 | 2021-11-03 20:12:14 +0100 | [diff] [blame^] | 612 | provided by the tokenizer. | 
 | 613 | Currently KorAP-tokenizer and certain external tokenizers support | 
 | 614 | these boundaries. | 
| Marc Kupietz | 985da0c | 2021-02-15 19:29:50 +0100 | [diff] [blame] | 615 |  | 
| Akron | 91705d7 | 2021-02-19 10:59:45 +0100 | [diff] [blame] | 616 | =item B<--tokens-file> <file> | 
 | 617 |  | 
 | 618 | Define the file (without extension) | 
 | 619 | to store generated token information in | 
 | 620 | (either from the KorAP tokenizer or an externally called tokenizer). | 
 | 621 | Defaults to C<tokens>. | 
 | 622 |  | 
| Akron | 3378dfd | 2020-08-01 15:01:36 +0200 | [diff] [blame] | 623 | =item B<--log|-l> | 
 | 624 |  | 
 | 625 | Loglevel for I<Log::Any>. Defaults to C<notice>. | 
 | 626 |  | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 627 | =back | 
 | 628 |  | 
| Akron | b364947 | 2020-09-29 08:24:46 +0200 | [diff] [blame] | 629 | =head1 ENVIRONMENT VARIABLES | 
 | 630 |  | 
 | 631 | =over 2 | 
 | 632 |  | 
 | 633 | =item B<KORAPXMLTEI_DEBUG> | 
 | 634 |  | 
 | 635 | Activate minimal debugging. | 
 | 636 | Defaults to C<false>. | 
 | 637 |  | 
| Akron | b364947 | 2020-09-29 08:24:46 +0200 | [diff] [blame] | 638 | =back | 
 | 639 |  | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 640 | =head1 COPYRIGHT AND LICENSE | 
 | 641 |  | 
| Marc Kupietz | e955ecc | 2021-02-17 17:42:01 +0100 | [diff] [blame] | 642 | Copyright (C) 2021, L<IDS Mannheim|https://www.ids-mannheim.de/> | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 643 |  | 
 | 644 | Author: Peter Harders | 
 | 645 |  | 
| Akron | aabd095 | 2020-09-29 07:35:08 +0200 | [diff] [blame] | 646 | Contributors: Nils Diewald, Marc Kupietz, Carsten Schnober | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 647 |  | 
 | 648 | L<KorAP::XML::TEI> is developed as part of the L<KorAP|https://korap.ids-mannheim.de/> | 
 | 649 | Corpus Analysis Platform at the | 
| Akron | d72baca | 2021-07-23 13:25:32 +0200 | [diff] [blame] | 650 | L<Leibniz Institute for the German Language (IDS)|https://www.ids-mannheim.de/>, | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 651 | member of the | 
 | 652 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>. | 
 | 653 |  | 
 | 654 | This program is free software published under the | 
| Marc Kupietz | e955ecc | 2021-02-17 17:42:01 +0100 | [diff] [blame] | 655 | L<BSD-2 License|https://opensource.org/licenses/BSD-2-Clause>. | 
| Akron | d949e18 | 2020-02-14 12:23:57 +0100 | [diff] [blame] | 656 |  | 
 | 657 | =cut | 
| Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 658 |  | 
 | 659 | # NOTES | 
 | 660 |  | 
| Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 661 | ## Notes on segfault prevention | 
 | 662 |  | 
| Akron | 9157792 | 2021-02-19 10:32:54 +0100 | [diff] [blame] | 663 | binmode on the input handler prevents segfaulting of 'XML::LibXML::Reader' inside the main loop | 
| Akron | f8088e6 | 2021-02-18 16:18:59 +0100 | [diff] [blame] | 664 | (see notes on 'PerlIO layers' in  'man XML::LibXML'), | 
 | 665 | removing 'use open qw(:std :utf8)' would fix this problem too, but using binmode on input is more granular | 
 | 666 | see in perluniintro: You can switch encodings on an already opened stream by using "binmode() | 
 | 667 | see in perlfunc: If LAYER is omitted or specified as ":raw" the filehandle is made suitable for passing binary data. |