Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 4 | use v5.10; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 5 | use FindBin; |
| 6 | BEGIN { unshift @INC, "$FindBin::Bin/../lib" }; |
| 7 | use File::Spec::Functions qw/catfile catdir/; |
| 8 | use Getopt::Long qw/GetOptions :config no_auto_abbrev/; |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 9 | use Benchmark qw/:hireswallclock/; |
| 10 | use IO::Compress::Gzip qw/$GzipError/; |
Akron | c11f798 | 2017-02-21 21:20:14 +0100 | [diff] [blame] | 11 | use POSIX qw/ceil/; |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 12 | use Log::Log4perl; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 13 | use Pod::Usage; |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 14 | use Cache::FastMmap; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 15 | use Directory::Iterator; |
Akron | 41127e3 | 2020-08-07 12:46:19 +0200 | [diff] [blame] | 16 | use KorAP::XML::Krill qw!get_file_name get_file_name_from_glob!; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 17 | use KorAP::XML::Archive; |
Akron | 93d620e | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 18 | use KorAP::XML::Tokenizer; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 19 | use KorAP::XML::Batch::File; |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 20 | use Config::Simple; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 21 | use Parallel::ForkManager; |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 22 | use File::Glob ':bsd_glob'; |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 23 | use File::Temp qw/tempdir/; |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 24 | use File::Path qw(remove_tree make_path); |
Akron | 9a062ce | 2017-07-04 19:12:05 +0200 | [diff] [blame] | 25 | use File::Basename; |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 26 | use Mojo::Collection 'c'; |
| 27 | use String::Random qw(random_string); |
Akron | 081639e | 2017-04-21 19:01:39 +0200 | [diff] [blame] | 28 | use IO::File; |
| 29 | use Archive::Tar::Builder; |
Akron | da3097e | 2017-04-23 19:53:57 +0200 | [diff] [blame] | 30 | use Fcntl qw(:flock SEEK_END); |
Akron | c11f798 | 2017-02-21 21:20:14 +0100 | [diff] [blame] | 31 | |
| 32 | # use KorAP::XML::ForkPool; |
Akron | 75ba57d | 2016-03-07 23:36:27 +0100 | [diff] [blame] | 33 | # TODO: use Parallel::Loops |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 34 | # TODO: make output files |
Akron | 93d620e | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 35 | |
Akron | c11f798 | 2017-02-21 21:20:14 +0100 | [diff] [blame] | 36 | # TODO: Use KorAP::XML::ForkPool! |
| 37 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 38 | # CHANGES: |
| 39 | # ---------------------------------------------------------- |
| 40 | # 2013/11/25 |
| 41 | # - Initial release |
| 42 | # |
| 43 | # 2014/10/29 |
| 44 | # - Merges foundry data to create indexer friendly documents |
| 45 | # |
Akron | 93d620e | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 46 | # 2016/02/04 |
| 47 | # - renamed to korapxml2krill |
| 48 | # - added Schreibgebrauch support |
Akron | 069bd71 | 2016-02-12 19:09:06 +0100 | [diff] [blame] | 49 | # |
| 50 | # 2016/02/12 |
| 51 | # - fixed foundry skipping |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 52 | # - Support overwrite in archive processing |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame] | 53 | # |
| 54 | # 2016/02/14 |
| 55 | # - Added version information |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 56 | # - Added support for archive files |
| 57 | # |
| 58 | # 2016/02/15 |
| 59 | # - Fixed temporary directory bug |
| 60 | # - Improved skipping before unzipping |
| 61 | # - Added EXPERIMENTAL concurrency support |
| 62 | # |
| 63 | # 2016/02/23 |
| 64 | # - Merge korapxml2krill and korapxml2krill_dir |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 65 | # |
| 66 | # 2016/02/27 |
| 67 | # - Added extract function |
Akron | 35db6e3 | 2016-03-17 22:42:22 +0100 | [diff] [blame] | 68 | # |
| 69 | # 2016/03/17 |
| 70 | # - Added meta switch |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 71 | # |
| 72 | # 2016/03/18 |
| 73 | # - Added meta data caching |
Akron | 2cfe809 | 2016-06-24 17:48:49 +0200 | [diff] [blame] | 74 | # |
Akron | f3f0c94 | 2016-06-27 13:27:14 +0200 | [diff] [blame] | 75 | # 2016/06/27 |
Akron | 2cfe809 | 2016-06-24 17:48:49 +0200 | [diff] [blame] | 76 | # - Added multi archive support |
| 77 | # - Added prefix negation support |
Akron | f3f0c94 | 2016-06-27 13:27:14 +0200 | [diff] [blame] | 78 | # - Added Malt#Dependency support |
Akron | 8b99052 | 2016-07-06 16:45:57 +0200 | [diff] [blame] | 79 | # |
| 80 | # 2016/07/06 |
| 81 | # - Added MDParser#Dependency |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 82 | # |
| 83 | # 2016/10/15 |
Nils Diewald | 0e48977 | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 84 | # - Fixed temporary path issue in script |
| 85 | # |
| 86 | # 2016/10/24 |
| 87 | # - Improved Windows support |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 88 | # |
Akron | b4bbec7 | 2016-10-26 20:21:02 +0200 | [diff] [blame] | 89 | # 2016/10/24 |
| 90 | # - Added support for document extraction |
| 91 | # |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 92 | # 2016/10/27 |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 93 | # - Added wildcard support for document extraction |
Akron | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 94 | # |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 95 | # 2016/12/21 |
| 96 | # - added support for base-sentences and base-tokenizations |
| 97 | # |
Akron | 4fa37c3 | 2017-01-20 14:43:10 +0100 | [diff] [blame] | 98 | # 2017/01/20 |
| 99 | # - added support for DRuKoLa annotations |
| 100 | # |
Akron | 41ac10b | 2017-02-08 22:47:25 +0100 | [diff] [blame] | 101 | # 2017/02/08 |
| 102 | # - added support for pagebreak annotations |
| 103 | # |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 104 | # 2017/04/06 |
| 105 | # - added support for wildcards in input |
| 106 | # |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 107 | # 2017/04/07 |
| 108 | # - support configuration option |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 109 | # - support for temporary extraction |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 110 | # |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 111 | # 2017/04/12 |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 112 | # - support serial processing |
| 113 | # - support input root |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 114 | # - introduced --sequential-extraction flag |
Akron | ce125b6 | 2017-06-19 11:54:36 +0200 | [diff] [blame] | 115 | # |
| 116 | # 2017/06/19 |
| 117 | # - added support for DCK |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 118 | # |
| 119 | # 2017/06/29 |
| 120 | # - Fixed exit codes |
Akron | 9a062ce | 2017-07-04 19:12:05 +0200 | [diff] [blame] | 121 | # |
| 122 | # 2017/07/04 |
| 123 | # - Fixed tar building process |
Akron | 4c67919 | 2018-01-16 17:41:49 +0100 | [diff] [blame] | 124 | # |
| 125 | # 2018/01/16 |
| 126 | # - Added LWC support |
Akron | 5fdc7e1 | 2018-07-19 12:37:48 +0200 | [diff] [blame] | 127 | # |
| 128 | # 2018/07/19 |
| 129 | # - Preliminary support for HNC. |
Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 130 | # |
| 131 | # 2019/01/22 |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 132 | # - Preliminary support for DGD. |
Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 133 | # - Support for non-word tokens. |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 134 | # |
Akron | 63d03ee | 2019-02-13 18:49:38 +0100 | [diff] [blame] | 135 | # 2019/02/13 |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 136 | # - Support for 'koral:field' array. |
| 137 | # - Support for Koral versioning. |
Akron | 63d03ee | 2019-02-13 18:49:38 +0100 | [diff] [blame] | 138 | # - Ignore temporary extract parameter on |
| 139 | # directory archiving. |
Akron | 7d5e638 | 2019-08-08 16:36:27 +0200 | [diff] [blame] | 140 | # |
| 141 | # 2019/08/08 |
| 142 | # - Support for Talismane. |
Akron | c29b8e1 | 2019-12-16 14:28:09 +0100 | [diff] [blame] | 143 | # |
Akron | f1849aa | 2019-12-16 23:35:33 +0100 | [diff] [blame] | 144 | # 2019/12/17 |
Akron | c29b8e1 | 2019-12-16 14:28:09 +0100 | [diff] [blame] | 145 | # - Added support for DGD pseudo-sentences |
| 146 | # based on anchor milestones. |
Akron | f1849aa | 2019-12-16 23:35:33 +0100 | [diff] [blame] | 147 | # - Support for non-verbal annotations. |
Akron | 07e2477 | 2020-04-23 14:00:54 +0200 | [diff] [blame] | 148 | # |
| 149 | # 2020/04/23 |
| 150 | # - Added support for Redewiedergabe-Korpus structure |
| 151 | # annotations, based on sentence and paragraph milestones |
| 152 | # - Added support for Redewiedergabe-Korpus morphology |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 153 | # ---------------------------------------------------------- |
Akron | 069bd71 | 2016-02-12 19:09:06 +0100 | [diff] [blame] | 154 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 155 | our $LAST_CHANGE = '2020/08/07'; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 156 | our $LOCAL = $FindBin::Bin; |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 157 | our $KORAL_VERSION = 0.03; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 158 | our $VERSION_MSG = <<"VERSION"; |
| 159 | Version $KorAP::XML::Krill::VERSION - diewald\@ids-mannheim.de - $LAST_CHANGE |
| 160 | VERSION |
| 161 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 162 | # Parse comand |
| 163 | my $cmd; |
| 164 | our @ARGV; |
| 165 | if ($ARGV[0] && index($ARGV[0], '-') != 0) { |
| 166 | $cmd = shift @ARGV; |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame] | 167 | }; |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 168 | my @keep_argv = @ARGV; |
Akron | 93d620e | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 169 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 170 | my (@skip, @sigle, @anno, @input); |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 171 | |
| 172 | # Configuration hash |
| 173 | my %cfg = (); |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 174 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 175 | # Parse options from the command line |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 176 | GetOptions( |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 177 | 'input|i=s' => \@input, |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 178 | 'input-base|ib=s' => \($cfg{input_base}), |
| 179 | 'output|o=s' => \($cfg{output}), |
| 180 | 'overwrite|w' => \($cfg{overwrite}), |
| 181 | 'meta|m=s' => \($cfg{meta}), |
| 182 | 'token|t=s' => \($cfg{token}), |
| 183 | 'base-sentences|bs=s' => \($cfg{base_sentences}), |
| 184 | 'base-paragraphs|bp=s' => \($cfg{base_paragraphs}), |
| 185 | 'base-pagebreaks|bpb=s' => \($cfg{base_pagebreaks}), |
| 186 | 'gzip|z' => \($cfg{gzip}), |
| 187 | 'temporary-extract|te=s' => \($cfg{extract_dir}), |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 188 | 'skip|s=s' => \@skip, |
| 189 | 'sigle|sg=s' => \@sigle, |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 190 | 'cache|c=s' => \($cfg{cache_file}), |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 191 | 'config|cfg=s' => \(my $cfg_file), |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 192 | 'log|l=s' => \($cfg{log}), |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 193 | 'anno|a=s' => \@anno, |
Akron | 11daf96 | 2020-08-07 16:29:22 +0200 | [diff] [blame] | 194 | 'primary|p!' => sub { |
| 195 | warn 'Primary flag no longer supported!'; |
| 196 | }, |
Akron | 6aed056 | 2020-08-07 16:46:00 +0200 | [diff] [blame] | 197 | 'pretty|y' => sub { |
| 198 | warn 'Pretty flag no longer supported!'; |
| 199 | }, |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 200 | 'jobs|j=i' => \($cfg{jobs}), |
| 201 | 'koral|k=f' => \($cfg{koral}), |
| 202 | 'to-tar' => \($cfg{to_tar}), |
| 203 | 'non-word-tokens|nwt' => \($cfg{non_word_tokens}), |
| 204 | 'non-verbal-tokens|nvt' => \($cfg{non_verbal_tokens}), |
| 205 | 'sequential-extraction|se' => \($cfg{sequential_extraction}), |
| 206 | 'cache-size|cs=s' => \($cfg{cache_size}), |
| 207 | 'cache-delete|cd!' => \($cfg{cache_delete}), |
| 208 | 'cache-init|ci!' => \($cfg{cache_init}), |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 209 | 'help|h' => sub { |
| 210 | pod2usage( |
| 211 | -sections => 'NAME|SYNOPSIS|ARGUMENTS|OPTIONS', |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 212 | -verbose => 99, |
| 213 | -msg => $VERSION_MSG, |
| 214 | -output => '-' |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 215 | ); |
| 216 | }, |
| 217 | 'version|v' => sub { |
| 218 | pod2usage( |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 219 | -verbose => 0, |
| 220 | -msg => $VERSION_MSG, |
| 221 | -output => '-' |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 222 | ) |
| 223 | } |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 224 | ); |
| 225 | |
Akron | e512b7c | 2020-08-07 16:16:12 +0200 | [diff] [blame] | 226 | my %ERROR_HASH = ( |
| 227 | -sections => 'NAME|SYNOPSIS|ARGUMENTS|OPTIONS', |
| 228 | -verbose => 99, |
| 229 | -msg => $VERSION_MSG, |
| 230 | -output => '-', |
| 231 | -exit => 1 |
| 232 | ); |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 233 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 234 | # Load from configuration and fill non-given data |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 235 | if ($cfg_file && -e $cfg_file) { |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 236 | my %config; |
| 237 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 238 | print "Reading config from $cfg_file\n"; |
| 239 | |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 240 | Config::Simple->import_from($cfg_file, \%config); |
| 241 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 242 | foreach (qw!output cache-size input-base token overwrite |
| 243 | meta base-sentences base-paragraphs base-pagebreaks |
| 244 | gzip to-tar log cache non-word-tokens |
| 245 | non-verbal-tokens sequential-extraction cache-init |
| 246 | koral extract-dir jobs!) { |
| 247 | my $underlined = $_ =~ tr/-/_/r; |
| 248 | if (!defined($cfg{$underlined}) && defined $config{$_}) { |
| 249 | $cfg{$underlined} = $config{$_}; |
| 250 | }; |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | # Skip |
| 254 | if (!scalar(@skip) && defined $config{'skip'}) { |
| 255 | @skip = split /\s*;\s*/, $config{'skip'} ; |
| 256 | }; |
| 257 | |
| 258 | # Sigle |
| 259 | if (!scalar(@sigle) && defined $config{'sigle'}) { |
| 260 | @sigle = split /\s*;\s*/, $config{'sigle'} ; |
| 261 | }; |
| 262 | |
| 263 | # Anno |
| 264 | if (!scalar(@anno) && defined $config{'anno'}) { |
| 265 | @anno = split /\s*;\s*/, $config{'anno'} ; |
| 266 | }; |
| 267 | }; |
| 268 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 269 | # Init variables and set default values |
| 270 | my $output = $cfg{output}; |
| 271 | my $input_base = $cfg{input_base}; |
| 272 | my $gzip = $cfg{gzip}; |
| 273 | my $to_tar = $cfg{to_tar}; |
| 274 | my $extract_dir = $cfg{extract_dir}; |
| 275 | my $token_base = $cfg{token} // 'OpenNLP#tokens'; |
| 276 | my $cache_file = $cfg{cache} // 'korapxml2krill.cache'; |
| 277 | my $jobs = $cfg{jobs} // 0; |
| 278 | my $cache_delete = $cfg{cache_delete} // 1; |
| 279 | my $base_sentences = lc($cfg{base_sentences} // ''); |
| 280 | my $base_paragraphs = lc($cfg{base_paragraphs} // ''); |
| 281 | my $base_pagebreaks = lc($cfg{base_pagebreaks} // ''); |
| 282 | my $sequential_extraction = $cfg{sequential_extraction} // 0; |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 283 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 284 | # Get tokenization basis |
| 285 | my ($token_base_foundry, $token_base_layer) = split(/#/, $token_base) if $token_base; |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 286 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 287 | # Remove file extension |
| 288 | $token_base_layer =~ s/\.xml$//i; |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 289 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 290 | # Convert sigle to path construct |
| 291 | s!^\s*([^_]+?)_([^\.]+?)\.(.+?)\s*$!$1/$2/$3! foreach @sigle; |
| 292 | |
| 293 | my %skip; |
| 294 | $skip{lc($_)} = 1 foreach @skip; |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 295 | |
| 296 | # Initialize log4perl object |
| 297 | Log::Log4perl->init({ |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 298 | 'log4perl.rootLogger' => uc($cfg{log} // 'ERROR') . ', STDERR', |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 299 | 'log4perl.appender.STDERR' => 'Log::Log4perl::Appender::ScreenColoredLevels', |
| 300 | 'log4perl.appender.STDERR.layout' => 'PatternLayout', |
| 301 | 'log4perl.appender.STDERR.layout.ConversionPattern' => '[%r] %F %L %c - %m%n' |
| 302 | }); |
| 303 | |
| 304 | my $log = Log::Log4perl->get_logger('main'); |
| 305 | |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 306 | if ($cmd && $output && (!defined($to_tar)) && (!-e $output || !-d $output)) { |
| 307 | $log->error("Directory '$output' does not exist."); |
| 308 | exit 1; |
| 309 | }; |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 310 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 311 | # Input has to be defined |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 312 | pod2usage(%ERROR_HASH) unless @input; |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 313 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 314 | # Gzip has no effect, if no output is given |
| 315 | pod2usage(%ERROR_HASH) if $gzip && !$output; |
Nils Diewald | 7364d1f | 2013-11-05 19:26:35 +0000 | [diff] [blame] | 316 | |
Akron | c11f798 | 2017-02-21 21:20:14 +0100 | [diff] [blame] | 317 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 318 | # Start serial processing |
Akron | 28c4e54 | 2017-07-04 20:30:33 +0200 | [diff] [blame] | 319 | if ($cmd && $cmd eq 'serial') { |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 320 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 321 | # Remove all inputs |
| 322 | my $remove_next = 0; |
| 323 | @keep_argv = @{c(@keep_argv)->grep( |
| 324 | sub { |
| 325 | # Input flag |
| 326 | if ($_ eq '-i' || $_ eq '--input' || $_ eq '--output' || $_ eq '-o') { |
| 327 | $remove_next = 1; |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | # input value |
| 332 | elsif ($remove_next) { |
| 333 | $remove_next = 0; |
| 334 | return 0; |
| 335 | }; |
| 336 | |
| 337 | # Pass parameter |
| 338 | return 1; |
| 339 | } |
| 340 | )->to_array}; |
| 341 | |
| 342 | |
| 343 | # Iterate over all inputs |
| 344 | foreach (@input) { |
| 345 | |
Akron | 081639e | 2017-04-21 19:01:39 +0200 | [diff] [blame] | 346 | # This will create a directory |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 347 | my $new_out = catdir($output, get_file_name_from_glob($_)); |
| 348 | |
Akron | 486f9ab | 2017-04-22 23:25:19 +0200 | [diff] [blame] | 349 | # Create new path, in case the output is not meant to be tarred |
Akron | 081639e | 2017-04-21 19:01:39 +0200 | [diff] [blame] | 350 | unless ($to_tar) { |
| 351 | if (make_path($new_out) == 0 && !-d $new_out) { |
| 352 | $log->error("Can\'t create path $new_out"); |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 353 | exit 1; |
Akron | 081639e | 2017-04-21 19:01:39 +0200 | [diff] [blame] | 354 | }; |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | # Create archive command |
| 358 | my @archive_cmd = ($^X, $0, 'archive', @keep_argv, '-i', $_, '-o', $new_out); |
| 359 | print "Start serial processing of $_ to $new_out\n"; |
| 360 | |
| 361 | # Start archiving |
| 362 | system @archive_cmd; |
| 363 | }; |
| 364 | |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 365 | exit; |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 366 | }; |
| 367 | |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 368 | # Define supported (and preinstalled) transformation modules |
| 369 | my @layers = (); |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 370 | push(@layers, ['Base', 'Sentences']) unless $base_sentences; |
| 371 | push(@layers, ['Base', 'Paragraphs']) unless $base_paragraphs; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 372 | |
| 373 | # Connexor |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 374 | push(@layers, ['Connexor', 'Morpho'], |
| 375 | ['Connexor', 'Syntax'], |
| 376 | ['Connexor', 'Phrase'], |
| 377 | ['Connexor', 'Sentences']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 378 | |
| 379 | # CoreNLP |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 380 | push(@layers, |
| 381 | ['CoreNLP', 'NamedEntities'], |
| 382 | ['CoreNLP', 'Sentences'], |
| 383 | ['CoreNLP', 'Morpho'], |
| 384 | ['CoreNLP', 'Constituency']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 385 | |
Akron | ce125b6 | 2017-06-19 11:54:36 +0200 | [diff] [blame] | 386 | # CMC |
| 387 | push(@layers, ['CMC', 'Morpho']); |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 388 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 389 | # DeReKo |
Akron | 41ac10b | 2017-02-08 22:47:25 +0100 | [diff] [blame] | 390 | my @dereko_attr = (); |
| 391 | if ($base_sentences eq 'dereko#structure') { |
| 392 | push @dereko_attr, 'sentences'; |
| 393 | }; |
| 394 | if ($base_paragraphs eq 'dereko#structure') { |
| 395 | push @dereko_attr, 'paragraphs'; |
| 396 | }; |
Akron | 636bd9c | 2017-02-09 17:13:00 +0100 | [diff] [blame] | 397 | |
Akron | 41ac10b | 2017-02-08 22:47:25 +0100 | [diff] [blame] | 398 | if ($base_pagebreaks eq 'dereko#structure') { |
| 399 | push @dereko_attr, 'pagebreaks'; |
| 400 | }; |
| 401 | |
| 402 | if ($dereko_attr[0]) { |
| 403 | push(@layers, ['DeReKo', 'Structure', 'base-' . join('-', @dereko_attr)]); |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 404 | } |
| 405 | else { |
| 406 | push(@layers, ['DeReKo', 'Structure']); |
| 407 | }; |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 408 | |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 409 | # DGD |
| 410 | push(@layers, ['DGD', 'Morpho']); |
Akron | c29b8e1 | 2019-12-16 14:28:09 +0100 | [diff] [blame] | 411 | if ($base_sentences eq 'dgd#structure') { |
| 412 | push(@layers, ['DGD', 'Structure', 'base-sentence']); |
| 413 | } |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 414 | |
| 415 | # DRuKoLa |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 416 | push(@layers, |
| 417 | ['DRuKoLa', 'Morpho']); |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 418 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 419 | # Glemm |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 420 | push(@layers, |
| 421 | ['Glemm', 'Morpho']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 422 | |
Akron | ea1aed5 | 2018-07-19 14:43:34 +0200 | [diff] [blame] | 423 | # HNC |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 424 | push(@layers, |
| 425 | ['HNC', 'Morpho']); |
Akron | ea1aed5 | 2018-07-19 14:43:34 +0200 | [diff] [blame] | 426 | |
Akron | 4c67919 | 2018-01-16 17:41:49 +0100 | [diff] [blame] | 427 | # LWC |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 428 | push(@layers, |
| 429 | ['LWC', 'Dependency']); |
Akron | 4c67919 | 2018-01-16 17:41:49 +0100 | [diff] [blame] | 430 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 431 | # Malt |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 432 | push(@layers, |
| 433 | ['Malt', 'Dependency']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 434 | |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 435 | # Marmot |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 436 | push(@layers, |
| 437 | ['MarMoT', 'Morpho']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 438 | |
| 439 | # Mate |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 440 | push(@layers, |
| 441 | ['Mate', 'Morpho'], |
| 442 | ['Mate', 'Dependency']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 443 | |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 444 | # MDParser |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 445 | push(@layers, |
| 446 | ['MDParser', 'Dependency']); |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 447 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 448 | # OpenNLP |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 449 | push(@layers, |
| 450 | ['OpenNLP', 'Morpho'], |
| 451 | ['OpenNLP', 'Sentences']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 452 | |
Akron | 07e2477 | 2020-04-23 14:00:54 +0200 | [diff] [blame] | 453 | # Redewiedergabe |
| 454 | push(@layers, ['RWK', 'Morpho']); |
| 455 | if ($base_sentences eq 'rwk#structure') { |
| 456 | push(@layers, ['RWK', 'Structure']); |
| 457 | }; |
| 458 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 459 | # Schreibgebrauch |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 460 | push(@layers, |
| 461 | ['Sgbr', 'Lemma'], |
| 462 | ['Sgbr', 'Morpho']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 463 | |
Akron | 7d5e638 | 2019-08-08 16:36:27 +0200 | [diff] [blame] | 464 | # Talismane |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 465 | push(@layers, |
| 466 | ['Talismane', 'Dependency'], |
| 467 | ['Talismane', 'Morpho']); |
Akron | 7d5e638 | 2019-08-08 16:36:27 +0200 | [diff] [blame] | 468 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 469 | # TreeTagger |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 470 | push(@layers, |
| 471 | ['TreeTagger', 'Morpho'], |
| 472 | ['TreeTagger', 'Sentences']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 473 | |
| 474 | # XIP |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 475 | push(@layers, |
| 476 | ['XIP', 'Morpho'], |
| 477 | ['XIP', 'Constituency'], |
| 478 | ['XIP', 'Sentences'], |
| 479 | ['XIP', 'Dependency']); |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 480 | |
Akron | 4fa37c3 | 2017-01-20 14:43:10 +0100 | [diff] [blame] | 481 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 482 | # Check filters |
| 483 | my @filtered_anno; |
| 484 | if ($skip{'#all'}) { |
| 485 | foreach (@anno) { |
| 486 | push @filtered_anno, [ split('#', $_) ]; |
| 487 | }; |
| 488 | } |
| 489 | |
| 490 | # Add all annotations that are not skipped |
| 491 | else { |
| 492 | # Add to index file - respect skipping |
| 493 | foreach my $info (@layers) { |
| 494 | # Skip if Foundry or Foundry#Layer should be skipped |
| 495 | unless ($skip{lc($info->[0])} || $skip{lc($info->[0]) . '#' . lc($info->[1])}) { |
| 496 | push @filtered_anno, $info; |
| 497 | }; |
| 498 | }; |
| 499 | }; |
| 500 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 501 | |
| 502 | # TODO: This should not be initialized for batch |
| 503 | my $cache = Cache::FastMmap->new( |
| 504 | share_file => $cache_file, |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 505 | cache_size => ($cfg{cache_size} // '50m'), |
| 506 | init_file => ($cfg{cache_init} // 1) |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 507 | ); |
| 508 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 509 | # Create batch object |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 510 | my $batch_file = KorAP::XML::Batch::File->new( |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 511 | cache => $cache, |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 512 | meta_type => $cfg{meta}, |
| 513 | overwrite => $cfg{overwrite}, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 514 | foundry => $token_base_foundry, |
| 515 | layer => $token_base_layer, |
| 516 | gzip => $gzip, |
| 517 | log => $log, |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 518 | koral => ($cfg{koral} // $KORAL_VERSION), |
Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 519 | anno => \@filtered_anno, |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 520 | non_word_tokens => ($cfg{non_word_tokens} // 0), |
| 521 | non_verbal_tokens => ($cfg{non_verbal_tokens} // 0) |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 522 | ); |
| 523 | |
Akron | e512b7c | 2020-08-07 16:16:12 +0200 | [diff] [blame] | 524 | |
| 525 | # Auto adjust jobs |
| 526 | if ($jobs eq '-1') { |
Akron | 0b04b31 | 2020-10-30 17:39:18 +0100 | [diff] [blame] | 527 | my $cores = 1; |
| 528 | if (eval("use Sys::Info; 1;") && eval("use Sys::Info::Constants qw( :device_cpu ); 1;")) { |
| 529 | $cores = Sys::Info->new->device('CPU')->count; |
| 530 | } |
| 531 | else { |
| 532 | $log->warn("Unable to determine number of cores"); |
| 533 | }; |
| 534 | |
Akron | e512b7c | 2020-08-07 16:16:12 +0200 | [diff] [blame] | 535 | $jobs = ceil(5 * $cores); |
| 536 | $log->info("Run using $jobs jobs on $cores cores"); |
| 537 | }; |
| 538 | |
| 539 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 540 | # Glob and prefix files |
Akron | e512b7c | 2020-08-07 16:16:12 +0200 | [diff] [blame] | 541 | if (@input > 0) { |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 542 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 543 | my @new_input = (); |
| 544 | |
| 545 | # Iterate over all inputs |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 546 | foreach my $wild_card (@input) { |
| 547 | |
| 548 | # Prefix with input root |
| 549 | $wild_card = $input_base ? catfile($input_base, $wild_card) : $wild_card; |
| 550 | |
| 551 | push (@new_input, bsd_glob($wild_card)); |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 552 | }; |
| 553 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 554 | # Sort files by length |
| 555 | @input = sort { length($a) <=> length($b) } @new_input; |
| 556 | |
| 557 | print 'Input is ' . join(', ', @input)."\n"; |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 561 | # Process a single file |
| 562 | unless ($cmd) { |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 563 | my $input = $input[0]; |
Nils Diewald | 59094f2 | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 564 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 565 | BEGIN { |
| 566 | $main::TIME = Benchmark->new; |
| 567 | $main::LAST_STOP = Benchmark->new; |
| 568 | }; |
| 569 | |
| 570 | sub stop_time { |
| 571 | my $new = Benchmark->new; |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 572 | $log->info( |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 573 | 'The code took: '. |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 574 | timestr(timediff($new, $main::LAST_STOP)) . |
| 575 | ' (overall: ' . timestr(timediff($new, $main::TIME)) . ')' |
| 576 | ); |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 577 | $main::LAST_STOP = $new; |
| 578 | }; |
| 579 | |
| 580 | # Create and parse new document |
Akron | e512b7c | 2020-08-07 16:16:12 +0200 | [diff] [blame] | 581 | $input =~ s{([^/])$}{$1/}o; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 582 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 583 | # Process file |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 584 | $batch_file->process($input, $output); |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 585 | |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 586 | # Delete cache file |
| 587 | unlink($cache_file) if $cache_delete; |
| 588 | |
Akron | 5f51d42 | 2016-08-16 16:26:43 +0200 | [diff] [blame] | 589 | stop_time; |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 590 | exit; |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 591 | }; |
| 592 | |
Nils Diewald | 59094f2 | 2014-11-05 18:20:50 +0000 | [diff] [blame] | 593 | |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 594 | # Extract XML files |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 595 | if ($cmd eq 'extract') { |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 596 | |
Akron | d5643ad | 2017-07-04 20:27:13 +0200 | [diff] [blame] | 597 | # Output is required |
| 598 | pod2usage(%ERROR_HASH) unless $output; |
| 599 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 600 | # Create new archive object |
Akron | b0c88db | 2016-06-29 16:33:18 +0200 | [diff] [blame] | 601 | if (-f($input[0]) && (my $archive = KorAP::XML::Archive->new($input[0]))) { |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 602 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 603 | # Check zip capabilities |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 604 | unless ($archive->test_unzip) { |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 605 | $log->error("Unzip is not installed or incompatible."); |
| 606 | exit 1; |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 607 | }; |
| 608 | |
Akron | b0c88db | 2016-06-29 16:33:18 +0200 | [diff] [blame] | 609 | # Add further annotation archived |
Akron | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 610 | $archive->attach($_) foreach @input[1..$#input]; |
Akron | b0c88db | 2016-06-29 16:33:18 +0200 | [diff] [blame] | 611 | |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 612 | # Will set @sigle |
| 613 | my $prefix = set_sigle($archive); |
Akron | 651cb8d | 2016-08-16 21:44:49 +0200 | [diff] [blame] | 614 | |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 615 | # Iterate over all given sigles and extract |
| 616 | foreach (@sigle) { |
Akron | 60a8caa | 2017-02-17 21:51:27 +0100 | [diff] [blame] | 617 | |
Akron | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 618 | print "$_ ...\n"; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 619 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 620 | # TODO: Make this OS independent |
Akron | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 621 | print '... ' . ( |
Akron | 60a8caa | 2017-02-17 21:51:27 +0100 | [diff] [blame] | 622 | |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 623 | # TODO: |
| 624 | # - prefix??? |
| 625 | $archive->extract_sigle([$_], $output, $jobs) |
| 626 | ? '' : 'not ' |
Akron | 651cb8d | 2016-08-16 21:44:49 +0200 | [diff] [blame] | 627 | ); |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 628 | print "extracted.\n"; |
| 629 | }; |
Akron | b0c88db | 2016-06-29 16:33:18 +0200 | [diff] [blame] | 630 | } |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 631 | |
| 632 | # Can't create archive object |
Akron | b0c88db | 2016-06-29 16:33:18 +0200 | [diff] [blame] | 633 | else { |
| 634 | $log->error('Unable to extract from primary archive ' . $input[0]); |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 635 | exit 1; |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 636 | }; |
| 637 | } |
| 638 | |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 639 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 640 | # Process an archive |
| 641 | elsif ($cmd eq 'archive') { |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 642 | |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 643 | my $archive_output; |
| 644 | |
| 645 | # First extract, then archive |
Akron | 63d03ee | 2019-02-13 18:49:38 +0100 | [diff] [blame] | 646 | if (defined $extract_dir && !-d $input[0]) { |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 647 | |
| 648 | # Create new archive object |
| 649 | if (-f($input[0]) && (my $archive = KorAP::XML::Archive->new($input[0]))) { |
| 650 | |
| 651 | # Check zip capabilities |
| 652 | unless ($archive->test_unzip) { |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 653 | $log->error("Unzip is not installed or incompatible."); |
| 654 | exit 1; |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 655 | }; |
| 656 | |
| 657 | # Add further annotation archived |
| 658 | $archive->attach($_) foreach @input[1..$#input]; |
| 659 | |
| 660 | # Create a temporary directory |
| 661 | if ($extract_dir eq ':temp:') { |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 662 | $extract_dir = tempdir(CLEANUP => 0); |
| 663 | print "Temporarily extract to $extract_dir\n"; |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 664 | }; |
| 665 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 666 | # Add some random extra to avoid clashes with multiple archives |
| 667 | $extract_dir = catdir($extract_dir, random_string('cccccc')); |
| 668 | |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 669 | # Extract to temporary directory |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 670 | if ($archive->extract_all($extract_dir, $sequential_extraction ? 1: $jobs)) { |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 671 | @input = ($extract_dir); |
| 672 | } |
| 673 | else { |
| 674 | $log->error('Unable to extract from primary archive ' . $input[0] . |
| 675 | ' to ' . $extract_dir); |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 676 | exit 1; |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 677 | }; |
| 678 | } |
| 679 | |
| 680 | # Can't create archive object |
| 681 | else { |
| 682 | $log->error('Unable to extract from primary archive ' . $input[0]); |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 683 | exit 1; |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 684 | }; |
| 685 | }; |
| 686 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 687 | # Zero means: everything runs in the parent process |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 688 | my $pool = Parallel::ForkManager->new($jobs); |
| 689 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 690 | my $count = 0; # Texts to process |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 691 | my $iter = 1; # Current text in process |
| 692 | |
Akron | da3097e | 2017-04-23 19:53:57 +0200 | [diff] [blame] | 693 | my $tar_archive; |
| 694 | my $output_dir = $output; |
| 695 | my $tar_fh; |
| 696 | |
| 697 | # Initialize tar archive |
| 698 | if ($to_tar) { |
| 699 | $tar_archive = Archive::Tar::Builder->new( |
| 700 | ignore_errors => 1 |
| 701 | ); |
| 702 | |
| 703 | # Set output name |
| 704 | my $tar_file = $output; |
| 705 | unless ($tar_file =~ /\.tar$/) { |
| 706 | $tar_file .= '.tar'; |
| 707 | }; |
| 708 | |
| 709 | # Initiate the tar file |
| 710 | print "Writing to file $tar_file\n"; |
| 711 | $tar_fh = IO::File->new($tar_file, 'w'); |
| 712 | $tar_fh->binmode(1); |
| 713 | |
| 714 | # Set handle |
| 715 | $tar_archive->set_handle($tar_fh); |
| 716 | |
| 717 | # Output to temporary directory |
| 718 | $output_dir = File::Temp->newdir; |
| 719 | }; |
| 720 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 721 | # Report on fork message |
| 722 | $pool->run_on_finish ( |
| 723 | sub { |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 724 | my ($pid, $code) = @_; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 725 | my $data = pop; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 726 | |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 727 | print 'Convert ['. ($jobs > 0 ? "\$$pid:" : '') . |
Akron | 651cb8d | 2016-08-16 21:44:49 +0200 | [diff] [blame] | 728 | ($iter++) . "/$count]" . |
| 729 | ($code ? " $code" : '') . |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 730 | ' ' . $data->[0] . "\n"; |
Akron | da3097e | 2017-04-23 19:53:57 +0200 | [diff] [blame] | 731 | |
| 732 | if (!$code && $to_tar && $data->[2]) { |
| 733 | my $filename = $data->[2]; |
| 734 | |
| 735 | # Lock filehandle |
| 736 | if (flock($tar_fh, LOCK_EX)) { |
| 737 | |
Akron | 9a062ce | 2017-07-04 19:12:05 +0200 | [diff] [blame] | 738 | my $clean_file = fileparse($filename); |
| 739 | |
Akron | da3097e | 2017-04-23 19:53:57 +0200 | [diff] [blame] | 740 | # Archive and remove file |
Akron | 9a062ce | 2017-07-04 19:12:05 +0200 | [diff] [blame] | 741 | $tar_archive->archive_as($filename => $clean_file); |
Akron | da3097e | 2017-04-23 19:53:57 +0200 | [diff] [blame] | 742 | unlink $filename; |
| 743 | |
| 744 | # Unlock filehandle |
| 745 | flock($tar_fh, LOCK_UN); |
| 746 | } |
| 747 | else { |
| 748 | $log->warn("Unable to add $filename to archive"); |
| 749 | }; |
| 750 | }; |
| 751 | |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 752 | $data->[1] = undef if $data->[1]; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 753 | } |
| 754 | ); |
| 755 | |
| 756 | my $t; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 757 | my $temp; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 758 | print "Reading data ...\n"; |
| 759 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 760 | # unless (Cache::FastMmap->new( |
| 761 | # share_file => $cache_file, |
| 762 | # cache_size => $cache_size, |
| 763 | # init_file => $cache_init |
| 764 | # )) { |
| 765 | # print "Unable to intialize cache '$cache_file'\n\n"; |
| 766 | # exit(1); |
| 767 | # }; |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 768 | |
Akron | 486f9ab | 2017-04-22 23:25:19 +0200 | [diff] [blame] | 769 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 770 | # Input is a directory |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 771 | if (-d $input[0]) { |
Akron | 5c602cb | 2020-08-07 17:00:52 +0200 | [diff] [blame] | 772 | # TODO: |
| 773 | # Replace with Mojo::File |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 774 | my $it = Directory::Iterator->new($input[0]); |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 775 | my @dirs; |
| 776 | my $dir; |
| 777 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 778 | # Todo: Make a DO WHILE |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 779 | while (1) { |
| 780 | if (!$it->is_directory && ($dir = $it->get) && $dir =~ s{/data\.xml$}{}) { |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 781 | push @dirs, $dir; |
| 782 | $it->prune; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 783 | }; |
| 784 | last unless $it->next; |
| 785 | }; |
| 786 | |
| 787 | print "Start processing ...\n"; |
| 788 | $t = Benchmark->new; |
| 789 | $count = scalar @dirs; |
| 790 | |
| 791 | DIRECTORY_LOOP: |
| 792 | for (my $i = 0; $i < $count; $i++) { |
| 793 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 794 | my $filename = catfile( |
Akron | 081639e | 2017-04-21 19:01:39 +0200 | [diff] [blame] | 795 | $output_dir, |
Akron | 41127e3 | 2020-08-07 12:46:19 +0200 | [diff] [blame] | 796 | get_file_name($input[0], $dirs[$i]) . '.json' . ($gzip ? '.gz' : '') |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 797 | ); |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 798 | |
| 799 | # Get the next fork |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 800 | $pool->start and next DIRECTORY_LOOP; |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 801 | |
Akron | 13d5662 | 2016-10-31 14:54:49 +0100 | [diff] [blame] | 802 | if (my $return = $batch_file->process($dirs[$i] => $filename)) { |
Akron | 486f9ab | 2017-04-22 23:25:19 +0200 | [diff] [blame] | 803 | $pool->finish( |
| 804 | 0, |
Akron | da3097e | 2017-04-23 19:53:57 +0200 | [diff] [blame] | 805 | [ |
| 806 | "Processed " . $filename . ($return == -1 ? " - already existing" : ''), |
| 807 | undef, |
| 808 | $filename |
| 809 | ] |
Akron | 486f9ab | 2017-04-22 23:25:19 +0200 | [diff] [blame] | 810 | ); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 811 | } |
| 812 | else { |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 813 | $pool->finish(1, ["Unable to process " . $dirs[$i]]); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 814 | }; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 815 | }; |
| 816 | } |
| 817 | |
| 818 | # Input is a file |
Akron | 29866ac | 2016-06-24 16:40:47 +0200 | [diff] [blame] | 819 | elsif (-f($input[0]) && (my $archive = KorAP::XML::Archive->new($input[0]))) { |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 820 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 821 | unless ($archive->test_unzip) { |
Akron | 3abc03e | 2017-06-29 16:23:35 +0200 | [diff] [blame] | 822 | $log->error("Unzip is not installed or incompatible."); |
| 823 | exit 1; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 824 | }; |
| 825 | |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 826 | # Add further annotation archived |
Akron | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 827 | $archive->attach($_) foreach @input[1..$#input]; |
Akron | 08385f6 | 2016-03-22 20:37:04 +0100 | [diff] [blame] | 828 | |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 829 | # Get sigles to extract |
| 830 | my $prefix = set_sigle($archive); |
| 831 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 832 | print "Start processing ...\n"; |
| 833 | $t = Benchmark->new; |
| 834 | my @dirs = $archive->list_texts; |
| 835 | $count = scalar @dirs; |
| 836 | |
| 837 | ARCHIVE_LOOP: |
| 838 | for (my $i = 0; $i < $count; $i++) { |
| 839 | |
| 840 | # Split path information |
| 841 | my ($prefix, $corpus, $doc, $text) = $archive->split_path($dirs[$i]); |
| 842 | |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 843 | my $filename = catfile( |
Akron | 486f9ab | 2017-04-22 23:25:19 +0200 | [diff] [blame] | 844 | $output_dir, |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 845 | get_file_name( |
Akron | 41127e3 | 2020-08-07 12:46:19 +0200 | [diff] [blame] | 846 | $input[0], |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 847 | catfile($corpus, $doc, $text) |
| 848 | . '.json' . ($gzip ? '.gz' : '') |
| 849 | ) |
Akron | e1dbc38 | 2016-07-08 22:24:52 +0200 | [diff] [blame] | 850 | ); |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 851 | |
| 852 | # Get the next fork |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 853 | $pool->start and next ARCHIVE_LOOP; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 854 | |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 855 | # Create temporary file |
| 856 | $temp = File::Temp->newdir; |
| 857 | |
Akron | bdf434a | 2016-10-24 17:42:07 +0200 | [diff] [blame] | 858 | # TODO: Check if $filename exist at the beginning, |
| 859 | # because extraction can be horrible slow! |
| 860 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 861 | # Extract from archive |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 862 | if ($archive->extract_sigle([join('/', $corpus, $doc, $text)], $temp, $sequential_extraction ? 1 : $jobs)) { |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 863 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 864 | # Create corpus directory |
| 865 | my $input = catdir("$temp", $corpus); |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 866 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 867 | # Temporary directory |
| 868 | my $dir = catdir($input, $doc, $text); |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 869 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 870 | # Write file |
Akron | 13d5662 | 2016-10-31 14:54:49 +0100 | [diff] [blame] | 871 | if (my $return = $batch_file->process($dir => $filename)) { |
Akron | 486f9ab | 2017-04-22 23:25:19 +0200 | [diff] [blame] | 872 | |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 873 | # Delete temporary file |
Akron | 13d5662 | 2016-10-31 14:54:49 +0100 | [diff] [blame] | 874 | $pool->finish( |
| 875 | 0, |
Akron | da3097e | 2017-04-23 19:53:57 +0200 | [diff] [blame] | 876 | [ |
| 877 | "Processed " . $filename . ($return == -1 ? " - already existing" : ''), |
| 878 | $temp, |
| 879 | $filename |
| 880 | ] |
Akron | 13d5662 | 2016-10-31 14:54:49 +0100 | [diff] [blame] | 881 | ); |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 882 | } |
| 883 | else { |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 884 | # Delete temporary file |
| 885 | $pool->finish(1, ["Unable to process " . $dir, $temp]); |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 886 | }; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 887 | } |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 888 | |
| 889 | # Unable to extract |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 890 | else { |
Akron | 4c0cf31 | 2016-10-15 16:42:09 +0200 | [diff] [blame] | 891 | $pool->finish(1, ["Unable to extract " . $dirs[$i], $temp]); |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 892 | }; |
| 893 | }; |
| 894 | } |
| 895 | |
| 896 | else { |
| 897 | print "Input is neither a directory nor an archive.\n\n"; |
| 898 | }; |
| 899 | |
| 900 | $pool->wait_all_children; |
| 901 | |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 902 | # Delete cache file |
| 903 | unlink($cache_file) if $cache_delete; |
| 904 | |
Akron | da3097e | 2017-04-23 19:53:57 +0200 | [diff] [blame] | 905 | # Close tar filehandle |
| 906 | if ($to_tar && $tar_fh) { |
| 907 | $tar_archive->finish; |
| 908 | $tar_fh->close; |
| 909 | print "Wrote to tar archive.\n"; |
| 910 | }; |
| 911 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 912 | print timestr(timediff(Benchmark->new, $t))."\n"; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 913 | print "Done.\n"; |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 914 | }; |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 915 | |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 916 | |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 917 | # For an archive, this will create the list |
| 918 | # of all sigles to process |
| 919 | sub set_sigle { |
| 920 | my $archive = shift; |
| 921 | |
| 922 | my $prefix = 1; |
| 923 | my @dirs = (); |
| 924 | |
| 925 | # No sigles given |
| 926 | unless (@sigle) { |
| 927 | |
| 928 | # Get files |
| 929 | foreach ($archive->list_texts) { |
| 930 | |
| 931 | push @dirs, $_; |
| 932 | |
| 933 | # Split path information |
| 934 | ($prefix, my ($corpus, $doc, $text)) = $archive->split_path($_); |
| 935 | |
| 936 | # TODO: Make this OS independent |
| 937 | push @sigle, join '/', $corpus, $doc, $text; |
| 938 | }; |
| 939 | } |
| 940 | |
| 941 | # Check sigle for doc sigles |
| 942 | else { |
| 943 | my @new_sigle; |
| 944 | |
| 945 | my $prefix_check = 0; |
| 946 | |
| 947 | # Iterate over all sigle |
| 948 | foreach (@sigle) { |
| 949 | |
| 950 | # Sigle is a doc sigle |
| 951 | if ($_ =~ m!^(?:\.[/\\])?[^/\\]+?[/\\][^/\\]+?$!) { |
| 952 | |
| 953 | print "$_ ..."; |
| 954 | # Check if a prefix is needed |
| 955 | unless ($prefix_check) { |
| 956 | |
| 957 | if ($prefix = $archive->check_prefix) { |
| 958 | print " with prefix ..."; |
| 959 | }; |
| 960 | $prefix_check = 1; |
| 961 | }; |
| 962 | |
| 963 | print "\n"; |
| 964 | |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 965 | print '... ' . ( |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 966 | $archive->extract_sigle([$_], $output, $sequential_extraction ? 1 : $jobs) |
| 967 | ? '' : 'not ' |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 968 | ); |
| 969 | print "extracted.\n"; |
| 970 | } |
| 971 | |
| 972 | # Sigle is a text sigle |
| 973 | else { |
| 974 | push @new_sigle, $_; |
| 975 | |
| 976 | unless ($prefix_check) { |
| 977 | |
| 978 | if ($prefix = $archive->check_prefix) { |
| 979 | print " with prefix ..."; |
| 980 | }; |
| 981 | $prefix_check = 1; |
| 982 | }; |
| 983 | }; |
| 984 | }; |
| 985 | @sigle = @new_sigle; |
| 986 | }; |
| 987 | |
| 988 | return $prefix; |
| 989 | }; |
| 990 | |
| 991 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 992 | # Cleanup temporary extraction directory |
| 993 | if ($extract_dir) { |
| 994 | my $objects = remove_tree($extract_dir, { safe => 1 }); |
Akron | f8df216 | 2020-08-07 15:03:39 +0200 | [diff] [blame] | 995 | $log->info("Removed directory $extract_dir with $objects objects"); |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 996 | }; |
| 997 | |
| 998 | |
| 999 | print "\n"; |
| 1000 | |
Nils Diewald | 2db9ad0 | 2013-10-29 19:26:43 +0000 | [diff] [blame] | 1001 | __END__ |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1002 | |
| 1003 | =pod |
| 1004 | |
| 1005 | =encoding utf8 |
| 1006 | |
| 1007 | =head1 NAME |
| 1008 | |
Akron | 42f48c1 | 2020-02-14 13:08:13 +0100 | [diff] [blame] | 1009 | korapxml2krill - Merge KorAP-XML data and create Krill documents |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1010 | |
| 1011 | |
| 1012 | =head1 SYNOPSIS |
| 1013 | |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 1014 | korapxml2krill [archive|extract] --input <directory|archive> [options] |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1015 | |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 1016 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1017 | =head1 DESCRIPTION |
| 1018 | |
| 1019 | L<KorAP::XML::Krill> is a library to convert KorAP-XML documents to files |
| 1020 | compatible with the L<Krill|https://github.com/KorAP/Krill> indexer. |
Akron | 8f69d63 | 2020-01-15 16:58:11 +0100 | [diff] [blame] | 1021 | The C<korapxml2krill> command line tool is a simple wrapper of this library. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1022 | |
| 1023 | |
| 1024 | =head1 INSTALLATION |
| 1025 | |
| 1026 | The preferred way to install L<KorAP::XML::Krill> is to use L<cpanm|App::cpanminus>. |
| 1027 | |
Akron | af38698 | 2016-10-12 00:33:25 +0200 | [diff] [blame] | 1028 | $ cpanm https://github.com/KorAP/KorAP-XML-Krill.git |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1029 | |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1030 | In case everything went well, the C<korapxml2krill> tool will |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1031 | be available on your command line immediately. |
Akron | 6eff23b | 2018-09-24 10:31:20 +0200 | [diff] [blame] | 1032 | Minimum requirement for L<KorAP::XML::Krill> is Perl 5.16. |
Akron | 0b04b31 | 2020-10-30 17:39:18 +0100 | [diff] [blame] | 1033 | Optional support for L<Sys::Info> to calculate available cores. |
Akron | a93d51b | 2016-10-24 20:27:48 +0200 | [diff] [blame] | 1034 | In addition to work with zip archives, the C<unzip> tool needs to be present. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1035 | |
| 1036 | =head1 ARGUMENTS |
| 1037 | |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 1038 | $ korapxml2krill -z --input <directory> --output <filename> |
| 1039 | |
| 1040 | Without arguments, C<korapxml2krill> converts a directory of a single KorAP-XML document. |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 1041 | It expects the input to point to the text level folder. |
Akron | 7606afa | 2016-10-25 16:23:49 +0200 | [diff] [blame] | 1042 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1043 | =over 2 |
| 1044 | |
| 1045 | =item B<archive> |
| 1046 | |
Akron | 081639e | 2017-04-21 19:01:39 +0200 | [diff] [blame] | 1047 | $ korapxml2krill archive -z --input <directory|archive> --output <directory|tar> |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 1048 | |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 1049 | Converts an archive of KorAP-XML documents. It expects a directory |
| 1050 | (pointing to the corpus level folder) or one or more zip files as input. |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 1051 | |
| 1052 | =item B<extract> |
| 1053 | |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 1054 | $ korapxml2krill extract --input <archive> --output <directory> --sigle <SIGLE> |
| 1055 | |
| 1056 | Extracts KorAP-XML documents from a zip file. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1057 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 1058 | =item B<serial> |
| 1059 | |
| 1060 | $ korapxml2krill serial -i <archive1> -i <archive2> -o <directory> -cfg <config-file> |
| 1061 | |
| 1062 | Convert archives sequentially. The inputs are not merged but treated |
| 1063 | as they are (so they may be premerged or globs). |
| 1064 | the C<--out> directory is treated as the base directory where subdirectories |
Akron | 081639e | 2017-04-21 19:01:39 +0200 | [diff] [blame] | 1065 | are created based on the archive name. In case the C<--to-tar> flag is given, |
| 1066 | the output will be a tar file. |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 1067 | |
| 1068 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1069 | =back |
| 1070 | |
| 1071 | |
| 1072 | =head1 OPTIONS |
| 1073 | |
| 1074 | =over 2 |
| 1075 | |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 1076 | =item B<--input|-i> <directory|zip file> |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1077 | |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 1078 | Directory or zip file(s) of documents to convert. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1079 | |
Akron | 7606afa | 2016-10-25 16:23:49 +0200 | [diff] [blame] | 1080 | Without arguments, C<korapxml2krill> expects a folder of a single KorAP-XML |
Akron | f1a1de9 | 2016-11-02 17:32:12 +0100 | [diff] [blame] | 1081 | document, while C<archive> expects a KorAP-XML corpus folder or a zip |
| 1082 | file to batch process multiple files. |
| 1083 | C<extract> expects zip files only. |
Akron | 7606afa | 2016-10-25 16:23:49 +0200 | [diff] [blame] | 1084 | |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 1085 | C<archive> supports multiple input zip files with the constraint, |
Akron | 2cfe809 | 2016-06-24 17:48:49 +0200 | [diff] [blame] | 1086 | that the first archive listed contains all primary data files |
| 1087 | and all meta data files. |
Akron | 2cfe809 | 2016-06-24 17:48:49 +0200 | [diff] [blame] | 1088 | |
Akron | 7606afa | 2016-10-25 16:23:49 +0200 | [diff] [blame] | 1089 | -i file/news.zip -i file/news.malt.zip -i "#file/news.tt.zip" |
Akron | 2cfe809 | 2016-06-24 17:48:49 +0200 | [diff] [blame] | 1090 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1091 | Input may also be defined using BSD glob wildcards. |
| 1092 | |
| 1093 | -i 'file/news*.zip' |
| 1094 | |
| 1095 | The extended input array will be sorted in length order, so the shortest |
| 1096 | path needs to contain all primary data files and all meta data files. |
| 1097 | |
Akron | 0c3e375 | 2016-06-28 15:55:53 +0200 | [diff] [blame] | 1098 | (The directory structure follows the base directory format, |
| 1099 | that may include a C<.> root folder. |
| 1100 | In this case further archives lacking a C<.> root folder |
Akron | 7606afa | 2016-10-25 16:23:49 +0200 | [diff] [blame] | 1101 | need to be passed with a hash sign in front of the archive's name. |
| 1102 | This may require to quote the parameter.) |
Akron | 2cfe809 | 2016-06-24 17:48:49 +0200 | [diff] [blame] | 1103 | |
Akron | 7606afa | 2016-10-25 16:23:49 +0200 | [diff] [blame] | 1104 | To support zip files, a version of C<unzip> needs to be installed that is |
| 1105 | compatible with the archive file. |
Akron | a93d51b | 2016-10-24 20:27:48 +0200 | [diff] [blame] | 1106 | |
Akron | 7606afa | 2016-10-25 16:23:49 +0200 | [diff] [blame] | 1107 | B<The root folder switch using the hash sign is experimental and |
| 1108 | may vanish in future versions.> |
Akron | 651cb8d | 2016-08-16 21:44:49 +0200 | [diff] [blame] | 1109 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1110 | |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 1111 | =item B<--input-base|-ib> <directory> |
| 1112 | |
| 1113 | The base directory for inputs. |
| 1114 | |
| 1115 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1116 | =item B<--output|-o> <directory|file> |
| 1117 | |
| 1118 | Output folder for archive processing or |
| 1119 | document name for single output (optional), |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1120 | writes to C<STDOUT> by default |
| 1121 | (in case C<output> is not mandatory due to further options). |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1122 | |
| 1123 | =item B<--overwrite|-w> |
| 1124 | |
| 1125 | Overwrite files that already exist. |
| 1126 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1127 | |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 1128 | =item B<--token|-t> <foundry>#<file> |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1129 | |
| 1130 | Define the default tokenization by specifying |
| 1131 | the name of the foundry and optionally the name |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1132 | of the layer-file. Defaults to C<OpenNLP#tokens>. |
Akron | f1849aa | 2019-12-16 23:35:33 +0100 | [diff] [blame] | 1133 | This will directly take the file instead of running |
| 1134 | the layer implementation! |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1135 | |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 1136 | |
| 1137 | =item B<--base-sentences|-bs> <foundry>#<layer> |
| 1138 | |
| 1139 | Define the layer for base sentences. |
| 1140 | If given, this will be used instead of using C<Base#Sentences>. |
Akron | c29b8e1 | 2019-12-16 14:28:09 +0100 | [diff] [blame] | 1141 | Currently C<DeReKo#Structure> and C<DGD#Structure> are the only additional |
| 1142 | layers supported. |
Akron | 3741f8b | 2016-12-21 19:55:21 +0100 | [diff] [blame] | 1143 | |
| 1144 | Defaults to unset. |
| 1145 | |
| 1146 | |
| 1147 | =item B<--base-paragraphs|-bp> <foundry>#<layer> |
| 1148 | |
| 1149 | Define the layer for base paragraphs. |
| 1150 | If given, this will be used instead of using C<Base#Paragraphs>. |
| 1151 | Currently C<DeReKo#Structure> is the only additional layer supported. |
| 1152 | |
| 1153 | Defaults to unset. |
| 1154 | |
| 1155 | |
Akron | 41ac10b | 2017-02-08 22:47:25 +0100 | [diff] [blame] | 1156 | =item B<--base-pagebreaks|-bpb> <foundry>#<layer> |
| 1157 | |
| 1158 | Define the layer for base pagebreaks. |
| 1159 | Currently C<DeReKo#Structure> is the only layer supported. |
| 1160 | |
| 1161 | Defaults to unset. |
| 1162 | |
| 1163 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1164 | =item B<--skip|-s> <foundry>[#<layer>] |
| 1165 | |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1166 | Skip specific annotations by specifying the foundry |
| 1167 | (and optionally the layer with a C<#>-prefix), |
| 1168 | e.g. C<Mate> or C<Mate#Morpho>. Alternatively you can skip C<#ALL>. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1169 | Can be set multiple times. |
| 1170 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1171 | |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1172 | =item B<--anno|-a> <foundry>#<layer> |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1173 | |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1174 | Convert specific annotations by specifying the foundry |
| 1175 | (and optionally the layer with a C<#>-prefix), |
| 1176 | e.g. C<Mate> or C<Mate#Morpho>. |
| 1177 | Can be set multiple times. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1178 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1179 | |
Akron | ed9baf0 | 2019-01-22 17:03:25 +0100 | [diff] [blame] | 1180 | =item B<--non-word-tokens|-nwt> |
| 1181 | |
| 1182 | Tokenize non-word tokens like word tokens (defined as matching |
| 1183 | C</[\d\w]/>). Useful to treat punctuations as tokens. |
| 1184 | |
| 1185 | Defaults to unset. |
| 1186 | |
Akron | f1849aa | 2019-12-16 23:35:33 +0100 | [diff] [blame] | 1187 | |
| 1188 | =item B<--non-verbal-tokens|-nvt> |
| 1189 | |
| 1190 | Tokenize non-verbal tokens marked as in the primary data as |
| 1191 | the unicode symbol 'Black Vertical Rectangle' aka \x25ae. |
| 1192 | |
| 1193 | Defaults to unset. |
| 1194 | |
| 1195 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1196 | =item B<--jobs|-j> |
| 1197 | |
| 1198 | Define the number of concurrent jobs in seperated forks |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1199 | for archive processing. |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 1200 | Defaults to C<0> (everything runs in a single process). |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 1201 | |
| 1202 | If C<sequential-extraction> is not set to false, this will |
| 1203 | also apply to extraction. |
| 1204 | |
Akron | c11f798 | 2017-02-21 21:20:14 +0100 | [diff] [blame] | 1205 | Pass -1, and the value will be set automatically to 5 |
Akron | 0b04b31 | 2020-10-30 17:39:18 +0100 | [diff] [blame] | 1206 | times the number of available cores, in case L<Sys::Info> |
| 1207 | is available. |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1208 | This is I<experimental>. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1209 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1210 | |
Akron | 263274c | 2019-02-07 09:48:30 +0100 | [diff] [blame] | 1211 | =item B<--koral|-k> |
| 1212 | |
| 1213 | Version of the output format. Supported versions are: |
| 1214 | C<0> for legacy serialization, C<0.03> for serialization |
| 1215 | with metadata fields as key-values on the root object, |
| 1216 | C<0.4> for serialization with metadata fields as a list |
| 1217 | of C<"@type":"koral:field"> objects. |
| 1218 | |
| 1219 | Currently defaults to C<0.03>. |
| 1220 | |
| 1221 | |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 1222 | =item B<--sequential-extraction|-se> |
| 1223 | |
| 1224 | Flag to indicate, if the C<jobs> value also applies to extraction. |
| 1225 | Some systems may have problems with extracting multiple archives |
| 1226 | to the same folder at the same time. |
| 1227 | Can be flagged using C<--no-sequential-extraction> as well. |
| 1228 | Defaults to C<false>. |
| 1229 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1230 | |
Akron | 35db6e3 | 2016-03-17 22:42:22 +0100 | [diff] [blame] | 1231 | =item B<--meta|-m> |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1232 | |
Akron | 35db6e3 | 2016-03-17 22:42:22 +0100 | [diff] [blame] | 1233 | Define the metadata parser to use. Defaults to C<I5>. |
| 1234 | Metadata parsers can be defined in the C<KorAP::XML::Meta> namespace. |
| 1235 | This is I<experimental>. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1236 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1237 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1238 | =item B<--gzip|-z> |
| 1239 | |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1240 | Compress the output. |
| 1241 | Expects a defined C<output> file in single processing. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1242 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1243 | |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 1244 | =item B<--cache|-c> |
| 1245 | |
| 1246 | File to mmap a cache (using L<Cache::FastMmap>). |
| 1247 | Defaults to C<korapxml2krill.cache> in the calling directory. |
| 1248 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1249 | |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 1250 | =item B<--cache-size|-cs> |
| 1251 | |
| 1252 | Size of the cache. Defaults to C<50m>. |
| 1253 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1254 | |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 1255 | =item B<--cache-init|-ci> |
| 1256 | |
| 1257 | Initialize cache file. |
| 1258 | Can be flagged using C<--no-cache-init> as well. |
| 1259 | Defaults to C<true>. |
| 1260 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1261 | |
Akron | 11c8030 | 2016-03-18 19:44:43 +0100 | [diff] [blame] | 1262 | =item B<--cache-delete|-cd> |
| 1263 | |
| 1264 | Delete cache file after processing. |
| 1265 | Can be flagged using C<--no-cache-delete> as well. |
| 1266 | Defaults to C<true>. |
| 1267 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1268 | |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 1269 | =item B<--config|-cfg> |
| 1270 | |
| 1271 | Configure the parameters of your call in a file |
| 1272 | of key-value pairs with whitespace separator |
| 1273 | |
| 1274 | overwrite 1 |
| 1275 | token DeReKo#Structure |
| 1276 | ... |
| 1277 | |
| 1278 | Supported parameters are: |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 1279 | C<overwrite>, C<gzip>, C<jobs>, C<input-base>, |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 1280 | C<token>, C<log>, C<cache>, C<cache-size>, C<cache-delete>, C<meta>, |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 1281 | C<output>, C<koral>, |
| 1282 | C<tempary-extract>, C<sequential-extraction>, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 1283 | C<base-sentences>, C<base-paragraphs>, |
| 1284 | C<base-pagebreaks>, |
| 1285 | C<skip> (semicolon separated), C<sigle> |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 1286 | (semicolon separated), C<anno> (semicolon separated). |
| 1287 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1288 | Configuration parameters will always be overwritten by |
| 1289 | passed parameters. |
| 1290 | |
| 1291 | |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 1292 | =item B<--temporary-extract|-te> |
| 1293 | |
| 1294 | Only valid for the C<archive> command. |
| 1295 | |
| 1296 | This will first extract all files into a |
| 1297 | directory and then will archive. |
| 1298 | If the directory is given as C<:temp:>, |
| 1299 | a temporary directory is used. |
| 1300 | This is especially useful to avoid |
| 1301 | massive unzipping and potential |
| 1302 | network latency. |
Akron | 636aa11 | 2017-04-07 18:48:56 +0200 | [diff] [blame] | 1303 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1304 | |
Akron | c93a080 | 2019-07-11 15:48:34 +0200 | [diff] [blame] | 1305 | =item B<--to-tar> |
| 1306 | |
| 1307 | Only valid for the C<archive> command. |
| 1308 | |
| 1309 | Writes the output into a tar archive. |
| 1310 | |
| 1311 | |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 1312 | =item B<--sigle|-sg> |
| 1313 | |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 1314 | Extract the given texts. |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 1315 | Can be set multiple times. |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1316 | I<Currently only supported on C<extract>.> |
Akron | b0c88db | 2016-06-29 16:33:18 +0200 | [diff] [blame] | 1317 | Sigles have the structure C<Corpus>/C<Document>/C<Text>. |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 1318 | In case the C<Text> path is omitted, the whole document will be extracted. |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 1319 | On the document level, the postfix wildcard C<*> is supported. |
Akron | e10ad32 | 2016-02-27 10:54:26 +0100 | [diff] [blame] | 1320 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1321 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1322 | =item B<--log|-l> |
| 1323 | |
| 1324 | The L<Log4perl> log level, defaults to C<ERROR>. |
| 1325 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1326 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1327 | =item B<--help|-h> |
| 1328 | |
Akron | 42f48c1 | 2020-02-14 13:08:13 +0100 | [diff] [blame] | 1329 | Print help information. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1330 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1331 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1332 | =item B<--version|-v> |
| 1333 | |
| 1334 | Print version information. |
| 1335 | |
| 1336 | =back |
| 1337 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1338 | |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1339 | =head1 ANNOTATION SUPPORT |
| 1340 | |
| 1341 | L<KorAP::XML::Krill> has built-in importer for some annotation foundries and layers |
| 1342 | developed in the KorAP project that are part of the KorAP preprocessing pipeline. |
| 1343 | The base foundry with paragraphs, sentences, and the text element are mandatory for |
| 1344 | L<Krill|https://github.com/KorAP/Krill>. |
| 1345 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1346 | Base |
| 1347 | #Paragraphs |
| 1348 | #Sentences |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1349 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1350 | Connexor |
| 1351 | #Morpho |
| 1352 | #Phrase |
| 1353 | #Sentences |
| 1354 | #Syntax |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1355 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1356 | CoreNLP |
| 1357 | #Constituency |
| 1358 | #Morpho |
| 1359 | #NamedEntities |
| 1360 | #Sentences |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1361 | |
Akron | ce125b6 | 2017-06-19 11:54:36 +0200 | [diff] [blame] | 1362 | CMC |
| 1363 | #Morpho |
| 1364 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1365 | DeReKo |
| 1366 | #Structure |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1367 | |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 1368 | DGD |
| 1369 | #Morpho |
Akron | c29b8e1 | 2019-12-16 14:28:09 +0100 | [diff] [blame] | 1370 | #Structure |
Akron | 57510c1 | 2019-01-04 14:58:53 +0100 | [diff] [blame] | 1371 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1372 | DRuKoLa |
| 1373 | #Morpho |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1374 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1375 | Glemm |
| 1376 | #Morpho |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1377 | |
Akron | ea1aed5 | 2018-07-19 14:43:34 +0200 | [diff] [blame] | 1378 | HNC |
| 1379 | #Morpho |
| 1380 | |
Akron | 4c67919 | 2018-01-16 17:41:49 +0100 | [diff] [blame] | 1381 | LWC |
| 1382 | #Dependency |
| 1383 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1384 | Malt |
| 1385 | #Dependency |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1386 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1387 | MarMoT |
| 1388 | #Morpho |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1389 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1390 | Mate |
| 1391 | #Dependency |
| 1392 | #Morpho |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1393 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1394 | MDParser |
| 1395 | #Dependency |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1396 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1397 | OpenNLP |
| 1398 | #Morpho |
| 1399 | #Sentences |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1400 | |
Akron | 07e2477 | 2020-04-23 14:00:54 +0200 | [diff] [blame] | 1401 | RWK |
| 1402 | #Morpho |
| 1403 | #Structure |
| 1404 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1405 | Sgbr |
| 1406 | #Lemma |
| 1407 | #Morpho |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1408 | |
Akron | 7d5e638 | 2019-08-08 16:36:27 +0200 | [diff] [blame] | 1409 | Talismane |
| 1410 | #Dependency |
| 1411 | #Morpho |
| 1412 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1413 | TreeTagger |
| 1414 | #Morpho |
| 1415 | #Sentences |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1416 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 1417 | XIP |
| 1418 | #Constituency |
| 1419 | #Morpho |
| 1420 | #Sentences |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1421 | |
Akron | c13a170 | 2016-03-15 19:33:14 +0100 | [diff] [blame] | 1422 | |
| 1423 | More importers are in preparation. |
| 1424 | New annotation importers can be defined in the C<KorAP::XML::Annotation> namespace. |
| 1425 | See the built-in annotation importers as examples. |
| 1426 | |
Akron | f73ffb6 | 2018-06-27 12:13:59 +0200 | [diff] [blame] | 1427 | |
Akron | 8f69d63 | 2020-01-15 16:58:11 +0100 | [diff] [blame] | 1428 | =head1 About KorAP-XML |
| 1429 | |
| 1430 | KorAP-XML (Bański et al. 2012) is an implementation of the KorAP |
| 1431 | data model (Bański et al. 2013), where text data are stored physically |
| 1432 | separated from their interpretations (i.e. annotations). |
| 1433 | A text document in KorAP-XML therefore consists of several files |
| 1434 | containing primary data, metadata and annotations. |
| 1435 | |
| 1436 | The structure of a single KorAP-XML document can be as follows: |
| 1437 | |
| 1438 | - data.xml |
| 1439 | - header.xml |
| 1440 | + base |
| 1441 | - tokens.xml |
| 1442 | - ... |
| 1443 | + struct |
| 1444 | - structure.xml |
| 1445 | - ... |
| 1446 | + corenlp |
| 1447 | - morpho.xml |
| 1448 | - constituency.xml |
| 1449 | - ... |
| 1450 | + tree_tagger |
| 1451 | - morpho.xml |
| 1452 | - ... |
| 1453 | - ... |
| 1454 | |
| 1455 | The C<data.xml> contains the primary data, the C<header.xml> contains |
| 1456 | the metadata, and the annotation layers are stored in subfolders |
| 1457 | like C<base>, C<struct> or C<corenlp> |
| 1458 | (so-called "foundries"; Bański et al. 2013). |
| 1459 | |
| 1460 | Metadata is available in the TEI-P5 variant I5 |
Akron | d4c5c10 | 2020-02-11 11:47:59 +0100 | [diff] [blame] | 1461 | (Lüngen and Sperberg-McQueen 2012). See the documentation in |
| 1462 | L<KorAP::XML::Meta::I5> for translatable fields. |
| 1463 | |
| 1464 | Annotations correspond to a variant of the TEI-P5 feature structures |
| 1465 | (TEI Consortium; Lee et al. 2004). |
Akron | 72bc522 | 2020-02-06 16:00:13 +0100 | [diff] [blame] | 1466 | Annotation feature structures refer to character sequences of the primary text |
| 1467 | inside the C<text> element of the C<data.xml>. |
| 1468 | A single annotation containing the lemma of a token can have the following structure: |
| 1469 | |
| 1470 | <span from="0" to="3"> |
| 1471 | <fs type="lex" xmlns="http://www.tei-c.org/ns/1.0"> |
| 1472 | <f name="lex"> |
| 1473 | <fs> |
| 1474 | <f name="lemma">zum</f> |
| 1475 | </fs> |
| 1476 | </f> |
| 1477 | </fs> |
| 1478 | </span> |
| 1479 | |
| 1480 | The C<from> and C<to> attributes are refering to the character span |
| 1481 | in the primary text. |
| 1482 | Depending on the kind of annotation (e.g. token-based, span-based, relation-based), |
| 1483 | the structure may vary. See L<KorAP::XML::Annotation::*> for various |
| 1484 | annotation preprocessors. |
Akron | 8f69d63 | 2020-01-15 16:58:11 +0100 | [diff] [blame] | 1485 | |
| 1486 | Multiple KorAP-XML documents are organized on three levels following |
| 1487 | the "IDS Textmodell" (Lüngen and Sperberg-McQueen 2012): |
| 1488 | corpus E<gt> document E<gt> text. On each level metadata information |
| 1489 | can be stored, that C<korapxml2krill> will merge to a single metadata |
| 1490 | object per text. A corpus is therefore structured as follows: |
| 1491 | |
| 1492 | + <corpus> |
| 1493 | - header.xml |
| 1494 | + <document> |
| 1495 | - header.xml |
| 1496 | + <text> |
| 1497 | - data.xml |
| 1498 | - header.xml |
| 1499 | - ... |
| 1500 | - ... |
| 1501 | |
| 1502 | A single text can be identified by the concatenation of |
| 1503 | the corpus identifier, the document identifier and the text identifier. |
| 1504 | This identifier is called the text sigle |
| 1505 | (e.g. a text with the identifier C<18486> in the document C<060> in the |
| 1506 | corpus C<WPD17> has the text sigle C<WPD17/060/18486>, see C<--sigle>). |
| 1507 | |
| 1508 | These corpora are often stored in zip files, with which C<korapxml2krill> |
| 1509 | can deal with. Corpora may also be split in multiple zip archives |
| 1510 | (e.g. one zip file per foundry), which is also supported (see C<--input>). |
| 1511 | |
| 1512 | Examples for KorAP-XML files are included in L<KorAP::XML::Krill> |
| 1513 | in form of a test suite. |
| 1514 | The resulting JSON format merges all annotation layers |
| 1515 | based on a single token stream. |
| 1516 | |
| 1517 | =head2 References |
| 1518 | |
| 1519 | Piotr Bański, Cyril Belica, Helge Krause, Marc Kupietz, Carsten Schnober, Oliver Schonefeld, and Andreas Witt (2011): |
| 1520 | KorAP data model: first approximation, December. |
| 1521 | |
| 1522 | Piotr Bański, Peter M. Fischer, Elena Frick, Erik Ketzan, Marc Kupietz, Carsten Schnober, Oliver Schonefeld and Andreas Witt (2012): |
| 1523 | "The New IDS Corpus Analysis Platform: Challenges and Prospects", |
| 1524 | Proceedings of the Eighth International Conference on Language Resources and Evaluation (LREC 2012). |
| 1525 | L<PDF|http://www.lrec-conf.org/proceedings/lrec2012/pdf/789_Paper.pdf> |
| 1526 | |
| 1527 | Piotr Bański, Elena Frick, Michael Hanl, Marc Kupietz, Carsten Schnober and Andreas Witt (2013): |
| 1528 | "Robust corpus architecture: a new look at virtual collections and data access", |
| 1529 | Corpus Linguistics 2013. Abstract Book. Lancaster: UCREL, pp. 23-25. |
| 1530 | L<PDF|https://ids-pub.bsz-bw.de/frontdoor/deliver/index/docId/4485/file/Ba%c5%84ski_Frick_Hanl_Robust_corpus_architecture_2013.pdf> |
| 1531 | |
| 1532 | Kiyong Lee, Lou Burnard, Laurent Romary, Eric de la Clergerie, Thierry Declerck, |
| 1533 | Syd Bauman, Harry Bunt, Lionel Clément, Tomaz Erjavec, Azim Roussanaly and Claude Roux (2004): |
| 1534 | "Towards an international standard on featurestructure representation", |
| 1535 | Proceedings of the fourth International Conference on Language Resources and Evaluation (LREC 2004), |
| 1536 | pp. 373-376. |
| 1537 | L<PDF|http://www.lrec-conf.org/proceedings/lrec2004/pdf/687.pdf> |
| 1538 | |
| 1539 | Harald Lüngen and C. M. Sperberg-McQueen (2012): |
| 1540 | "A TEI P5 Document Grammar for the IDS Text Model", |
| 1541 | Journal of the Text Encoding Initiative, Issue 3 | November 2012. |
| 1542 | L<PDF|https://journals.openedition.org/jtei/pdf/508> |
| 1543 | |
| 1544 | TEI Consortium, eds: |
| 1545 | "Feature Structures", |
| 1546 | Guidelines for Electronic Text Encoding and Interchange. |
| 1547 | L<html|https://www.tei-c.org/release/doc/tei-p5-doc/en/html/FS.html> |
| 1548 | |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1549 | =head1 AVAILABILITY |
| 1550 | |
| 1551 | https://github.com/KorAP/KorAP-XML-Krill |
| 1552 | |
| 1553 | |
| 1554 | =head1 COPYRIGHT AND LICENSE |
| 1555 | |
Akron | 8f69d63 | 2020-01-15 16:58:11 +0100 | [diff] [blame] | 1556 | Copyright (C) 2015-2020, L<IDS Mannheim|https://www.ids-mannheim.de/> |
Akron | f7ad89e | 2016-03-16 18:22:47 +0100 | [diff] [blame] | 1557 | |
Akron | 8f69d63 | 2020-01-15 16:58:11 +0100 | [diff] [blame] | 1558 | Author: L<Nils Diewald|https://nils-diewald.de/> |
Akron | 8150010 | 2017-04-07 20:45:44 +0200 | [diff] [blame] | 1559 | |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 1560 | Contributor: Eliza Margaretha |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1561 | |
| 1562 | L<KorAP::XML::Krill> is developed as part of the L<KorAP|http://korap.ids-mannheim.de/> |
| 1563 | Corpus Analysis Platform at the |
Akron | 94262ce | 2019-02-28 21:42:43 +0100 | [diff] [blame] | 1564 | L<Leibniz Institute for the German Language (IDS)|http://ids-mannheim.de/>, |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1565 | member of the |
Akron | f1849aa | 2019-12-16 23:35:33 +0100 | [diff] [blame] | 1566 | L<Leibniz-Gemeinschaft|http://www.leibniz-gemeinschaft.de/>. |
Akron | 941c1a6 | 2016-02-23 17:41:41 +0100 | [diff] [blame] | 1567 | |
| 1568 | This program is free software published under the |
| 1569 | L<BSD-2 License|https://raw.githubusercontent.com/KorAP/KorAP-XML-Krill/master/LICENSE>. |
| 1570 | |
| 1571 | =cut |