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; |
| 5 | use v5.16; |
| 6 | use Getopt::Long; |
| 7 | use Directory::Iterator; |
| 8 | |
| 9 | my $local = $FindBin::Bin; |
| 10 | |
| 11 | sub printhelp { |
| 12 | print <<'EOHELP'; |
| 13 | |
| 14 | Merge foundry data based on a tokenization and create indexer friendly documents |
| 15 | for whole directories. |
| 16 | |
| 17 | Call: |
| 18 | wrap_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 | |
| 42 | diewald@ids-mannheim.de, 2013/11/25 |
| 43 | |
| 44 | EOHELP |
| 45 | |
| 46 | exit(defined $_[0] ? $_[0] : 0); |
| 47 | }; |
| 48 | |
| 49 | my ($input, $output, $text, $gzip, $log_level, @skip, $token_base, $primary, @allow, $pretty); |
| 50 | GetOptions( |
| 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 | |
| 64 | printhelp(1) if !$input || !$output; |
| 65 | |
| 66 | |
| 67 | sub 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 | |
| 86 | my $it = Directory::Iterator->new($input); |
| 87 | my $dir; |
| 88 | while (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__ |