blob: 7c38efcc5408ca194fb83ee70a5713737d52743f [file] [log] [blame]
Nils Diewald092178e2013-11-26 16:18:48 +00001#!/usr/bin/env perl
2use strict;
3use warnings;
4use FindBin;
Nils Diewald092178e2013-11-26 16:18:48 +00005use Getopt::Long;
6use Directory::Iterator;
7
8my $local = $FindBin::Bin;
9
Akron9a04c712016-02-05 19:40:05 +010010# Changes
11# 2013/11/25
12# - Initial release
13#
14# 2016/02/04
15# - Rename to korapxml2krill_dir
16
Nils Diewald092178e2013-11-26 16:18:48 +000017sub printhelp {
18 print <<'EOHELP';
19
20Merge foundry data based on a tokenization and create indexer friendly documents
21for whole directories.
22
23Call:
Akron9a04c712016-02-05 19:40:05 +010024korapxml2krill_dir -z --input <directory> --output <directory>
Nils Diewald092178e2013-11-26 16:18:48 +000025
26--input|-i <directory> Directory of documents to index
27--output|-o <directory> Name of output folder
28--token|-t <foundry>[#<layer>] Define the default tokenization by specifying
29 the name of the foundry and optionally the name
30 of the layer. Defaults to OpenNLP#tokens.
31--skip|-s <foundry>[#<layer>] Skip specific foundries by specifying the name
32 or specific layers by defining the name
33 with a # in front of the foundry,
34 e.g. Mate#Morpho. Alternatively you can skip #ALL.
35 Can be set multiple times.
36--allow|-a <foundry>#<layer> Allow specific foundries and layers by defining them
37 combining the foundry name with a # and the layer name.
38--primary|-p Output primary data or not. Defaults to true.
39 Can be flagged using --no-primary as well.
40--human|-m Represent the data human friendly,
41 while the output defaults to JSON
42--pretty|-y Pretty print json output
43--gzip|-z Compress the output
44 (expects a defined output file)
45--log|-l The Log4perl log level, defaults to ERROR.
46--help|-h Print this document (optional)
47
Akron9a04c712016-02-05 19:40:05 +010048diewald@ids-mannheim.de, 2016/02/04
Nils Diewald092178e2013-11-26 16:18:48 +000049
50EOHELP
51
52 exit(defined $_[0] ? $_[0] : 0);
53};
54
55my ($input, $output, $text, $gzip, $log_level, @skip, $token_base, $primary, @allow, $pretty);
56GetOptions(
57 'input|i=s' => \$input,
58 'output|o=s' => \$output,
59 'human|m' => \$text,
60 'token|t=s' => \$token_base,
61 'gzip|z' => \$gzip,
62 'skip|s=s' => \@skip,
63 'log|l=s' => \$log_level,
64 'allow|a=s' => \@allow,
65 'primary|p!' => \$primary,
66 'pretty|y' => \$pretty,
67 'help|h' => sub { printhelp }
68);
69
70printhelp(1) if !$input || !$output;
71
72
73sub write_file {
74 my $anno = shift;
75 my $file = $anno;
76 $file =~ s/^?\/?$input//;
77 $file =~ tr/\//-/;
78 $file =~ s{^-+}{};
79
Akron9a04c712016-02-05 19:40:05 +010080 my $call = 'perl ' . $local . '/korapxml2krill -i ' . $anno . ' -o ' . $output . '/' . $file . '.json';
Nils Diewald092178e2013-11-26 16:18:48 +000081 $call .= '.gz -z' if $gzip;
82 $call .= ' -m' if $text;
83 $call .= ' -l ' . $log_level if $log_level;
84 $call .= ' --no-primary ' if $primary;
85 $call .= ' -y ' . $pretty if $pretty;
86 $call .= ' -a ' . $_ foreach @allow;
87 $call .= ' -s ' . $_ foreach @skip;
Akron9a04c712016-02-05 19:40:05 +010088 print "Convert $file\n";
Nils Diewald092178e2013-11-26 16:18:48 +000089 system($call);
90};
91
92
93my $it = Directory::Iterator->new($input);
94my $dir;
95while (1) {
96
97 if (!$it->is_directory && ($dir = $it->get) && $dir =~ s{/data\.xml$}{}) {
98 write_file($dir);
99 $it->prune;
100 };
101 last unless $it->next;
102};
103
104
105__END__