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