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