blob: 5b09566d8796e069394b7940086f9ee1c15fd41b [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
27
28sub printversion {
29 print "Version " . $KorAP::XML::Krill::VERSION . "\n\n";
30 exit(1);
31};
Akron93d620e2016-02-05 19:40:05 +010032
Nils Diewald092178e2013-11-26 16:18:48 +000033sub printhelp {
34 print <<'EOHELP';
35
36Merge foundry data based on a tokenization and create indexer friendly documents
37for whole directories.
38
39Call:
Akron93d620e2016-02-05 19:40:05 +010040korapxml2krill_dir -z --input <directory> --output <directory>
Nils Diewald092178e2013-11-26 16:18:48 +000041
Akron150b29e2016-02-14 23:06:48 +010042 --input|-i <directory|file> Directory or archive file of documents to index
Akron069bd712016-02-12 19:09:06 +010043 --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)
Akron150b29e2016-02-14 23:06:48 +010064 --version|-v Print version information
Nils Diewald092178e2013-11-26 16:18:48 +000065
Akron150b29e2016-02-14 23:06:48 +010066diewald@ids-mannheim.de, 2016/02/14
Nils Diewald092178e2013-11-26 16:18:48 +000067
68EOHELP
69
70 exit(defined $_[0] ? $_[0] : 0);
71};
72
Akron069bd712016-02-12 19:09:06 +010073my ($input, $output, $text, $gzip, $log_level, @skip,
74 $token_base, $primary, @allow, $pretty, $overwrite);
Nils Diewald092178e2013-11-26 16:18:48 +000075GetOptions(
76 'input|i=s' => \$input,
77 'output|o=s' => \$output,
78 'human|m' => \$text,
Akron069bd712016-02-12 19:09:06 +010079 'overwrite|w' => \$overwrite,
Nils Diewald092178e2013-11-26 16:18:48 +000080 '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,
Akron150b29e2016-02-14 23:06:48 +010087 'help|h' => sub { printhelp },
88 'version|v' => sub { printversion }
Nils Diewald092178e2013-11-26 16:18:48 +000089);
90
91printhelp(1) if !$input || !$output;
92
93
Akron150b29e2016-02-14 23:06:48 +010094# write file
Nils Diewald092178e2013-11-26 16:18:48 +000095sub write_file {
96 my $anno = shift;
97 my $file = $anno;
98 $file =~ s/^?\/?$input//;
99 $file =~ tr/\//-/;
100 $file =~ s{^-+}{};
101
Akron93d620e2016-02-05 19:40:05 +0100102 my $call = 'perl ' . $local . '/korapxml2krill -i ' . $anno . ' -o ' . $output . '/' . $file . '.json';
Nils Diewald092178e2013-11-26 16:18:48 +0000103 $call .= '.gz -z' if $gzip;
104 $call .= ' -m' if $text;
Akron069bd712016-02-12 19:09:06 +0100105 $call .= ' -w' if $overwrite;
Akrona9d47722016-02-07 23:54:15 +0100106 $call .= ' -t ' . $token_base if $token_base;
Nils Diewald092178e2013-11-26 16:18:48 +0000107 $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;
Akrona98a14c2016-02-12 16:28:39 +0100112 print "$file ";
Nils Diewald092178e2013-11-26 16:18:48 +0000113 system($call);
Akronfd0707e2016-02-11 22:13:36 +0100114 print "\n";
Nils Diewald092178e2013-11-26 16:18:48 +0000115};
116
Akron150b29e2016-02-14 23:06:48 +0100117# Input is a directory
118if (-d $input) {
119 my $it = Directory::Iterator->new($input);
120 my @dirs;
121 my $dir;
122 while (1) {
Nils Diewald092178e2013-11-26 16:18:48 +0000123 if (!$it->is_directory && ($dir = $it->get) && $dir =~ s{/data\.xml$}{}) {
Akron150b29e2016-02-14 23:06:48 +0100124 push @dirs, $dir;
125 $it->prune;
Nils Diewald092178e2013-11-26 16:18:48 +0000126 };
Akron150b29e2016-02-14 23:06:48 +0100127 last unless $it->next;
128 };
Nils Diewald092178e2013-11-26 16:18:48 +0000129
Akron150b29e2016-02-14 23:06:48 +0100130 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
138elsif (-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
180else {
181 print "Input is neither a directory nor an archive.\n\n";
Akronfd0707e2016-02-11 22:13:36 +0100182};
183
Nils Diewald092178e2013-11-26 16:18:48 +0000184
185__END__