blob: 6293a7d378b466639065b41813609750d718f9f5 [file] [log] [blame]
Nils Diewald092178e2013-11-26 16:18:48 +00001#!/usr/bin/env perl
2use strict;
3use warnings;
Akron150b29e2016-02-14 23:06:48 +01004use lib 'lib';
Nils Diewald092178e2013-11-26 16:18:48 +00005use FindBin;
Akron150b29e2016-02-14 23:06:48 +01006use File::Temp qw/tempdir/;
7use File::Spec::Functions qw/catfile catdir/;
Nils Diewald092178e2013-11-26 16:18:48 +00008use Getopt::Long;
9use Directory::Iterator;
Akron150b29e2016-02-14 23:06:48 +010010use KorAP::XML::Krill;
11use KorAP::XML::Archive;
Nils Diewald092178e2013-11-26 16:18:48 +000012
13my $local = $FindBin::Bin;
14
Akron93d620e2016-02-05 19:40:05 +010015# Changes
16# 2013/11/25
17# - Initial release
18#
19# 2016/02/04
20# - Rename to korapxml2krill_dir
Akron069bd712016-02-12 19:09:06 +010021#
22# 2016/02/12
23# - Support overwrite
Akron150b29e2016-02-14 23:06:48 +010024#
25# 2016/02/14
26# - Added version information
Akronc1babed2016-02-15 11:48:18 +010027# - Added support for archive files
28#
29# 2016/02/15
30# - Fixed temporary directory bug
31# - Improved skipping before unzipping
Akron150b29e2016-02-14 23:06:48 +010032
33sub printversion {
34 print "Version " . $KorAP::XML::Krill::VERSION . "\n\n";
35 exit(1);
36};
Akron93d620e2016-02-05 19:40:05 +010037
Nils Diewald092178e2013-11-26 16:18:48 +000038sub printhelp {
39 print <<'EOHELP';
40
41Merge foundry data based on a tokenization and create indexer friendly documents
42for whole directories.
43
44Call:
Akron93d620e2016-02-05 19:40:05 +010045korapxml2krill_dir -z --input <directory> --output <directory>
Nils Diewald092178e2013-11-26 16:18:48 +000046
Akron150b29e2016-02-14 23:06:48 +010047 --input|-i <directory|file> Directory or archive file of documents to index
Akron069bd712016-02-12 19:09:06 +010048 --output|-o <directory> Name of output folder
49 --overwrite|-w Overwrite files that already exist
50 --token|-t <foundry>[#<layer>] Define the default tokenization by specifying
51 the name of the foundry and optionally the name
52 of the layer. Defaults to OpenNLP#tokens.
53 --skip|-s <foundry>[#<layer>] Skip specific foundries by specifying the name
54 or specific layers by defining the name
55 with a # in front of the foundry,
56 e.g. Mate#Morpho. Alternatively you can skip #ALL.
57 Can be set multiple times.
58 --allow|-a <foundry>#<layer> Allow specific foundries and layers by defining them
59 combining the foundry name with a # and the layer name.
60 --primary|-p Output primary data or not. Defaults to true.
61 Can be flagged using --no-primary as well.
62 --human|-m Represent the data human friendly,
63 while the output defaults to JSON
64 --pretty|-y Pretty print json output
65 --gzip|-z Compress the output
66 (expects a defined output file)
67 --log|-l The Log4perl log level, defaults to ERROR.
68 --help|-h Print this document (optional)
Akron150b29e2016-02-14 23:06:48 +010069 --version|-v Print version information
Nils Diewald092178e2013-11-26 16:18:48 +000070
Akronc1babed2016-02-15 11:48:18 +010071diewald@ids-mannheim.de, 2016/02/15
Nils Diewald092178e2013-11-26 16:18:48 +000072
73EOHELP
74
75 exit(defined $_[0] ? $_[0] : 0);
76};
77
Akron069bd712016-02-12 19:09:06 +010078my ($input, $output, $text, $gzip, $log_level, @skip,
79 $token_base, $primary, @allow, $pretty, $overwrite);
Nils Diewald092178e2013-11-26 16:18:48 +000080GetOptions(
81 'input|i=s' => \$input,
82 'output|o=s' => \$output,
83 'human|m' => \$text,
Akron069bd712016-02-12 19:09:06 +010084 'overwrite|w' => \$overwrite,
Nils Diewald092178e2013-11-26 16:18:48 +000085 'token|t=s' => \$token_base,
86 'gzip|z' => \$gzip,
87 'skip|s=s' => \@skip,
88 'log|l=s' => \$log_level,
89 'allow|a=s' => \@allow,
90 'primary|p!' => \$primary,
91 'pretty|y' => \$pretty,
Akron150b29e2016-02-14 23:06:48 +010092 'help|h' => sub { printhelp },
93 'version|v' => sub { printversion }
Nils Diewald092178e2013-11-26 16:18:48 +000094);
95
96printhelp(1) if !$input || !$output;
97
Akronc1babed2016-02-15 11:48:18 +010098sub get_file_name {
99 my $file = shift;
100 $file =~ s/^?\/?$input//;
101 $file =~ tr/\//-/;
102 $file =~ s{^-+}{};
103 return $file;
104};
Nils Diewald092178e2013-11-26 16:18:48 +0000105
Akron150b29e2016-02-14 23:06:48 +0100106# write file
Nils Diewald092178e2013-11-26 16:18:48 +0000107sub write_file {
108 my $anno = shift;
Akronc1babed2016-02-15 11:48:18 +0100109 my $file = get_file_name($anno);
Nils Diewald092178e2013-11-26 16:18:48 +0000110
Akron93d620e2016-02-05 19:40:05 +0100111 my $call = 'perl ' . $local . '/korapxml2krill -i ' . $anno . ' -o ' . $output . '/' . $file . '.json';
Nils Diewald092178e2013-11-26 16:18:48 +0000112 $call .= '.gz -z' if $gzip;
113 $call .= ' -m' if $text;
Akron069bd712016-02-12 19:09:06 +0100114 $call .= ' -w' if $overwrite;
Akrona9d47722016-02-07 23:54:15 +0100115 $call .= ' -t ' . $token_base if $token_base;
Nils Diewald092178e2013-11-26 16:18:48 +0000116 $call .= ' -l ' . $log_level if $log_level;
117 $call .= ' --no-primary ' if $primary;
118 $call .= ' -y ' . $pretty if $pretty;
119 $call .= ' -a ' . $_ foreach @allow;
120 $call .= ' -s ' . $_ foreach @skip;
Akrona98a14c2016-02-12 16:28:39 +0100121 print "$file ";
Nils Diewald092178e2013-11-26 16:18:48 +0000122 system($call);
Akronfd0707e2016-02-11 22:13:36 +0100123 print "\n";
Nils Diewald092178e2013-11-26 16:18:48 +0000124};
125
Akron150b29e2016-02-14 23:06:48 +0100126# Input is a directory
127if (-d $input) {
128 my $it = Directory::Iterator->new($input);
129 my @dirs;
130 my $dir;
131 while (1) {
Nils Diewald092178e2013-11-26 16:18:48 +0000132 if (!$it->is_directory && ($dir = $it->get) && $dir =~ s{/data\.xml$}{}) {
Akron150b29e2016-02-14 23:06:48 +0100133 push @dirs, $dir;
134 $it->prune;
Nils Diewald092178e2013-11-26 16:18:48 +0000135 };
Akron150b29e2016-02-14 23:06:48 +0100136 last unless $it->next;
137 };
Nils Diewald092178e2013-11-26 16:18:48 +0000138
Akron150b29e2016-02-14 23:06:48 +0100139 my $count = scalar @dirs;
140 for (my $i = 0; $i < $count; $i++) {
141 print 'Convert [' . ($i + 1) . "/$count] ";
142 write_file($dirs[$i]);
143 };
144}
145
146# Input is a file
147elsif (-f($input) && (my $archive = KorAP::XML::Archive->new($input))) {
148 unless ($archive->test_unzip) {
149 print "Unzip is not installed or incompatible.\n\n";
150 exit(1);
151 };
152
153 unless ($archive->test) {
154 print "Zip archive not compatible.\n\n";
155 exit(1);
156 };
157
158 my @dirs = $archive->list_texts;
159 my $count = scalar @dirs;
160 for (my $i = 0; $i < $count; $i++) {
161 print 'Convert [' . ($i + 1) . "/$count] ";
162
163 # Split path information
164 my ($prefix, $corpus, $doc, $text) = $archive->split_path($dirs[$i]);
165
Akronc1babed2016-02-15 11:48:18 +0100166 unless ($overwrite) {
167
168 my $filename = catfile(
169 $output,
170 get_file_name(catdir($doc, $text)) . '.json' . ($gzip ? '.gz' : '')
171 );
172 if (-e $filename) {
173 print "Skip $filename\n";
174 next;
175 };
176 };
177
Akron150b29e2016-02-14 23:06:48 +0100178 # Create temporary file
Akronc1babed2016-02-15 11:48:18 +0100179 my $temp = File::Temp->newdir;
Akron150b29e2016-02-14 23:06:48 +0100180
181 # Extract from archive
182 if ($archive->extract($dirs[$i], $temp)) {
183
184 # Create corpus directory
Akronc1babed2016-02-15 11:48:18 +0100185 $input = catdir("$temp", $corpus);
Akron150b29e2016-02-14 23:06:48 +0100186
187 # Temporary directory
188 my $dir = catdir($input, $doc, $text);
189
190 # Write file
191 write_file($dir);
192 }
193 else {
194 print "Unable to extract " . $dirs[$i] . "\n";
195 };
196
Akronc1babed2016-02-15 11:48:18 +0100197 $temp = undef;
Akron150b29e2016-02-14 23:06:48 +0100198 };
199}
200
201else {
202 print "Input is neither a directory nor an archive.\n\n";
Akronfd0707e2016-02-11 22:13:36 +0100203};
204
Nils Diewald092178e2013-11-26 16:18:48 +0000205
206__END__