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