Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 4 | use lib 'lib'; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 5 | use FindBin; |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 6 | use File::Temp qw/tempdir/; |
| 7 | use File::Spec::Functions qw/catfile catdir/; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 8 | use Getopt::Long; |
| 9 | use Directory::Iterator; |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 10 | use KorAP::XML::Krill; |
| 11 | use KorAP::XML::Archive; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 12 | |
| 13 | my $local = $FindBin::Bin; |
| 14 | |
Akron | 93d620e | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 15 | # Changes |
| 16 | # 2013/11/25 |
| 17 | # - Initial release |
| 18 | # |
| 19 | # 2016/02/04 |
| 20 | # - Rename to korapxml2krill_dir |
Akron | 069bd71 | 2016-02-12 19:09:06 +0100 | [diff] [blame] | 21 | # |
| 22 | # 2016/02/12 |
| 23 | # - Support overwrite |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 24 | # |
| 25 | # 2016/02/14 |
| 26 | # - Added version information |
| 27 | |
| 28 | sub printversion { |
| 29 | print "Version " . $KorAP::XML::Krill::VERSION . "\n\n"; |
| 30 | exit(1); |
| 31 | }; |
Akron | 93d620e | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 32 | |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 33 | sub printhelp { |
| 34 | print <<'EOHELP'; |
| 35 | |
| 36 | Merge foundry data based on a tokenization and create indexer friendly documents |
| 37 | for whole directories. |
| 38 | |
| 39 | Call: |
Akron | 93d620e | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 40 | korapxml2krill_dir -z --input <directory> --output <directory> |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 41 | |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 42 | --input|-i <directory|file> Directory or archive file of documents to index |
Akron | 069bd71 | 2016-02-12 19:09:06 +0100 | [diff] [blame] | 43 | --output|-o <directory> Name of output folder |
| 44 | --overwrite|-w Overwrite files that already exist |
| 45 | --token|-t <foundry>[#<layer>] Define the default tokenization by specifying |
| 46 | the name of the foundry and optionally the name |
| 47 | of the layer. Defaults to OpenNLP#tokens. |
| 48 | --skip|-s <foundry>[#<layer>] Skip specific foundries by specifying the name |
| 49 | or specific layers by defining the name |
| 50 | with a # in front of the foundry, |
| 51 | e.g. Mate#Morpho. Alternatively you can skip #ALL. |
| 52 | Can be set multiple times. |
| 53 | --allow|-a <foundry>#<layer> Allow specific foundries and layers by defining them |
| 54 | combining the foundry name with a # and the layer name. |
| 55 | --primary|-p Output primary data or not. Defaults to true. |
| 56 | Can be flagged using --no-primary as well. |
| 57 | --human|-m Represent the data human friendly, |
| 58 | while the output defaults to JSON |
| 59 | --pretty|-y Pretty print json output |
| 60 | --gzip|-z Compress the output |
| 61 | (expects a defined output file) |
| 62 | --log|-l The Log4perl log level, defaults to ERROR. |
| 63 | --help|-h Print this document (optional) |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 64 | --version|-v Print version information |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 65 | |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 66 | diewald@ids-mannheim.de, 2016/02/14 |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 67 | |
| 68 | EOHELP |
| 69 | |
| 70 | exit(defined $_[0] ? $_[0] : 0); |
| 71 | }; |
| 72 | |
Akron | 069bd71 | 2016-02-12 19:09:06 +0100 | [diff] [blame] | 73 | my ($input, $output, $text, $gzip, $log_level, @skip, |
| 74 | $token_base, $primary, @allow, $pretty, $overwrite); |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 75 | GetOptions( |
| 76 | 'input|i=s' => \$input, |
| 77 | 'output|o=s' => \$output, |
| 78 | 'human|m' => \$text, |
Akron | 069bd71 | 2016-02-12 19:09:06 +0100 | [diff] [blame] | 79 | 'overwrite|w' => \$overwrite, |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 80 | 'token|t=s' => \$token_base, |
| 81 | 'gzip|z' => \$gzip, |
| 82 | 'skip|s=s' => \@skip, |
| 83 | 'log|l=s' => \$log_level, |
| 84 | 'allow|a=s' => \@allow, |
| 85 | 'primary|p!' => \$primary, |
| 86 | 'pretty|y' => \$pretty, |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 87 | 'help|h' => sub { printhelp }, |
| 88 | 'version|v' => sub { printversion } |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 89 | ); |
| 90 | |
| 91 | printhelp(1) if !$input || !$output; |
| 92 | |
| 93 | |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 94 | # write file |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 95 | sub write_file { |
| 96 | my $anno = shift; |
| 97 | my $file = $anno; |
| 98 | $file =~ s/^?\/?$input//; |
| 99 | $file =~ tr/\//-/; |
| 100 | $file =~ s{^-+}{}; |
| 101 | |
Akron | 93d620e | 2016-02-05 19:40:05 +0100 | [diff] [blame] | 102 | my $call = 'perl ' . $local . '/korapxml2krill -i ' . $anno . ' -o ' . $output . '/' . $file . '.json'; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 103 | $call .= '.gz -z' if $gzip; |
| 104 | $call .= ' -m' if $text; |
Akron | 069bd71 | 2016-02-12 19:09:06 +0100 | [diff] [blame] | 105 | $call .= ' -w' if $overwrite; |
Akron | a9d4772 | 2016-02-07 23:54:15 +0100 | [diff] [blame] | 106 | $call .= ' -t ' . $token_base if $token_base; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 107 | $call .= ' -l ' . $log_level if $log_level; |
| 108 | $call .= ' --no-primary ' if $primary; |
| 109 | $call .= ' -y ' . $pretty if $pretty; |
| 110 | $call .= ' -a ' . $_ foreach @allow; |
| 111 | $call .= ' -s ' . $_ foreach @skip; |
Akron | a98a14c | 2016-02-12 16:28:39 +0100 | [diff] [blame] | 112 | print "$file "; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 113 | system($call); |
Akron | fd0707e | 2016-02-11 22:13:36 +0100 | [diff] [blame] | 114 | print "\n"; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 117 | # Input is a directory |
| 118 | if (-d $input) { |
| 119 | my $it = Directory::Iterator->new($input); |
| 120 | my @dirs; |
| 121 | my $dir; |
| 122 | while (1) { |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 123 | if (!$it->is_directory && ($dir = $it->get) && $dir =~ s{/data\.xml$}{}) { |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 124 | push @dirs, $dir; |
| 125 | $it->prune; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 126 | }; |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 127 | last unless $it->next; |
| 128 | }; |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 129 | |
Akron | 150b29e | 2016-02-14 23:06:48 +0100 | [diff] [blame^] | 130 | my $count = scalar @dirs; |
| 131 | for (my $i = 0; $i < $count; $i++) { |
| 132 | print 'Convert [' . ($i + 1) . "/$count] "; |
| 133 | write_file($dirs[$i]); |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | # Input is a file |
| 138 | elsif (-f($input) && (my $archive = KorAP::XML::Archive->new($input))) { |
| 139 | unless ($archive->test_unzip) { |
| 140 | print "Unzip is not installed or incompatible.\n\n"; |
| 141 | exit(1); |
| 142 | }; |
| 143 | |
| 144 | unless ($archive->test) { |
| 145 | print "Zip archive not compatible.\n\n"; |
| 146 | exit(1); |
| 147 | }; |
| 148 | |
| 149 | my @dirs = $archive->list_texts; |
| 150 | my $count = scalar @dirs; |
| 151 | for (my $i = 0; $i < $count; $i++) { |
| 152 | print 'Convert [' . ($i + 1) . "/$count] "; |
| 153 | |
| 154 | # Split path information |
| 155 | my ($prefix, $corpus, $doc, $text) = $archive->split_path($dirs[$i]); |
| 156 | |
| 157 | # Create temporary file |
| 158 | my $temp = tempdir(CLEANUP => 1); |
| 159 | |
| 160 | # Extract from archive |
| 161 | if ($archive->extract($dirs[$i], $temp)) { |
| 162 | |
| 163 | # Create corpus directory |
| 164 | $input = catdir($temp, $corpus); |
| 165 | |
| 166 | # Temporary directory |
| 167 | my $dir = catdir($input, $doc, $text); |
| 168 | |
| 169 | # Write file |
| 170 | write_file($dir); |
| 171 | } |
| 172 | else { |
| 173 | print "Unable to extract " . $dirs[$i] . "\n"; |
| 174 | }; |
| 175 | |
| 176 | $temp = 0; |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | else { |
| 181 | print "Input is neither a directory nor an archive.\n\n"; |
Akron | fd0707e | 2016-02-11 22:13:36 +0100 | [diff] [blame] | 182 | }; |
| 183 | |
Nils Diewald | 092178e | 2013-11-26 16:18:48 +0000 | [diff] [blame] | 184 | |
| 185 | __END__ |