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